Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: Removed python 2 support #4

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ Input:
```
Output:
```
ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpmYWtlX3B3ZEBnaXRodWIuY29tOjgzODg=#ShadowSocksServer
ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpmYWtlX3B3ZEBnaXRodWIuY29tOjgzODg=#ShadowSocks%20Server
```

### Requirements
Python (compatible with both 2 and 3 interpeter versions).
Python 3.9+

### Contribution
Feel free to send your PRs and report any issues.
Expand Down
53 changes: 34 additions & 19 deletions ss-genuri.py
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()
13 changes: 6 additions & 7 deletions test_generates_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class TestScript(unittest.TestCase):
def setUp(self):
self.config_path = 'test_config.json'
sample_config = {
"server": "my-server.com",
"server_port": 8388,
"password": "mypassword",
"method": "aes-256-gcm",
"remarks": "Test Server"
"remarks": "ShadowSocks Server",
"server":"github.com",
"server_port":8388,
"password":"fake_pwd",
"method":"chacha20-ietf-poly1305"
}
with open(self.config_path, 'w') as f:
json.dump(sample_config, f)
Expand All @@ -26,8 +26,7 @@ def test_generate_uri(self):
capture_output=True,
text=True
)
self.assertEqual('ss://YWVzLTI1Ni1nY206bXlwYXNzd29yZEBteS1zZXJ2ZXIuY29tOjgzODg=#TestServer\n', result.stdout)
self.assertEqual('ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpmYWtlX3B3ZEBnaXRodWIuY29tOjgzODg=#ShadowSocks%20Server\n', result.stdout)

if __name__ == '__main__':
unittest.main()

Loading