def multiple(): shell.run('mkdir test') cmds = ['touch test/{}.txt'.format(i) for i in range(3)] shell.run_all(cmds) result = [os.path.isfile('test/{}.txt'.format(i)) for i in range(3)] shell.run('rm -rf test') return all(result)
def single(): shell.run('touch test.txt') shell.run('echo 123 >> test.txt') shell.run('echo abc >> test.txt') lines = shell.run('cat test.txt') shell.run('rm test.txt') return lines[0] == '123' and lines[1] == 'abc'
def gen_pac_action(file_path, ip, port, gfwlist_url=_GFWLIST_URL): # remove old pac file if os.path.isfile(file_path): os.remove(file_path) bash = shell.run('which bash', output='single') gfwlist2privoxy = os.path.join(os.path.dirname(__file__), 'gfwlist2privoxy') ret = shell.run('{} {} {} {}:{} {}'.format(bash, gfwlist2privoxy, file_path, ip, port, gfwlist_url)) if isinstance(ret, shell.CmdRunError): raise Exception(str(ret))
def add_autostart(ss_config_path): service_content = '[Unit] \n' + \ 'Description=sslocal \n' +\ 'After=network.target \n' + \ '\n' + \ '[Service] \n' + \ 'Type=forking \n' + \ 'ExecStart={} -c {} -d start \n'.format(_get_sslocal_path(), ss_config_path) + \ 'ExecStop={} -c {} -d stop \n'.format(_get_sslocal_path(), ss_config_path) + \ '\n' + \ '[Install] \n' + \ 'WantedBy=multi-user.target \n' service_path = os.path.join('/etc/systemd/system', _SSLOCAL_SERVICE) with open(service_path, 'w') as f: f.write(service_content) # reload ret = shell.run('systemctl daemon-reload') if isinstance(ret, shell.CmdRunError): raise Exception(str(ret)) # set autostart enable_autostart() # restart stop() start()
def set_mode(mode, ip='127.0.0.1', port=1080, config_file=_DEFAULT_CONFIG_FILE, pac_action_file=_DEFAULT_PAC_ACTION_FILE): lines = _load_config(config_file) if mode == 'pac': lines = _forward_on_off(lines, False, ip, port) lines = _pac_on_off(lines, True, pac_action_file) elif mode == 'global': lines = _forward_on_off(lines, True, ip, port) lines = _pac_on_off(lines, False, pac_action_file) elif mode == 'off': lines = _forward_on_off(lines, False, ip, port) lines = _pac_on_off(lines, False, pac_action_file) else: raise Exception('Invalid mode {}'.format(mode)) _save_config(config_file, lines) # restart privoxy ret = shell.run('service privoxy restart') if isinstance(ret, shell.CmdRunError): raise Exception(str(ret))
def pid(): ret = shell.run("ps -ef | grep sslocal | grep -v grep | awk '{print $2}'", output='single') if isinstance(ret, shell.CmdRunError): raise Exception(str(ret)) try: return int(ret) except: return -1
def _request_sudo(user=None): if user is not None: return # sudo run command sudo_path = shell.run('which sudo', output='single') command = '{} {} '.format(sudo_path, sys.executable) for arg in sys.argv: command += arg + ' ' if user is None: command += '--user={} '.format(pwd.getpwuid(os.getuid()).pw_name) os.system(command) # reload envs shell.run('source {}'.format(_DEFAULT_GFWPROXY_PROFILE_PATH)) # exit exit(0)
def set_mode(mode, user, pac_file=None, sock_host=None, sock_port=None): # env commands export_proxy_cmds = [ 'export http_proxy="{}:{}"'.format(sock_host, 8118), 'export https_proxy="{}:{}"'.format(sock_host, 8118), 'export ftp_proxy="{}:{}"'.format(sock_host, 8118), 'export all_proxy="socks5://{}:{}"'.format(sock_host, sock_port) ] unset_proxy_cmds = [ 'unset http_proxy', 'unset https_proxy', 'unset ftp_proxy', 'unset all_proxy', ] cmds = [] if mode == 'auto': if pac_file is None: raise Exception('Invalid PAC file path') cmds.append('gsettings set org.gnome.system.proxy mode auto') cmds.append('gsettings set org.gnome.system.proxy autoconfig-url "file://{}"'.format(pac_file)) cmds += export_proxy_cmds elif mode == 'disabled': cmds.append('gsettings set org.gnome.system.proxy mode none') cmds += unset_proxy_cmds elif mode == 'manual': if sock_host is None or sock_port is None: raise Exception('Invalid host and port of socks') cmds.append('gsettings set org.gnome.system.proxy mode manual') cmds.append('gsettings set org.gnome.system.proxy.socks host {}'.format(sock_host, sock_port)) cmds += export_proxy_cmds else: raise Exception('Invalid mode {}'.format(mode)) # run with user with_user_cmd = 'su - {}'.format(user) + ' -c "{}"' for cmd in cmds: ret = shell.run(with_user_cmd.format(cmd)) if isinstance(ret, shell.CmdRunError): raise Exception(str(ret))
def disable_autostart(): ret = shell.run('systemctl disable {}'.format(_SSLOCAL_SERVICE)) if isinstance(ret, shell.CmdRunError): raise Exception(str(ret))
def _get_sslocal_path(): return shell.run('which sslocal', output='single') def stop():
def start(): # start sslocal ret = shell.run('systemctl start {}'.format(_SSLOCAL_SERVICE)) if isinstance(ret, shell.CmdRunError): raise Exception(str(ret))
def cmd_init(args): if args.proxy_host is None: print('Proxy server host of shadowsocks need to be provided') args.proxy_host = input('Proxy host: ') if args.proxy_port is None: print('Proxy server port of shadowsocks need to be provided') args.proxy_port = int(input('Proxy port: ')) if args.proxy_password is None: print('proxy password of shadowsocks need to be provided') args.proxy_password = input('Proxy password: '******'The path of user profile need to be provided, example ~/.profile') args.profile = input('Profile path: ') args.profile = os.path.expanduser(args.profile) if not os.path.isfile(args.profile): raise Exception('Invalid user profile {}'.format(args.profile)) # check sslocal ret = shell.run('which sslocal') if isinstance(ret, shell.CmdRunError): raise Exception(str(ret)) # check genpac ret = shell.run('which genpac') if isinstance(ret, shell.CmdRunError): raise Exception(str(ret)) # create config dir if os.path.isdir(_DEFAULT_CONF_DIR): shutil.rmtree(_DEFAULT_CONF_DIR) os.makedirs(_DEFAULT_CONF_DIR, exist_ok=True) # bug fix for shadowsocks ss.bug_fix() # gen pac pac.gen(_DEFAULT_PAC_PATH, args.local_host, args.local_port) # gen shadowsocks config ss.gen_config(_DEFAULT_SS_CONFIG_PATH, server=args.proxy_host, server_port=args.proxy_port, password=args.proxy_password, local_address=args.local_host, local_port=args.local_port, timeout=args.proxy_timeout, method=args.proxy_method) # gen pac action privoxy.gen_pac_action(privoxy._DEFAULT_PAC_ACTION_FILE, args.local_host, args.local_port) # add sslocal to autostart ss.add_autostart(_DEFAULT_SS_CONFIG_PATH) # save config config.init(_DEFAULT_CONFIG_PATH, profile_path=args.profile) # init gfwproxy profile profile.init(args.profile, _DEFAULT_GFWPROXY_PROFILE_PATH, local_host=args.local_host, sock_port=args.local_port) # set access previlage for current user gid = pwd.getpwnam(args.user).pw_gid gname = grp.getgrgid(gid).gr_name shell.run('chown -R {}:{} {}'.format(args.user, gname, _DEFAULT_CONF_DIR))
def _get_genpac_path(): return shell.run('which genpac', output='single')
def gen(pac_file, local_host, local_port, gfwlist_url=_GFWLIST_URL): ret = shell.run( '{} --proxy="SOCKS5 {}:{}" -o {} --gfwlist-url="{}"'.format( _get_genpac_path(), local_host, local_port, pac_file, gfwlist_url)) if isinstance(ret, shell.CmdRunError): raise Exception(str(ret))