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
#coding: utf-8
#-------------------------------------------------------------------
# 宝塔Linux面板
#-------------------------------------------------------------------
# Copyright (c) 2015-2018 宝塔软件(http:#bt.cn) All rights reserved.
#-------------------------------------------------------------------
# Author: 黄文良 <287962566@qq.com>
#-------------------------------------------------------------------
#------------------------------
# Nginx管理模块
#------------------------------
import public,os,re,shutil
os.chdir("/www/server/panel")
class nginx:
setupPath = '/www/server'
nginxconf = "%s/nginx/conf/nginx.conf" % (setupPath)
proxyfile = "%s/nginx/conf/proxy.conf" % (setupPath)
def GetNginxValue(self):
ngconfcontent = public.readFile(self.nginxconf)
proxycontent = public.readFile(self.proxyfile)
unitrep = "[kmgKMG]"
conflist = []
ps = ["%s,%s" % (public.GetMsg("WORKER_PROCESSES"),public.GetMsg("WORKER_PROCESSES_AUTO")),
public.GetMsg("WORKER_CONNECTIONS"),
public.GetMsg("CONNECT_TIMEOUT_TIME"),
public.GetMsg("NGINX_ZIP"),
public.GetMsg("NGINX_ZIP_MIN"),
public.GetMsg("ZIP_COMP_LEVEL"),
public.GetMsg("UPLOAD_MAX_FILE"),
public.GetMsg("SERVER_NAME_HASH"),
public.GetMsg("CLIENT_HEADER_BUFF")]
gets = ["worker_processes","worker_connections","keepalive_timeout","gzip","gzip_min_length","gzip_comp_level","client_max_body_size","server_names_hash_bucket_size","client_header_buffer_size"]
n = 0
for i in gets:
rep = "(%s)\s+(\w+)" % i
k = re.search(rep, ngconfcontent).group(1)
v = re.search(rep, ngconfcontent).group(2)
if re.search(unitrep,v):
u = str.upper(v[-1])
v = v[:-1]
if len(u) == 1:
psstr = u+"B,"+ps[n]
else:
psstr = u + "," + ps[n]
else:
u = ""
psstr = ps[n]
kv = {"name":k,"value":v,"unit":u,"ps":psstr}
conflist.append(kv)
n += 1
ps = [public.GetMsg("CLIENT_BODY_BUFF")]
gets = ["client_body_buffer_size"]
n = 0
for i in gets:
rep = "(%s)\s+(\w+)" % i
k = re.search(rep, proxycontent).group(1)
v = re.search(rep, proxycontent).group(2)
if re.search(unitrep, v):
u = str.upper(v[-1])
v = v[:-1]
if len(u) == 1:
psstr = u+"B,"+ps[n]
else:
psstr = u + "," + ps[n]
else:
psstr = ps[n]
u = ""
kv = {"name":k, "value":v, "unit":u,"ps":psstr}
conflist.append(kv)
n+=1
print(conflist)
return conflist
def SetNginxValue(self,get):
ngconfcontent = public.readFile(self.nginxconf)
proxycontent = public.readFile(self.proxyfile)
if public.get_webserver() == 'nginx':
shutil.copyfile(self.nginxconf, '/tmp/ng_file_bk.conf')
shutil.copyfile(self.proxyfile, '/tmp/proxyfile_bk.conf')
conflist = []
getdict = get.__dict__
for i in getdict.keys():
if i != "__module__" and i != "__doc__" and i != "data" and i != "args" and i != "action":
getpost = {
"name": i,
"value": str(getdict[i])
}
conflist.append(getpost)
for c in conflist:
rep = "%s\s+[^kKmMgG\;\n]+" % c["name"]
if c["name"] == "worker_processes" or c["name"] == "gzip":
if not re.search("auto|on|off|\d+", c["value"]):
return public.returnMsg(False, 'INIT_ARGS_ERR')
else:
if not re.search("\d+", c["value"]):
return public.returnMsg(False, 'INIT_ARGS_ERR')
if re.search(rep,ngconfcontent):
newconf = "%s %s" % (c["name"],c["value"])
ngconfcontent = re.sub(rep,newconf,ngconfcontent)
elif re.search(rep,proxycontent):
newconf = "%s %s" % (c["name"], c["value"])
proxycontent = re.sub(rep, newconf , proxycontent)
public.writeFile(self.nginxconf,ngconfcontent)
public.writeFile(self.proxyfile, proxycontent)
isError = public.checkWebConfig()
if (isError != True):
shutil.copyfile('/tmp/ng_file_bk.conf', self.nginxconf)
shutil.copyfile('/tmp/proxyfile_bk.conf', self.proxyfile)
return public.returnMsg(False, 'ERROR: <br><a style="color:red;">' + isError.replace("\n",
'<br>') + '</a>')
public.serviceReload()
return public.returnMsg(True, 'SET_SUCCESS')