Exemplo n.º 1
0
 def remove_dead_server(self, link):
     servers = self.get_dead_server_list()
     config = ssrlink.decode_ssrlink(link)
     addr, port = config['server'], config['server_port']
     for link_ in servers:
         config_ = ssrlink.decode_ssrlink(link_)
         addr_, port_ = config_['server'], config_['server_port']
         if addr == addr_ and port == port_:
             servers.remove(link_)
     self.update_dead_server_list(servers)
Exemplo n.º 2
0
def user_chooice(options, message):
    # for i in range(len(options)):
    #     print('%-2d ' % (i+1) + options[i])
    print_server_info((decode_ssrlink(ssr) if isinstance(ssr, str) else ssr
                       for ssr in options),
                      indexed=True)
    print(message)
    return input()
Exemplo n.º 3
0
 def list(self):
     target = self.target
     ssrs = target.get_server_list()
     servers = [decode_ssrlink(ssr) for ssr in ssrs]
     header = ' ' * 40 + 'SERVER LIST' + ' ' * 40
     print_server_info(servers,
                       header=header,
                       indexed=True,
                       verbose=True,
                       hightlight=True)
Exemplo n.º 4
0
 def filter(self):
     target = self.target
     for link in target.get_server_list():
         server_config = decode_ssrlink(link)
         latency = network.ping(server_config['server'],
                                int(server_config['server_port']))
         print('server %s:%s\t\tlatency: %.2f' %
               (server_config['server'], server_config['server_port'],
                latency))
         if latency == float('inf'):
             print('move server to dead group')
             target.config_manager.set_server_dead(link)
Exemplo n.º 5
0
 def random_switch_ssr(self):
     import random
     ssrs = self.get_server_list()
     ssr = random.choice(ssrs)
     config_from_link = decode_ssrlink(ssr)
     local_address = self.config['local_address']
     local_port = self.config['local_port']
     self.config = shell.parse_config(True, config_from_link)
     self.config['local_address'] = local_address
     self.config['local_port'] = local_port
     print_server_info(self.config, verbose=True, hightlight=True)
     self.network.switch(self.config)
Exemplo n.º 6
0
 def remove(self):
     server_list = self.target.get_server_list()
     if self.extra_args:
         try:
             choice = int(self.extra_args[0]) - 1
         except ValueError as e:
             print(5)
             print(e)
             return
     else:
         choice = user_chooice(
             server_list,
             message='please input the number which you want to remove')
         choice = int(choice) - 1
     print_server_info(decode_ssrlink(server_list[choice]),
                       verbose=True,
                       hightlight=True)
     del server_list[choice]
     self.target.config_manager.update_server_list(server_list)
Exemplo n.º 7
0
 def connect(self):
     target = self.target
     if self.extra_args:
         try:
             ssr = target.get_server_list()[int(self.extra_args[0]) - 1]
         except ValueError as e:
             print(e)
             return
     else:
         ssr = target.get_server_list()[0]
     config_from_link = decode_ssrlink(ssr)
     config = shell.parse_config(True, config_from_link)
     target.config = config
     target.server_link = ssr
     print_server_info(target.config, verbose=True, hightlight=True)
     if target.network is None:
         print('creating ClientNetwork')
         target.network = network.ClientNetwork(target.config)
         target.network.start()
     else:
         print('ClientNetwork already exists')
         target.network.add(target.config)
Exemplo n.º 8
0
 def print_server_list(self, ssrs, header=None, indexed=True, verbose=True, hightlight=True):
     shell.print_server_info((decode_ssrlink(link) for link in ssrs))
     for ssr in ssrs:
         server = decode_ssrlink(ssr)
         print_server_info(server)
Exemplo n.º 9
0
 def get_server_by_addr(self, addr):
     links = self.get_server_list()
     for link in links:
         server = ssrlink.decode_ssrlink(link)
         if server['server'] == addr:
             return link
