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.

81 lines
2.4 KiB

13 years ago
#!/usr/bin/env python
11 years ago
# -*- coding: utf-8 -*-
13 years ago
#
# Copyright 2012-2015 clowwindy
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
13 years ago
#
# http://www.apache.org/licenses/LICENSE-2.0
13 years ago
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
13 years ago
from __future__ import absolute_import, division, print_function, \
with_statement
12 years ago
import sys
import os
import logging
import signal
if __name__ == '__main__':
import inspect
file_path = os.path.dirname(os.path.realpath(inspect.getfile(inspect.currentframe())))
os.chdir(file_path)
sys.path.insert(0, os.path.join(file_path, '../'))
from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, asyncdns
13 years ago
12 years ago
def main():
shell.check_python()
12 years ago
# fix py2exe
if hasattr(sys, "frozen") and sys.frozen in \
("windows_exe", "console_exe"):
p = os.path.dirname(os.path.abspath(sys.executable))
os.chdir(p)
config = shell.get_config(True)
9 years ago
if not config.get('ipv6', False):
asyncdns.IPV6_CONNECTION_SUPPORT = False
10 years ago
daemon.daemon_exec(config)
12 years ago
try:
11 years ago
logging.info("starting local at %s:%d" %
11 years ago
(config['local_address'], config['local_port']))
11 years ago
dns_resolver = asyncdns.DNSResolver()
tcp_server = tcprelay.TCPRelay(config, dns_resolver, True)
udp_server = udprelay.UDPRelay(config, dns_resolver, True)
11 years ago
loop = eventloop.EventLoop()
11 years ago
dns_resolver.add_to_loop(loop)
11 years ago
tcp_server.add_to_loop(loop)
udp_server.add_to_loop(loop)
def handler(signum, _):
logging.warn('received SIGQUIT, doing graceful shutting down..')
tcp_server.close(next_tick=True)
udp_server.close(next_tick=True)
10 years ago
signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler)
def int_handler(signum, _):
sys.exit(1)
signal.signal(signal.SIGINT, int_handler)
daemon.set_user(config.get('user', None))
11 years ago
loop.run()
10 years ago
except Exception as e:
shell.print_exception(e)
10 years ago
sys.exit(1)
12 years ago
if __name__ == '__main__':
main()