Browse Source

TCP: bind the IP which connecting in, or set the IP by config file

allow setting method/bind/bindv6 for each port
dev
BreakWa11 9 years ago
parent
commit
f91e04b0bd
  1. 12
      shadowsocks/server.py
  2. 16
      shadowsocks/tcprelay.py

12
shadowsocks/server.py

@ -65,10 +65,13 @@ def main():
config_password = config.get('password', 'm') config_password = config.get('password', 'm')
del config['port_password'] del config['port_password']
for port, password_obfs in port_password.items(): for port, password_obfs in port_password.items():
method = config["method"]
protocol = config.get("protocol", 'origin') protocol = config.get("protocol", 'origin')
protocol_param = config.get("protocol_param", '') protocol_param = config.get("protocol_param", '')
obfs = config.get("obfs", 'plain') obfs = config.get("obfs", 'plain')
obfs_param = config.get("obfs_param", '') obfs_param = config.get("obfs_param", '')
bind = config.get("bind", '')
bindv6 = config.get("bindv6", '')
if type(password_obfs) == list: if type(password_obfs) == list:
password = password_obfs[0] password = password_obfs[0]
obfs = password_obfs[1] obfs = password_obfs[1]
@ -76,10 +79,13 @@ def main():
protocol = password_obfs[2] protocol = password_obfs[2]
elif type(password_obfs) == dict: elif type(password_obfs) == dict:
password = password_obfs.get('password', config_password) password = password_obfs.get('password', config_password)
method = password_obfs.get('method', method)
protocol = password_obfs.get('protocol', protocol) protocol = password_obfs.get('protocol', protocol)
protocol_param = password_obfs.get('protocol_param', protocol_param) protocol_param = password_obfs.get('protocol_param', protocol_param)
obfs = password_obfs.get('obfs', obfs) obfs = password_obfs.get('obfs', obfs)
obfs_param = password_obfs.get('obfs_param', obfs_param) obfs_param = password_obfs.get('obfs_param', obfs_param)
bind = password_obfs.get('bind', bind)
bindv6 = password_obfs.get('bindv6', bindv6)
else: else:
password = password_obfs password = password_obfs
a_config = config.copy() a_config = config.copy()
@ -92,10 +98,13 @@ def main():
a_config['server_ipv6'] = a_config['server_ipv6'][1:-1] a_config['server_ipv6'] = a_config['server_ipv6'][1:-1]
a_config['server_port'] = int(port) a_config['server_port'] = int(port)
a_config['password'] = password a_config['password'] = password
a_config['method'] = method
a_config['protocol'] = protocol a_config['protocol'] = protocol
a_config['protocol_param'] = protocol_param a_config['protocol_param'] = protocol_param
a_config['obfs'] = obfs a_config['obfs'] = obfs
a_config['obfs_param'] = obfs_param a_config['obfs_param'] = obfs_param
a_config['bind'] = bind
a_config['bindv6'] = bindv6
a_config['server'] = a_config['server_ipv6'] a_config['server'] = a_config['server_ipv6']
logging.info("starting server at [%s]:%d" % logging.info("starting server at [%s]:%d" %
(a_config['server'], int(port))) (a_config['server'], int(port)))
@ -110,10 +119,13 @@ def main():
a_config = config.copy() a_config = config.copy()
a_config['server_port'] = int(port) a_config['server_port'] = int(port)
a_config['password'] = password a_config['password'] = password
a_config['method'] = method
a_config['protocol'] = protocol a_config['protocol'] = protocol
a_config['protocol_param'] = protocol_param a_config['protocol_param'] = protocol_param
a_config['obfs'] = obfs a_config['obfs'] = obfs
a_config['obfs_param'] = obfs_param a_config['obfs_param'] = obfs_param
a_config['bind'] = bind
a_config['bindv6'] = bindv6
logging.info("starting server at %s:%d" % logging.info("starting server at %s:%d" %
(a_config['server'], int(port))) (a_config['server'], int(port)))
tcp_servers.append(tcprelay.TCPRelay(a_config, dns_resolver, False)) tcp_servers.append(tcprelay.TCPRelay(a_config, dns_resolver, False))

16
shadowsocks/tcprelay.py

@ -138,6 +138,8 @@ class TCPRelayHandler(object):
self._protocol.set_server_info(server_info) self._protocol.set_server_info(server_info)
self._redir_list = config.get('redirect', ["0.0.0.0:0"]) self._redir_list = config.get('redirect', ["0.0.0.0:0"])
self._bind = config.get('bind', '')
self._bindv6 = config.get('bindv6', '')
self._fastopen_connected = False self._fastopen_connected = False
self._data_to_write_to_local = [] self._data_to_write_to_local = []
@ -516,6 +518,20 @@ class TCPRelayHandler(object):
remote_sock_v6.setblocking(False) remote_sock_v6.setblocking(False)
else: else:
remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
if not self._is_local:
bind_addr = ''
if self._bind and af == socket.AF_INET:
bind_addr = self._bind
elif self._bindv6 and af == socket.AF_INET:
bind_addr = self._bindv6
else:
bind_addr = self._local_sock.getsockname()[0]
bind_addr = bind_addr.replace("::ffff:", "")
local_addrs = socket.getaddrinfo(bind_addr, port, 0, socket.SOCK_STREAM, socket.SOL_TCP)
if local_addrs[0][0] == af:
logging.debug("bind %s" % (bind_addr,))
remote_sock.bind((bind_addr, 0))
return remote_sock return remote_sock
def _handle_dns_resolved(self, result, error): def _handle_dns_resolved(self, result, error):

Loading…
Cancel
Save