From 580cf442b6b9a2e4326d37cba2c6617378aea689 Mon Sep 17 00:00:00 2001 From: Ycarus Date: Sun, 15 Nov 2020 19:56:56 +0100 Subject: [PATCH] Add functions to add v2ray user and routes --- omr-admin.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/omr-admin.py b/omr-admin.py index 4d513a1..1dea41d 100755 --- a/omr-admin.py +++ b/omr-admin.py @@ -20,6 +20,7 @@ import hashlib import pathlib import psutil import time +import uuid from pprint import pprint from datetime import datetime, timedelta from tempfile import mkstemp @@ -226,6 +227,85 @@ def remove_ss_user(port): except socket.error as err: LOG.debug("Shadowsocks remove error (" + str(err) + ")") +def v2ray_add_user(user, restart=1): + v2rayuuid = str(uuid.uuid1()) + initial_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + with open('/etc/v2ray/v2ray-server.json') as f: + data = json.load(f) + exist = 0 + for inbounds in data['inbounds']: + if inbounds['tag'] == 'omrin-tunnel': + inbounds['settings']['clients'].append({'id': v2rayuuid, 'level': 0, 'alterId': 0, 'email': user}) + with open('/etc/v2ray/v2ray-server.json', 'w') as f: + json.dump(data, f, indent=4) + final_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + if initial_md5 != final_md5 and restart == 1: + os.system("systemctl -q restart v2ray") + +def v2ray_del_user(user, restart=1): + v2rayuuid = str(uuid.uuid1()) + initial_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + with open('/etc/v2ray/v2ray-server.json') as f: + data = json.load(f) + for inbounds in data['inbounds']: + if inbounds['tag'] == 'omrin-tunnel': + for v2rayuser in inbounds['settings']['clients']: + if v2rayuser['email'] == user: + inbounds['settings']['clients'].remove(v2rayuser) + with open('/etc/v2ray/v2ray-server.json', 'w') as f: + json.dump(data, f, indent=4) + final_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + if initial_md5 != final_md5 and restart == 1: + os.system("systemctl -q restart v2ray") + +def v2ray_add_outbound(tag,ip, restart=1): + initial_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + with open('/etc/v2ray/v2ray-server.json') as f: + data = json.load(f) + data['outbounds'].append({'protocol': 'freedom', 'settings': { 'userLevel': 0 }, 'tag': tag, 'sendThrough': ip}) + with open('/etc/v2ray/v2ray-server.json', 'w') as f: + json.dump(data, f, indent=4) + final_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + if initial_md5 != final_md5 and restart == 1: + os.system("systemctl -q restart v2ray") + +def v2ray_del_outbound(tag, restart=1): + initial_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + with open('/etc/v2ray/v2ray-server.json') as f: + data = json.load(f) + for outbounds in data['outbounds']: + if outbounds['tag'] == tag: + data['outbounds'].remove(outbounds) + with open('/etc/v2ray/v2ray-server.json', 'w') as f: + json.dump(data, f, indent=4) + final_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + if initial_md5 != final_md5 and restart == 1: + os.system("systemctl -q restart v2ray") + +def v2ray_add_routing(tag, restart=1): + initial_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + with open('/etc/v2ray/v2ray-server.json') as f: + data = json.load(f) + data['routing']['rules'].append({'type': 'field', 'inboundTag': ( 'omrintunnel' ), 'outboundTag': tag}) + with open('/etc/v2ray/v2ray-server.json', 'w') as f: + json.dump(data, f, indent=4) + final_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + if initial_md5 != final_md5 and restart == 1: + os.system("systemctl -q restart v2ray") + +def v2ray_del_routing(tag, restart=1): + initial_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + with open('/etc/v2ray/v2ray-server.json') as f: + data = json.load(f) + for rules in data['routing']['rules']: + if rules['outboundTag'] == tag: + data['routing']['rules'].remove(rules) + with open('/etc/v2ray/v2ray-server.json', 'w') as f: + json.dump(data, f, indent=4) + final_md5 = hashlib.md5(file_as_bytes(open('/etc/v2ray/v2ray-server.json', 'rb'))).hexdigest() + if initial_md5 != final_md5 and restart == 1: + os.system("systemctl -q restart v2ray") + def add_gre_tunnels(): nbip = 0