-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Removed python 2 support (#4)
- Loading branch information
Showing
3 changed files
with
42 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,44 @@ | ||
#!/usr/bin/env python | ||
#!/usr/bin/env python3 | ||
import argparse | ||
import json | ||
import base64 | ||
from pathlib import Path | ||
from urllib.parse import quote | ||
|
||
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') | ||
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() | ||
|
||
args = 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}") | ||
|
||
if args.config is None: | ||
args.config='/etc/shadowsocks-libev/config.json' | ||
print('Using default config path: {}'.format(args.config)) | ||
with open(config_file, 'r') as file: | ||
return json.load(file) | ||
|
||
config = json.load(open(args.config, 'r')) | ||
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') | ||
|
||
uri = "%s:%s@%s:%d" % ( | ||
config['method'], | ||
config['password'], | ||
config['server'], | ||
config['server_port'], | ||
) | ||
remarks = config.get('remarks', 'Shadowsocks Server') | ||
encoded_remarks = quote(remarks) | ||
|
||
encoded_uri = base64.b64encode(uri.encode('utf-8')).decode('utf-8') | ||
return f'ss://{encoded_uri}#{encoded_remarks}' | ||
|
||
remarks = config.get('remarks', 'Shadowsocks Server') | ||
remarks = "".join(remarks.split(' ')) | ||
print('ss://%s#%s' % (encoded_uri, remarks)) | ||
def main(): | ||
args = parse_arguments() | ||
config = load_config(args.config) | ||
ss_uri = generate_ss_uri(config) | ||
print(ss_uri) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters