dansalvato

vbaserver_aw.py

Sep 23rd, 2020
789
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket
  2. import threading
  3. import time
  4.  
  5. mode = 0
  6. timer_mult = 1.0
  7. fakebuffer_server = [bytearray([0x4d, 0x03, 0xff, 0x7f, 0x80, 0x04, 0x31, 0xa4]),
  8.                      bytearray([0x4d, 0x03, 0x00, 0x84, 0x80, 0x04, 0x31, 0xa4]), #431a4
  9.                      bytearray([0x4d, 0x03, 0x00, 0x88, 0x80, 0x04, 0x31, 0xa4]),
  10.                      bytearray([0x4d, 0x03, 0x00, 0x90, 0x80, 0x04, 0x31, 0xa4]),
  11.                      bytearray([0x4d, 0x03, 0x00, 0x94, 0x80, 0x04, 0x31, 0xa4]),
  12.                      bytearray([0x4d, 0x03, 0x00, 0x98, 0x80, 0x04, 0x31, 0xa4]),
  13.                      bytearray([0x4d, 0x03, 0x00, 0x9c, 0x80, 0x04, 0x31, 0xa4]),
  14.                      bytearray([0x4d, 0x03, 0xff, 0x7f, 0x80, 0x04, 0x31, 0xa4]),
  15.                      bytearray([0x4d, 0x03, 0xbc, 0x9a, 0x80, 0x04, 0x31, 0xa4]),
  16.                      ]
  17. fakebuffer_client = [bytearray([0x4d, 0x43, 0xff, 0x5f]),
  18.                      bytearray([0x4d, 0x43, 0x00, 0x84]),
  19.                      bytearray([0x4d, 0x43, 0x00, 0x88]),
  20.                      bytearray([0x4d, 0x43, 0x00, 0x90]),
  21.                      bytearray([0x4d, 0x43, 0x00, 0x94]),
  22.                      bytearray([0x4d, 0x43, 0x00, 0x98]),
  23.                      bytearray([0x4d, 0x43, 0x00, 0x9c]),
  24.                      bytearray([0x4d, 0x43, 0xff, 0x7f]),
  25.                      bytearray([0x4d, 0x43, 0xbc, 0x9a]),
  26.                      ]
  27.  
  28. buffer_server = bytearray()
  29. buffer_client = bytearray()
  30.  
  31. def keepalive_server(server, client):
  32.     global buffer_client
  33.     global buffer_server
  34.     global mode
  35.     keepalive = True
  36.     if len(buffer_client) == 0:
  37.         try:
  38.             server.send(fakebuffer_client[mode])
  39.             server.send(fakebuffer_client[mode])
  40.         except: keepalive = False
  41.     else:
  42.         #print([hex(i) for i in buffer_client])
  43.         try: server.send(buffer_client[:4])
  44.         except: keepalive = False
  45.         buffer_client = buffer_client[4:]
  46.     if len(buffer_server) != 0:
  47.         try: client.send(buffer_server)
  48.         except: keepalive = False
  49.         buffer_server = bytearray()
  50.     if mode == 7: time = 0.01
  51.     else: time = 0.05
  52.     if keepalive: threading.Timer(time, keepalive_server, [server, client]).start()
  53.  
  54. def keepalive_client(server, client):
  55.     global buffer_client
  56.     global buffer_server
  57.     global mode
  58.     keepalive = True
  59.     if len(buffer_server) == 0:
  60.         try: client.send(fakebuffer_server[mode])
  61.         except: keepalive = False
  62.     else:
  63.         #print([hex(i) for i in buffer_server])
  64.         try: client.send(buffer_server[:8])
  65.         except: keepalive = False
  66.         buffer_server = buffer_server[8:]
  67.     if len(buffer_client) != 0:
  68.         try: server.send(buffer_client)
  69.         except: keepalive = False
  70.         buffer_client = bytearray()
  71.     if mode == 7: time = 0.01
  72.     else: time = 0.025
  73.     if keepalive: threading.Timer(time * timer_mult, keepalive_client, [server, client]).start()
  74.  
  75.  
  76. def listen_server(server, client):
  77.     global buffer_server
  78.     global mode
  79.     global host
  80.     global timer_mult
  81.     lastbyte = 0
  82.     try:
  83.         while True:
  84.             sbuffer = server.recv(8)
  85.             if host:
  86.                 last_mode = mode
  87.                 if sbuffer[3] == 0x84: mode = 1
  88.                 elif sbuffer[3] == 0x88: mode = 2
  89.                 elif sbuffer[3] == 0x90: mode = 3
  90.                 elif sbuffer[3] == 0x94: mode = 4
  91.                 elif sbuffer[3] == 0x98: mode = 5
  92.                 elif sbuffer[3] == 0x9c: mode = 6
  93.                 elif sbuffer[3] == 0x7f or sbuffer[3] == 0x4f: mode = 7
  94.                 elif sbuffer[3] == 0x9a and sbuffer[2] == 0xbc: mode = 7
  95.                 elif sbuffer[3] == 0x5f: pass
  96.                 else: mode = 7
  97.                 if last_mode != mode: print("Mode changed to " + str(mode))
  98.             if mode != 7:
  99.                 if sbuffer[2] != lastbyte:
  100.                     lastbyte = sbuffer[2]
  101.                     if not host: sbuffer += sbuffer + sbuffer + sbuffer + sbuffer + sbuffer + sbuffer + sbuffer
  102.                     buffer_server += sbuffer
  103.                     #print([hex(i) for i in buffer_server])
  104.             else:
  105.                 if sbuffer[3] == 0x4f:
  106.                     sbuffer += (server.recv(8))
  107.                     datalength = (int)(sbuffer[10])
  108.                     sbuffer2 = bytearray()
  109.                     while len(sbuffer2) < 8 * (datalength + 2):
  110.                         sbuffer2 += (server.recv(8 * (datalength + 2) - len(sbuffer2)))
  111.                     buffer_server += sbuffer + sbuffer2
  112.                     #print([hex(i) for i in buffer_server])
  113.     except:
  114.         print("P1 has disconnected.")
  115.  
  116. def listen_client(server, client):
  117.     global buffer_client
  118.     global mode
  119.     global host
  120.     global timer_mult
  121.     first = False
  122.     lastbyte = 0
  123.     try:
  124.         while True:
  125.             sbuffer = client.recv(4)
  126.             #print([hex(i) for i in sbuffer])
  127.             if not host:
  128.                 last_mode = mode
  129.                 if sbuffer[3] == 0x84: mode = 1
  130.                 elif sbuffer[3] == 0x88: mode = 2
  131.                 elif sbuffer[3] == 0x90: mode = 3
  132.                 elif sbuffer[3] == 0x94: mode = 4
  133.                 elif sbuffer[3] == 0x98: mode = 5
  134.                 elif sbuffer[3] == 0x9c: mode = 6
  135.                 elif sbuffer[3] == 0x7f or sbuffer[3] == 0x4f: mode = 7
  136.                 elif sbuffer[3] == 0x9a and sbuffer[2] == 0xbc: mode = 8
  137.                 elif sbuffer[3] == 0x5f: pass
  138.                 else: mode = 7
  139.                 if last_mode != mode: print("Mode changed to " + str(mode))
  140.             if mode != 7 and mode != 8:
  141.                 if sbuffer[2] != lastbyte:
  142.                     lastbyte = sbuffer[2]
  143.                     if host: sbuffer += sbuffer + sbuffer + sbuffer + sbuffer + sbuffer + sbuffer + sbuffer
  144.                     success = False
  145.                     while not success:
  146.                         try:
  147.                             buffer_client += sbuffer
  148.                             success = True
  149.                         except:
  150.                             pass
  151.             else:
  152.                 #print([hex(i) for i in sbuffer])
  153.                 if sbuffer[3] == 0x4f:
  154.                     timer_mult *= 2
  155.                     sbuffer += (client.recv(4))
  156.                     datalength = (int)(sbuffer[6])
  157.                     sbuffer2 = bytearray()
  158.                     while len(sbuffer2) < 4 * (datalength + 2):
  159.                         sbuffer2 += (client.recv(4 * (datalength + 2) - len(sbuffer2)))
  160.                     buffer_client += sbuffer + sbuffer2
  161.                     timer_mult /= 2
  162.                 elif sbuffer[3] == 0x9a and sbuffer[2] == 0xbc:
  163.                     success = False
  164.                     while not success:
  165.                         try:
  166.                             buffer_client += sbuffer
  167.                             success = True
  168.                         except:
  169.                             pass
  170.  
  171.             if sbuffer[3] == 0xff or sbuffer[3] == 0x5f:
  172.                 timer_mult += 0.1
  173.                 while client.recv(4)[3] == 0xff: timer_mult += 0.002
  174.             else:
  175.                 timer_mult -= 0.002
  176.                 if timer_mult < 1.0: timer_mult = 1.0
  177.     except:
  178.         print("P2 has disconnected.")
  179.  
  180. server_address = input("Enter the host IP, or leave blank if you are hosting: ")
  181. if server_address == "": host = True
  182. else: host = False
  183.  
  184. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  185.  
  186. sock.bind(('', 6478))
  187. sock.listen(4)
  188.  
  189. if host:
  190.     print("Waiting for your local client...")
  191.  
  192.     client0, address0 = sock.accept()
  193.  
  194.     print("Waiting for P2...")
  195.  
  196.     client1, address1 = sock.accept()
  197.  
  198.     client0.send(bytes([0x00, 0x00, 0x00, 0x00]))
  199.     client1.send(bytes([0x01, 0x00, 0x00, 0x00]))
  200.  
  201.     print("Ready.")
  202.  
  203.     threading.Thread(target = listen_server, args = (client0,client1)).start()
  204.     threading.Thread(target = listen_client, args = (client0,client1)).start()
  205.     keepalive_server(client0, client1)
  206.     #keepalive_client(client0, client1)
  207. else:
  208.     print("Waiting for your local client...")
  209.     client1, address1 = sock.accept()
  210.     print("Connecting to host...")
  211.     client0 = socket.create_connection((server_address, 6478))
  212.     client1.send(client0.recv(4))
  213.     print("Ready.")
  214.  
  215.     threading.Thread(target = listen_server, args = (client0,client1)).start()
  216.     threading.Thread(target = listen_client, args = (client0,client1)).start()
  217.     keepalive_client(client0, client1)
RAW Paste Data