-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathss-genuri.py
executable file
·44 lines (36 loc) · 1.3 KB
/
ss-genuri.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
import argparse
import json
import base64
from pathlib import Path
from urllib.parse import quote
def parse_arguments():
parser = argparse.ArgumentParser(
description='A script that parses Shadowsocks server configuration file and generates a client configuration URI'
)
parser.add_argument(
'--config', '-c',
type=str,
help='A path to your Shadowsocks server config.json',
default='/etc/shadowsocks-libev/config.json'
)
return parser.parse_args()
def load_config(config_path: str) -> dict:
config_file = Path(config_path)
if not config_file.exists():
raise FileNotFoundError(f"Configuration file not found: {config_path}")
with open(config_file, 'r') as file:
return json.load(file)
def generate_ss_uri(config: dict) -> str:
uri = f"{config['method']}:{config['password']}@{config['server']}:{config['server_port']}"
encoded_uri = base64.b64encode(uri.encode('utf-8')).decode('utf-8')
remarks = config.get('remarks', 'Shadowsocks Server')
encoded_remarks = quote(remarks)
return f'ss://{encoded_uri}#{encoded_remarks}'
def main():
args = parse_arguments()
config = load_config(args.config)
ss_uri = generate_ss_uri(config)
print(ss_uri)
if __name__ == '__main__':
main()