<s> '''<STR_LIT>''' <EOL> import logging <EOL> import socket <EOL> import socks <EOL> import os <EOL> import time <EOL> import json <EOL> import traceback <EOL> def encode ( data ) : <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> return data . encode ( '<STR_LIT:utf-8>' ) <EOL> return data <EOL> def load_config ( ) : <EOL> fmt = '<STR_LIT>' <EOL> logging . basicConfig ( filename = os . path . join ( os . getcwd ( ) , '<STR_LIT>' ) , level = logging . DEBUG , format = fmt ) <EOL> console = logging . StreamHandler ( ) <EOL> console . setLevel ( logging . DEBUG ) <EOL> formatter = logging . Formatter ( fmt ) <EOL> console . setFormatter ( formatter ) <EOL> logging . getLogger ( '<STR_LIT>' ) . addHandler ( console ) <EOL> bind = ( "<STR_LIT>" , <NUM_LIT> ) <EOL> dns_list = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> proxy = ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) <EOL> try : <EOL> f = file ( os . path . join ( os . getcwd ( ) , "<STR_LIT>" ) , "<STR_LIT:r>" ) <EOL> config = json . loads ( f . read ( ) ) <EOL> if config : <EOL> local_address = config . get ( "<STR_LIT>" , None ) <EOL> local_port = config . get ( "<STR_LIT>" , None ) <EOL> dns = config . get ( "<STR_LIT>" , None ) <EOL> proxy_address = config . get ( "<STR_LIT>" , None ) <EOL> proxy_port = config . get ( "<STR_LIT>" , None ) <EOL> if local_address is not None and local_port is not None : <EOL> bind = ( local_address , local_port ) <EOL> logging . info ( "<STR_LIT>" % ( local_address , local_port ) ) <EOL> if proxy_address is not None and proxy_port is not None : <EOL> proxy = ( proxy_address , proxy_port ) <EOL> logging . info ( "<STR_LIT>" % ( proxy_address , proxy_port ) ) <EOL> if dns is not None : <EOL> dns_list = [ ] <EOL> for item in dns : <EOL> dns_list . append ( encode ( item ) ) <EOL> logging . info ( "<STR_LIT>" % ( dns_list , ) ) <EOL> except Exception as e : <EOL> logging . error ( e ) <EOL> pass <EOL> return bind , dns_list , proxy <EOL> def test ( bindaddr , dnslist , proxy ) : <EOL> for try_cnt in xrange ( <NUM_LIT:4> ) : <EOL> if proxy : <EOL> addrs = socket . getaddrinfo ( proxy [ <NUM_LIT:0> ] , <NUM_LIT:0> , <NUM_LIT:0> , socket . SOCK_DGRAM , socket . SOL_UDP ) <EOL> af , socktype , proto , canonname , sa = addrs [ <NUM_LIT:0> ] <EOL> s = socks . socksocket ( af , socket . SOCK_DGRAM ) <EOL> s . set_proxy ( socks . SOCKS5 , * proxy ) <EOL> else : <EOL> addrs = socket . getaddrinfo ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , socket . SOCK_DGRAM , socket . SOL_UDP ) <EOL> af , socktype , proto , canonname , sa = addrs [ <NUM_LIT:0> ] <EOL> s = socket . socket ( af , socktype , proto ) <EOL> s . setblocking ( False ) <EOL> try : <EOL> r = s . sendto ( "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" , <EOL> ( dnslist [ <NUM_LIT:0> ] , <NUM_LIT> ) ) <EOL> except Exception as e : <EOL> traceback . print_exc ( ) <EOL> break <EOL> res = None <EOL> for i in xrange ( <NUM_LIT:5> * <NUM_LIT:5> ) : <EOL> time . sleep ( <NUM_LIT> ) <EOL> try : <EOL> res = s . recvfrom ( <NUM_LIT> ) <EOL> break <EOL> except Exception as e : <EOL> pass <EOL> if res is not None : <EOL> print ( '<STR_LIT>' % ( proxy , ) ) <EOL> break <EOL> else : <EOL> print ( '<STR_LIT>' % ( proxy , ) ) <EOL> s . close ( ) <EOL> print ( "<STR_LIT>" ) <EOL> try : <EOL> input ( ) <EOL> except Exception as e : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( * load_config ( ) ) </s>
