Beispiel #1
0
    def decode_cmd_318(self, v_user, data):
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.short_host = bot_param.ShortHost
                self.long_host = bot_param.LongHost
                self.wechat_client = WechatClient.WechatClient(
                    self.long_host, 80, True)

        # send notify_req
        notify_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                               version=CONST_PROTOCOL_DICT['version'],
                               timeStamp=get_time_stamp(),
                               iP=get_public_ip(),
                               baseMsg=BaseMsg(cmd=-318,
                                               user=v_user,
                                               payloads=data))
        notify_rsp = grpc_client.send(notify_req)
        body = notify_rsp.baseMsg.payloads.encode('utf-8')
        add_msg_digest = json.loads(body)
        print add_msg_digest
        payload_dict = {
            "ChatroomId": add_msg_digest['ChatRoomId'],
            "MsgSeq": add_msg_digest['NewMsgSeq']
        }
        payload_dict_json = json.dumps(payload_dict)

        # get chatroom req
        get_chatroom_msg_req = WechatMsg(
            token=CONST_PROTOCOL_DICT['machine_code'],
            timeStamp=get_time_stamp(),
            iP=get_public_ip(),
            baseMsg=BaseMsg(cmd=805,
                            user=v_user,
                            payloads=payload_dict_json.encode('utf-8')))
        get_chatroom_msg_rsp = grpc_client.send(get_chatroom_msg_req)
        body = get_chatroom_msg_rsp.baseMsg.payloads
        upload_url = get_chatroom_msg_rsp.baseMsg.cmdUrl
        buffers = requests.post("http://" + self.short_host + upload_url,
                                data=body)
        print buffers.text

        if buffers is None or buffers.text.encode('utf-8')[0] == 191:
            # TODO:hextostr
            print("unknown package:{0}".format(buffers))
            return False

        # send grpc and decode again
        get_chatroom_msg_rsp.baseMsg.cmd = -805
        get_chatroom_msg_rsp.baseMsg.payloads = buffers.text.encode('utf-8')
        get_chatroom_msg_rsp = grpc_client.send(get_chatroom_msg_rsp)
        buffers = get_chatroom_msg_rsp.baseMsg.payloads
        print buffers
        return True
Beispiel #2
0
    def get_contact(self, v_user, wx_id_list):
        """
        TODO 根据联系人wxid,获取contact
        private void btn_GetContact_Click(object sender, EventArgs e)
        :param wx_id_list: 
        :param v_user: 
        :return: 
        """
        payLoadJson = "{\"UserNameList\":\"" + wx_id_list + "\"}"

        contacts_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                                 version=CONST_PROTOCOL_DICT['version'],
                                 timeStamp=get_time_stamp(),
                                 iP=get_public_ip(),
                                 baseMsg=BaseMsg(cmd=182,
                                                 user=v_user,
                                                 payloads=payLoadJson))
        get_contacts_rsp = grpc_client.send(contacts_req)

        (buffers, seq) = grpc_utils.get_seq_buffer(get_contacts_rsp)
        buffers = self.wechat_client.sync_send_and_return(buffers)

        if not check_buffer_16_is_191(buffers):
            print("未知包 init")
        else:
            get_contacts_rsp.baseMsg.cmd = -182
            get_contacts_rsp.baseMsg.payloads = char_to_str(buffers)
            get_contacts_rsp = grpc_client.send(get_contacts_rsp)

        print(get_contacts_rsp.baseMsg.payloads)
        print('获取联系人成功')
Beispiel #3
0
 def create_chatroom(self, v_user, wx_id_list):
     """
     建群        private void btn_CreateChatRoom_Click(object sender, EventArgs e)
     :param v_user: 
     :param wx_id_list: 
     :return: 
     """
     with app.app_context():
         bot_param = model.BotParam.query.filter_by(
             Username=v_user.userame).first()
         if bot_param:
             self.long_host = bot_param.LongHost
             self.wechat_client = WechatClient.WechatClient(
                 self.long_host, 80, True)
     payload_json = "{\"Membernames\":\"" + wx_id_list + "\"}"
     create_chatroom_req = WechatMsg(
         token=CONST_PROTOCOL_DICT['machine_code'],
         version=CONST_PROTOCOL_DICT['version'],
         timeStamp=get_time_stamp(),
         iP=get_public_ip(),
         baseMsg=BaseMsg(cmd=119,
                         user=v_user,
                         payloads=payload_json.encode('utf-8')))
     create_chatroom_rsp = grpc_client.send(create_chatroom_req)
     (buffers, seq) = grpc_utils.get_seq_buffer(create_chatroom_rsp)
     buffers = self.wechat_client.sync_send_and_return(buffers)
     check_buffer_16_is_191(buffers)
     create_chatroom_rsp.baseMsg.cmd = -119
     create_chatroom_rsp.baseMsg.payloads = char_to_str(buffers)
     payloads = grpc_client.send(create_chatroom_rsp)
     chatroom_detail = json.loads(payloads.baseMsg.payloads)
     chatroom_id = chatroom_detail['Roomeid']
     print('新建的群id是{}'.format(chatroom_id))
     self.send_text_msg(chatroom_id, "欢迎进群", v_user)
