Exemplo n.º 1
0
def servers_export(index, path):
    j = parse_json_from_file(PROJECT_CONFIG_FILE)

    manager = Manager(server_file_name=j['servers_file'],
                      binary=j['v2ray_binary'])
    s = manager.get_server(index)
    write_to_file(path, "w", json.dumps(s['config']))
Exemplo n.º 2
0
def basic_config_v2ray(v2ray_binary=None):
    create_basic_config_file()

    if v2ray_binary is not None:
        f = open(PROJECT_CONFIG_FILE, 'r')
        j = json.load(f)
        f.close()

        j['v2ray_binary'] = v2ray_binary

        write_to_file(PROJECT_CONFIG_FILE, 'w', json.dumps(j))
Exemplo n.º 3
0
def basic_config_subscribe(subscribe_file=None):
    create_basic_config_file()

    if subscribe_file is not None:
        f = open(PROJECT_CONFIG_FILE, 'r')
        j = json.load(f)
        f.close()

        j['subscribe_file'] = subscribe_file

        write_to_file(SUBSCRIBE_FILE, "w", "{}")

        write_to_file(PROJECT_CONFIG_FILE, 'w', json.dumps(j))
Exemplo n.º 4
0
def basic_config_servers(servers_file=None):
    create_basic_config_file()

    if servers_file is not None:
        f = open(PROJECT_CONFIG_FILE, 'r')
        j = json.load(f)
        f.close()

        j['servers_file'] = servers_file

        write_to_file(servers_file, "w",
                      '{"servers_subscribe": [] ,"servers_original": []}')

        write_to_file(PROJECT_CONFIG_FILE, 'w', json.dumps(j))
Exemplo n.º 5
0
def auto_config():
    create_basic_config_file()

    if os.path.exists(RESOURCES_FOLDER) is False:
        os.mkdir(RESOURCES_FOLDER)
        print("Create resources folder.(%s)" % RESOURCES_FOLDER)

    if os.path.exists(SERVER_FILE) is False:
        # os.mknod(SERVER_FILE)
        open(SERVER_FILE, "w").close()  # for mac os x
        print("Create servers file.(%s)" % SERVER_FILE)
        write_to_file(SERVER_FILE, "w",
                      '{"servers_subscribe": [] ,"servers_original": []}')

    if os.path.exists(SUBSCRIBE_FILE) is False:
        # os.mknod(SUBSCRIBE_FILE)
        open(SUBSCRIBE_FILE, "w").close()
        print("Create subscribe file.(%s)" % SUBSCRIBE_FILE)
        write_to_file(SUBSCRIBE_FILE, "w", '{}')

    basic_config_subscribe(SUBSCRIBE_FILE)
    basic_config_servers(SERVER_FILE)

    if os.path.exists(V2RAY_FOLDER) is False:
        os.mkdir(V2RAY_FOLDER)
        print("Create v2ray-core folder.(%s)" % V2RAY_FOLDER)

    s = None
    while s is None:
        t = input(
            "Do you want to download the v2ray-core automatically?(Y/N)\n")
        s = parse_yes_or_no(t)

    if os.path.exists("v2ray") is False:
        os.mkdir("v2ray")

    if s:
        download_latest_v2ray()
    else:
        print("Please setup by yourself.")
