-
Notifications
You must be signed in to change notification settings - Fork 0
/
top.py
163 lines (136 loc) · 4.87 KB
/
top.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
from retrying import retry
import time
import requests
import base64
import json
import logging
import configparser
import warnings
from requests.packages import urllib3
# 关闭警告
urllib3.disable_warnings()
warnings.filterwarnings("ignore")
class AutoLoginTopSap():
def __init__(self, host, port, username, password, ocr):
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
self.host = host
self.port = port
self.username = username
self.password = password
self.code_url = "https://localhost:7443/api/v1/get_gid?serverAddr=111.33.112.66&serverPort=10443&vpn_version=ngvone&auth_protocol=0&auth_port=10443&data_port=10443&data_protocol=0&cert_type=0&proxyType=&proxyAddr=&proxyPort=&proxyUser=&proxyPwd=&proxyDomain=&rnd=0.21450320730512473"
self.auto_code_url = ocr
self.logout_url = "https://localhost:7443/api/v1/logout"
self.login_url = "https://localhost:7443/api/v1/login_by_pwd"
self.query_statistics_url = "https://localhost:7443/api/v1/query_statistics"
self.session = requests.session()
self.json_headers = {
'Content-Type': 'application/json'
}
self.form_headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
def get_code_img(self):
"""
获取验证码图片
"""
response = self.session.get(self.code_url, verify=False)
with open('result.png', 'wb') as f:
f.write(response.content)
return response.content
def get_code_base64(self, content):
"""
返回base64图片文本
"""
return "data:image/png;base64," + base64.b64encode(content).decode('utf-8')
def get_code_text(self):
"""
获取验证码Code
"""
result = self.get_code_base64(self.get_code_img())
data = {
"base64": result,
"name": "erp",
"threshold": 0,
"count": 0
}
code = self.session.post(self.auto_code_url, headers=self.json_headers, data=json.dumps(data), verify=False).text
return code
def query_statistics(self):
"""
统计连接数据
"""
data = {"method": "query_statistics"}
response = self.session.post(self.query_statistics_url, headers=self.json_headers, data=json.dumps(data), verify=False).json()
terr_code = response.get('terr_code') # != 0
return response
def logout(self):
"""
登出
"""
data = {"method":"logout"}
response = self.session.post(self.logout_url, headers=self.json_headers, data=json.dumps(data), verify=False).text
return response
@retry(stop_max_attempt_number=10)
def login(self):
code = self.get_code_text()
"""
自动登陆
"""
data = {
"method": "login_by_pwd",
"vone": {
"addr": self.host,
"port": self.port,
"user": base64.b64encode(self.username.encode('utf-8')).decode('utf-8'),
"pwd": base64.b64encode(self.password.encode('utf-8')).decode('utf-8')
},
"proxy": {
"type":"",
"addr":"",
"port":"",
"user":"",
"pwd":"",
"domain":""
},
"gid" : {
"cgid":"" ,
"gid": code
} ,
"vpn_version" :"ngvone",
"auth_protocol":"0" ,
"auth_port": "10443" ,
"data_port": "10443" ,
"data_protocol": "0" ,
"cert_type": "0" ,
"remember_pwd": "off"
}
response = self.session.post(self.login_url, headers=self.json_headers, data=json.dumps(data), verify=False).json()
if response.get('err_code') != 0:
if response.get('err_code') == -18:
self.logout()
raise Exception("Login failed")
print("登录成功")
return response
def listen(self):
while True:
try:
stat = self.query_statistics()
print(stat)
if stat.get('terr_code') != 0 or stat.get('session_id') == '':
self.login()
except Exception as e:
print(e)
pass
time.sleep(1)
# def auto_login(self, retry=3):
config = configparser.ConfigParser()
config.read('env.ini')
env = config.get('ENV', 'env')
host = config.get(env, 'host')
port = config.get(env, 'port')
username = config.get(env, 'username')
password = config.get(env, 'password')
ocr = config.get(env, 'ocr_url')
print("start")
auto_login = AutoLoginTopSap(host, port, username, password, ocr)
auto_login.listen()