Beispiel #4
0
    def invite_chatroom(self, v_user, chatroom_id, wx_id):
        """
        邀请进群:
        向指定用户发送群名片进行邀请
        :param v_user: 
        :param chatroom_id: 
        :param wx_id: 
        :return: 
        """
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.short_host = bot_param.ShortHost
        payloadJson = "{\"ChatRoom\":\"" + chatroom_id + "\",\"Username\":\"" + wx_id + "\"}"
        req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                        version=CONST_PROTOCOL_DICT['version'],
                        timeStamp=get_time_stamp(),
                        iP=get_public_ip(),
                        baseMsg=BaseMsg(cmd=610,
                                        user=v_user,
                                        payloads=payloadJson.encode('utf-8')))
        rsp = grpc_client.send(req)
        (buffers, seq) = grpc_utils.get_seq_buffer(rsp)
        buffers = requests.post("http://" + self.short_host +
                                rsp.baseMsg.cmdUrl,
                                data=buffers)

        # 似乎每次登陆后的返回都不一样
        if not ord(buffers.text[0]) == 191:
            print "invite_chatroom_member 返回码{0}".format(ord(buffers.text[0]))
        return True
Beispiel #5
0
    def add_chatroom_member(self, v_user, chatroom_id, wx_id):
        """
        添加微信群成员为好友 btn_AddChatRoomMember_Click
        :param v_user: 
        :param wxid: 
        :return: 
        """
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.long_host = bot_param.LongHost
                self.wechat_client = WechatClient.WechatClient(
                    self.long_host, 80, True)
        payloadJson = "{\"Roomeid\":\"" + chatroom_id + "\",\"Membernames\":\"" + wx_id + "\"}"
        req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                        version=CONST_PROTOCOL_DICT['version'],
                        timeStamp=get_time_stamp(),
                        iP=get_public_ip(),
                        baseMsg=BaseMsg(cmd=120,
                                        user=v_user,
                                        payloads=payloadJson.encode('utf-8')))

        rsp = grpc_client.send(req)
        (buffers, seq) = grpc_utils.get_seq_buffer(rsp)
        buffers = self.wechat_client.sync_send_and_return(buffers)
        check_buffer_16_is_191(buffers)
        rsp.baseMsg.cmd = -120
        rsp.baseMsg.payloads = char_to_str(buffers)
        payloads = grpc_client.send(rsp)
Beispiel #6
0
    def delete_chatroom_member(self, v_user, chatroom_id, wx_id):
        """
        踢出微信群 btn_DelChatRoomUser_Click
        :param v_user: 
        :param wxid: 
        :return: 
        """
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.short_host = bot_param.ShortHost

        payloadJson = "{\"ChatRoom\":\"" + chatroom_id + "\",\"Username\":\"" + wx_id + "\"}"
        req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                        version=CONST_PROTOCOL_DICT['version'],
                        timeStamp=get_time_stamp(),
                        iP=get_public_ip(),
                        baseMsg=BaseMsg(cmd=179,
                                        user=v_user,
                                        payloads=payloadJson.encode('utf-8')))

        rsp = grpc_client.send(req)
        (buffers, seq) = grpc_utils.get_seq_buffer(rsp)
        buffers = requests.post(
            "http://" + self.short_host + rsp.baseMsg.cmdUrl, buffers)
        # 检测buffers[0] == 191
        if not ord(buffers.text.encode('utf-8')[0]):
            print "delete_chatroom_member 未知包"
            return False
        else:
            return True
