Browse Source

add udp data transfer

dev
BreakWa11 9 years ago
parent
commit
d6640b2e81
  1. 8
      server_pool.py
  2. 12
      shadowsocks/udprelay.py

8
server_pool.py

@ -215,14 +215,22 @@ class ServerPool(object):
if port in self.tcp_servers_pool: if port in self.tcp_servers_pool:
ret[0] = self.tcp_servers_pool[port].server_transfer_ul ret[0] = self.tcp_servers_pool[port].server_transfer_ul
ret[1] = self.tcp_servers_pool[port].server_transfer_dl ret[1] = self.tcp_servers_pool[port].server_transfer_dl
if port in self.udp_servers_pool:
ret[0] += self.udp_servers_pool[port].server_transfer_ul
ret[1] += self.udp_servers_pool[port].server_transfer_dl
if port in self.tcp_ipv6_servers_pool: if port in self.tcp_ipv6_servers_pool:
ret[0] += self.tcp_ipv6_servers_pool[port].server_transfer_ul ret[0] += self.tcp_ipv6_servers_pool[port].server_transfer_ul
ret[1] += self.tcp_ipv6_servers_pool[port].server_transfer_dl ret[1] += self.tcp_ipv6_servers_pool[port].server_transfer_dl
if port in self.udp_ipv6_servers_pool:
ret[0] += self.udp_ipv6_servers_pool[port].server_transfer_ul
ret[1] += self.udp_ipv6_servers_pool[port].server_transfer_dl
return ret return ret
def get_servers_transfer(self): def get_servers_transfer(self):
servers = self.tcp_servers_pool.copy() servers = self.tcp_servers_pool.copy()
servers.update(self.tcp_ipv6_servers_pool) servers.update(self.tcp_ipv6_servers_pool)
servers.update(self.udp_servers_pool)
servers.update(self.udp_ipv6_servers_pool)
ret = {} ret = {}
for port in servers.keys(): for port in servers.keys():
ret[port] = self.get_server_transfer(port) ret[port] = self.get_server_transfer(port)

12
shadowsocks/udprelay.py

@ -529,6 +529,7 @@ class TCPRelayHandler(object):
self.destroy() self.destroy()
return return
try: try:
self._server.server_transfer_dl += len(data)
recv_data = data recv_data = data
beg_pos = 0 beg_pos = 0
max_len = len(recv_data) max_len = len(recv_data)
@ -546,7 +547,6 @@ class TCPRelayHandler(object):
post_data = self._pack_post_data(CMD_POST, pack_id, data) post_data = self._pack_post_data(CMD_POST, pack_id, data)
addr = self.get_local_address() addr = self.get_local_address()
self._write_to_sock(post_data, self._local_sock, addr) self._write_to_sock(post_data, self._local_sock, addr)
self._server.server_transfer_dl += len(post_data)
if pack_id <= DOUBLE_SEND_BEG_IDS: if pack_id <= DOUBLE_SEND_BEG_IDS:
post_data = self._pack_post_data(CMD_POST, pack_id, data) post_data = self._pack_post_data(CMD_POST, pack_id, data)
self._write_to_sock(post_data, self._local_sock, addr) self._write_to_sock(post_data, self._local_sock, addr)
@ -1026,6 +1026,7 @@ class UDPRelay(object):
#(cmd, request_id, data) #(cmd, request_id, data)
#logging.info("UDP data %d %d %s" % (data[0], data[1], binascii.hexlify(data[2]))) #logging.info("UDP data %d %d %s" % (data[0], data[1], binascii.hexlify(data[2])))
try: try:
self.server_transfer_ul += len(data[2])
if data[0] == 0: if data[0] == 0:
if len(data[2]) >= 4: if len(data[2]) >= 4:
for i in range(64): for i in range(64):
@ -1152,6 +1153,7 @@ class UDPRelay(object):
try: try:
#logging.info('UDP handle_server sendto %s:%d %d bytes' % (common.to_str(server_addr), server_port, len(data))) #logging.info('UDP handle_server sendto %s:%d %d bytes' % (common.to_str(server_addr), server_port, len(data)))
client.sendto(data, (server_addr, server_port)) client.sendto(data, (server_addr, server_port))
self.server_transfer_ul += len(data)
except IOError as e: except IOError as e:
err = eventloop.errno_from_exception(e) err = eventloop.errno_from_exception(e)
if err in (errno.EINPROGRESS, errno.EAGAIN): if err in (errno.EINPROGRESS, errno.EAGAIN):
@ -1196,6 +1198,7 @@ class UDPRelay(object):
response = b'\x00\x00\x00' + data response = b'\x00\x00\x00' + data
client_addr = self._client_fd_to_server_addr.get(sock.fileno()) client_addr = self._client_fd_to_server_addr.get(sock.fileno())
if client_addr: if client_addr:
self.server_transfer_dl += len(response)
self.write_to_server_socket(response, client_addr) self.write_to_server_socket(response, client_addr)
else: else:
# this packet is from somewhere else we know # this packet is from somewhere else we know
@ -1203,15 +1206,9 @@ class UDPRelay(object):
pass pass
def write_to_server_socket(self, data, addr): def write_to_server_socket(self, data, addr):
#self._server_socket.sendto(data, addr)
#'''
uncomplete = False uncomplete = False
retry = 0 retry = 0
try: try:
#"""
#if self._data_to_write_to_server_socket:
# self._data_to_write_to_server_socket.append([(data, addr), 0])
#else:
self._server_socket.sendto(data, addr) self._server_socket.sendto(data, addr)
data = None data = None
while self._data_to_write_to_server_socket: while self._data_to_write_to_server_socket:
@ -1220,7 +1217,6 @@ class UDPRelay(object):
del self._data_to_write_to_server_socket[0] del self._data_to_write_to_server_socket[0]
data, addr = data_buf[0] data, addr = data_buf[0]
self._server_socket.sendto(data, addr) self._server_socket.sendto(data, addr)
#"""
except (OSError, IOError) as e: except (OSError, IOError) as e:
error_no = eventloop.errno_from_exception(e) error_no = eventloop.errno_from_exception(e)
uncomplete = True uncomplete = True

Loading…
Cancel
Save