<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> import os <EOL> import json <EOL> import sys <EOL> import getopt <EOL> import logging <EOL> from shadowsocks . common import to_bytes , to_str , IPNetwork <EOL> from shadowsocks import encrypt <EOL> VERBOSE_LEVEL = <NUM_LIT:5> <EOL> verbose = <NUM_LIT:0> <EOL> def check_python ( ) : <EOL> info = sys . version_info <EOL> if info [ <NUM_LIT:0> ] == <NUM_LIT:2> and not info [ <NUM_LIT:1> ] >= <NUM_LIT:6> : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> elif info [ <NUM_LIT:0> ] == <NUM_LIT:3> and not info [ <NUM_LIT:1> ] >= <NUM_LIT:3> : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> elif info [ <NUM_LIT:0> ] not in [ <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def print_exception ( e ) : <EOL> global verbose <EOL> logging . error ( e ) <EOL> if verbose > <NUM_LIT:0> : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> def print_shadowsocks ( ) : <EOL> version = '<STR_LIT>' <EOL> try : <EOL> import pkg_resources <EOL> version = pkg_resources . get_distribution ( '<STR_LIT>' ) . version <EOL> except Exception : <EOL> pass <EOL> print ( '<STR_LIT>' % version ) <EOL> def find_config ( ) : <EOL> config_path = '<STR_LIT>' <EOL> if os . path . exists ( config_path ) : <EOL> return config_path <EOL> config_path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os . path . exists ( config_path ) : <EOL> return config_path <EOL> return None <EOL> def check_config ( config , is_local ) : <EOL> if config . get ( '<STR_LIT>' , None ) == '<STR_LIT>' : <EOL> return <EOL> if is_local and not config . get ( '<STR_LIT:password>' , None ) : <EOL> logging . error ( '<STR_LIT>' ) <EOL> print_help ( is_local ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if not is_local and not config . get ( '<STR_LIT:password>' , None ) and not config . get ( '<STR_LIT>' , None ) : <EOL> logging . error ( '<STR_LIT>' ) <EOL> print_help ( is_local ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if '<STR_LIT>' in config : <EOL> config [ '<STR_LIT>' ] = int ( config [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in config and type ( config [ '<STR_LIT>' ] ) != list : <EOL> config [ '<STR_LIT>' ] = int ( config [ '<STR_LIT>' ] ) <EOL> if config . get ( '<STR_LIT>' , '<STR_LIT>' ) in [ b'<STR_LIT>' ] : <EOL> logging . warn ( '<STR_LIT>' ) <EOL> if config . get ( '<STR_LIT>' , '<STR_LIT>' ) in [ '<STR_LIT:127.0.0.1>' , '<STR_LIT:localhost>' ] : <EOL> logging . warn ( '<STR_LIT>' % <EOL> ( to_str ( config [ '<STR_LIT>' ] ) , config [ '<STR_LIT>' ] ) ) <EOL> if ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) or '<STR_LIT>' ) . lower ( ) == '<STR_LIT>' : <EOL> logging . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) or '<STR_LIT>' ) . lower ( ) == '<STR_LIT>' : <EOL> logging . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if config . get ( '<STR_LIT>' , <NUM_LIT> ) < <NUM_LIT:100> : <EOL> logging . warn ( '<STR_LIT>' % <EOL> int ( config . get ( '<STR_LIT>' ) ) ) <EOL> if config . get ( '<STR_LIT>' , <NUM_LIT> ) > <NUM_LIT> : <EOL> logging . warn ( '<STR_LIT>' % <EOL> int ( config . get ( '<STR_LIT>' ) ) ) <EOL> if config . get ( '<STR_LIT:password>' ) in [ b'<STR_LIT>' ] : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if config . get ( '<STR_LIT:user>' , None ) is not None : <EOL> if os . name != '<STR_LIT>' : <EOL> logging . error ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> encrypt . try_cipher ( config [ '<STR_LIT:password>' ] , config [ '<STR_LIT>' ] ) <EOL> def get_config ( is_local ) : <EOL> global verbose <EOL> logging . basicConfig ( level = logging . INFO , <EOL> format = '<STR_LIT>' ) <EOL> if is_local : <EOL> shortopts = '<STR_LIT>' <EOL> longopts = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:version>' ] <EOL> else : <EOL> shortopts = '<STR_LIT>' <EOL> longopts = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:version>' ] <EOL> try : <EOL> config_path = find_config ( ) <EOL> optlist , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , shortopts , longopts ) <EOL> for key , value in optlist : <EOL> if key == '<STR_LIT:-c>' : <EOL> config_path = value <EOL> if config_path : <EOL> logging . info ( '<STR_LIT>' % config_path ) <EOL> with open ( config_path , '<STR_LIT:rb>' ) as f : <EOL> try : <EOL> config = parse_json_in_str ( f . read ( ) . decode ( '<STR_LIT:utf8>' ) ) <EOL> except ValueError as e : <EOL> logging . error ( '<STR_LIT>' , <EOL> e . message ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> config = { } <EOL> v_count = <NUM_LIT:0> <EOL> for key , value in optlist : <EOL> if key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = int ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT:password>' ] = to_bytes ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = int ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = to_str ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = to_str ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = to_str ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> v_count += <NUM_LIT:1> <EOL> config [ '<STR_LIT>' ] = v_count <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = int ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = True <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = int ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = value <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT:user>' ] = to_str ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = to_str ( value ) . split ( '<STR_LIT:U+002C>' ) <EOL> elif key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if is_local : <EOL> print_local_help ( ) <EOL> else : <EOL> print_server_help ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> elif key == '<STR_LIT>' : <EOL> print_shadowsocks ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = to_str ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = to_str ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = to_str ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> v_count -= <NUM_LIT:1> <EOL> config [ '<STR_LIT>' ] = v_count <EOL> except getopt . GetoptError as e : <EOL> print ( e , file = sys . stderr ) <EOL> print_help ( is_local ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if not config : <EOL> logging . error ( '<STR_LIT>' ) <EOL> print_help ( is_local ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> config [ '<STR_LIT:password>' ] = to_bytes ( config . get ( '<STR_LIT:password>' , b'<STR_LIT>' ) ) <EOL> config [ '<STR_LIT>' ] = to_str ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , None ) <EOL> config [ '<STR_LIT>' ] = int ( config . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , False ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , False ) <EOL> config [ '<STR_LIT>' ] = to_str ( config . get ( '<STR_LIT>' , '<STR_LIT:127.0.0.1>' ) ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> if is_local : <EOL> if config . get ( '<STR_LIT>' , None ) is None : <EOL> logging . error ( '<STR_LIT>' ) <EOL> print_local_help ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> else : <EOL> config [ '<STR_LIT>' ] = to_str ( config [ '<STR_LIT>' ] ) <EOL> else : <EOL> config [ '<STR_LIT>' ] = to_str ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> try : <EOL> config [ '<STR_LIT>' ] = IPNetwork ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except Exception as e : <EOL> logging . error ( e ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> logging . getLogger ( '<STR_LIT>' ) . handlers = [ ] <EOL> logging . addLevelName ( VERBOSE_LEVEL , '<STR_LIT>' ) <EOL> if config [ '<STR_LIT>' ] >= <NUM_LIT:2> : <EOL> level = VERBOSE_LEVEL <EOL> elif config [ '<STR_LIT>' ] == <NUM_LIT:1> : <EOL> level = logging . DEBUG <EOL> elif config [ '<STR_LIT>' ] == - <NUM_LIT:1> : <EOL> level = logging . WARN <EOL> elif config [ '<STR_LIT>' ] <= - <NUM_LIT:2> : <EOL> level = logging . ERROR <EOL> else : <EOL> level = logging . INFO <EOL> verbose = config [ '<STR_LIT>' ] <EOL> logging . basicConfig ( level = level , <EOL> format = '<STR_LIT>' , <EOL> datefmt = '<STR_LIT>' ) <EOL> check_config ( config , is_local ) <EOL> return config <EOL> def print_help ( is_local ) : <EOL> if is_local : <EOL> print_local_help ( ) <EOL> else : <EOL> print_server_help ( ) <EOL> def print_local_help ( ) : <EOL> print ( '''<STR_LIT>''' ) <EOL> def print_server_help ( ) : <EOL> print ( '''<STR_LIT>''' ) <EOL> def _decode_list ( data ) : <EOL> rv = [ ] <EOL> for item in data : <EOL> if hasattr ( item , '<STR_LIT>' ) : <EOL> item = item . encode ( '<STR_LIT:utf-8>' ) <EOL> elif isinstance ( item , list ) : <EOL> item = _decode_list ( item ) <EOL> elif isinstance ( item , dict ) : <EOL> item = _decode_dict ( item ) <EOL> rv . append ( item ) <EOL> return rv <EOL> def _decode_dict ( data ) : <EOL> rv = { } <EOL> for key , value in data . items ( ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> elif isinstance ( value , list ) : <EOL> value = _decode_list ( value ) <EOL> elif isinstance ( value , dict ) : <EOL> value = _decode_dict ( value ) <EOL> rv [ key ] = value <EOL> return rv <EOL> def parse_json_in_str ( data ) : <EOL> return json . loads ( data , object_hook = _decode_dict ) </s>
