Skip to content

Commit

Permalink
tui v-1.0.0 added
Browse files Browse the repository at this point in the history
  • Loading branch information
ningwang-arch committed Aug 3, 2021
1 parent 915e849 commit 3e41304
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 162 deletions.
33 changes: 33 additions & 0 deletions completer_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import json
from prompt_toolkit.completion import NestedCompleter
from settings import CONFIG_DIR, nested_dict_base


def get_sub_node_set():
if not os.path.exists(CONFIG_DIR+'groups.json') or not os.path.exists(CONFIG_DIR+'connections.json'):
return None
with open(CONFIG_DIR+'groups.json') as f:
groups_info = json.load(f)
with open(CONFIG_DIR+'connections.json', encoding='utf-8') as f:
conn = json.load(f)
res = {}
for item in groups_info:
sub_name = groups_info[item]['displayName'].replace(' ', '%20')
connections = [conn[i]['displayName'].replace(' ', '%20')
for i in groups_info[item]['connections']]

res[sub_name] = set(connections)
return res


def generate_completer():
res = get_sub_node_set()
nested_dict_base['update'] = set(res.keys())
nested_dict_base['connect'] = res
nested_dict_base['info'] = res
nested_dict_base['delete'] = res

completer = NestedCompleter.from_nested_dict(nested_dict_base)

return completer
57 changes: 21 additions & 36 deletions connect_node.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import os
from settings import LAST_CONNECT, CONFIG_DIR, PLATFORM
from settings import LAST_CONNECT, CONFIG_DIR, PLATFORM, clean_blocks, get_node_str, load_default_config
from fill_config import config
import click

config_path = CONFIG_DIR+'config.json'
log_path = CONFIG_DIR+'connect.log'
Expand All @@ -21,11 +22,12 @@ def print_node():
info = {}
for item in sub.keys():
connections = sub[item]['connections']
cnt = 1
for i in range(len(connections)):
connections[i] = str(cnt)+'. '+conn[connections[i]]['displayName']
cnt += 1
info[sub[item]['displayName']] = connections
print(json.dumps(info, indent=4, ensure_ascii=False))
click.echo_via_pager(json.dumps(info, indent=4, ensure_ascii=False))


def disconnect():
Expand Down Expand Up @@ -64,35 +66,7 @@ def current():
print("Current connect: "+node_dict[node]['displayName'])


def convet_num_to_nodestr(choice):
sub_path = CONFIG_DIR+'groups.json'
con_path = CONFIG_DIR+'connections.json'
if not os.path.exists(con_path):
print('No node, please update the subscription and try again')
return ""
conn = {}
sub = {}
with open(sub_path, 'r', encoding='utf-8') as f:
sub = json.load(f)
with open(con_path, 'r', encoding='utf-8') as f:
conn = json.load(f)
# format {"sub_name":[node_name_list]}
info = {}
values = []
for item in sub.keys():
connections = sub[item]['connections']
for i in range(len(connections)):
values.append(conn[connections[i]]['displayName'])
if choice > len(values):
print('Bad choice!')
return ""
node_name = values[choice-1]
for item in conn.keys():
if conn[item]['displayName'] == node_name:
return item


def connect(choice, path="/usr/bin/v2ray", http_port=8889, socks_port=11223):
def connect_by_nodestr(node_str, path="/usr/bin/v2ray", http_port=8889, socks_port=11223):
path = path.replace("\\", '/')

if (("/" in path) and (not os.path.exists(path))):
Expand All @@ -110,7 +84,6 @@ def connect(choice, path="/usr/bin/v2ray", http_port=8889, socks_port=11223):
if result != 0:
print('Port occupied or no executable program')
return
node_str = convet_num_to_nodestr(choice)
node_name = ''

connect_info = {"node": node_str, "path": path,
Expand All @@ -120,6 +93,7 @@ def connect(choice, path="/usr/bin/v2ray", http_port=8889, socks_port=11223):
if node_str == "":
print('Invalid choice')
return

with open(CONFIG_DIR+'connections.json') as f:
node_name = json.load(f)[node_str]['displayName']
pass
Expand All @@ -141,18 +115,29 @@ def connect_default():
info = json.load(f)
path = info['path']
path = path.replace("\\", '/')

if (("/" in path) and (not os.path.exists(path))):
print("No such file!")
return
if PLATFORM == 'linux':
os.system("exec %s -config %s > %s 2>&1 &" %
(path, config_path, log_path))
elif PLATFORM == 'win32':
filepath, fullflname = os.path.split(path)
os.chdir(filepath)
os.system("start /b %s -config %s > %s 2>&1 &" %
(fullflname, config_path, log_path))
(path, config_path, log_path))
print("Connect successfully")
else:
print('No default config file')


