mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-13 13:17:54 +03:00
Changed check handshake method
This commit is contained in:
parent
4577552773
commit
0ca85c5749
@ -175,8 +175,6 @@ class TunnelProtocol(asyncio.Protocol):
|
|||||||
else:
|
else:
|
||||||
raise Exception('Invalid command')
|
raise Exception('Invalid command')
|
||||||
except Exception:
|
except Exception:
|
||||||
if consts.DEBUG:
|
|
||||||
logger.exception('COMMAND')
|
|
||||||
logger.error('ERROR from %s', self.pretty_source())
|
logger.error('ERROR from %s', self.pretty_source())
|
||||||
self.transport.write(b'ERROR_COMMAND')
|
self.transport.write(b'ERROR_COMMAND')
|
||||||
self.close_connection()
|
self.close_connection()
|
||||||
|
@ -22,7 +22,7 @@ lognumber = 3
|
|||||||
address = 0.0.0.0
|
address = 0.0.0.0
|
||||||
|
|
||||||
# Number of workers. Defaults to 0 (means "as much as cores")
|
# Number of workers. Defaults to 0 (means "as much as cores")
|
||||||
workers = 1
|
workers = 2
|
||||||
|
|
||||||
# Listening port
|
# Listening port
|
||||||
port = 7777
|
port = 7777
|
||||||
|
@ -38,6 +38,7 @@ import signal
|
|||||||
import ssl
|
import ssl
|
||||||
import socket
|
import socket
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
import setproctitle
|
import setproctitle
|
||||||
@ -103,32 +104,13 @@ async def tunnel_proc_async(
|
|||||||
|
|
||||||
def get_socket() -> typing.Optional[socket.socket]:
|
def get_socket() -> typing.Optional[socket.socket]:
|
||||||
try:
|
try:
|
||||||
data: bytes = b''
|
|
||||||
while True:
|
while True:
|
||||||
# Clear back event, for next data
|
# Clear back event, for next data
|
||||||
msg: typing.Optional[
|
msg: typing.Optional[
|
||||||
typing.Tuple[socket.socket, typing.Tuple[str, int]]
|
typing.Tuple[socket.socket, typing.Tuple[str, int]]
|
||||||
] = pipe.recv()
|
] = pipe.recv()
|
||||||
if msg:
|
if msg:
|
||||||
# Connection done, check for handshake
|
return msg[0]
|
||||||
source: socket.socket
|
|
||||||
source, address = msg
|
|
||||||
|
|
||||||
try:
|
|
||||||
# First, ensure handshake (simple handshake) and command
|
|
||||||
data = source.recv(len(consts.HANDSHAKE_V1))
|
|
||||||
|
|
||||||
if data != consts.HANDSHAKE_V1:
|
|
||||||
raise Exception() # Invalid handshake
|
|
||||||
except Exception:
|
|
||||||
if consts.DEBUG:
|
|
||||||
logger.exception('HANDSHAKE')
|
|
||||||
logger.error('HANDSHAKE invalid from %s (%s)', address, data.hex())
|
|
||||||
# Close Source and continue
|
|
||||||
source.close()
|
|
||||||
continue
|
|
||||||
|
|
||||||
return source
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Receiving data from parent process')
|
logger.exception('Receiving data from parent process')
|
||||||
return None
|
return None
|
||||||
@ -168,15 +150,35 @@ async def tunnel_proc_async(
|
|||||||
del tasks[:tasks_number]
|
del tasks[:tasks_number]
|
||||||
|
|
||||||
|
|
||||||
|
def process_connection(
|
||||||
|
client: socket.socket, addr: typing.Tuple[str, str], conn: 'Connection'
|
||||||
|
) -> None:
|
||||||
|
data: bytes = b''
|
||||||
|
try:
|
||||||
|
# First, ensure handshake (simple handshake) and command
|
||||||
|
data = client.recv(len(consts.HANDSHAKE_V1))
|
||||||
|
|
||||||
|
if data != consts.HANDSHAKE_V1:
|
||||||
|
raise Exception() # Invalid handshake
|
||||||
|
conn.send((client, addr))
|
||||||
|
del client # Ensure socket is controlled on child process
|
||||||
|
except Exception:
|
||||||
|
logger.error('HANDSHAKE invalid from %s (%s)', addr, data.hex())
|
||||||
|
# Close Source and continue
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
|
||||||
def tunnel_main():
|
def tunnel_main():
|
||||||
cfg = config.read()
|
cfg = config.read()
|
||||||
|
|
||||||
# Try to bind to port as running user
|
# Try to bind to port as running user
|
||||||
# Wait for socket incoming connections and spread them
|
# Wait for socket incoming connections and spread them
|
||||||
|
socket.setdefaulttimeout(
|
||||||
|
3.0
|
||||||
|
) # So we can check for stop from time to time and not block forever
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
|
||||||
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
sock.settimeout(3.0) # So we can check for stop from time to time
|
|
||||||
# We will not reuse port, we only want a UDS tunnel server running on a port
|
# We will not reuse port, we only want a UDS tunnel server running on a port
|
||||||
# but this may change on future...
|
# but this may change on future...
|
||||||
# try:
|
# try:
|
||||||
@ -223,11 +225,14 @@ def tunnel_main():
|
|||||||
while not do_stop:
|
while not do_stop:
|
||||||
try:
|
try:
|
||||||
client, addr = sock.accept()
|
client, addr = sock.accept()
|
||||||
client.settimeout(3.0)
|
|
||||||
logger.info('CONNECTION from %s', addr)
|
logger.info('CONNECTION from %s', addr)
|
||||||
# Select BEST process for sending this new connection
|
|
||||||
prcs.best_child().send((client, addr))
|
# Check if we have reached the max number of connections
|
||||||
del client # Ensure socket is controlled on child process
|
# First part is checked on a thread, if HANDSHAKE is valid
|
||||||
|
# we will send socket to process pool
|
||||||
|
threading.Thread(
|
||||||
|
target=process_connection, args=(client, addr, prcs.best_child())
|
||||||
|
).start()
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass # Continue and retry
|
pass # Continue and retry
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Loading…
Reference in New Issue
Block a user