forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
165 lines (144 loc) · 6.25 KB
/
common.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: 黄文良 <[email protected]>
# +-------------------------------------------------------------------
from flask import request,redirect,g
from BTPanel import session,cache
from datetime import datetime
import os,public,json,sys
class dict_obj:
def __contains__(self, key):
return getattr(self,key,None)
def __setitem__(self, key, value): setattr(self,key,value)
def __getitem__(self, key): return getattr(self,key,None)
def __delitem__(self,key): delattr(self,key)
def __delattr__(self, key): delattr(self,key)
def get_items(self): return self
class panelSetup:
def init(self):
ua = request.headers.get('User-Agent')
if ua:
ua = ua.lower();
if ua.find('spider') != -1 or ua.find('bot') != -1: return redirect('https://www.baidu.com');
g.version = '6.6.6'
g.title = public.GetConfigValue('title')
g.uri = request.path
session['version'] = g.version;
session['title'] = g.title
return None
class panelAdmin(panelSetup):
setupPath = '/www/server'
#本地请求
def local(self):
result = panelSetup().init()
if result: return result
result = self.checkLimitIp()
if result: return result
result = self.setSession();
if result: return result
result = self.checkClose();
if result: return result
result = self.checkWebType();
if result: return result
result = self.checkDomain();
if result: return result
result = self.checkConfig();
#self.checkSafe();
self.GetOS();
#检查IP白名单
def checkAddressWhite(self):
token = self.GetToken();
if not token: return redirect('/login');
if not request.remote_addr in token['address']: return redirect('/login');
#检查IP限制
def checkLimitIp(self):
if os.path.exists('data/limitip.conf'):
iplist = public.ReadFile('data/limitip.conf')
if iplist:
iplist = iplist.strip();
if not request.remote_addr in iplist.split(','): return redirect('/login')
#设置基础Session
def setSession(self):
session['menus'] = sorted(json.loads(public.ReadFile('config/menu.json')),key=lambda x:x['sort'])
session['yaer'] = datetime.now().year
session['download_url'] = 'http://download.bt.cn';
if not 'brand' in session:
session['brand'] = public.GetConfigValue('brand');
session['product'] = public.GetConfigValue('product');
session['rootPath'] = '/www'
session['download_url'] = 'http://download.bt.cn';
session['setupPath'] = session['rootPath'] + '/server';
session['logsPath'] = '/www/wwwlogs';
session['yaer'] = datetime.now().year
if not 'menu' in session:
session['menu'] = public.GetLan('menu');
if not 'lan' in session:
session['lan'] = public.GetLanguage();
if not 'home' in session:
session['home'] = 'http://www.bt.cn';
#检查Web服务器类型
def checkWebType(self):
if os.path.exists(self.setupPath + '/nginx'):
session['webserver'] = 'nginx'
else:
session['webserver'] = 'apache'
if os.path.exists(self.setupPath+'/'+session['webserver']+'/version.pl'):
session['webversion'] = public.ReadFile(self.setupPath+'/'+session['webserver']+'/version.pl').strip()
filename = self.setupPath+'/data/phpmyadminDirName.pl'
if os.path.exists(filename):
session['phpmyadminDir'] = public.ReadFile(filename).strip()
#检查面板是否关闭
def checkClose(self):
if os.path.exists('data/close.pl'):
return redirect('/close');
#检查域名绑定
def checkDomain(self):
try:
if not session['login']: return redirect('/login')
tmp = public.GetHost()
domain = public.ReadFile('data/domain.conf')
if domain:
if(tmp.strip().lower() != domain.strip().lower()): return redirect('/login')
except:
return redirect('/login')
#检查系统配置
def checkConfig(self):
if not 'config' in session:
session['config'] = public.M('config').where("id=?",('1',)).field('webserver,sites_path,backup_path,status,mysql_root').find();
if not 'email' in session['config']:
session['config']['email'] = public.M('users').where("id=?",('1',)).getField('email');
if not 'address' in session:
session['address'] = public.GetLocalIp()
def checkSafe(self):
mods = ['/','/site','/ftp','/database','/plugin','/soft','/public'];
if not os.path.exists('/www/server/panel/data/userInfo.json'):
if 'vip' in session: del(session.vip);
if not request.path in mods: return True
if 'vip' in session: return True
import panelAuth
data = panelAuth.panelAuth().get_order_status(None);
try:
if data['status'] == True:
session.vip = data
return True
return redirect('/vpro');
except:pass
return False
#获取操作系统类型
def GetOS(self):
if not 'server_os' in session:
tmp = {}
if os.path.exists('/etc/redhat-release'):
tmp['x'] = 'RHEL';
tmp['osname'] = public.ReadFile('/etc/redhat-release').split()[0];
elif os.path.exists('/usr/bin/yum'):
tmp['x'] = 'RHEL';
tmp['osname'] = public.ReadFile('/etc/issue').split()[0];
elif os.path.exists('/etc/issue'):
tmp['x'] = 'Debian';
tmp['osname'] = public.ReadFile('/etc/issue').split()[0];
session['server_os'] = tmp