-
Notifications
You must be signed in to change notification settings - Fork 6
/
huawei_api.py
executable file
·188 lines (168 loc) · 7.34 KB
/
huawei_api.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python2
#
# coding: utf-8
#
import requests
import xmltodict
import uuid
import hashlib
import hmac
import logging
from binascii import hexlify
from collections import OrderedDict
from datetime import datetime
class HuaweiAPIException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class HuaweiAPI:
HOME_URL = "http://{host}/html/home.html"
API_URL = "http://{host}/api/"
def __init__(self, passwd, host="192.168.8.1", user="admin", logfile=None):
if logfile:
logging.basicConfig(filename=logfile)
stderrLogger = logging.StreamHandler()
stderrLogger.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
logging.getLogger().addHandler(stderrLogger)
self.log = logging.getLogger("huawei-api")
self.api_url = self.API_URL.format(host=host)
self.session = requests.Session()
self.log.debug("Connect to {host}".format(host=host))
try:
self.session.get(self.HOME_URL.format(host=host),
timeout=(5.0, 5.0))
except Exception as e:
raise HuaweiAPIException("Connection failed: " + str(e))
dev_info = self.device_info()
if dev_info:
self.log.info("Detected Device: " + dev_info['devicename'])
self.log.debug("Authenticate for user " + user)
self.__login(user, passwd)
def __get_client_proof(self, clientnonce, servernonce,
password, salt, iterations):
msg = "%s,%s,%s" % (clientnonce, servernonce, servernonce)
salted_pass = hashlib.pbkdf2_hmac('sha256', password,
bytearray.fromhex(salt), iterations)
client_key = hmac.new(b'Client Key', msg=salted_pass,
digestmod=hashlib.sha256)
stored_key = hashlib.sha256()
stored_key.update(client_key.digest())
signature = hmac.new(msg.encode('utf_8'),
msg=stored_key.digest(), digestmod=hashlib.sha256)
client_key_digest = client_key.digest()
signature_digest = signature.digest()
client_proof = bytearray()
i = 0
while i < client_key.digest_size:
val = ord(client_key_digest[i]) ^ ord(signature_digest[i])
client_proof.append(val)
i = i + 1
return hexlify(client_proof)
def __login(self, user, password):
d = OrderedDict()
d['username'] = user
client_nonce = uuid.uuid4().hex + uuid.uuid4().hex
d['firstnonce'] = client_nonce
d['mode'] = 1
data_login = self.__api_post('user/challenge_login', d)
d = OrderedDict()
proof = self.__get_client_proof(client_nonce,
data_login['servernonce'],
password,
data_login['salt'],
int(data_login['iterations']))
d['clientproof'] = proof
d['finalnonce'] = data_login['servernonce']
self.__api_post('user/authentication_login', d)
if self.__api_request('user/state-login'):
return True
return False
def __get_token(self, session=True):
api_method_url = 'webserver/SesTokInfo'
if session:
r = self.session.get(url=self.api_url + api_method_url,
allow_redirects=False, timeout=(1.5, 1.5))
else:
r = requests.get(url=self.api_url + api_method_url,
allow_redirects=False, timeout=(1.5, 1.5))
if r.status_code != 200:
raise HuaweiAPIException("Error getting token .HTTP error: %d" %
r.status_code)
return xmltodict.parse(r.text)['response']['TokInfo']
def __api_request(self, api_method_url, session=True):
headers = {'__RequestVerificationToken': self.__get_token(session)}
try:
r = self.session.get(url=self.api_url + api_method_url,
headers=headers,
allow_redirects=False, timeout=(1.5, 1.5))
except requests.exceptions.RequestException as e:
raise HuaweiAPIException("Request %s failed: %s" %
(api_method_url, str(e)))
if r.status_code != 200:
raise HuaweiAPIException("Request returned HTTP error %d" %
r.status_code)
self.log.debug("Request: " + api_method_url +
"\nResponse:\n" + r.content)
resp = xmltodict.parse(r.text).get('error', None)
if resp is not None:
error_code = resp['code']
raise HuaweiAPIException("Request returned error " + error_code)
resp = xmltodict.parse(r.text).get('response', None)
if resp is None:
raise HuaweiAPIException("Request returned empty response")
else:
return resp
def __api_post(self, api_method_url, data, session=True):
headers = {'__RequestVerificationToken': self.__get_token(session)}
request = {}
request['request'] = data
try:
r = self.session.post(url=self.api_url + api_method_url,
data=xmltodict.unparse(request, pretty=True),
headers=headers, timeout=(1.5, 1.5))
except requests.exceptions.RequestException as e:
raise HuaweiAPIException("Request %s failed: %s" %
(api_method_url, str(e)))
if r.status_code != 200:
raise HuaweiAPIException("Request returned HTTP error %d" %
r.status_code)
self.log.debug("Request: " + api_method_url +
"\nResponse:\n" + r.content)
resp = xmltodict.parse(r.text).get('error', None)
if resp is not None:
error_code = resp['code']
raise HuaweiAPIException("Request returned error " + error_code)
resp = xmltodict.parse(r.text).get('response', None)
if resp is None:
raise HuaweiAPIException("Request returned empty response")
else:
return resp
def send_sms(self, number, text):
d = OrderedDict()
d['Index'] = -1
d['Phones'] = {'Phone': number}
d['Sca'] = ''
d['Content'] = text
d['Length'] = len(text)
d['Reserved'] = 1
d['Date'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.__api_post('sms/send-sms', d)
def device_info(self):
return self.__api_request('device/basic_information', session=False)
def state_login(self):
return self.__api_request('user/state-login')
def check_notifications(self):
return self.__api_request('monitoring/check-notifications')
def device_signal(self):
return self.__api_request('device/signal')
def net_mode(self, params=None):
if params is None:
return self.__api_request('net/net-mode')
else:
return self.__api_post('net/net-mode', params)
def net_mode_list(self, params=None):
if params is None:
return self.__api_request('net/net-mode-list')
else:
return self.__api_post('net/net-mode-list', params)