Beispiel #7
0
    def heart_beat(self, v_user):
        """
        30到60s的定时心跳包
        主要用来维持socket的链接状态
        :param v_user: 
        :return: 
        """
        sync_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                             version=CONST_PROTOCOL_DICT['version'],
                             timeStamp=get_time_stamp(),
                             iP=get_public_ip(),
                             baseMsg=BaseMsg(cmd=205, user=v_user))
        sync_rsp = grpc_client.send(sync_req)
        (buffers, seq) = grpc_utils.get_seq_buffer(sync_rsp)
        buffers = self.wechat_client.sync_send_and_return(buffers)
        if buffers is None:
            return False

        ret_code = ord(buffers[16])
        if ret_code is not 191:
            selector = read_int(buffers, 16)
            print "--heartbeat() selector is:{}--".format(selector)
            # if selector == -1:
            #     print "user logout when heartbeat"
            #
            # if selector > 0:
            #     v_user = pickle.loads(red.get('v_user_' + v_user.username))
            #     self.async_check(v_user)

        return True
Beispiel #8
0
    def get_chatroom_detail(self, v_user, room_id):
        """
        获取群的信息
        :param room_id: 
        :param v_user: 
        :return: 
        """
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.short_host = bot_param.ShortHost
        pay_load_json = "{\"Chatroom\":\"" + room_id + "\"}"
        get_room_detail_req = WechatMsg(
            token=CONST_PROTOCOL_DICT['machine_code'],
            version=CONST_PROTOCOL_DICT['version'],
            timeStamp=get_time_stamp(),
            iP=get_public_ip(),
            baseMsg=BaseMsg(cmd=551,
                            user=v_user,
                            payloads=pay_load_json.encode('utf-8')))
        get_room_detail_rsp = grpc_client.send(get_room_detail_req)
        # (buffers, seq) = grpc_utils.get_seq_buffer(get_room_detail_rsp)
        body = get_room_detail_rsp.baseMsg.payloads

        buffers = requests.post(
            "http://" + self.short_host + get_room_detail_rsp.baseMsg.cmdUrl,
            body)

        get_room_detail_rsp.baseMsg.cmd = -551
        get_room_detail_rsp.baseMsg.payloads = buffers.content
        get_room_detail_rsp = grpc_client.send(get_room_detail_rsp)
        buffers = get_room_detail_rsp.baseMsg.payloads
        print buffers.encode('utf-8')
Beispiel #9
0
    def sns_post(self, v_user, xml):
        """
        TODO 发送朋友圈 btn_SnsPost_Click
        :param v_user:
        :return: 
        """
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.long_host = bot_param.LongHost
                self.short_host = bot_param.ShortHost
                self.wechat_client = WechatClient.WechatClient(
                    self.long_host, 80, True)
        pay_load = "{\"Content\":\"" + xml + "\"}"
        sns_post_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                                 version=CONST_PROTOCOL_DICT['version'],
                                 timeStamp=get_time_stamp(),
                                 iP=get_public_ip(),
                                 baseMsg=BaseMsg(
                                     cmd=209,
                                     user=v_user,
                                     payloads=pay_load.encode('utf-8')))

        sns_post_rsp = grpc_client.send(sns_post_req)
        (buffers, seq) = grpc_utils.get_seq_buffer(sns_post_rsp)
        buffers = self.wechat_client.sync_send_and_return(buffers)
        if not self.wechat_client.check_buffer_16_is_191(buffers):
            print "sns post 未知包"
            return False
        else:
            return True
