Python port of ShadowsocksR
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.8 KiB

13 years ago
#!/usr/bin/env python
11 years ago
# -*- coding: utf-8 -*-
13 years ago
11 years ago
# Copyright (c) 2014 clowwindy
13 years ago
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
12 years ago
import sys
11 years ago
import os
11 years ago
import logging
12 years ago
import utils
11 years ago
import encrypt
import eventloop
11 years ago
import tcprelay
11 years ago
import udprelay
13 years ago
12 years ago
def main():
11 years ago
utils.check_python()
11 years ago
config = utils.get_config(False)
12 years ago
11 years ago
utils.print_shadowsocks()
11 years ago
if config['port_password']:
if config['server_port'] or config['password']:
11 years ago
logging.warn('warning: port_password should not be used with '
'server_port and password. server_port and password '
'will be ignored')
else:
11 years ago
config['port_password'] = {}
config['port_password'][str(config['server_port'])] = config['password']
11 years ago
11 years ago
encrypt.init_table(config['password'], config['method'])
11 years ago
tcp_servers = []
udp_servers = []
11 years ago
for port, password in config['port_password'].items():
a_config = config.copy()
11 years ago
a_config['server_port'] = int(port)
11 years ago
a_config['password'] = password
11 years ago
logging.info("starting server at %s:%d" %
11 years ago
(a_config['server'], int(port)))
11 years ago
tcp_servers.append(tcprelay.TCPRelay(a_config, False))
udp_servers.append(udprelay.UDPRelay(a_config, False))
11 years ago
def run_server():
11 years ago
try:
11 years ago
loop = eventloop.EventLoop()
11 years ago
for tcp_server in tcp_servers:
11 years ago
tcp_server.add_to_loop(loop)
11 years ago
for udp_server in udp_servers:
11 years ago
udp_server.add_to_loop(loop)
loop.run()
11 years ago
except (KeyboardInterrupt, IOError, OSError) as e:
logging.error(e)
11 years ago
os._exit(0)
11 years ago
11 years ago
if int(config['workers']) > 1:
11 years ago
if os.name == 'posix':
11 years ago
children = []
is_child = False
11 years ago
for i in xrange(0, int(config['workers'])):
11 years ago
r = os.fork()
if r == 0:
11 years ago
logging.info('worker started')
is_child = True
run_server()
11 years ago
break
11 years ago
else:
children.append(r)
if not is_child:
11 years ago
def handler(signum, _):
11 years ago
for pid in children:
os.kill(pid, signum)
os.waitpid(pid, 0)
sys.exit()
import signal
signal.signal(signal.SIGTERM, handler)
11 years ago
11 years ago
# master
11 years ago
for a_tcp_server in tcp_servers:
a_tcp_server.close()
for a_udp_server in udp_servers:
a_udp_server.close()
11 years ago
11 years ago
for child in children:
os.waitpid(child, 0)
11 years ago
else:
logging.warn('worker is only available on Unix/Linux')
11 years ago
run_server()
11 years ago
else:
run_server()
11 years ago
12 years ago
if __name__ == '__main__':
11 years ago
main()