-
Notifications
You must be signed in to change notification settings - Fork 64
/
byte_lib.py
127 lines (108 loc) · 3.17 KB
/
byte_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Author: EDB
# Created On: 11st August 2017
# E-mail: [email protected]
import os,sys
import json
import codecs
import math
import time
import re
def get_traffic(port):
result = os.popen("iptables -n -v -L -t filter -x |grep -i 'spt:" + port + "'|awk '{sum +=$2} END {print sum}'")
res = result.read()
if not re.compile('\d').search(res):
return ""
else:
return int(round(float(res)/math.pow(1000,2)))
def add_rules(port):
res = get_traffic(port)
if res!="":
del_rules(port)
os.system('iptables -A OUTPUT -p tcp --sport ' + port)
os.system('iptables -A INPUT -p tcp --dport ' + port)
os.system('iptables -A OUTPUT -p udp --sport ' + port)
os.system('iptables -A INPUT -p udp --dport ' + port)
else:
os.system('iptables -A OUTPUT -p tcp --sport ' + port)
os.system('iptables -A INPUT -p tcp --dport ' + port)
os.system('iptables -A OUTPUT -p udp --sport ' + port)
os.system('iptables -A INPUT -p udp --dport ' + port)
def del_rules(port):
os.system('iptables -D OUTPUT -p tcp --sport ' + port)
os.system('iptables -D INPUT -p tcp --dport ' + port)
os.system('iptables -D OUTPUT -p udp --sport ' + port)
os.system('iptables -D INPUT -p udp --dport ' + port)
def r_config():
with codecs.open('/etc/byte/byte_ss.json') as json_file:
data = json.load(json_file)
return data
def w_cofig(data):
with codecs.open('/etc/byte/byte_ss.json', 'w', 'utf-8') as json_file:
json_file.write(json.dumps(data, sort_keys=True, indent=4).decode('utf-8'))
def r_json():
with codecs.open(r_config()['ssconf_path']) as json_file:
data = json.load(json_file)
return data
def w_json(data):
with codecs.open(r_config()['ssconf_path'], 'w', 'utf-8') as json_file:
json_file.write(json.dumps(data, sort_keys=True, indent=4).decode('utf-8'))
def a_limit(port,size):
data = r_config()
data['port_limit'][port] = size
w_cofig(data)
def c_limit(port):
data = r_config()
return data['port_limit'][port]
def d_limit(port):
data = r_config()
del data['port_limit'][port]
w_cofig(data)
def a_json(port,pwd):
data=r_json()
data['port_password'][port] = pwd
w_json(data)
def add_rules_from_limit():
data = r_config()
data = data['port_limit']
p = list(data)
if len(p)!=0:
for i,port in enumerate(p):
add_rules(port)
else:
print "there is no limit"
def d_json(port):
data=r_json()
del data['port_password'][port]
w_json(data)
def restart_ss():
sys.stdout.flush()
sys.stderr.flush()
si = file("/dev/null", 'r')
so = file("/dev/null", 'a+')
se = file("/dev/null", 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
os.popen(r_config()['ress_cmd'])
def c_json(port):
data=r_json()
return data['port_password'][port]
def start():
while 1:
data = r_config()
data = data['port_limit']
p = list(data)
if len(p)==0:
with open('bsp_pid','w+') as f:
f.write("")
f.close()
sys.exit('without any limit bsp stoped')
else:
for i,port in enumerate(p):
# print i,port,data[port],get_traffic(port)
if int(get_traffic(port))>=int(data[port]):
d_json(port)
d_limit(port)
del_rules(port)
restart_ss()
time.sleep(float(r_config()['update_time']))