shadowsocks: Read configuration as root, update description

- Also use the service helper to start stop shadowsocks service.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Sunil Mohan Adapa 2017-11-29 16:13:47 +05:30
parent 9cfcc08434
commit aeae8c55a4
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
3 changed files with 27 additions and 25 deletions

View File

@ -27,7 +27,8 @@ import sys
from plinth import action_utils from plinth import action_utils
from plinth.modules import shadowsocks from plinth.modules import shadowsocks
from plinth.modules.shadowsocks.views import SHADOWSOCKS_CONFIG
SHADOWSOCKS_CONFIG = '/etc/shadowsocks-libev/freedombox.json'
def parse_arguments(): def parse_arguments():
@ -37,10 +38,8 @@ def parse_arguments():
subparsers.add_parser('setup', subparsers.add_parser('setup',
help='Perform initial setup steps') help='Perform initial setup steps')
subparsers.add_parser('enable', subparsers.add_parser(
help='Enable Shadowsocks client socks5 proxy') 'get-config', help='Read and print JSON config to stdout')
subparsers.add_parser('disable',
help='Disable Shadowsocks client socks5 proxy')
subparsers.add_parser( subparsers.add_parser(
'merge-config', help='Merge JSON config from stdin with existing') 'merge-config', help='Merge JSON config from stdin with existing')
@ -55,14 +54,12 @@ def subcommand_setup(_):
action_utils.service_disable('shadowsocks-libev') action_utils.service_disable('shadowsocks-libev')
def subcommand_enable(_): def subcommand_get_config(arguments):
"""Enable Shadowsocks client socks5 proxy.""" """Read and print Shadowsocks configuration."""
action_utils.service_enable(shadowsocks.managed_services[0]) try:
print(open(SHADOWSOCKS_CONFIG, 'r').read())
except Exception:
def subcommand_disable(_): sys.exit(1)
"""Disable Shadowsocks client socks5 proxy."""
action_utils.service_disable(shadowsocks.managed_services[0])
def subcommand_merge_config(arguments): def subcommand_merge_config(arguments):

View File

@ -24,9 +24,11 @@ from django.utils.translation import ugettext_lazy as _
from plinth import actions from plinth import actions
from plinth import action_utils from plinth import action_utils
from plinth import cfg
from plinth import frontpage from plinth import frontpage
from plinth import service as service_module from plinth import service as service_module
from plinth.menu import main_menu from plinth.menu import main_menu
from plinth.utils import format_lazy
version = 1 version = 1
@ -42,14 +44,17 @@ managed_services = ['shadowsocks-libev-local@freedombox']
managed_packages = ['shadowsocks-libev'] managed_packages = ['shadowsocks-libev']
description = [ description = [
_('Shadowsocks is a lightweight and secure socks5 proxy, designed to ' _('Shadowsocks is a lightweight and secure SOCKS5 proxy, designed to '
'protect your Internet traffic. It can be used to bypass Internet ' 'protect your Internet traffic. It can be used to bypass Internet '
'filtering and censorship.'), 'filtering and censorship.'),
_('Your FreedomBox can run a Shadowsocks client, that can connect ' format_lazy(
'to a Shadowsocks server. The FreedomBox will also run a socks5 ' _('Your {box_name} can run a Shadowsocks client, that can connect to '
'server. Local devices can connect to the socks5 server, and ' 'a Shadowsocks server. It will also run a SOCKS5 proxy. Local '
'their data will be encrypted and proxied through the Shadowsocks ' 'devices can connect to this proxy, and their data will be '
'server.'), 'encrypted and proxied through the Shadowsocks server.'),
box_name=_(cfg.box_name)),
_('To use Shadowsocks after setup, set the SOCKS5 proxy URL in your '
'device, browser or application to http://freedombox_address:1080/')
] ]
@ -106,13 +111,13 @@ def is_running():
def enable(): def enable():
"""Enable service.""" """Enable service."""
actions.superuser_run('shadowsocks', ['enable']) actions.superuser_run('service', ['enable', managed_services[0]])
add_shortcut() add_shortcut()
def disable(): def disable():
"""Disable service.""" """Disable service."""
actions.superuser_run('shadowsocks', ['disable']) actions.superuser_run('service', ['disable', managed_services[0]])
frontpage.remove_shortcut('shadowsocks') frontpage.remove_shortcut('shadowsocks')

View File

@ -26,10 +26,9 @@ from django.utils.translation import ugettext_lazy as _
from .forms import ShadowsocksForm from .forms import ShadowsocksForm
from plinth import actions from plinth import actions
from plinth import views from plinth import views
from plinth.errors import ActionError
from plinth.modules import shadowsocks from plinth.modules import shadowsocks
SHADOWSOCKS_CONFIG = '/etc/shadowsocks-libev/freedombox.json'
class ShadowsocksServiceView(views.ServiceView): class ShadowsocksServiceView(views.ServiceView):
"""Configuration view for Shadowsocks local socks5 proxy.""" """Configuration view for Shadowsocks local socks5 proxy."""
@ -41,9 +40,10 @@ class ShadowsocksServiceView(views.ServiceView):
def get_initial(self, *args, **kwargs): def get_initial(self, *args, **kwargs):
"""Get initial values for form.""" """Get initial values for form."""
try: try:
configuration = open(SHADOWSOCKS_CONFIG, 'r').read() configuration = actions.superuser_run('shadowsocks',
['get-config'])
status = json.loads(configuration) status = json.loads(configuration)
except (OSError, json.JSONDecodeError): except ActionError:
status = { status = {
'server': '', 'server': '',
'server_port': 8388, 'server_port': 8388,