Beispiel #10
0
    def auto_auth(self, v_user, uuid, device_type, new_socket=True):
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.long_host = bot_param.LongHost
                if new_socket:
                    self.wechat_client = WechatClient.WechatClient(
                        self.long_host, 80, True)

        pay_load = "{\"UUid\":\"" + uuid + "\",\"DeviceType\":\"" + device_type + "\"}"
        auto_auth_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                                  version=CONST_PROTOCOL_DICT['version'],
                                  timeStamp=get_time_stamp(),
                                  iP=get_public_ip(),
                                  baseMsg=BaseMsg(
                                      cmd=702,
                                      user=v_user,
                                      payloads=pay_load.encode('utf-8')))

        auto_auth_rsp = grpc_client.send(auto_auth_req)
        (buffers, seq) = grpc_utils.get_seq_buffer(auto_auth_rsp)
        buffers = self.wechat_client.sync_send_and_return(
            buffers, close_socket=new_socket)
        self.wechat_client.check_buffer_16_is_191(buffers)

        auto_auth_rsp.baseMsg.cmd = -702
        auto_auth_rsp.baseMsg.payloads = buffers
        auto_auth_rsp_2 = grpc_client.send(auto_auth_rsp)
        if auto_auth_rsp_2.baseMsg.ret == 0:
            user = auto_auth_rsp_2.baseMsg.user
            print("二次登陆成功")
            v_user_pickle = pickle.dumps(user)
            red.set('v_user_' + v_user.userame, v_user_pickle)
            return True
        elif auto_auth_rsp_2.baseMsg.ret == -100 or auto_auth_rsp_2.baseMsg.ret == -2023:
            print("二次登陆失败,重新扫码吧朋友")

            ret_reason = ''
            try:
                payload = auto_auth_rsp_2.baseMsg.payloads
                start = "<Content><![CDATA["
                end = "]]></Content>"
                ret_reason = payload[payload.find(start) +
                                     len(start):payload.find(end)]
            except Exception as e:
                ret_reason = "未知"

            # oss_utils.beary_chat("淘宝客:{0} 已掉线,原因:{1}".format(v_user.nickname, ret_reason))
            self.wechat_client.close_when_done()
            return False
        else:
            print("二次登陆未知返回码")
            ret_code = auto_auth_rsp_2.baseMsg.ret
            # oss_utils.beary_chat("淘宝客:{0} 已掉线,未知返回码:{1}".format(v_user.nickname, ret_code))
            self.wechat_client.close_when_done()
            return False
Beispiel #11
0
 def new_init(self, v_user):
     """
     登陆初始化
     :param v_user: 
     :return: 
     """
     with app.app_context():
         bot_param = model.BotParam.query.filter_by(
             Username=v_user.userame).first()
         if bot_param:
             self.long_host = bot_param.LongHost
             self.wechat_client = WechatClient.WechatClient(
                 self.long_host, 80, True)
     new_init_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                              version=CONST_PROTOCOL_DICT['version'],
                              timeStamp=get_time_stamp(),
                              iP=get_public_ip(),
                              baseMsg=BaseMsg(cmd=1002, user=v_user))
     new_init_rsp = grpc_client.send(new_init_req)
     (buffers, seq) = grpc_utils.get_seq_buffer(new_init_rsp)
     buffers = self.wechat_client.sync_send_and_return(buffers, time_out=3)
     if not check_buffer_16_is_191(buffers):
         print("未知包 init")
         self.wechat_client.close_when_done()
         return False
     else:
         new_init_rsp.baseMsg.cmd = -1002
         new_init_rsp.baseMsg.payloads = char_to_str(buffers)
         new_init_rsp = grpc_client.send(new_init_rsp)
         # 打印出同步消息的结构体
         msg_list = json.loads(new_init_rsp.baseMsg.payloads)
         if msg_list is not None:
             for msg_dict in msg_list:
                 # save msg
                 # print(msg_dict)
                 if msg_dict['MsgType'] == 2:
                     with app.app_context():
                         model.save_contact(msg_dict)
                 else:
                     print(msg_dict)
         v_user = new_init_rsp.baseMsg.user
         v_user_pickle = pickle.dumps(v_user)
         red.set('v_user_' + v_user.userame, v_user_pickle)
         if new_init_rsp.baseMsg.ret == 8888:
             print("同步完成")
             self.wechat_client.close_when_done()
         else:
             self.wechat_client.close_when_done()
             self.new_init(v_user)
         return True
Beispiel #12
0
 def search_contact(self, user_name, v_user):
     payLoadJson = "{\"Username\":\"" + user_name + "\"}"
     search_contact_req = WechatMsg(
         token=CONST_PROTOCOL_DICT['machine_code'],
         version=CONST_PROTOCOL_DICT['version'],
         timeStamp=get_time_stamp(),
         iP=get_public_ip(),
         baseMsg=BaseMsg(cmd=106,
                         user=v_user,
                         payloads=payLoadJson.encode('utf-8')))
     search_contact_rsp = grpc_client.send(search_contact_req)
     (buffers, seq) = grpc_utils.get_seq_buffer(search_contact_rsp)
     buffers = self.wechat_client.sync_send_and_return(buffers)
     check_buffer_16_is_191(buffers)
     search_contact_rsp.baseMsg.cmd = -106
     search_contact_rsp.baseMsg.payloads = char_to_str(buffers)
     payloads = grpc_client.send(search_contact_rsp)
     print(payloads)
