panelAuth.py 8.85 KB
Newer Older
jose's avatar
jose committed
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
#coding: utf-8
#-------------------------------------------------------------------
# 宝塔Linux面板
#-------------------------------------------------------------------
# Copyright (c) 2015-2019 宝塔软件(http:#bt.cn) All rights reserved.
#-------------------------------------------------------------------
# Author: 黄文良 <287962566@qq.com>
#-------------------------------------------------------------------

#------------------------------
# AUTH验证接口
#------------------------------

import public,time,json,os
from BTPanel import session

class panelAuth:
    __product_list_path = 'data/product_list.pl'
    __product_bay_path = 'data/product_bay.pl';
    __product_id = '100000011';
    
    def create_serverid(self,get):
        userPath = 'data/userInfo.json';
        if not os.path.exists(userPath): return public.returnMsg(False,'LOGIN_FIRST');
        tmp = public.readFile(userPath);
        if len(tmp) < 2: tmp = '{}'
        data = json.loads(tmp);
        if not data: return public.returnMsg(False,'LOGIN_FIRST');
        if not hasattr(data,'serverid'):
            s1 = self.get_mac_address() + self.get_hostname()
            s2 = self.get_cpuname();
            serverid = public.md5(s1) + public.md5(s2);
            data['serverid'] = serverid;
            public.writeFile(userPath,json.dumps(data));
        return data;


    def create_plugin_other_order(self,get):
        pdata = self.create_serverid(get)
        pdata['pid'] = get.pid;
        pdata['cycle'] = get.cycle
        p_url = public.GetConfigValue('home') + '/api/Pluginother/create_order'
        if get.type == '1':
            pdata['renew'] = 1;
            p_url = public.GetConfigValue('home') + '/api/Pluginother/renew_order'
        return json.loads(public.httpPost(p_url,pdata))

    def get_order_stat(self,get):
        pdata = self.create_serverid(get)
        pdata['order_id'] = get.oid;
        p_url = public.GetConfigValue('home') + '/api/Pluginother/order_stat'
        if get.type == '1':  p_url = public.GetConfigValue('home') + '/api/Pluginother/re_order_stat'
        return json.loads(public.httpPost(p_url,pdata))
    
    def check_serverid(self,get):
        if get.serverid != self.create_serverid(get): return False;
        return True;
    
    def get_plugin_price(self,get):
        userPath = 'data/userInfo.json';
        if not 'pluginName' in get: return public.returnMsg(False,'INIT_ARGS_ERR');
        if not os.path.exists(userPath): return public.returnMsg(False,'LOGIN_FIRST');
        params = {}
        params['pid'] = self.get_plugin_info(get.pluginName)['id'];
        #params['ajax2'] = '1';
        data = self.send_cloud('get_product_discount', params)
        return data;
    
    def get_plugin_info(self,pluginName):
        data = self.get_business_plugin(None);
        if not data: return None
        for d in data:
            if d['name'] == pluginName: return d;
        return None;
    
    def get_plugin_list(self,get):
        try:
            if not session.get('get_product_bay') or not os.path.exists(self.__product_bay_path):
                data = self.send_cloud('get_order_list_byuser', {});
                if data: public.writeFile(self.__product_bay_path,json.dumps(data));
                session['get_product_bay'] = True;
            data = json.loads(public.readFile(self.__product_bay_path))
            return data
        except: return None
    
    def get_buy_code(self,get):
        params = {}
        params['pid'] = get.pid;
        params['cycle'] = get.cycle;
        data = self.send_cloud('create_order', params);
        if not data: return public.returnMsg(False,'AJAX_CONN_ERR')
        return data;
    
    def check_pay_status(self,get):
        params = {}
        params['id'] = get.id;
        data = self.send_cloud('check_product_pays', params);
        if not data: return public.returnMsg(False,'AJAX_CONN_ERR')
        if data['status'] == True:
            self.flush_pay_status(get);
            if 'get_product_bay' in session: del(session['get_product_bay']);
        return data;
    
    def flush_pay_status(self,get):
        if 'get_product_bay' in session: del(session['get_product_bay'])
        data = self.get_plugin_list(get)
        if not data: return public.returnMsg(False,'AJAX_CONN_ERR')
        return public.returnMsg(True,'FLUSH_STATUS_SUCCESS')
    
    def get_renew_code(self):
        pass
    
    def check_renew_code(self):
        pass
    
    def get_business_plugin(self,get):
        try:
            if not session.get('get_product_list') or not os.path.exists(self.__product_list_path):
                data = self.send_cloud('get_product_list', {});
                if data: public.writeFile(self.__product_list_path,json.dumps(data));
                session['get_product_list'] = True
            data = json.loads(public.readFile(self.__product_list_path))
            return data
        except: return None
    
    def get_ad_list(self):
        pass
    
    def check_plugin_end(self):
        pass
    
    def get_re_order_status_plugin(self,get):
        params = {}
        params['pid'] = getattr(get,'pid',0);
        data = self.send_cloud('get_re_order_status', params);
        if not data: return public.returnMsg(False,'AJAX_CONN_ERR');
        if data['status'] == True:
            self.flush_pay_status(get);
            if 'get_product_bay' in session: del(session['get_product_bay']);
        return data;
    
    def get_voucher_plugin(self,get):
        params = {}
        params['pid'] = getattr(get,'pid',0);
        params['status'] = '0';
        data = self.send_cloud('get_voucher', params);
        if not data: return [];
        return data;
    
    def create_order_voucher_plugin(self,get):
        params = {}
        params['pid'] = getattr(get,'pid',0);
        params['code'] = getattr(get,'code',0);
        data = self.send_cloud('create_order_voucher', params);
        if not data: return public.returnMsg(False,'AJAX_CONN_ERR');
        if data['status'] == True:
            self.flush_pay_status(get);
            if 'get_product_bay' in session: del(session['get_product_bay']);
        return data;
    
    
    def send_cloud(self,module,params):
        try:
            cloudURL = 'http://www.bt.cn/api/Plugin/';
            userInfo = self.create_serverid(None);
            params['os'] = 'Linux';
            if 'status' in userInfo:
                params['uid'] = 0;
                params['serverid'] = '';
            else:
                params['uid'] = userInfo['uid'];
                params['serverid'] = userInfo['serverid'];
            result = public.httpPost(cloudURL + module,params);
            result = json.loads(result.strip());
            if not result: return None;
            return result;
        except: return None
        
    def send_cloud_pro(self,module,params):
        try:
            cloudURL = 'http://www.bt.cn/api/invite/';
            userInfo = self.create_serverid(None);
            params['os'] = 'Linux';
            if 'status' in userInfo:
                params['uid'] = 0;
                params['serverid'] = '';
            else:
                params['uid'] = userInfo['uid'];
                params['serverid'] = userInfo['serverid'];
            result = public.httpPost(cloudURL + module,params);
            
            result = json.loads(result);
            if not result: return None;
            return result;
        except: return None
    
    def get_voucher(self,get):
        params = {}
        params['product_id'] = self.__product_id;
        params['status'] = '0';
        data = self.send_cloud_pro('get_voucher', params);
        return data;
    
    def get_order_status(self,get):
        params = {}
        data = self.send_cloud_pro('get_order_status', params);
        return data;
        
    
    def get_product_discount_by(self,get):
        params = {}
        data = self.send_cloud_pro('get_product_discount_by', params);
        return data;
    
    def get_re_order_status(self,get):
        params = {}
        data = self.send_cloud_pro('get_re_order_status', params);
        return data;
    
    def create_order_voucher(self,get):
        code = getattr(get,'code','1')
        params = {}
        params['code'] = code;
        data = self.send_cloud_pro('create_order_voucher', params);
        return data;
    
    def create_order(self,get):
        cycle = getattr(get,'cycle','1');
        params = {}
        params['cycle'] = cycle;
        data = self.send_cloud_pro('create_order', params);
        return data;
    
    def get_mac_address(self):
        import uuid
        mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
        return ":".join([mac[e:e+2] for e in range(0,11,2)])
    
    def get_hostname(self):
        import socket
        return socket.getfqdn(socket.gethostname())
    
    def get_cpuname(self):
        return public.ExecShell("cat /proc/cpuinfo|grep 'model name'|cut -d : -f2")[0].strip();