Пример #1
0
def main():
    parser = argparse.ArgumentParser(
        description='A light tunnel proxy that helps you bypass firewalls')
    parser.add_argument('--version',
                        action='store_true',
                        default=False,
                        help='show version information')

    proxy_options = parser.add_argument_group('Proxy options')

    proxy_options.add_argument('--save',
                               metavar='CONFIG',
                               help='path to dump config')
    proxy_options.add_argument('-c',
                               metavar='CONFIG',
                               help='path to config file')
    proxy_options.add_argument(
        '-u',
        metavar='URL',
        help='url contains server address, port and password')
    proxy_options.add_argument('-s',
                               metavar='SERVER_ADDR',
                               help='server address')
    proxy_options.add_argument('-p',
                               metavar='SERVER_PORT',
                               type=int,
                               help='server port, default: 8388')
    proxy_options.add_argument(
        '-b',
        metavar='LOCAL_ADDR',
        help='local binding address, default: 127.0.0.1')
    proxy_options.add_argument('-l',
                               metavar='LOCAL_PORT',
                               type=int,
                               help='local port, default: 1080')
    proxy_options.add_argument('-k', metavar='PASSWORD', help='password')

    args = parser.parse_args()

    if args.version:
        print('lightsocks 0.1.0')
        sys.exit(0)

    config = lsConfig.Config(None, None, None, None, None)
    if args.c:
        try:
            with open(args.c, encoding='utf-8') as f:
                file_config = lsConfig.load(f)
        except lsConfig.InvalidFileError:
            parser.print_usage()
            print(f'invalid config file {args.c!r}')
            sys.exit(1)
        except FileNotFoundError:
            parser.print_usage()
            print(f'config file {args.c!r} not found')
            sys.exit(1)
        config = config._replace(**file_config._asdict())

    if args.u:
        try:
            url_config = lsConfig.loadURL(args.u)
        except lsConfig.InvalidURLError:
            parser.print_usage()
            print(f'invalid config URL {args.u!r}')
            sys.exit(1)
        config = config._replace(**url_config._asdict())

    if args.s:
        serverAddr = args.s
        # TODO: 验证 serverAddr 有效性
        config = config._replace(serverAddr=serverAddr)

    if args.p:
        serverPort = args.p
        # TODO: 验证 serverPort 有效性
        config = config._replace(serverPort=serverPort)

    if args.b:
        localAddr = args.b
        # TODO: 验证 localPort 有效性
        config = config._replace(localAddr=localAddr)

    if args.l:
        localPort = args.l
        # TODO: 验证 localPort 有效性
        config = config._replace(localPort=localPort)

    if args.k:
        try:
            password = loadsPassword(args.k)
            config = config._replace(password=password)
        except InvalidPasswordError:
            parser.print_usage()
            print('invalid password')
            sys.exit(1)

    if config.localAddr is None:
        config = config._replace(localAddr='127.0.0.1')

    if config.localPort is None:
        config = config._replace(localPort=1080)

    if config.serverPort is None:
        config = config._replace(serverPort=8388)

    if config.password is None:
        parser.print_usage()
        print('need PASSWORD, please use [-k PASSWORD]')
        sys.exit(1)

    if config.serverAddr is None:
        parser.print_usage()
        print('need SERVER_ADDR, please use [-s SERVER_ADDR]')

    if args.save:
        print(f'dump config file into {args.save!r}')
        with open(args.save, 'w', encoding='utf-8') as f:
            lsConfig.dump(f, config)

    run_server(config)
Пример #2
0
def main():
    parser = argparse.ArgumentParser(
        description='A light tunnel proxy that helps you bypass firewalls')
    parser.add_argument('--version',
                        action='store_true',
                        default=False,
                        help='show version information')

    proxy_options = parser.add_argument_group('Proxy options')

    proxy_options.add_argument('--save',
                               metavar='CONFIG',
                               help='path to dump config')
    proxy_options.add_argument('-c',
                               metavar='CONFIG',
                               help='path to config file')
    proxy_options.add_argument('-s',
                               metavar='SERVER_ADDR',
                               help='server address, default: 0.0.0.0')
    proxy_options.add_argument('-p',
                               metavar='SERVER_PORT',
                               type=int,
                               help='server port, default: 8388')
    proxy_options.add_argument('-k', metavar='PASSWORD', help='password')
    proxy_options.add_argument('--random',
                               action='store_true',
                               default=False,
                               help='generate a random password to use')

    args = parser.parse_args()

    if args.version:
        print('lightsocks 0.1.0')
        sys.exit(0)

    config = lsConfig.Config(None, None, None, None, None)
    if args.c:
        try:
            with open(args.c, encoding='utf-8') as f:
                file_config = lsConfig.load(f)
        except lsConfig.InvalidFileError:
            parser.print_usage()
            print(f'invalid config file {args.c!r}')
            sys.exit(1)
        except FileNotFoundError:
            parser.print_usage()
            print(f'config file {args.c!r} not found')
            sys.exit(1)
        config = config._replace(**file_config._asdict())

    if args.s:
        serverAddr = args.s
        # TODO: 验证 serverAddr 有效性
        config = config._replace(serverAddr=serverAddr)

    if args.p:
        serverPort = args.p
        # TODO: 验证 serverPort 有效性
        config = config._replace(serverPort=serverPort)

    if args.k:
        try:
            password = loadsPassword(args.k)
            config = config._replace(password=password)
        except InvalidPasswordError:
            parser.print_usage()
            print('invalid password')
            sys.exit(1)

    if config.serverAddr is None:
        config = config._replace(serverAddr='0.0.0.0')

    if config.serverPort is None:
        config = config._replace(serverPort=8388)

    if config.password is None and not args.random:
        parser.print_usage()
        print('need PASSWORD, please use [-k PASSWORD] or '
              'use [--random] to generate a random password')
        sys.exit(1)

    if args.random:
        print('generate random password')
        config = config._replace(password=randomPassword())

    if args.save:
        print(f'dump config file into {args.save!r}')
        with open(args.save, 'w', encoding='utf-8') as f:
            lsConfig.dump(f, config)

    run_server(config)