Beispiel #13
0
    def get_qrcode(self, md_username):
        """
        获取qrcode
        :return: 
        """
        # session_key = '5326451F200E0D130CE4AE27262B5897'.decode('hex')
        # 构造qrcode请求
        self.wechat_client = WechatClient.WechatClient(self.long_host, 80,
                                                       True)
        self.deviceId = get_md5(md_username)

        qrcode_req = WechatMsg(
            token=CONST_PROTOCOL_DICT['machine_code'],
            version=CONST_PROTOCOL_DICT['version'],
            timeStamp=get_time_stamp(),
            iP=get_public_ip(),
            baseMsg=BaseMsg(cmd=502,
                            user=User(sessionKey=int_list_convert_to_byte_list(
                                CONST_PROTOCOL_DICT['random_encry_key']),
                                      deviceId=self.deviceId)))
        qrcode_rsp = grpc_client.send(qrcode_req)
        check_grpc_response(qrcode_rsp.baseMsg.ret)
        (buffers, seq) = grpc_utils.get_seq_buffer(qrcode_rsp)
        data = self.wechat_client.sync_send_and_return(buffers)
        qrcode_req.baseMsg.cmd = -502
        qrcode_req.baseMsg.payloads = char_to_str(data)
        qrcode_rsp = grpc_client.send(qrcode_req)

        buffers = qrcode_rsp.baseMsg.payloads
        # 保存二维码图片
        qr_code = json.loads(buffers)
        imgData = base64.b64decode(qr_code['ImgBuf'])
        uuid = qr_code['Uuid']

        with app.app_context():
            model.save_qr_code(qr_code)

        try:
            oss_path = oss_utils.put_object_to_oss(uuid + ".png", imgData)
            print("oss_path is: {}".format(oss_path))
        except Exception as e:
            print(e)
            print('upload oss err by uuid:{}'.format(uuid))
        return oss_path, qrcode_rsp, self.deviceId
Beispiel #14
0
 def send_voice_msg(self, v_user, to_user_name, url):
     """
     发送语音 btn_SendVoice_Click
     :param v_user: 
     :param url: 
     :return: 
     """
     with app.app_context():
         bot_param = model.BotParam.query.filter_by(
             Username=v_user.userame).first()
         if bot_param:
             self.long_host = bot_param.LongHost
             self.wechat_client = WechatClient.WechatClient(
                 self.long_host, 80, True)
     voice_path = 'img/test.mp3'
     with open(voice_path, 'rb') as voice_file:
         data = voice_file.read()
     payload = {
         'ToUserName': to_user_name,
         'Offset': 0,
         'Length': len(data),
         'EndFlag': 1,
         'Data': data,
         'VoiceFormat': 0
     }
     payload_json = json.dumps(payload)
     send_voice_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                                version=CONST_PROTOCOL_DICT['version'],
                                timeStamp=get_time_stamp(),
                                iP=get_public_ip(),
                                baseMsg=BaseMsg(
                                    cmd=127,
                                    user=v_user,
                                    payloads=payload_json.encode('utf-8')))
     send_voice_rsp = grpc_client.send(send_voice_req)
     (buffers, seq) = grpc_utils.get_seq_buffer(send_voice_rsp)
     buffers = self.wechat_client.sync_send_and_return(buffers)
     self.wechat_client.close_when_done()
     return check_buffer_16_is_191(buffers)
Beispiel #15
0
 def modify_chatroom_name(self, v_user, chatroom_id, room_name):
     with app.app_context():
         bot_param = model.BotParam.query.filter_by(
             Username=v_user.userame).first()
         if bot_param:
             self.short_host = bot_param.ShortHost
     payload_json = "{\"Cmdid\":27,\"ChatRoom\":\"" + chatroom_id + "\",\"Roomname\":\"" + room_name + "\"}"
     modify_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                            version=CONST_PROTOCOL_DICT['version'],
                            timeStamp=get_time_stamp(),
                            iP=get_public_ip(),
                            baseMsg=BaseMsg(
                                cmd=681,
                                user=v_user,
                                payloads=payload_json.encode('utf-8')))
     modify_rsp = grpc_client.send(modify_req)
     (buffers, seq) = grpc_utils.get_seq_buffer(modify_rsp)
     buffers = requests.post("http://" + self.short_host +
                             modify_rsp.baseMsg.cmdUrl,
                             data=buffers)
     self.wechat_client.close_when_done()
     return check_buffer_16_is_191(buffers)
