shadowsocks/shadowsocks/local.py

78 lines
2.7 KiB
Python
Raw Normal View History

2012-04-20 14:26:00 +00:00
#!/usr/bin/env python
2014-04-23 08:31:17 +00:00
# -*- coding: utf-8 -*-
2012-04-20 14:26:00 +00:00
2014-04-07 09:03:35 +00:00
# Copyright (c) 2014 clowwindy
2012-04-20 14:26:00 +00:00
#
# 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.
2014-10-31 10:28:22 +00:00
from __future__ import absolute_import, division, print_function, \
with_statement
2012-12-30 05:28:51 +00:00
import sys
2012-11-02 09:36:57 +00:00
import os
import logging
import signal
2014-10-31 08:29:19 +00:00
2014-10-31 10:28:22 +00:00
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../'))
2014-10-31 08:29:19 +00:00
from shadowsocks import utils, encrypt, eventloop, tcprelay, udprelay, asyncdns
2012-04-20 14:26:00 +00:00
2013-06-22 09:30:31 +00:00
def main():
2014-06-01 06:10:18 +00:00
utils.check_python()
2013-06-22 09:30:31 +00:00
# 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)
2014-06-01 06:10:18 +00:00
config = utils.get_config(True)
2013-07-04 15:16:10 +00:00
2014-06-01 06:20:40 +00:00
utils.print_shadowsocks()
encrypt.try_cipher(config['password'], config['method'])
2013-07-04 15:16:10 +00:00
2012-12-06 06:39:06 +00:00
try:
2014-04-23 08:36:27 +00:00
logging.info("starting local at %s:%d" %
2014-06-01 06:10:18 +00:00
(config['local_address'], config['local_port']))
2014-06-08 07:58:59 +00:00
dns_resolver = asyncdns.DNSResolver()
tcp_server = tcprelay.TCPRelay(config, dns_resolver, True)
udp_server = udprelay.UDPRelay(config, dns_resolver, True)
2014-06-01 11:09:52 +00:00
loop = eventloop.EventLoop()
2014-06-08 07:58:59 +00:00
dns_resolver.add_to_loop(loop)
2014-06-01 11:09:52 +00:00
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)
2014-09-20 11:11:33 +00:00
signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler)
2014-06-01 11:09:52 +00:00
loop.run()
2014-06-01 09:56:41 +00:00
except (KeyboardInterrupt, IOError, OSError) as e:
logging.error(e)
2014-09-12 15:06:18 +00:00
if config['verbose']:
import traceback
traceback.print_exc()
2014-09-12 14:20:33 +00:00
os._exit(1)
2013-06-22 09:30:31 +00:00
if __name__ == '__main__':
main()