Exemplo n.º 6
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   shortopts=COMMAND_SHORT,
                                   longopts=COMMAND_LONG)
    except Exception as e:
        print("some error occurs when parsing your command args.")
        return 1

    if len(opts) == 0:
        print("Use shadowray --help to get more information.")

    for op_name, op_value in opts:
        if op_name in ("-h", "--help"):
            print(HELP_INFO)
            break

        if op_name in ("-v", "--version"):
            print("Shadowray: " + VERSION_ID)
            break

        if op_name in ("--subscribe-add", ):
            if have_config() is True:
                add_subscribe(op_value)
            break

        if op_name in ("--config-v2ray", ):
            basic_config_v2ray(op_value)
            break

        if op_name in ("--config-servers", ):
            basic_config_servers(op_value)
            break

        if op_name in ("--config-subscribe", ):
            basic_config_subscribe(op_value)
            break

        if op_name in ("--autoconfig", ):
            auto_config()
            break

        if op_name in ("--subscribe-update", ):
            port = 1082
            if "--port" in sys.argv:
                port = int(find_arg_in_opts(opts, "--port"))
            if have_config():
                update_subscribe(port=port)
            break

        if op_name in ("--list", "-l"):
            if have_config():
                show_servers()
            break

        if op_name in ("--start", "-s"):
            if have_config():
                proxy(op_value)
            break

        if op_name in ("--config-file", "-f"):
            proxy(config_file=op_value)
            break

        if op_name in ("--servers-export", ):
            if have_config():
                v = op_value.split(':')
                servers_export(int(v[0]), v[1])
            break

        if op_name in ("--stop", ):
            f = open(V2RAY_PID_FILE, "r")
            s = f.read().strip()
            f.close()

            if s == "":
                print("no running process.")
            else:
                try:
                    os.kill(int(s), signal.SIGKILL)
                except ProcessLookupError:
                    print("Process[%s] not exist" % s)
                write_to_file(V2RAY_PID_FILE, "w", "")

        if op_name in ("--v2ray-update", ):
            if have_config():
                download_latest_v2ray()

        if op_name in ("--ping", ):
            if have_config():
                prepare_ping()
Exemplo n.º 7
0
def auto_config():
    create_basic_config_file()

    if os.path.exists(RESOURCES_FOLDER) is False:
        os.mkdir(RESOURCES_FOLDER)
        print("Create resources folder.(%s)" % RESOURCES_FOLDER)

    if os.path.exists(SERVER_FILE) is False:
        os.mknod(SERVER_FILE)
        print("Create servers file.(%s)" % SERVER_FILE)
        write_to_file(SERVER_FILE, "w",
                      '{"servers_subscribe": [] ,"servers_original": []}')

    if os.path.exists(SUBSCRIBE_FILE) is False:
        os.mknod(SUBSCRIBE_FILE)
        print("Create subscribe file.(%s)" % SUBSCRIBE_FILE)
        write_to_file(SUBSCRIBE_FILE, "w", '{}')

    basic_config_subscribe(SUBSCRIBE_FILE)
    basic_config_servers(SERVER_FILE)

    if os.path.exists(V2RAY_FOLDER) is False:
        os.mkdir(V2RAY_FOLDER)
        print("Create v2ray-core folder.(%s)" % V2RAY_FOLDER)

    s = None
    while s is None:
        t = input(
            "Do you want to download the v2ray-core automatically?(Y/N)\n")
        s = parse_yes_or_no(t)

    if os.path.exists("v2ray") is False:
        os.mkdir("v2ray")

    if s:
        r = json.loads(requests.get(RELEASE_API).text)
        print("Latest publish date of v2ray-core: " + r['published_at'])
        print("Latest version of v2ray-core: " + r['tag_name'])

        os_str = str(platform.system())
        arch = str(platform.architecture()[0])
        print("Platform: " + os_str + " " + arch)

        assets = r['assets']

        download_url = None
        for asset in assets:
            name = str(asset['name'])

            if name.endswith("zip") and name.find(
                    os_str.lower()) != -1 and name.find(arch[0:2]) != -1:
                download_url = str(asset['browser_download_url'])
                break

        if download_url is None:
            print("Download failed,you can download by yourself.")
        else:
            print('Download from %s' % download_url)

            download_file_name = os.path.join(V2RAY_FOLDER,
                                              download_url.split('/')[-1])
            download_file(download_url, download_file_name, show_progress=True)

            print("\nUncompression:")
            f = zipfile.ZipFile(download_file_name, 'r')
            total = len(f.filelist)
            count = 0
            for file in f.filelist:
                f.extract(file, V2RAY_FOLDER)
                count += 1
                print_progress(100 * count / total,
                               extra="%d/%d" % (count, total))

            print("\nSuccess!")
            os.remove(download_file_name)

            os.chmod(path=V2RAY_BINARY, mode=stat.S_IXUSR)
            os.chmod(path=V2CTL_BINARY, mode=stat.S_IXUSR)
            basic_config_v2ray(V2RAY_BINARY)
    else:
        print("Please setup by yourself.")