Beispiel #16
0
    def send_text_msg(self, user_name, content, v_user):
        """
        参考btn_SendMsg_Click
        :param user_name: 
        :param content: 
        :param at_user_list: 
        :param v_user: 
        :return: 
        """
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.long_host = bot_param.LongHost
                self.wechat_client = WechatClient.WechatClient(
                    self.long_host, 80, True)
        payLoadJson = "{\"ToUserName\":\"" + user_name + "\",\"Content\":\"" + content + "\",\"Type\":0,\"MsgSource\":\"\"}"
        send_text_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                                  version=CONST_PROTOCOL_DICT['version'],
                                  timeStamp=get_time_stamp(),
                                  iP=get_public_ip(),
                                  baseMsg=BaseMsg(
                                      cmd=522,
                                      user=v_user,
                                      payloads=payLoadJson.encode('utf-8')))
        send_text_rsp = grpc_client.send(send_text_req)

        (buffers, seq) = grpc_utils.get_seq_buffer(send_text_rsp)
        import binascii
        print(binascii.b2a_hex(buffers))
        buffers = self.wechat_client.sync_send_and_return(buffers)
        if not check_buffer_16_is_191(buffers):
            if read_int(buffers, 18) == -13:
                print("Session Time out 离线或用户取消登陆 需执行二次登录")
            return False
        self.wechat_client.close_when_done()
        return True
Beispiel #17
0
    def confirm_qrcode_login(self, qr_code, keep_heart_beat):
        # 重置longHost
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=qr_code['Username']).first()
            if bot_param and bot_param.LongHost is not None and bot_param.LongHost != "":
                self.long_host = bot_param.LongHost
                self.wechat_client = WechatClient.WechatClient(
                    self.long_host, 80, True)

        # 微信确认登陆模块
        UUid = u"667D18B1-BCE3-4AA2-8ED1-1FDC19446567"
        DeviceType = u"<k21>TP_lINKS_5G</k21><k22>中国移动</k22><k24>c1:cd:2d:1c:5b:11</k24>"
        payLoadJson = "{\"Username\":\"" + qr_code[
            'Username'] + "\",\"PassWord\":\"" + qr_code[
                'Password'] + "\",\"UUid\":\"" + UUid + "\",\"DeviceType\":\"" + DeviceType + "\"}"

        qrcode_login_req = WechatMsg(
            token=CONST_PROTOCOL_DICT['machine_code'],
            version=CONST_PROTOCOL_DICT['version'],
            timeStamp=get_time_stamp(),
            iP=get_public_ip(),
            baseMsg=BaseMsg(cmd=1111,
                            user=User(sessionKey=int_list_convert_to_byte_list(
                                CONST_PROTOCOL_DICT['random_encry_key']),
                                      deviceId=self.deviceId),
                            payloads=payLoadJson.encode('utf-8')))

        qrcode_login_rsp = grpc_client.send(qrcode_login_req)
        (buffers, seq) = grpc_utils.get_seq_buffer(qrcode_login_rsp)
        buffers = self.wechat_client.sync_send_and_return(buffers)
        check_buffer_16_is_191(buffers)

        # 解微信包
        qrcode_login_rsp.baseMsg.cmd = -1001
        qrcode_login_rsp.baseMsg.payloads = char_to_str(buffers)
        qrcode_login_rsp = grpc_client.send(qrcode_login_rsp)
        with app.app_context():
            bot_param_db = model.BotParam.query.filter_by(
                Username=qr_code['Username']).first()
            if bot_param_db is None:
                model.save_bot_param(qr_code['Username'], self.deviceId,
                                     qrcode_login_rsp.baseMsg.longHost,
                                     qrcode_login_rsp.baseMsg.shortHost)
            else:
                model.update_bot_param(bot_param_db, qr_code['Username'],
                                       self.deviceId,
                                       qrcode_login_rsp.baseMsg.longHost,
                                       qrcode_login_rsp.baseMsg.shortHost)
        if qrcode_login_rsp.baseMsg.ret == -301:
            # 返回-301代表重定向
            self.wechat_client.close_when_done()
            # 在user表写上gg,麻烦重新登录吧
            return False
        elif qrcode_login_rsp.baseMsg.ret == 0:
            # 返回0代表登陆成功
            print('login successful')
            # 将User赋值
            v_user = qrcode_login_rsp.baseMsg.user
            # 存下v_user
            with app.app_context():
                model.save_user(v_user)

            with app.app_context():
                try:
                    user_db = model.User.query.filter_by(
                        userame=v_user.userame).first()
                    user_db.login = 1
                    db.session.commit()
                except Exception as e:
                    print(e)

            # if keep_heart_beat:
            # 登陆成功,维持心跳
            #     asyn_rec_thread = threading.Thread(target=self, WXBot.heart_beat(v_user))
            #     asyn_rec_thread.start()

            red.set('v_user_' + str(v_user.userame), pickle.dumps(v_user))
            self.wechat_client.close_when_done()
            return True
        else:
            print("qrcode_login_rsp.baseMsg.ret is {}".format(
                qrcode_login_rsp.baseMsg.ret))
            print("原因是{}".format(qrcode_login_rsp.baseMsg.payloads))
            return False
