|
@@ -7,48 +7,25 @@ import time
|
|
|
import socket
|
|
|
#
|
|
|
class ClientProtocol(basic_protocols.Protocol):
|
|
|
- def __init__(self, endpoint, total_bytes, proxy=None, username=None, wait_until=None, send_buffer_len=None, use_acceleration=None):
|
|
|
- self.endpoint = endpoint
|
|
|
+ def __init__(self, socket, total_bytes, wait_until=None, send_buffer_len=None, use_acceleration=None):
|
|
|
+ self.socket = socket
|
|
|
self.total_bytes = total_bytes
|
|
|
- self.proxy = proxy
|
|
|
- self.username = username
|
|
|
self.wait_until = wait_until
|
|
|
self.send_buffer_len = send_buffer_len
|
|
|
self.use_acceleration = use_acceleration
|
|
|
#
|
|
|
- self.states = enum.Enum('CLIENT_CONN_STATES', 'READY_TO_BEGIN CONNECT_TO_PROXY SEND_GROUP_ID PUSH_DATA DONE')
|
|
|
+ self.states = enum.Enum('CLIENT_CONN_STATES', 'READY_TO_BEGIN SEND_GROUP_ID PUSH_DATA DONE')
|
|
|
self.state = self.states.READY_TO_BEGIN
|
|
|
#
|
|
|
- self.socket = socket.socket()
|
|
|
self.sub_protocol = None
|
|
|
self.group_id = int(self.wait_until*1000) if self.wait_until is not None else 0
|
|
|
# a group id of 0 means no group
|
|
|
- #
|
|
|
- if self.proxy is None:
|
|
|
- logging.debug('Socket %d connecting to endpoint %r...', self.socket.fileno(), self.endpoint)
|
|
|
- self.socket.connect(self.endpoint)
|
|
|
- else:
|
|
|
- logging.debug('Socket %d connecting to proxy %r...', self.socket.fileno(), self.proxy)
|
|
|
- self.socket.connect(self.proxy)
|
|
|
- #
|
|
|
#
|
|
|
def _run_iteration(self, block=True):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
- if self.proxy is None:
|
|
|
- group_id_bytes = self.group_id.to_bytes(8, byteorder='big', signed=False)
|
|
|
- self.sub_protocol = basic_protocols.SendDataProtocol(self.socket, group_id_bytes)
|
|
|
- self.state = self.states.SEND_GROUP_ID
|
|
|
- else:
|
|
|
- self.sub_protocol = basic_protocols.Socks4Protocol(self.socket, self.endpoint, username=self.username)
|
|
|
- self.state = self.states.CONNECT_TO_PROXY
|
|
|
- #
|
|
|
- #
|
|
|
- if self.state is self.states.CONNECT_TO_PROXY:
|
|
|
- if self.sub_protocol.run(block=block):
|
|
|
- group_id_bytes = self.group_id.to_bytes(8, byteorder='big', signed=False)
|
|
|
- self.sub_protocol = basic_protocols.SendDataProtocol(self.socket, group_id_bytes)
|
|
|
- self.state = self.states.SEND_GROUP_ID
|
|
|
- #
|
|
|
+ group_id_bytes = self.group_id.to_bytes(8, byteorder='big', signed=False)
|
|
|
+ self.sub_protocol = basic_protocols.SendDataProtocol(self.socket, group_id_bytes)
|
|
|
+ self.state = self.states.SEND_GROUP_ID
|
|
|
#
|
|
|
if self.state is self.states.SEND_GROUP_ID:
|
|
|
if block and self.wait_until is not None:
|