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.
359 lines
12 KiB
359 lines
12 KiB
12 years ago
|
#!/usr/bin/python
|
||
|
# -*- coding: utf-8 -*-
|
||
11 years ago
|
#
|
||
10 years ago
|
# Copyright 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
|
||
11 years ago
|
#
|
||
10 years ago
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
11 years ago
|
#
|
||
10 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.
|
||
11 years ago
|
|
||
10 years ago
|
from __future__ import absolute_import, division, print_function, \
|
||
|
with_statement
|
||
|
|
||
12 years ago
|
import os
|
||
11 years ago
|
import json
|
||
|
import sys
|
||
|
import getopt
|
||
12 years ago
|
import logging
|
||
10 years ago
|
from shadowsocks.common import to_bytes, to_str, IPNetwork
|
||
10 years ago
|
from shadowsocks import encrypt
|
||
12 years ago
|
|
||
|
|
||
11 years ago
|
VERBOSE_LEVEL = 5
|
||
|
|
||
10 years ago
|
verbose = 0
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
def check_python():
|
||
|
info = sys.version_info
|
||
10 years ago
|
if info[0] == 2 and not info[1] >= 6:
|
||
|
print('Python 2.6+ required')
|
||
|
sys.exit(1)
|
||
10 years ago
|
elif info[0] == 3 and not info[1] >= 3:
|
||
10 years ago
|
print('Python 3.3+ required')
|
||
|
sys.exit(1)
|
||
10 years ago
|
elif info[0] not in [2, 3]:
|
||
10 years ago
|
print('Python version not supported')
|
||
|
sys.exit(1)
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
def print_exception(e):
|
||
|
global verbose
|
||
|
logging.error(e)
|
||
|
if verbose > 0:
|
||
|
import traceback
|
||
|
traceback.print_exc()
|
||
|
|
||
|
|
||
11 years ago
|
def print_shadowsocks():
|
||
|
version = ''
|
||
|
try:
|
||
|
import pkg_resources
|
||
|
version = pkg_resources.get_distribution('shadowsocks').version
|
||
|
except Exception:
|
||
|
pass
|
||
10 years ago
|
print('Shadowsocks %s' % version)
|
||
11 years ago
|
|
||
|
|
||
12 years ago
|
def find_config():
|
||
|
config_path = 'config.json'
|
||
|
if os.path.exists(config_path):
|
||
|
return config_path
|
||
|
config_path = os.path.join(os.path.dirname(__file__), '../', 'config.json')
|
||
|
if os.path.exists(config_path):
|
||
|
return config_path
|
||
|
return None
|
||
12 years ago
|
|
||
11 years ago
|
|
||
10 years ago
|
def check_config(config, is_local):
|
||
10 years ago
|
if config.get('daemon', None) == 'stop':
|
||
|
# no need to specify configuration for daemon stop
|
||
|
return
|
||
|
|
||
10 years ago
|
if is_local and not config.get('password', None):
|
||
|
logging.error('password not specified')
|
||
|
print_help(is_local)
|
||
|
sys.exit(2)
|
||
|
|
||
|
if not is_local and not config.get('password', None) \
|
||
|
and not config.get('port_password', None):
|
||
|
logging.error('password or port_password not specified')
|
||
|
print_help(is_local)
|
||
|
sys.exit(2)
|
||
|
|
||
|
if 'local_port' in config:
|
||
|
config['local_port'] = int(config['local_port'])
|
||
|
|
||
|
if 'server_port' in config and type(config['server_port']) != list:
|
||
|
config['server_port'] = int(config['server_port'])
|
||
|
|
||
10 years ago
|
if config.get('local_address', '') in [b'0.0.0.0']:
|
||
10 years ago
|
logging.warn('warning: local set to listen on 0.0.0.0, it\'s not safe')
|
||
10 years ago
|
if config.get('server', '') in ['127.0.0.1', 'localhost']:
|
||
10 years ago
|
logging.warn('warning: server set to listen on %s:%s, are you sure?' %
|
||
10 years ago
|
(to_str(config['server']), config['server_port']))
|
||
10 years ago
|
if (config.get('method', '') or '').lower() == 'table':
|
||
11 years ago
|
logging.warn('warning: table is not safe; please use a safer cipher, '
|
||
|
'like AES-256-CFB')
|
||
10 years ago
|
if (config.get('method', '') or '').lower() == 'rc4':
|
||
11 years ago
|
logging.warn('warning: RC4 is not safe; please use a safer cipher, '
|
||
11 years ago
|
'like AES-256-CFB')
|
||
11 years ago
|
if config.get('timeout', 300) < 100:
|
||
11 years ago
|
logging.warn('warning: your timeout %d seems too short' %
|
||
11 years ago
|
int(config.get('timeout')))
|
||
11 years ago
|
if config.get('timeout', 300) > 600:
|
||
11 years ago
|
logging.warn('warning: your timeout %d seems too long' %
|
||
11 years ago
|
int(config.get('timeout')))
|
||
10 years ago
|
if config.get('password') in [b'mypassword']:
|
||
11 years ago
|
logging.error('DON\'T USE DEFAULT PASSWORD! Please change it in your '
|
||
|
'config.json!')
|
||
10 years ago
|
sys.exit(1)
|
||
|
if config.get('user', None) is not None:
|
||
|
if os.name != 'posix':
|
||
|
logging.error('user can be used only on Unix')
|
||
|
sys.exit(1)
|
||
11 years ago
|
|
||
10 years ago
|
encrypt.try_cipher(config['password'], config['method'])
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
def get_config(is_local):
|
||
10 years ago
|
global verbose
|
||
|
|
||
11 years ago
|
logging.basicConfig(level=logging.INFO,
|
||
10 years ago
|
format='%(levelname)-s: %(message)s')
|
||
11 years ago
|
if is_local:
|
||
10 years ago
|
shortopts = 'hd:s:b:p:k:l:m:c:t:vq'
|
||
10 years ago
|
longopts = ['help', 'fast-open', 'pid-file=', 'log-file=', 'user=',
|
||
|
'version']
|
||
11 years ago
|
else:
|
||
10 years ago
|
shortopts = 'hd:s:p:k:m:c:t:vq'
|
||
10 years ago
|
longopts = ['help', 'fast-open', 'pid-file=', 'log-file=', 'workers=',
|
||
10 years ago
|
'forbidden-ip=', 'user=', 'version']
|
||
11 years ago
|
try:
|
||
|
config_path = find_config()
|
||
|
optlist, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
|
||
|
for key, value in optlist:
|
||
|
if key == '-c':
|
||
|
config_path = value
|
||
|
|
||
|
if config_path:
|
||
|
logging.info('loading config from %s' % config_path)
|
||
|
with open(config_path, 'rb') as f:
|
||
|
try:
|
||
10 years ago
|
config = json.loads(f.read().decode('utf8'),
|
||
|
object_hook=_decode_dict)
|
||
11 years ago
|
except ValueError as e:
|
||
|
logging.error('found an error in config.json: %s',
|
||
|
e.message)
|
||
|
sys.exit(1)
|
||
|
else:
|
||
|
config = {}
|
||
|
|
||
11 years ago
|
v_count = 0
|
||
11 years ago
|
for key, value in optlist:
|
||
|
if key == '-p':
|
||
|
config['server_port'] = int(value)
|
||
|
elif key == '-k':
|
||
10 years ago
|
config['password'] = to_bytes(value)
|
||
11 years ago
|
elif key == '-l':
|
||
|
config['local_port'] = int(value)
|
||
|
elif key == '-s':
|
||
10 years ago
|
config['server'] = to_str(value)
|
||
11 years ago
|
elif key == '-m':
|
||
10 years ago
|
config['method'] = to_str(value)
|
||
11 years ago
|
elif key == '-b':
|
||
10 years ago
|
config['local_address'] = to_str(value)
|
||
11 years ago
|
elif key == '-v':
|
||
11 years ago
|
v_count += 1
|
||
|
# '-vv' turns on more verbose mode
|
||
|
config['verbose'] = v_count
|
||
11 years ago
|
elif key == '-t':
|
||
|
config['timeout'] = int(value)
|
||
11 years ago
|
elif key == '--fast-open':
|
||
|
config['fast_open'] = True
|
||
|
elif key == '--workers':
|
||
10 years ago
|
config['workers'] = int(value)
|
||
10 years ago
|
elif key == '--user':
|
||
|
config['user'] = to_str(value)
|
||
10 years ago
|
elif key == '--forbidden-ip':
|
||
|
config['forbidden_ip'] = to_str(value).split(',')
|
||
10 years ago
|
elif key in ('-h', '--help'):
|
||
11 years ago
|
if is_local:
|
||
|
print_local_help()
|
||
|
else:
|
||
|
print_server_help()
|
||
|
sys.exit(0)
|
||
10 years ago
|
elif key == '--version':
|
||
|
print_shadowsocks()
|
||
|
sys.exit(0)
|
||
10 years ago
|
elif key == '-d':
|
||
10 years ago
|
config['daemon'] = to_str(value)
|
||
10 years ago
|
elif key == '--pid-file':
|
||
10 years ago
|
config['pid-file'] = to_str(value)
|
||
10 years ago
|
elif key == '--log-file':
|
||
10 years ago
|
config['log-file'] = to_str(value)
|
||
11 years ago
|
elif key == '-q':
|
||
|
v_count -= 1
|
||
|
config['verbose'] = v_count
|
||
11 years ago
|
except getopt.GetoptError as e:
|
||
10 years ago
|
print(e, file=sys.stderr)
|
||
11 years ago
|
print_help(is_local)
|
||
11 years ago
|
sys.exit(2)
|
||
|
|
||
11 years ago
|
if not config:
|
||
|
logging.error('config not specified')
|
||
|
print_help(is_local)
|
||
|
sys.exit(2)
|
||
11 years ago
|
|
||
10 years ago
|
config['password'] = to_bytes(config.get('password', b''))
|
||
|
config['method'] = to_str(config.get('method', 'aes-256-cfb'))
|
||
11 years ago
|
config['port_password'] = config.get('port_password', None)
|
||
|
config['timeout'] = int(config.get('timeout', 300))
|
||
|
config['fast_open'] = config.get('fast_open', False)
|
||
|
config['workers'] = config.get('workers', 1)
|
||
10 years ago
|
config['pid-file'] = config.get('pid-file', '/var/run/shadowsocks.pid')
|
||
|
config['log-file'] = config.get('log-file', '/var/log/shadowsocks.log')
|
||
11 years ago
|
config['verbose'] = config.get('verbose', False)
|
||
10 years ago
|
config['local_address'] = to_str(config.get('local_address', '127.0.0.1'))
|
||
11 years ago
|
config['local_port'] = config.get('local_port', 1080)
|
||
|
if is_local:
|
||
|
if config.get('server', None) is None:
|
||
|
logging.error('server addr not specified')
|
||
|
print_local_help()
|
||
|
sys.exit(2)
|
||
10 years ago
|
else:
|
||
|
config['server'] = to_str(config['server'])
|
||
11 years ago
|
else:
|
||
10 years ago
|
config['server'] = to_str(config.get('server', '0.0.0.0'))
|
||
10 years ago
|
try:
|
||
|
config['forbidden_ip'] = \
|
||
|
IPNetwork(config.get('forbidden_ip', '127.0.0.0/8,::1/128'))
|
||
|
except Exception as e:
|
||
|
logging.error(e)
|
||
|
sys.exit(2)
|
||
11 years ago
|
config['server_port'] = config.get('server_port', 8388)
|
||
|
|
||
11 years ago
|
logging.getLogger('').handlers = []
|
||
|
logging.addLevelName(VERBOSE_LEVEL, 'VERBOSE')
|
||
11 years ago
|
if config['verbose'] >= 2:
|
||
11 years ago
|
level = VERBOSE_LEVEL
|
||
11 years ago
|
elif config['verbose'] == 1:
|
||
11 years ago
|
level = logging.DEBUG
|
||
11 years ago
|
elif config['verbose'] == -1:
|
||
|
level = logging.WARN
|
||
|
elif config['verbose'] <= -2:
|
||
|
level = logging.ERROR
|
||
11 years ago
|
else:
|
||
11 years ago
|
level = logging.INFO
|
||
10 years ago
|
verbose = config['verbose']
|
||
11 years ago
|
logging.basicConfig(level=level,
|
||
|
format='%(asctime)s %(levelname)-8s %(message)s',
|
||
10 years ago
|
datefmt='%Y-%m-%d %H:%M:%S')
|
||
11 years ago
|
|
||
10 years ago
|
check_config(config, is_local)
|
||
11 years ago
|
|
||
11 years ago
|
return config
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
def print_help(is_local):
|
||
|
if is_local:
|
||
|
print_local_help()
|
||
|
else:
|
||
|
print_server_help()
|
||
|
|
||
|
|
||
11 years ago
|
def print_local_help():
|
||
10 years ago
|
print('''usage: sslocal [OPTION]...
|
||
10 years ago
|
A fast tunnel proxy that helps you bypass firewalls.
|
||
|
|
||
|
You can supply configurations via either config file or command line arguments.
|
||
|
|
||
|
Proxy options:
|
||
|
-c CONFIG path to config file
|
||
|
-s SERVER_ADDR server address
|
||
|
-p SERVER_PORT server port, default: 8388
|
||
|
-b LOCAL_ADDR local binding address, default: 127.0.0.1
|
||
|
-l LOCAL_PORT local port, default: 1080
|
||
|
-k PASSWORD password
|
||
|
-m METHOD encryption method, default: aes-256-cfb
|
||
|
-t TIMEOUT timeout in seconds, default: 300
|
||
|
--fast-open use TCP_FASTOPEN, requires Linux 3.7+
|
||
|
|
||
|
General options:
|
||
10 years ago
|
-h, --help show this help message and exit
|
||
10 years ago
|
-d start/stop/restart daemon mode
|
||
|
--pid-file PID_FILE pid file for daemon mode
|
||
|
--log-file LOG_FILE log file for daemon mode
|
||
10 years ago
|
--user USER username to run as
|
||
10 years ago
|
-v, -vv verbose mode
|
||
|
-q, -qq quiet mode, only show warnings/errors
|
||
10 years ago
|
--version show version information
|
||
11 years ago
|
|
||
10 years ago
|
Online help: <https://github.com/shadowsocks/shadowsocks>
|
||
10 years ago
|
''')
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
def print_server_help():
|
||
10 years ago
|
print('''usage: ssserver [OPTION]...
|
||
10 years ago
|
A fast tunnel proxy that helps you bypass firewalls.
|
||
|
|
||
|
You can supply configurations via either config file or command line arguments.
|
||
|
|
||
|
Proxy options:
|
||
|
-c CONFIG path to config file
|
||
|
-s SERVER_ADDR server address, default: 0.0.0.0
|
||
|
-p SERVER_PORT server port, default: 8388
|
||
|
-k PASSWORD password
|
||
|
-m METHOD encryption method, default: aes-256-cfb
|
||
|
-t TIMEOUT timeout in seconds, default: 300
|
||
|
--fast-open use TCP_FASTOPEN, requires Linux 3.7+
|
||
|
--workers WORKERS number of workers, available on Unix/Linux
|
||
10 years ago
|
--forbidden-ip IPLIST comma seperated IP list forbidden to connect
|
||
10 years ago
|
|
||
|
General options:
|
||
10 years ago
|
-h, --help show this help message and exit
|
||
10 years ago
|
-d start/stop/restart daemon mode
|
||
|
--pid-file PID_FILE pid file for daemon mode
|
||
|
--log-file LOG_FILE log file for daemon mode
|
||
10 years ago
|
--user USER username to run as
|
||
10 years ago
|
-v, -vv verbose mode
|
||
|
-q, -qq quiet mode, only show warnings/errors
|
||
10 years ago
|
--version show version information
|
||
11 years ago
|
|
||
10 years ago
|
Online help: <https://github.com/shadowsocks/shadowsocks>
|
||
10 years ago
|
''')
|
||
11 years ago
|
|
||
|
|
||
|
def _decode_list(data):
|
||
|
rv = []
|
||
|
for item in data:
|
||
10 years ago
|
if hasattr(item, 'encode'):
|
||
11 years ago
|
item = item.encode('utf-8')
|
||
|
elif isinstance(item, list):
|
||
|
item = _decode_list(item)
|
||
|
elif isinstance(item, dict):
|
||
|
item = _decode_dict(item)
|
||
|
rv.append(item)
|
||
|
return rv
|
||
|
|
||
|
|
||
|
def _decode_dict(data):
|
||
|
rv = {}
|
||
10 years ago
|
for key, value in data.items():
|
||
|
if hasattr(value, 'encode'):
|
||
11 years ago
|
value = value.encode('utf-8')
|
||
|
elif isinstance(value, list):
|
||
|
value = _decode_list(value)
|
||
|
elif isinstance(value, dict):
|
||
|
value = _decode_dict(value)
|
||
|
rv[key] = value
|
||
10 years ago
|
return rv
|