Beispiel #18
0
    def async_check(self, v_user, new_socket=True):
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.long_host = bot_param.LongHost
                if new_socket:
                    self.wechat_client = WechatClient.WechatClient(
                        self.long_host, 80, True)

        sync_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                             version=CONST_PROTOCOL_DICT['version'],
                             timeStamp=get_time_stamp(),
                             iP=get_public_ip(),
                             baseMsg=BaseMsg(cmd=138, user=v_user))
        sync_rsp = grpc_client.send(sync_req)
        (buffers, seq) = grpc_utils.get_seq_buffer(sync_rsp)
        buffers = self.wechat_client.sync_send_and_return(
            buffers, close_socket=new_socket)
        if not check_buffer_16_is_191(buffers):
            try:
                # TODO:uuid 和 devicetype 存起来?
                UUid = u"667D18B1-BCE3-4AA2-8ED1-1FDC19446567"
                DeviceType = u"<k21>TP_lINKS_5G</k21><k22>中国移动</k22><k24>c1:cd:2d:1c:5b:11</k24>"
                if self.auto_auth(v_user,
                                  UUid,
                                  DeviceType,
                                  new_socket=new_socket):
                    v_user = pickle.loads(red.get('v_user_' + v_user.userame))
                    self.async_check(v_user, new_socket=new_socket)
                    return True

                self.logout_bot(v_user)

                print(read_int(buffers, 18))
                if read_int(buffers, 18) == -13:
                    print("Session Time out 离线或用户取消登陆 需执行二次登录")
            except Exception as e:
                print(e)
            return False
        else:
            sync_rsp.baseMsg.cmd = -138
            sync_rsp.baseMsg.payloads = char_to_str(buffers)
            sync_rsp = grpc_client.send(sync_rsp)
            # 刷新用户信息
            v_user = sync_rsp.baseMsg.user

            v_user_pickle = pickle.dumps(v_user)
            red.set('v_user_' + v_user.userame, v_user_pickle)

            msg_list = json.loads(sync_rsp.baseMsg.payloads)
            if msg_list is not None:
                for msg_dict in msg_list:
                    try:
                        if msg_dict['MsgType'] == 2:
                            with app.app_context():
                                model.save_contact(msg_dict)
                        elif msg_dict['Status'] is not None:
                            try:
                                action_rule.filter_keyword_rule(
                                    v_user.userame, msg_dict)
                            except Exception as e:
                                print(e)
                            with app.app_context():
                                model.save_message(msg_dict)
                        else:
                            print(msg_dict)
                    except Exception as e:
                        print(e)
                        print(msg_dict)
                self.async_check(v_user, new_socket=new_socket)
            else:
                print "sync 完成"
