Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.
Open
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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
v2ray
venv
dist
shadowray.egg*
shadowray.egg*
/build/
**/__pycache__/
85 changes: 78 additions & 7 deletions shadowray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import platform
import zipfile
import os, stat, signal
from bullet import ScrollBar
try:
from bullet import ScrollBar
except ModuleNotFoundError:
pass


def create_basic_config_file():
Expand All @@ -26,6 +29,7 @@ def create_basic_config_file():
f.write('{ \
"v2ray_binary": null, \
"servers_file": null, \
"template_file": null, \
"subscribe_file": null \
}')
f.close()
Expand Down Expand Up @@ -57,6 +61,10 @@ def have_config():
"Servers file not config.\nYou can config it by --config-servers path.Also, you can use --autoconfig to config it automatic.")
return False

# if j['template_file'] is None:
# print(
# "Template file not config (this is optional).\nYou can config it by --config-template path.\nNote: This warning does not affect what you are doing next.")

return True


Expand All @@ -70,6 +78,23 @@ def add_subscribe(args):
manager.add_subscribe(v[0], v[1])
manager.save_subscribe()

def ls_subscribe():
j = parse_json_from_file(PROJECT_CONFIG_FILE)

manager = Manager(subscribe_file_name=j['subscribe_file'])
subscribes = manager.get_subscribe()
for k in subscribes.keys():
sys.stdout.write(k + " " + subscribes[k] + "\n")

def rm_subscribe(arg):
j = parse_json_from_file(PROJECT_CONFIG_FILE)

manager = Manager(subscribe_file_name=j['subscribe_file'])
try:
subscribes = manager.rm_subscribe(arg)
return True
except KeyError as err:
sys.stderr.write("Error: subscription %s does not exist\n" % err.args[0])

def basic_config_v2ray(v2ray_binary=None):
create_basic_config_file()
Expand Down Expand Up @@ -113,6 +138,20 @@ def basic_config_servers(servers_file=None):

write_to_file(PROJECT_CONFIG_FILE, 'w', json.dumps(j))

def basic_config_template(template_file=None):
create_basic_config_file()

f = open(PROJECT_CONFIG_FILE, 'r')
j = json.load(f)
f.close()

if not template_file or template_file.strip() == "":
template_file = None

j['template_file'] = template_file

write_to_file(PROJECT_CONFIG_FILE, 'w', json.dumps(j))


def download_latest_v2ray():
r = json.loads(requests.get(RELEASE_API).text)
Expand Down Expand Up @@ -200,12 +239,12 @@ def auto_config():
if s:
download_latest_v2ray()
else:
print("Please setup by yourself.")
print("Please setup v2ray by yourself.")


def update_subscribe(**kwargs):
j = parse_json_from_file(PROJECT_CONFIG_FILE)
manager = Manager(server_file_name=j['servers_file'], subscribe_file_name=j['subscribe_file'])
manager = Manager(server_file_name=j['servers_file'], subscribe_file_name=j['subscribe_file'], template_file_name=j.get('template_file'))

manager.update_subscribe(show_info=True, **kwargs)
manager.save_servers()
Expand Down Expand Up @@ -255,7 +294,7 @@ def servers_export(index, path):
j = parse_json_from_file(PROJECT_CONFIG_FILE)

manager = Manager(server_file_name=j['servers_file'], binary=j['v2ray_binary'])
s = manager.get_server(index)
s = manager.get_server(index - 1)
write_to_file(path, "w", json.dumps(s['config']))


Expand Down Expand Up @@ -312,6 +351,16 @@ def main():
add_subscribe(op_value)
break

if op_name in ("--subscribe-ls",):
if have_config() is True:
ls_subscribe()
break

if op_name in ("--subscribe-rm",):
if have_config() is True:
rm_subscribe(op_value)
break

if op_name in ("--config-v2ray",):
basic_config_v2ray(op_value)
break
Expand All @@ -324,16 +373,36 @@ def main():
basic_config_subscribe(op_value)
break

if op_name in ("--config-template",):
basic_config_template(None if not op_value else op_value)
break

if op_name in ("--autoconfig",):
auto_config()
break

if op_name in ("--subscribe-update",):
port = 1082
socks_port = 1082
http_port = 8118
mux = 0
listen_addr = "127.0.0.1"

if "--port" in sys.argv:
port = int(find_arg_in_opts(opts, "--port"))
socks_port = int(find_arg_in_opts(opts, "--port"))
if "--socks-port" in sys.argv:
socks_port = int(find_arg_in_opts(opts, "--socks-port"))
if "--http-port" in sys.argv:
http_port = int(find_arg_in_opts(opts, "--http-port"))
if "--listen-addr" in sys.argv:
listen_addr = find_arg_in_opts(opts, "--listen-addr")
if "--mux" in sys.argv:
mux = int(find_arg_in_opts(opts, "--mux"))
if mux < 0 or mux > 1024:
mux = 0

if have_config():
update_subscribe(port=port)
update_subscribe(socks_port=socks_port, http_port=http_port, listen_addr=listen_addr, mux=mux)

break

if op_name in ("--list", "-l"):
Expand Down Expand Up @@ -378,4 +447,6 @@ def main():
if have_config():
prepare_ping()

if __name__ == "__main__":
main()
# TODO: configure single proxy by users
1 change: 1 addition & 0 deletions shadowray/config/v2ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
USER_HOME = os.path.expanduser("~")

