mirror of
https://github.com/Ysurac/openmptcprouter-vps-admin.git
synced 2025-03-09 15:40:05 +00:00
Check if json not empty before writing config file
This commit is contained in:
parent
8e3a334acc
commit
58171a876c
1 changed files with 21 additions and 14 deletions
35
omr-admin.py
35
omr-admin.py
|
@ -160,8 +160,9 @@ def check_username_serial(username, serial):
|
||||||
return True
|
return True
|
||||||
if 'serial' not in data['users'][0][username]:
|
if 'serial' not in data['users'][0][username]:
|
||||||
data['users'][0][username]['serial'] = serial
|
data['users'][0][username]['serial'] = serial
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
if data:
|
||||||
json.dump(data, outfile, indent=4)
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
||||||
|
json.dump(data, outfile, indent=4)
|
||||||
return True
|
return True
|
||||||
if data['users'][0][username]['serial'] == serial:
|
if data['users'][0][username]['serial'] == serial:
|
||||||
return True
|
return True
|
||||||
|
@ -169,8 +170,9 @@ def check_username_serial(username, serial):
|
||||||
data['users'][0][username]['serial_error'] = 1
|
data['users'][0][username]['serial_error'] = 1
|
||||||
else:
|
else:
|
||||||
data['users'][0][username]['serial_error'] = int(data['users'][0][username]['serial_error']) + 1
|
data['users'][0][username]['serial_error'] = int(data['users'][0][username]['serial_error']) + 1
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
if data:
|
||||||
json.dump(data, outfile, indent=4)
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
||||||
|
json.dump(data, outfile, indent=4)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def set_global_param(key, value):
|
def set_global_param(key, value):
|
||||||
|
@ -182,15 +184,17 @@ def set_global_param(key, value):
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return {'error': 'Config file not readable', 'route': 'global_param'}
|
return {'error': 'Config file not readable', 'route': 'global_param'}
|
||||||
data[key] = value
|
data[key] = value
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
if data:
|
||||||
json.dump(data, outfile, indent=4)
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
||||||
|
json.dump(data, outfile, indent=4)
|
||||||
|
|
||||||
def modif_config_user(user, changes):
|
def modif_config_user(user, changes):
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json') as f:
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json') as f:
|
||||||
content = json.load(f)
|
content = json.load(f)
|
||||||
content['users'][0][user].update(changes)
|
content['users'][0][user].update(changes)
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as f:
|
if data:
|
||||||
json.dump(content, f, indent=4)
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as f:
|
||||||
|
json.dump(content, f, indent=4)
|
||||||
|
|
||||||
|
|
||||||
def add_ss_user(port, key, userid=0, ip=''):
|
def add_ss_user(port, key, userid=0, ip=''):
|
||||||
|
@ -749,8 +753,9 @@ def set_lastchange(sync=0):
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return {'error': 'Config file not readable', 'route': 'lastchange'}
|
return {'error': 'Config file not readable', 'route': 'lastchange'}
|
||||||
data["lastchange"] = time.time() + sync
|
data["lastchange"] = time.time() + sync
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
if data:
|
||||||
json.dump(data, outfile, indent=4)
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as outfile:
|
||||||
|
json.dump(data, outfile, indent=4)
|
||||||
|
|
||||||
|
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json') as f:
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json') as f:
|
||||||
|
@ -2312,8 +2317,9 @@ def add_user(*, params: NewUser, current_user: User = Depends(get_current_user))
|
||||||
if params.vpn is not None:
|
if params.vpn is not None:
|
||||||
user_json[params.username].update({"vpn": params.vpn})
|
user_json[params.username].update({"vpn": params.vpn})
|
||||||
content['users'][0].update(user_json)
|
content['users'][0].update(user_json)
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as f:
|
if content:
|
||||||
json.dump(content, f, indent=4)
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as f:
|
||||||
|
json.dump(content, f, indent=4)
|
||||||
# Create VPNs configuration
|
# Create VPNs configuration
|
||||||
os.system('cd /etc/openvpn/ca && EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full "' + params.username + '" nopass')
|
os.system('cd /etc/openvpn/ca && EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full "' + params.username + '" nopass')
|
||||||
add_glorytun_tcp(userid)
|
add_glorytun_tcp(userid)
|
||||||
|
@ -2336,8 +2342,9 @@ def remove_user(*, params: RemoveUser, current_user: User = Depends(get_current_
|
||||||
userid = int(content['users'][0][params.username]['userid'])
|
userid = int(content['users'][0][params.username]['userid'])
|
||||||
del content['users'][0][params.username]
|
del content['users'][0][params.username]
|
||||||
remove_ss_user(str(shadowsocks_port))
|
remove_ss_user(str(shadowsocks_port))
|
||||||
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as f:
|
if content:
|
||||||
json.dump(content, f, indent=4)
|
with open('/etc/openmptcprouter-vps-admin/omr-admin-config.json', 'w') as f:
|
||||||
|
json.dump(content, f, indent=4)
|
||||||
os.system('cd /etc/openvpn/ca && ./easyrsa --batch revoke ' + params.username)
|
os.system('cd /etc/openvpn/ca && ./easyrsa --batch revoke ' + params.username)
|
||||||
os.system('cd /etc/openvpn/ca && ./easyrsa gen-crl')
|
os.system('cd /etc/openvpn/ca && ./easyrsa gen-crl')
|
||||||
os.system("systemctl -q restart openvpn@tun0")
|
os.system("systemctl -q restart openvpn@tun0")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue