handling unauthorized connnections
This commit is contained in:
parent
7dd807502a
commit
0cb0d6c035
45
zk/base.py
45
zk/base.py
@ -10,17 +10,17 @@ from zk.user import User
|
||||
|
||||
|
||||
class ZK(object):
|
||||
|
||||
is_connect = False
|
||||
|
||||
__data_recv = None
|
||||
__sesion_id = 0
|
||||
__reply_id = 0
|
||||
|
||||
def __init__(self, ip, port=4370, timeout=60):
|
||||
def __init__(self, ip, port=4370, timeout=60,password=0):
|
||||
self.__address = (ip, port)
|
||||
self.__sock = socket(AF_INET, SOCK_DGRAM)
|
||||
self.__sock.settimeout(timeout)
|
||||
self.__password = password
|
||||
|
||||
def __create_header(self, command, command_string, checksum, session_id, reply_id):
|
||||
'''
|
||||
@ -146,6 +146,11 @@ class ZK(object):
|
||||
response_size = 8
|
||||
|
||||
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
|
||||
self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2]
|
||||
if cmd_response.get('code') == const.CMD_ACK_UNAUTH:
|
||||
command_string = make_commkey(self.__password, self.__sesion_id)
|
||||
cmd_response = self.__send_command(const.CMD_AUTH, command_string, checksum, self.__sesion_id,
|
||||
self.__reply_id, response_size)
|
||||
if cmd_response.get('status'):
|
||||
self.is_connect = True
|
||||
# set the session id
|
||||
@ -528,3 +533,39 @@ class ZK(object):
|
||||
return True
|
||||
else:
|
||||
raise ZKErrorResponse("Invalid response")
|
||||
|
||||
|
||||
def make_commkey(key, session_id, ticks=50):
|
||||
"""take a password and session_id and scramble them to send to the time
|
||||
clock.
|
||||
copied from commpro.c - MakeKey"""
|
||||
key = int(key)
|
||||
session_id = int(session_id)
|
||||
k = 0
|
||||
for i in range(32):
|
||||
if (key & (1 << i)):
|
||||
k = (k << 1 | 1)
|
||||
else:
|
||||
k = k << 1
|
||||
k += session_id
|
||||
|
||||
k = pack(b'I', k)
|
||||
k = unpack(b'BBBB', k)
|
||||
k = pack(
|
||||
b'BBBB',
|
||||
k[0] ^ ord('Z'),
|
||||
k[1] ^ ord('K'),
|
||||
k[2] ^ ord('S'),
|
||||
k[3] ^ ord('O'))
|
||||
k = unpack(b'HH', k)
|
||||
k = pack(b'HH', k[1], k[0])
|
||||
|
||||
B = 0xff & ticks
|
||||
k = unpack(b'BBBB', k)
|
||||
k = pack(
|
||||
b'BBBB',
|
||||
k[0] ^ B,
|
||||
k[1] ^ B,
|
||||
B,
|
||||
k[3] ^ B)
|
||||
return k
|
Loading…
Reference in New Issue
Block a user