FreedomBox/actions/shadowsocks
Nektarios Katakis af713d23fd
shadowshocks: Fix setting configuration on Buster
- Ensure that /var/lib/private/shadowsocks-libev/freedombox always exists. This
fixes not being able to save configuration after setup on fresh Buster installs.

- Merge migration path from version 1 to 2 into setup process in an idempotent
way.

- Always creating an initial configuration file so that daemon starts soon after
install. Set a default random password. Localhost as default server.

Closes: #1792

Signed-off-by: Nektarios Katakis <iam@nektarioskatakis.xyz>
[sunil: Minor indentation, update commit message]
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
2020-03-22 20:07:12 +00:00

112 lines
3.3 KiB
Python
Executable File

#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Helper script for configuring Shadowsocks.
"""
import argparse
import json
import os
import sys
from shutil import move
import random
import string
from plinth import action_utils
from plinth.modules import shadowsocks
SHADOWSOCKS_CONFIG_SYMLINK = '/etc/shadowsocks-libev/freedombox.json'
SHADOWSOCKS_CONFIG_ACTUAL = \
'/var/lib/private/shadowsocks-libev/freedombox/freedombox.json'
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('setup', help='Perform initial setup steps')
subparsers.add_parser('get-config',
help='Read and print JSON config to stdout')
subparsers.add_parser('merge-config',
help='Merge JSON config from stdin with existing')
subparsers.required = True
return parser.parse_args()
def subcommand_setup(_):
"""Perform initial setup steps."""
# Only client socks5 proxy is supported for now. Disable the
# server component.
action_utils.service_disable('shadowsocks-libev')
os.makedirs('/var/lib/private/shadowsocks-libev/freedombox/',
exist_ok=True)
# if existing configuration from version 1 which is normal file
# move it to new location.
if (
not os.path.islink(SHADOWSOCKS_CONFIG_SYMLINK) and
os.path.isfile(SHADOWSOCKS_CONFIG_SYMLINK)
):
move(SHADOWSOCKS_CONFIG_SYMLINK, SHADOWSOCKS_CONFIG_ACTUAL)
else:
# new install
if not os.path.isfile(SHADOWSOCKS_CONFIG_ACTUAL):
initial_config = {
"server": "127.0.0.1",
"mode": "tcp_and_udp",
"server_port": 8388,
"local_port": 1080,
"password": ''.join(
random.choice(string.ascii_letters) for i in range(20)),
"timeout": 60,
"method": "chacha20-ietf-poly1305"
}
open(SHADOWSOCKS_CONFIG_ACTUAL, 'w').write(
json.dumps(initial_config))
if not os.path.islink(SHADOWSOCKS_CONFIG_SYMLINK):
os.symlink(SHADOWSOCKS_CONFIG_ACTUAL, SHADOWSOCKS_CONFIG_SYMLINK)
def subcommand_get_config(_):
"""Read and print Shadowsocks configuration."""
try:
print(open(SHADOWSOCKS_CONFIG_SYMLINK, 'r').read())
except Exception:
sys.exit(1)
def subcommand_merge_config(_):
"""Configure Shadowsocks."""
config = sys.stdin.read()
config = json.loads(config)
try:
current_config = open(SHADOWSOCKS_CONFIG_SYMLINK, 'r').read()
current_config = json.loads(current_config)
except (OSError, json.JSONDecodeError):
current_config = {}
new_config = current_config
new_config.update(config)
new_config = json.dumps(new_config, indent=4, sort_keys=True)
open(SHADOWSOCKS_CONFIG_SYMLINK, 'w').write(new_config)
action_utils.service_restart(shadowsocks.managed_services[0])
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()