-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
221 lines (211 loc) · 8.91 KB
/
Copy pathserver.py
File metadata and controls
221 lines (211 loc) · 8.91 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
# -*- conding:utf-8 -*-
from http.server import HTTPServer, BaseHTTPRequestHandler
import re,time,os,base64
import json,requests
from urllib.parse import unquote
from urllib import parse
from socketserver import ThreadingMixIn
from WXBizMsgCrypt3 import WXBizMsgCrypt
import xml.etree.cElementTree as ET
import sys
#监听IP、端口
host = ('0.0.0.0', 9001)
#自动分段长度
length=1000
#企业微信应用key
#可在url中指定botID=botskey的key值的方式来选择向哪个BOT发送信息,第一个botskey的key必须为default,即默认bot,当url中不包含该参数默认使用该bot
botskey={"default":["企业ID","应用key","应用ID"],"应用名称":["企业ID","应用key","应用ID"]}
#接口访问key
key=""
#返回报文格式
result={"result":"","text":"","botID":""}
#企业微信后台上设置参数,用于解密用户发送的消息
#在企微应用后台设置回复API地址为http://IP:PORT/key/respkey的key/,程序根据respkey的key值判断使用哪个bot的解密参数
respkey={"机器人ID":["token","AES KEY","企业ID"],"机器人ID":["token","AES KEY","企业ID"]}
#自动分段
def send_to_wecom(text,botID):
if len(text)>length:
while True:
cutA = text[:length]
cutB = text[length:]
send_to_wecom1(cutA,botID)
if len(cutB)>length:
text = cutB
else:
send_to_wecom1(cutB,botID)
break
else:
send_to_wecom1(text,botID)
#发送消息
def send_to_wecom1(text,botID,wecom_touid='@all'):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={botskey[botID][0]}&corpsecret={botskey[botID][1]}"
response = requests.get(get_token_url).content
access_token = json.loads(response).get('access_token')
if access_token and len(access_token) > 0:
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser":wecom_touid,
"agentid":botskey[botID][2],
"msgtype":"text",
"text":{
"content":text
},
"duplicate_check_interval":600
}
response = requests.post(send_msg_url,data=json.dumps(data)).content
return response
else:
return False
#对发送的消息进行处理
def resp(message,bot_id):
return "发送的消息是:"+message+"\nbotID为:"+bot_id
#获取botID
def get_botID(agentid):
for item in botskey.items():
if (item[1][2]==agentid):
return item[0]
#获取应答bot
def get_resp_bot(path):
for item in respkey.items():
if (item[0] in path) == True:
return item[0]
class Resquest(BaseHTTPRequestHandler):
def do_GET(self):
#判断key是否正确
if (key in self.path) != True :
data = '''<script>window.location.href='https://www.baidu.com';</script>'''
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=UTF-8')
self.end_headers()
self.wfile.write(data.encode())
return
#企微URL验证
if("msg_signature" in self.path) == True:
bot=get_resp_bot(self.path)
wxcpt=WXBizMsgCrypt(respkey[bot][0],respkey[bot][1],respkey[bot][2])
path= parse.urlparse(self.path)
query=parse.parse_qs(path.query)
sVerifyMsgSig = query["msg_signature"][0]
sVerifyTimeStamp = query["timestamp"][0]
sVerifyNonce = query["nonce"][0]
sVerifyEchoStr = query["echostr"][0]
ret,sEchoStr=wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp,sVerifyNonce,sVerifyEchoStr)
self.send_response(200)
self.end_headers()
self.wfile.write(sEchoStr)
print("[DEBUG]企微URL验证")
return
#获取botID
path= parse.urlparse(self.path)
query=parse.parse_qs(path.query)
if("botID" in query) == True:
botID = query["botID"][0]
if(botID in botskey) == False:
botID = "default"
else:
botID = "default"
#判断发送内容是否为空
if("text" in query) == False:
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
result={"result":"error! text is empty","text":"","botID":""}
self.wfile.write(json.dumps(result).encode())
return
temp = query["text"][0].replace(" ","+")
#使用base64及URL编码方式解码并发送
try:
text=base64.b64decode(temp).decode('utf8')
print("[DEBUG]GET方式发送消息:"+text)
send_to_wecom(text,botID)
except:
text=unquote(temp,'utf-8')
print("[DEBUG]GET方式发送消息:"+text)
send_to_wecom(text,botID)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
result={"result":"success","text":text,"botID":botID}
self.wfile.write(json.dumps(result).encode())
def do_POST(self):
#判断key是否正确
if (key in self.path) != True :
data = '''<script>window.location.href='https://www.baidu.com';</script>'''
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=UTF-8')
self.end_headers()
self.wfile.write(data.encode())
return
#处理企微发送的消息
if("msg_signature" in self.path) == True:
bot=get_resp_bot(self.path)
wxcpt=WXBizMsgCrypt(respkey[bot][0],respkey[bot][1],respkey[bot][2])
path= parse.urlparse(self.path)
query=parse.parse_qs(path.query)
sReqMsgSig = query["msg_signature"][0]
sReqTimeStamp = query["timestamp"][0]
sReqNonce = query["nonce"][0]
sReqData = self.rfile.read(int(self.headers['content-length']))
ret,sMsg=wxcpt.DecryptMsg( sReqData, sReqMsgSig, sReqTimeStamp, sReqNonce)
xml_tree = ET.fromstring(sMsg)
content = xml_tree.find("Content").text
agnetid = xml_tree.find("AgentID").text
print("[DEBUG]企微接收消息:"+content+" 应用ID:"+agnetid)
#直接回复企微200,异步回复用户信息
self.send_response(200)
self.end_headers()
bot_id=get_botID(agnetid)
send_to_wecom(resp(content,bot_id),bot_id)
return
#获取botID
path= parse.urlparse(self.path)
query=parse.parse_qs(path.query)
if("botID" in query) == True:
botID = query["botID"][0]
if(botID in botskey) == False:
botID = "default"
else:
botID = "default"
#判断body是否为空
if(self.headers['content-length']==None):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
result={"result":"error! POST data is empty","text":"","botID":""}
self.wfile.write(json.dumps(result).encode())
self.wfile.write(str1.encode())
return
req_datas = self.rfile.read(int(self.headers['content-length']))
req_datas = req_datas.decode()
temp = re.search('text=(.*?)$',req_datas)
#判断发送内容是否为空
if(temp==None):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
result={"result":"error! text is empty","text":"","botID":""}
self.wfile.write(json.dumps(result).encode())
return
#使用base64及URL编码方式解码并发送
try:
text=base64.b64decode(temp.group(1)).decode('utf8')
print("[DEBUG]POST方式发送消息:"+text)
send_to_wecom(text,botID)
except:
text=unquote(temp.group(1),'utf-8')
print("[DEBUG]POST方式发送消息:"+text)
send_to_wecom(text,botID)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
result={"result":"success","text":text,"botID":botID}
self.wfile.write(json.dumps(result).encode())
#多线程,解决拒绝服务漏洞
class ThreadingHttpServer (ThreadingMixIn,HTTPServer):
pass
if __name__ == '__main__':
server = ThreadingHttpServer(host, Resquest)
print("Starting server, listen at: %s:%s" % host)
server.serve_forever()
server.server_close()