From af713d23fd3005f3c220f76f0d2c6347e8e6ca2d Mon Sep 17 00:00:00 2001 From: Nektarios Katakis Date: Mon, 16 Mar 2020 14:29:28 +0000 Subject: [PATCH] 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 [sunil: Minor indentation, update commit message] Reviewed-by: Sunil Mohan Adapa --- actions/shadowsocks | 48 ++++++++++++++++---------- plinth/modules/shadowsocks/__init__.py | 5 --- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/actions/shadowsocks b/actions/shadowsocks index 21ebc62d1..de394f1a8 100755 --- a/actions/shadowsocks +++ b/actions/shadowsocks @@ -7,8 +7,10 @@ Helper script for configuring Shadowsocks. import argparse import json import os -import subprocess import sys +from shutil import move +import random +import string from plinth import action_utils from plinth.modules import shadowsocks @@ -29,11 +31,6 @@ def parse_arguments(): subparsers.add_parser('merge-config', help='Merge JSON config from stdin with existing') - # Migrations - subparsers.add_parser( - 'migrate-1-2', - help='Move shadowsocks config file to a secure location') - subparsers.required = True return parser.parse_args() @@ -43,6 +40,33 @@ def subcommand_setup(_): # 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) @@ -74,18 +98,6 @@ def subcommand_merge_config(_): action_utils.service_restart(shadowsocks.managed_services[0]) -def subcommand_migrate_1_2(_): - """Move shadowsocks config file to a secure location.""" - if os.path.isfile(SHADOWSOCKS_CONFIG_SYMLINK): # ignoring symlinks - os.makedirs('/var/lib/private/shadowsocks-libev/freedombox/', - exist_ok=True) - os.replace(SHADOWSOCKS_CONFIG_SYMLINK, SHADOWSOCKS_CONFIG_ACTUAL) - os.symlink(SHADOWSOCKS_CONFIG_ACTUAL, SHADOWSOCKS_CONFIG_SYMLINK) - - subprocess.check_call(['systemctl', 'daemon-reload']) - action_utils.service_restart(shadowsocks.managed_services[0]) - - def main(): """Parse arguments and perform all duties.""" arguments = parse_arguments() diff --git a/plinth/modules/shadowsocks/__init__.py b/plinth/modules/shadowsocks/__init__.py index 773749333..64b7f5be3 100644 --- a/plinth/modules/shadowsocks/__init__.py +++ b/plinth/modules/shadowsocks/__init__.py @@ -89,11 +89,6 @@ def init(): def setup(helper, old_version=None): """Install and configure the module.""" - - if old_version == 1: - helper.call('migration', actions.superuser_run, 'shadowsocks', - ['migrate-1-2']) - helper.install(managed_packages) helper.call('post', actions.superuser_run, 'shadowsocks', ['setup']) helper.call('post', app.enable)