Exemplo n.º 10
0
def parse_args(args_=None):
    # FIXME: called twice, service, parse_config
    def args_error(
            message):  # TODO: print help information when invalid arguments
        nonlocal parser
        sys.stderr.write('error: %s\n'.format(message))
        print('something wrong')
        parser.print_help()

    parser = argparse.ArgumentParser(
        description='A fast tunnel proxy that helps you bypass firewalls.',
        usage='ssclient <command> [OPTION]',
        epilog='Online help: <https://github.com/shadowsocks/shadowsocks>')
    # TODO: add conflicts of -L with others.
    # default to old version config path, if args.command is set, change it to new version config path
    parser.add_argument('-s', metavar='SERVER_ADDR', help='server address')
    parser.add_argument('-p',
                        metavar='SERVER_PORT',
                        help='server port',
                        default='8388')
    parser.add_argument('-k', metavar='PASSWORD', help='password')
    parser.add_argument('-m',
                        metavar='METHOD',
                        help='encryption method',
                        default='aes-256-cfb')
    parser.add_argument('-O',
                        metavar='PROTOCOL',
                        help='protocol',
                        default='http_simple')
    parser.add_argument('-G',
                        metavar='PROTOCOL_PARAM',
                        help='protocol param',
                        default='')
    parser.add_argument('-o',
                        metavar='OBFS',
                        help='obfsplugin',
                        default='http_simple')
    parser.add_argument('-g',
                        metavar='OBFS_PARAM',
                        help='obfs param',
                        default='')

    subparsers = parser.add_subparsers(title='command title',
                                       dest='command',
                                       help='sub-commands',
                                       metavar='<command>')
    server_parser = subparsers.add_parser('server', help='xxx')
    feed_parser = subparsers.add_parser('feed', help='yyy')
    status_parser = subparsers.add_parser('status', help='show current status')
    service_parser = subparsers.add_parser('service',
                                           help='service operations')
    config_parser = subparsers.add_parser('config', help='yyy')

    server_parser.add_argument('subcmd', help='server command')

    feed_parser.add_argument('--link',
                             help='ssr link')  # TODO: if no link, ask later.
    feed_parser.add_argument('subcmd', help='subscription command')
    feed_parser.add_argument('--source', help='souurce address')
    # status_parser.add_argument('subcmd', help='show current status')
    service_parser.add_argument('subcmd', help='show current status')
    config_parser.add_argument('subcmd', help='show current status')
    # config_parser.add_argument('-c', help='path to the import config file')
    config_parser.add_argument('-o', help='path to the :xport config file')

    for p in (parser, server_parser, feed_parser, status_parser, config_parser,
              service_parser):
        p.add_argument('-b',
                       metavar='LOCAL_ADDR',
                       help='local address',
                       default='127.0.0.1')
        p.add_argument('-l',
                       metavar='LOCAL_PORT',
                       help='local port',
                       default='1080')
        p.add_argument('-i',
                       action='store_true',
                       help='start in interactive mode')
        p.add_argument('-c', metavar='CONFIG', help='path to config file')
        p.add_argument('-L', metavar='SSR_LINK', help='connect using ssr link')
        p.add_argument('-t',
                       metavar='TIMEOUT',
                       help='timeout in seconds',
                       default=300)
        p.add_argument('--fast-open',
                       action='store_true',
                       help='use TCP_FAST_OPEN, requires Linux 3.7+')
        # p.add_argument('-d', metavar='', help='daemon mode (start/stop/restart)', choices=['start', 'stop', 'restart'])
        # TODO: change help message about daemon, we don't need stop/restart option now
        p.add_argument('-d',
                       action='store_true',
                       help='daemon mode (start/stop/restart)')
        p.add_argument('--version',
                       help='show version information',
                       action='store_true')
        p.add_argument('--pid-file',
                       metavar='PID_FILE',
                       help='pid file for daemon mode')
        p.add_argument('--log-file',
                       metavar='LOG_FILE',
                       help='log file daemon mode')
        p.add_argument('--user', metavar='USER', help='run as user')
        p.add_argument('--workers', metavar='WORKERS', default=1)
        p.add_argument('-v',
                       '-vv',
                       action='count',
                       help='verbose mode',
                       default=0)
        # TODO: remove quiet mode
        p.add_argument('-q', '-qq', action='count', help='quiet mode')

    try:
        if args_:
            args, extra_args = parser.parse_known_args(args_)
        else:
            args, extra_args = parser.parse_known_args()
    # except argparse.ArgumentParser as e:
    # we cannot catch argparse.ArgumentParser since it does not inherit from BaseException,
    # when errors in args is founded, argparse just raise SystemExit, this is horrible,
    # we really need to handle the exception to stop daemon from exit.
    except SystemExit as e:
        # TODO: add subcommand `help`, and `help` message
        # args = parser.parse_known_args(['-h'])[0]
        args = parser.parse_known_args(['feed', 'list'])[0]
        pass

    global verbose
    global config
    config = {}
    global config_path
    config_path = None
    logging.basicConfig(level=logging.INFO,
                        format='%(levelname)-s: %(message)s')
    if args.version:
        print_shadowsocks()
        # sys.exit(0)

    if args.c:
        # FIXME: enable default config_path
        if args.command:
            # if args.c == 'default':
            #     config_path = find_config(True)
            # else:
            config_path = args.c
        else:
            # if args.c == 'default':
            #     config_path = find_config(False)
            # else:
            config_path = args.c
            logger.debug('loading config from %s' % config_path)
            with open(config_path, 'rb') as f:
                try:
                    config = parse_json_in_str(
                        remove_comment(f.read().decode('utf8')))
                except ValueError as e:
                    logger.error('found an error in config.json: %s', str(e))
                    sys.exit(1)
    if config_path:
        config['config_path'] = config_path
    else:
        config['config_path'] = find_config(args.command)

    if args.p:
        config['server_port'] = int(args.p)
    if args.k:
        config['password'] = to_bytes(args.k)
    if args.l:
        config['local_port'] = int(args.l)
    if args.s:
        config['server'] = to_str(args.s)
    if args.m:
        config['method'] = to_str(args.m)
    if args.O:
        config['protocol'] = to_str(args.O)
    if args.o:
        config['obfs'] = to_str(args.o)
    if args.G:
        config['protocol_param'] = to_str(args.G)
    if args.g:
        config['obfs_param'] = to_str(args.g)
    if args.b:
        config['local_address'] = to_str(args.b)
    if args.t:
        config['timeout'] = int(args.t)
    # FIXME:
    # if key == '--fast-open':
    #     config['fast_open'] = True
    if args.workers:
        config['workers'] = int(args.workers)
    # FIXME:
    # if key == '--manager-address':
    #     config['manager_address'] = value
    if args.user:
        config['user'] = to_str(args.user)
    # FIXME:
    # if key == '--forbidden-ip':
    #     config['forbidden_ip'] = to_str(value)
    if args.d:
        config['daemon'] = to_str(args.d)
    # FIXME:
    # if key == '--pid-file':
    #     config['pid-file'] = to_str(value)
    # FIXME:
    # if key == '--log-file':
    #     config['log-file'] = to_str(value)
    config['verbose'] = args.v
    if args.q:
        config['verbose'] -= 1
    if args.L:
        config_from_ssrlink = decode_ssrlink(args.L)
        # config.update(config_from_ssrlink)
        config_from_ssrlink.update(config)
        config = config_from_ssrlink

    return (args, extra_args)