lwzm
5/26/2015 - 1:34 AM

ly_old_api.py

import hashlib
import struct
import socket
import time

from Crypto.Cipher import DES

e = b''

class Msg:
    boss_key = 'ef23ge23'.encode()
    boss_code = '103601'.encode()
    time_stamp = time.strftime("%Y%m%d%H%M%S").encode()
    auth_code = hashlib.md5(boss_code +  boss_key + time_stamp).digest()
    struct_head = struct.Struct("!HHHH")
    struct_login = struct.Struct("!6sB14s16s")
    code_login = 0x0001
    socket = None
    
    def __init__(self, fmt, code):
        if not fmt.startswith("!"):
            fmt = "!" + fmt
        self.struct = struct.Struct(fmt)
        assert self.struct.size % 8 == 0, (fmt, self.struct.size)
        self.code = code
    
    @classmethod
    def connect(self, addr=("172.16.1.12", 8116)):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(addr)
        s.settimeout(1)
        body = self.struct_login.pack(self.boss_code, 0x10, self.time_stamp, self.auth_code)
        msg = self.struct_head.pack(self.struct_head.size + self.struct_login.size, self.code_login, 0, 0) + body
        s.sendall(msg)
        data = s.recv(4096)
        des_key = data[struct_head.size:]
        self.des = DES.new(des_key)
        self.socket = s
    
    def send(self, *args):
        if not self.socket:
            self.connect()
        body = self.struct.pack(self.boss_code, *(i.encode() if isinstance(i, str) else i for i in args))
        msg = self.struct_head.pack(self.struct_head.size + self.struct.size, self.code, 0, 0) + self.des.encrypt(body)
        self.socket.sendall(msg)
        data = self.socket.recv(4096)
        return self.struct_head.unpack(data[:self.struct_head.size]), self.des.decrypt(data[self.struct_head.size:])
    
    __call__ = send

struct_head = struct.Struct("!HHHH")
struct_login = struct.Struct("!6sB14s16s")
code_login = 0x0001
code_active = 0x0002
e = b'x'

struct_plan = struct.Struct("!6s16s64sBHIBBB10sII32s22s")
struct_plan_del = struct.Struct("!6s16s32s18s")
struct_plan_pat = struct.Struct("!6s16s64sBBBB10sII32s20s")
struct_area = struct.Struct("!6s10s64s32s16s")
struct_user_plan = struct.Struct("!6s32s16sBH32s7s")
e = b'x'

code_online = 0x0305
code_bind = 0x0303
code_user = 0x0201
code_user_del = 0x0202
code_plan = 0x0107
code_plan_del = 0x0108
code_plan_pat = 0x0109
code_area = 0x0101
code_user_plan = 0x0204
code_user_pause = 0x0205
code_user_resume = 0x0206
code_user_move = 0x0207
code_user_password = 0x0301
code_user_bind_del = 0x0304
code_user_online_del = 0x0306

    
msg_user_online = Msg("6s32s32s18s", 0x305)
msg_user_add = Msg("6s32s32s64s16s10s10s10s10s128s15s12s18sBHB32s17s", 0x201)
msg_user_del = Msg("6s32sII32s10s", 0x202)
msg_user_pat_plan = Msg("6s32s16sBH32s15s", 0x204)
msg_user_pat_pause = Msg("6s32sH32s16s", 0x205)
msg_user_pat_resume = Msg("6s32sH32s16s", 0x206)
msg_user_pat_move = Msg("6s32s32s32s128sH32s16s", 0x207)
msg_user_pat_password = Msg("6s32s32s32s18s", 0x301)
msg_user_pat_domain = Msg("6s32s6sBBB32s17s", 0x307)
msg_user_del_binding = Msg("6s32s32s18s", 0x304)
msg_user_del_online = Msg("6s17s32s32s17s", 0x306)
msg_plan_add = Msg("6s16s64sBHIBBB10sII32s22s", 0x107)
msg_plan_del = Msg("6s16s32s18s", 0x108)
msg_plan_pat = Msg("6s16s64sBBBB10sII32s20s", 0x109)



################

user = 'qwer----------------------------'
[
    msg_user_add(user.strip("-"),'q',e,'CDBAND001M01210','2015-05-22','3000-11-30',e,e,e,e,e,e,0,0,0,e,e),
    msg_user_pat_plan(user,'p1', 0,0,e,e),
    msg_user_pat_pause(user, 0,e,e),
    msg_user_pat_pause(user, 0,e,e),
    msg_user_pat_resume(user, 0,e,e),
    msg_user_pat_move(user, e+e, e,e,0,e,e),
    msg_user_pat_password(user, 'new-pw', e,e),
    msg_user_pat_domain(user, 'dodo', 0,0,0, e,e),
    msg_user_del_binding(user, e,e),
    msg_user_del_online('0.0.0.0', 's1', e,e),
    msg_plan_add('p3','pp', 0,0,0, 1, 1, 3, 'p', 10, 20, e,e),
    msg_plan_pat('p3', 'new-name', 0, 1, 0, 3, 'p', 10, 20, e,e),
    msg_plan_del('p3', e,e),
    msg_user_del(user,0,0,e,e),
]