Beispiel #19
0
    def send_img_msg(self, user_name, v_user, url):
        """
        btn_SendMsgimg_Click
        :param user_name: 
        :param v_user: 
        :return: 
        """
        with app.app_context():
            bot_param = model.BotParam.query.filter_by(
                Username=v_user.userame).first()
            if bot_param:
                self.long_host = bot_param.LongHost
                self.wechat_client = WechatClient.WechatClient(
                    self.long_host, 80, True)
        data = urllib2.urlopen(url).read()
        # img_path = 'img/no.png'
        # with codecs.open(img_path, 'rb') as img_file:
        #     data = img_file.read()
        # with open(img_path, 'rb') as img_file:
        #     data = img_file.read()
        # 起始位置
        start_pos = 0
        # 数据分块长度
        data_len = 16535
        # 总长度
        data_total_length = len(data)
        print('preparing sent img length length {}'.format(data_total_length))
        # 客户图像id
        client_img_id = v_user.userame + "_" + str(get_time_stamp())
        while start_pos != data_total_length:
            # 每次最多只传65535
            count = 0
            if data_total_length - start_pos > data_len:
                count = data_len
            else:
                count = data_total_length - start_pos
            upload_data = base64.b64encode(data[start_pos:start_pos + count])

            payLoadJson = {
                'ClientImgId': client_img_id.encode('utf-8'),
                'ToUserName': user_name.encode('utf-8'),
                'StartPos': start_pos,
                'TotalLen': data_total_length,
                'DataLen': len(data[start_pos:start_pos + count]),
                'Data': upload_data
            }
            pay_load_json = json.dumps(payLoadJson)
            start_pos = start_pos + count
            print("Send Img Block {}".format(count))
            print("start_pos is {}".format(start_pos))
            print("data_total_length is {}".format(data_total_length))
            img_msg_req = WechatMsg(token=CONST_PROTOCOL_DICT['machine_code'],
                                    version=CONST_PROTOCOL_DICT['version'],
                                    timeStamp=get_time_stamp(),
                                    iP=get_public_ip(),
                                    baseMsg=BaseMsg(
                                        cmd=110,
                                        user=v_user,
                                        payloads=pay_load_json.encode('utf-8'),
                                    ))

            img_msg_rsp = grpc_client.send(img_msg_req)
            (buffers, seq) = grpc_utils.get_seq_buffer(img_msg_rsp)
            buffers = self.wechat_client.sync_send_and_return(buffers,
                                                              time_out=3)
            check_buffer_16_is_191(buffers)
        self.wechat_client.close_when_done()
        return True
Beispiel #20
0
    def check_qrcode_login(self, qrcode_rsp, device_id):
        """
        检测扫描是否登陆
        :param qr_code: 
        :return: 
        """
        buffers = qrcode_rsp.baseMsg.payloads
        qr_code = json.loads(buffers)
        uuid = qr_code['Uuid']
        notify_key_str = base64.b64decode(qr_code['NotifyKey'].encode('utf-8'))
        long_head = qrcode_rsp.baseMsg.longHead
        start_time = datetime.now()
        self.deviceId = device_id
        while qr_code['Status'] is not 2:
            # 构造扫描确认请求
            check_qrcode_grpc_req = WechatMsg(
                token=CONST_PROTOCOL_DICT['machine_code'],
                version=CONST_PROTOCOL_DICT['version'],
                timeStamp=get_time_stamp(),
                iP=get_public_ip(),
                baseMsg=BaseMsg(
                    cmd=503,
                    longHead=long_head,
                    payloads=str(uuid),
                    user=User(sessionKey=int_list_convert_to_byte_list(
                        CONST_PROTOCOL_DICT['random_encry_key']),
                              deviceId=self.deviceId,
                              maxSyncKey=notify_key_str)))

            checkqrcode_grpc_rsp = grpc_client.send(check_qrcode_grpc_req)
            (buffers, seq) = grpc_utils.get_seq_buffer(checkqrcode_grpc_rsp)
            buffers = self.wechat_client.sync_send_and_return(buffers)
            check_buffer_16_is_191(buffers)

            # 将微信包传给grpc服务器,进行一次解包操作
            checkqrcode_grpc_rsp.baseMsg.cmd = -503
            checkqrcode_grpc_rsp.baseMsg.payloads = char_to_str(buffers)
            checkqrcode_grpc_rsp_2 = grpc_client.send(checkqrcode_grpc_rsp)
            payloads = checkqrcode_grpc_rsp_2.baseMsg.payloads
            if 'unpack err' not in payloads:
                qr_code = json.loads(payloads)

            if qr_code['Status'] is 2:
                # save qr_code
                with app.app_context():
                    qr_code_db = model.Qrcode.query.filter_by(
                        Uuid=uuid).order_by(model.Qrcode.id.desc()).first()
                    model.update_qr_code(qr_code, qr_code_db)
                # 成功登陆
                return qr_code
            elif qr_code['Status'] is 0:
                # 未扫描等待扫描
                pass
            elif qr_code['Status'] is 1:
                # 已扫描未确认
                pass
            elif qr_code['Status'] is 4:
                # 已取消扫描
                pass
            # 等待5s再检测
            time.sleep(5)
            # 如果3分钟都没有正常返回status 2 返回False
            if (datetime.now() - start_time).seconds >= 60 * 3:
                return False