def DelAcceptPort(self, get): port = get.port id = get.id try: if (port == public.GetHost(True) or port == public.readFile('data/port.pl').strip()): return public.returnMsg(False, 'FIREWALL_PORT_PANEL') if self.__isUfw: public.ExecShell('ufw delete allow ' + port + '/tcp') public.ExecShell('ufw delete allow ' + port + '/udp') else: if self.__isFirewalld: #self.__Obj.DelAcceptPort(port) public.ExecShell( 'firewall-cmd --permanent --zone=public --remove-port=' + port + '/tcp') public.ExecShell( 'firewall-cmd --permanent --zone=public --remove-port=' + port + '/udp') else: public.ExecShell( 'iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport ' + port + ' -j ACCEPT') public.ExecShell( 'iptables -D INPUT -p tcp -m state --state NEW -m udp --dport ' + port + ' -j ACCEPT') public.WriteLog("TYPE_FIREWALL", 'FIREWALL_DROP_PORT', (port, )) public.M('firewall').where("id=?", (id, )).delete() self.FirewallReload() return public.returnMsg(True, 'DEL_SUCCESS') except: return public.returnMsg(False, 'DEL_ERROR')
def checkDomain(self): try: api_check = True if not 'login' in session: api_check = self.get_sk() if api_check: return api_check else: if session['login'] == False: return redirect('/login') tmp = public.GetHost() domain = public.ReadFile('data/domain.conf') if domain: if (tmp.strip().lower() != domain.strip().lower()): return redirect('/login') if api_check: try: sess_out_path = 'data/session_timeout.pl' sess_input_path = 'data/session_last.pl' if not os.path.exists(sess_out_path): public.writeFile(sess_out_path, '86400') if not os.path.exists(sess_input_path): public.writeFile(sess_input_path, str(int(time.time()))) session_timeout = int(public.readFile(sess_out_path)) session_last = int(public.readFile(sess_input_path)) if time.time() - session_last > session_timeout: os.remove(sess_input_path) session['login'] = False cache.set('dologin', True) return redirect('/login') public.writeFile(sess_input_path, str(int(time.time()))) except: pass except: return redirect('/login')
def request_get(self,get): #if os.path.exists('/www/server/panel/install.pl'): raise redirect('/install'); if not 'title' in session: session['title'] = public.getMsg('NAME') domain = public.readFile('data/domain.conf') if domain: if(public.GetHost().lower() != domain.strip().lower()): errorStr = public.ReadFile('./BTPanel/templates/' + public.GetConfigValue('template') + '/error2.html') try: errorStr = errorStr.format(public.getMsg('PAGE_ERR_TITLE'),public.getMsg('PAGE_ERR_DOMAIN_H1'),public.getMsg('PAGE_ERR_DOMAIN_P1'),public.getMsg('PAGE_ERR_DOMAIN_P2'),public.getMsg('PAGE_ERR_DOMAIN_P3'),public.getMsg('NAME'),public.getMsg('PAGE_ERR_HELP')) except IndexError:pass return errorStr if os.path.exists('data/limitip.conf'): iplist = public.readFile('data/limitip.conf') if iplist: iplist = iplist.strip() if not public.GetClientIp() in iplist.split(','): errorStr = public.ReadFile('./BTPanel/templates/' + public.GetConfigValue('template') + '/error2.html') try: errorStr = errorStr.format(public.getMsg('PAGE_ERR_TITLE'),public.getMsg('PAGE_ERR_IP_H1'),public.getMsg('PAGE_ERR_IP_P1',(public.GetClientIp(),)),public.getMsg('PAGE_ERR_IP_P2'),public.getMsg('PAGE_ERR_IP_P3'),public.getMsg('NAME'),public.getMsg('PAGE_ERR_HELP')) except IndexError:pass return errorStr if 'login' in session: if session['login'] == True: return redirect('/') if not 'code' in session: session['code'] = False self.error_num(False)
def request_get(self, get): #if os.path.exists('/www/server/panel/install.pl'): raise redirect('/install'); if not 'title' in session: session['title'] = public.getMsg('NAME') domain = public.readFile('data/domain.conf') if domain: if (public.GetHost().lower() != domain.strip().lower()): errorStr = ''' <meta charset="utf-8"> <title>%s</title> </head><body> <h1>%s</h1> <p>%s</p> <p>%s</p> <p>%s</p> <hr> <address>%s 6.x <a href="http://www.bt.cn/bbs" target="_blank">%s</a></address> </body></html> ''' % (public.getMsg('PAGE_ERR_TITLE'), public.getMsg('PAGE_ERR_DOMAIN_H1'), public.getMsg('PAGE_ERR_DOMAIN_P1'), public.getMsg('PAGE_ERR_DOMAIN_P2'), public.getMsg('PAGE_ERR_DOMAIN_P3'), public.getMsg('NAME'), public.getMsg('PAGE_ERR_HELP')) return errorStr if os.path.exists('data/limitip.conf'): iplist = public.readFile('data/limitip.conf') if iplist: iplist = iplist.strip() if not public.GetClientIp() in iplist.split(','): errorStr = ''' <meta charset="utf-8"> <title>%s</title> </head><body> <h1>%s</h1> <p>%s</p> <p>%s</p> <p>%s</p> <hr> <address>%s 6.x <a href="http://www.bt.cn/bbs" target="_blank">%s</a></address> </body></html> ''' % (public.getMsg('PAGE_ERR_TITLE'), public.getMsg('PAGE_ERR_IP_H1'), public.getMsg('PAGE_ERR_IP_P1', (public.GetClientIp(), )), public.getMsg('PAGE_ERR_IP_P2'), public.getMsg('PAGE_ERR_IP_P3'), public.getMsg('NAME'), public.getMsg('PAGE_ERR_HELP')) return errorStr sql = db.Sql() if 'login' in session: if session['login'] == True: return redirect('/') if not 'code' in session: session['code'] = False self.error_num(False)
def set_phpmyadmin_session(self): import re conf_file = self.get_phpmyadmin_conf() conf = public.readFile(conf_file["conf_file"]) rep = conf_file["rep"] if conf: port = re.search(rep,conf).group(1) path = session['phpmyadminDir'].split("/")[-1] ip = public.GetHost() session['phpmyadminDir'] = "https://{}:{}/{}".format(ip, port, path)
def checkDomain(self): try: if not 'login' in session: if not self.get_sk(): return redirect('/login') else: if session['login'] == False: 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 GetSystemTotal(self,get,interval = 1): #取系统统计信息 data = self.GetMemInfo(); cpu = self.GetCpuInfo(interval); data['cpuNum'] = cpu[1]; data['cpuRealUsed'] = cpu[0]; data['time'] = self.GetBootTime(); data['system'] = self.GetSystemVersion(); data['isuser'] = public.M('users').where('username=?',('admin',)).count(); try: data['isport'] = public.GetHost(True) == '8888' except:data['isport'] = False data['version'] = session['version']; return data
def database(pdata = None): comReturn = comm.local() if comReturn: return comReturn if request.method == method_get[0] and not pdata: pmd = get_phpmyadmin_dir(); session['phpmyadminDir'] = False if pmd: session['phpmyadminDir'] = 'http://' + public.GetHost() + ':'+ pmd[1] + '/' + pmd[0]; data = {} data['isSetup'] = os.path.exists(public.GetConfigValue('setup_path') + '/mysql/bin'); data['mysql_root'] = public.M('config').where('id=?',(1,)).getField('mysql_root'); data['lan'] = public.GetLan('database') return render_template('database.html',data=data) import database databaseObject = database.database() defs = ('GetdataInfo','GetInfo','ReTable','OpTable','AlTable','GetSlowLogs','GetRunStatus','SetDbConf','GetDbStatus','BinLog','GetErrorLog','GetMySQLInfo','SetDataDir','SetMySQLPort','AddDatabase','DeleteDatabase','SetupPassword','ResDatabasePassword','ToBackup','DelBackup','InputSql','SyncToDatabases','SyncGetDatabases','GetDatabaseAccess','SetDatabaseAccess') return publicObject(databaseObject,defs,None,pdata);
def DelAcceptPort(self,get): port = get.port id = get.id types=get.type type_list = ['tcp', 'udp'] if not types in type_list: return public.returnMsg(False, 'FIREWALL_PORT_EXISTS') try: if(port == public.GetHost(True)): return public.returnMsg(False,'FIREWALL_PORT_PANEL') if self.__isUfw: public.ExecShell('ufw delete allow ' + port + '/' + types+ ''); else: if self.__isFirewalld: public.ExecShell('firewall-cmd --permanent --zone=public --remove-port='+port+'/' + types + '') else: public.ExecShell('iptables -D INPUT -p tcp -m state --state NEW -m ' + types +' --dport '+port+' -j ACCEPT') public.WriteLog("TYPE_FIREWALL", 'FIREWALL_DROP_PORT',(port,)) public.M('firewall').where("id=?",(id,)).delete() self.FirewallReload() return public.returnMsg(True,'DEL_SUCCESS') except: return public.returnMsg(False,'DEL_ERROR')
def GetPanelInfo(self, get=None): #取面板配置 address = public.GetLocalIp() try: port = public.GetHost(True) except: port = '8888' domain = '' if os.path.exists('data/domain.conf'): domain = public.readFile('data/domain.conf') autoUpdate = '' if os.path.exists('data/autoUpdate.pl'): autoUpdate = 'checked' limitip = '' if os.path.exists('data/limitip.conf'): limitip = public.readFile('data/limitip.conf') admin_path = '/' if os.path.exists('data/admin_path.pl'): admin_path = public.readFile('data/admin_path.pl').strip() templates = [] #for template in os.listdir('BTPanel/templates/'): # if os.path.isdir('templates/' + template): templates.append(template); template = public.GetConfigValue('template') check502 = '' if os.path.exists('data/502Task.pl'): check502 = 'checked' return { 'port': port, 'address': address, 'domain': domain, 'auto': autoUpdate, '502': check502, 'limitip': limitip, 'templates': templates, 'template': template, 'admin_path': admin_path }
def GetLocalIp(self): # 取本地外网IP try: filename = '/www/server/panel/data/iplist.txt' ipaddress = public.readFile(filename) if not ipaddress: import urllib2 url = 'http://pv.sohu.com/cityjson?ie=utf-8' opener = urllib2.urlopen(url) m_str = opener.read() ipaddress = re.search('\d+.\d+.\d+.\d+', m_str).group(0) public.WriteFile(filename, ipaddress) c_ip = public.check_ip(ipaddress) if not c_ip: a, e = public.ExecShell("curl ifconfig.me") return a return ipaddress except: try: url = public.GetConfigValue('home') + '/Api/getIpAddress' return public.HttpGet(url) except: return public.GetHost()
def setPanel(self, get): if not public.IsRestart(): return public.returnMsg(False, 'EXEC_ERR_TASK') isReWeb = False sess_out_path = 'data/session_timeout.pl' if 'session_timeout' in get: session_timeout = int(get.session_timeout) s_time_tmp = public.readFile(sess_out_path) if not s_time_tmp: s_time_tmp = '0' if int(s_time_tmp) != session_timeout: if session_timeout < 300: return public.returnMsg(False, '超时时间不能小于300秒') public.writeFile(sess_out_path, str(session_timeout)) isReWeb = True workers_p = 'data/workers.pl' if 'workers' in get: workers = int(get.workers) if int(public.readFile(workers_p)) != workers: if workers < 1 or workers > 1024: return public.returnMsg(False, '面板线程数范围应该在1-1024之间') public.writeFile(workers_p, str(workers)) isReWeb = True if get.domain: reg = "^([\w\-\*]{1,100}\.){1,4}(\w{1,10}|\w{1,10}\.\w{1,10})$" if not re.match(reg, get.domain): return public.returnMsg(False, 'SITE_ADD_ERR_DOMAIN') oldPort = public.GetHost(True) newPort = get.port if oldPort != get.port: get.port = str(int(get.port)) if self.IsOpen(get.port): return public.returnMsg(False, 'PORT_CHECK_EXISTS', (get.port, )) if int(get.port) >= 65535 or int(get.port) < 100: return public.returnMsg(False, 'PORT_CHECK_RANGE') public.writeFile('data/port.pl', get.port) import firewalls get.ps = public.getMsg('PORT_CHECK_PS') fw = firewalls.firewalls() fw.AddAcceptPort(get) get.port = oldPort get.id = public.M('firewall').where("port=?", (oldPort, )).getField('id') fw.DelAcceptPort(get) isReWeb = True if get.webname != session['title']: session['title'] = get.webname public.SetConfigValue('title', get.webname) limitip = public.readFile('data/limitip.conf') if get.limitip != limitip: public.writeFile('data/limitip.conf', get.limitip) public.writeFile('data/domain.conf', get.domain.strip()) public.writeFile('data/iplist.txt', get.address) public.M('config').where("id=?", ('1', )).save( 'backup_path,sites_path', (get.backup_path, get.sites_path)) session['config']['backup_path'] = os.path.join('/', get.backup_path) session['config']['sites_path'] = os.path.join('/', get.sites_path) mhost = public.GetHost() if get.domain.strip(): mhost = get.domain data = { 'uri': request.path, 'host': mhost + ':' + newPort, 'status': True, 'isReWeb': isReWeb, 'msg': public.getMsg('PANEL_SAVE') } public.WriteLog('TYPE_PANEL', 'PANEL_SET_SUCCESS', (newPort, get.domain, get.backup_path, get.sites_path, get.address, get.limitip)) if isReWeb: public.restart_panel() return data