SHADOWRAY_CONFIG_FOLDER = os.path.join(PROJECT_PATH, ".shadowray")
#SHADOWRAY_CONFIG_FOLDER = os.path.join(USER_HOME, ".shadowray")

V2RAY_FOLDER = os.path.join(SHADOWRAY_CONFIG_FOLDER, "v2ray")
V2RAY_BINARY = os.path.join(V2RAY_FOLDER, "v2ray")
Expand Down
14 changes: 10 additions & 4 deletions shadowray/config/version.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
VERSION_ID = "0.1.6"
VERSION_ID = "0.1.7"
AUTHOR = "RMT"
EMAIL = "d.rong@outlook.com"

COMMAND_LONG = ["version", "help", "subscribe-add=", "subscribe-update", "config-v2ray=", "config-subscribe=",
"config-servers=", "autoconfig", "subscribe-update", "list", "start=", "config-file=", "port=",
COMMAND_LONG = ["version", "help", "subscribe-add=", "subscribe-ls", "subscribe-rm=", "subscribe-update", "config-v2ray=", "config-subscribe=",
"config-servers=", "config-template=", "autoconfig", "subscribe-update", "list", "start=", "config-file=", "port=", "socks-port=", "http-port=", "listen-addr=", "mux=",
"servers-export=", "daemon", "stop", "v2ray-update", "ping"]
COMMAND_SHORT = "vhs:lf:d"

HELP_INFO = '''
--help[-h] print help message
--version[-v] show current version of shadowray
--subscribe-add '<name>,<url>' add subscribe
--subscribe-ls list subscribes
--subscribe-update update subscribe
--subscribe-rm <name> delete a subscription (you will need a subscribe-update)
--config-v2ray <path> setup the path of v2ray binary
--config-subscribe <path> setup the path of subscribe file
--config-servers <path> setup the path of servers file
--config-template <path> setup the path of template file (to cancel this setting, set path to "")
--autoconfig setup basic setting automatically
--subscribe-update [--port <number>] update subscribe
--subscribe-update [--socks-port <port>] update subscribe (if template set, the configurations will be generated
[--http-port <port>] according to the template, so the port is ignored)
[--mux <0/1-1024>]
[--listen-addr <127.0.0.1>]
--list[-l] show all servers
--start[-s] <index> [-d|--daemon] start v2ray,the '-d or --daemon argument used to run v2ray as a daemon'
--config-file[-f] <path> run v2ray use the config file that provided by yourself
Expand Down
15 changes: 10 additions & 5 deletions shadowray/core/configuration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json

import copy

class BaseConfig:
def __init__(self, obj, **kwargs):
Expand All @@ -21,11 +21,13 @@ def json_obj(self):


class Configuration(BaseConfig):
def __init__(self):
self.__configuration = {
def __init__(self, default_conf=None):
if default_conf is None:
default_conf = {
"inbounds": [],
"outbounds": []
}
self.__configuration = copy.deepcopy(default_conf)

super().__init__(self.__configuration)

Expand All @@ -41,6 +43,9 @@ def add_inbound(self, inbound_obj):
def add_ontbound(self, outbound_obj):
self.__configuration['outbounds'].append(outbound_obj.json_obj)

def insert_outbound(self, i, outbound_obj):
self.__configuration['outbounds'].insert(i, outbound_obj.json_obj)

class Log(BaseConfig):
DEBUG = "debug"
INFO = "info"
Expand Down Expand Up @@ -437,7 +442,7 @@ def add_client(self, id, level, aid, email):
})

class Socks(BaseConfig):
def __init__(self, auth="noauth", udp=False, **kwargs):
def __init__(self, auth="noauth", udp=True, **kwargs):
self.__socks = {
"auth": auth,
"udp": udp,
Expand Down Expand Up @@ -507,7 +512,7 @@ def __init__(self, domain_strategy, redirect, user_level):

super().__init__(self.__freedom)

class ShadowSocks(BaseConfig):
class Shadowsocks(BaseConfig):
def __init__(self):
self.__shadowsocks = {
"servers": []
Expand Down
9 changes: 6 additions & 3 deletions shadowray/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


class Manager:
def __init__(self, subscribe_file_name=None, server_file_name=None, binary=None):
def __init__(self, subscribe_file_name=None, server_file_name=None, binary=None, template_file_name=None):
if subscribe_file_name is not None:
self.__subscribe = Parser(filename=subscribe_file_name)
self.__subscribe = Parser(filename=subscribe_file_name, template=template_file_name)

if server_file_name is not None:
self.__server = Server(filename=server_file_name)
Expand All @@ -19,6 +19,9 @@ def __init__(self, subscribe_file_name=None, server_file_name=None, binary=None)
def add_subscribe(self, name, url):
self.__subscribe.add(name, url)

def get_subscribe(self):
return self.__subscribe.subscribes

def update_subscribe(self, show_info=False, **kwargs):
self.__subscribe.update(show_info=show_info, **kwargs)

Expand All @@ -30,7 +33,7 @@ def update_subscribe(self, show_info=False, **kwargs):
self.__server.add(protocol=i['protocol'], config=i['config'], ps=i['ps'], key=SERVER_KEY_FROM_SUBSCRIBE,
host=i['host'])

def delete_subscribe(self, name):
def rm_subscribe(self, name):
self.__subscribe.delete(name)

def show_servers(self):
Expand Down
Loading