Skip to content

Commit

Permalink
Fix a connection bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch0s committed Jun 24, 2022
1 parent fb7df5a commit dbc138a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
6 changes: 3 additions & 3 deletions config/1.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"hostname": "0.0.0.0",
"host": "0.0.0.0",
"port": 25577,
"proxy_protocol": true
"host": "54.39.130.114",
"port": 25565,
"proxy_protocol": false
}
31 changes: 18 additions & 13 deletions gamma/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def __init__(self, **kwargs):

if self.upstream_address[2] == True:
data = b'PROXY TCP4 ' + self.downstream_address[0].encode() + b' 255.255.255.255 ' + str(self.downstream_address[1]).encode() + b' 25565\r\n' + data
if self.debug:
print(data)

# If the IP for hostname == None (Not found), return
# invalid hostname motd to the downstream connection
Expand All @@ -62,8 +64,9 @@ def __init__(self, **kwargs):
self.conn_alive = False
sys.exit()

threading.Thread(target=self.upstream, kwargs={'data': data}).start() # Starts a `PROXY <---> SERVER` thread
threading.Thread(target=self.downstream).start() # Starts a `CLIENT <---> PROXY` thread
threading.Thread(target=self.upstream).start() # Starts a `PROXY <---> SERVER` thread

threading.Thread(target=self.downstream, kwargs={'data': data}).start() # Starts a `CLIENT <---> PROXY` thread

sys.exit()

Expand All @@ -74,22 +77,20 @@ def upstream(self, **kwargs):
self.upstream_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.upstream_conn.connect((self.upstream_address[0], self.upstream_address[1]))

time.sleep(0.1)

self.upstream_conn_packets = 0

if 'data' in kwargs:
self.upstream_conn.send(kwargs['data'])

while self.conn_alive:
data = self.upstream_conn.recv(8192)
data = self.upstream_conn.recv(16384)

if data:
if self.debug:
print('[<]', data)

self.upstream_conn_packets += 1
self.conn_bandwidth += len(data)
self.downstream_conn.send(data)

if self.conn_type == 'PING' and self.upstream_conn_packets >= 2:
if self.conn_type == 'PING' and self.upstream_conn_packets >= 3:
self.on_ping()
self.conn_alive = False

Expand All @@ -111,19 +112,23 @@ def downstream(self, **kwargs):
PLAYER <--> PROXY
"""

time.sleep(0.1)
time.sleep(0.5)

self.downstream_conn_packets = 0
self.missed_downstream_packets = 0

try:

time.sleep(0.5)
if 'data' in kwargs:
self.upstream_conn.send(kwargs['data'])

while self.conn_alive:
data = self.downstream_conn.recv(8192)
data = self.downstream_conn.recv(16384)

if data:
if self.debug:
print('[>]', data)

self.downstream_conn_packets += 1

self.conn_bandwidth += len(data) # Adds packet length to bandwidth counter
Expand All @@ -135,7 +140,7 @@ def downstream(self, **kwargs):
if self.player_username is not None and self.conn_type:
self.on_player_connect()

if self.conn_type == 'PING' and self.upstream_conn_packets >= 2:
if self.conn_type == 'PING' and self.upstream_conn_packets >= 5:
self.on_ping()
self.conn_alive = False

Expand Down

0 comments on commit dbc138a

Please sign in to comment.