forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanelApi.py
90 lines (75 loc) · 3.26 KB
/
panelApi.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
#coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2017 宝塔软件(http://bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: 阿良 <[email protected]>
# +-------------------------------------------------------------------
#
# ┏┓ ┏┓
# ┏┛┻━━━━━━┛┻┓
# ┃ ☃ ┃
# ┃ ┳┛ ┗┳ ┃
# ┃ ┻ ┃
# ┗━┓ ┏━┛
# ┃ ┗━━━┓
# ┃ 神兽保佑 ┣┓
# ┃ 永无BUG! ┏┛
# ┗┓┓┏━┳┓┏━━━┛
# ┃┫┫ ┃┫┫
# ┗┻┛ ┗┻┛
#
#+------------------------------
#| RESTful API控制器
#+------------------------------
import public,os
from json import loads,dumps
class panelApi:
tokenFile = 'data/token.json'
#获取Token
def GetToken(self,get):
if not os.path.exists(self.tokenFile): return public.returnMsg(False,'错误:当前未开启API接口服务!')
if os.path.exists(self.tokenFile): self.CreateToken(get);
token = loads(public.readFile(self.tokenFile))
return token;
#设置Token
def SetToken(self,get):
if not os.path.exists(self.tokenFile): return public.returnMsg(False,'错误:当前未开启API接口服务!')
token = loads(public.readFile(self.tokenFile))
#设置AK/SK
if hasattr(get,'access_key'):
token['access_key'] = get.access_key
token['secret_key'] = get.secret_key
#设置权限
if hasattr(get,'rule'):
token['rule'] = get.rule.split(',')
#设置IP白名单
if hasattr(get,'address'):
token['address'] = get.address.split(',')
public.writeFile(self.tokenFile,dumps(token))
public.WriteLog('API','修改API配置成功!');
return public.returnMsg(True,'设置成功!')
#初始化API接口
def CreateToken(self,get):
token = {}
token['access_key'] = public.GetRandomString(24)
token['secret_key'] = public.GetRandomString(48)
token['rule'] = []
token['address'] = []
token['status'] = False
public.writeFile(self.tokenFile,dumps(token))
public.WriteLog('API','开启API接口成功!');
return public.returnMsg(True,'初始化API接口成功!');
#设置API接口状态
def SetTokenStatus(self,get):
if not os.path.exists(self.tokenFile): return public.returnMsg(False,'错误:当前未开启API接口服务!')
token = loads(public.readFile(self.tokenFile))
if token['status']:
token['status'] = False
public.WriteLog('API','已关闭API服务!');
else:
token['status'] = True
public.WriteLog('API','已开启API服务!');
public.writeFile(self.tokenFile,dumps(token))
return public.returnMsg(True,'设置成功!')