def connect(blocks: list):
clean_blocks(blocks)
if not blocks:
connect_default()
elif len(blocks) < 2 or len(blocks) > 2:
print('Error format,should be `connect group nodeName` ')
return
else:
conf = load_default_config()
node_str = get_node_str(blocks[len(blocks)-1])
connect_by_nodestr(
node_str, conf['path'], conf['http_port'], conf['socks_port'])
28 changes: 24 additions & 4 deletions del_func.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import os
from settings import CONFIG_DIR, CONNECTIONS_DIR
from connect_node import convet_num_to_nodestr
from settings import CONFIG_DIR, CONNECTIONS_DIR, clean_blocks, get_node_str


def delete_outbounds(node_str: str):
Expand All @@ -26,10 +25,9 @@ def delete_outbounds(node_str: str):
return ""


def delete_node(choice: int):
def delete_node(node_str):
sub_path = CONFIG_DIR+'groups.json'
sub_info = {}
node_str = convet_num_to_nodestr(choice)
if node_str == '':
print('Delete failed')
return
Expand Down Expand Up @@ -69,3 +67,25 @@ def delete_sub(sub_name: str):
return
print('No such subscription')
return


def delete_func(blocks: list):
blocks = clean_blocks(blocks)
if (not blocks) or (len(blocks) > 2):
print('Error format,should be `delete sub_name` or `delete sub_name node_name`')
return
elif len(blocks) == 1:
sub_name = blocks[0]
choice = input('Be sure to delete subscription %s ? [y/n] ' % sub_name)
if choice == 'y' or choice == 'Y':
delete_sub(sub_name)
else:
return
elif len(blocks) == 2:
node_name = blocks[len(blocks)-1].replace('%20', ' ')
choice = input('Be sure to delete node %s ? [y/n] ' % node_name)
if choice == 'y' or choice == 'Y':
delete_node(get_node_str(node_name))
else:
return
pass
13 changes: 4 additions & 9 deletions json2vmess.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from settings import CONFIG_DIR, CONNECTIONS_DIR
from settings import CONFIG_DIR, CONNECTIONS_DIR, clean_blocks, get_node_str
import json
import base64
from connect_node import convet_num_to_nodestr


class UnknowProtocolException(Exception):
pass


def show_info(choice):
node_str = convet_num_to_nodestr(choice)
def show_info(blocks: list):
blocks = clean_blocks(blocks)
node_str = get_node_str(blocks[len(blocks)-1])
info_list = convert(node_str)
for info in info_list:
print('Group %s\nName %s\nProtocol %s\nAddress %s\nPort %s\nLink %s' % (
Expand Down Expand Up @@ -140,8 +140,3 @@ def outbounds2vmess(outbound):
# print(json.dumps(vobj))
vobj.append(vobj_tmp)
return vobj


if __name__ == '__main__':
# get_group('zuwltsbpadjy')
show_info(98)
55 changes: 0 additions & 55 deletions parser_create.py

This file was deleted.

39 changes: 39 additions & 0 deletions port_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
from settings import LAST_CONNECT, clean_blocks, load_default_config


# port
def port_about(blocks: list):
conf = load_default_config()
blocks = clean_blocks(blocks)
if (not len(blocks)) or len(blocks) > 3 or len(blocks) < 1:
print(
'Error Format,shoould be `port show port` or `port set port [port]`')
return
if blocks[0] == 'show':
print('Http port : %s\nSocks port : %s' %
(conf['http_port'], conf['socks_port']))
elif blocks[0] == 'set':
http_port = int(input('Please input http port : '))
socks_port = int(input('Please input socks port : '))
conf['http_port'] = http_port
conf['socks_port'] = socks_port
with open(LAST_CONNECT, 'w', encoding='utf-8') as f:
f.write(json.dumps(conf, ensure_ascii=False, indent=4))


# path
def path_about(blocks: list):
conf = load_default_config()
blocks = clean_blocks(blocks)
if (not len(blocks)) or len(blocks) > 2 or len(blocks) < 1:
print(
'Error Format,shoould be `path show` or `path set [path]`')
return
if blocks[0] == 'show':
print('Path : %s' % conf['path'])
elif blocks[0] == 'set':
path = input('Please input v2ray path')
conf['path'] = path
with open(LAST_CONNECT, 'w', encoding='utf-8') as f:
f.write(json.dumps(conf, ensure_ascii=False, indent=4))
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
click==8.0.1
prompt-toolkit==3.0.19
urllib3==1.26.6
wcwidth==0.2.5
Loading

0 comments on commit 3e41304

Please sign in to comment.