@@ -18,41 +18,45 @@ ServerUrl为你的云函数地址
18
18
将以下内容配置到云函数,并建议将云函数执行时间和api网关后端超时设置为15秒或以上:
19
19
20
20
``` python
21
- from socket import *
21
+ import socket
22
22
from concurrent.futures import ThreadPoolExecutor
23
+ from collections import defaultdict
23
24
import threading
24
25
25
- Tport = " "
26
+ # 使用defaultdict存储每个线程找到的开放端口
27
+ results_per_thread = defaultdict(list )
26
28
ip = " "
27
29
ports = " "
28
- lock = threading.Lock()
29
30
30
31
def Scan (port ):
31
- global Tport
32
32
try :
33
- conn = socket( AF_INET ,SOCK_STREAM )
33
+ conn = socket.socket(socket. AF_INET , socket. SOCK_STREAM )
34
34
conn.settimeout(1 )
35
- res = conn.connect_ex((str (ip),int (port)))
35
+ res = conn.connect_ex((str (ip), int (port)))
36
36
if res == 0 :
37
- with lock:
38
- Tport = Tport + " , " + port
37
+ # 将结果添加到当前线程局部变量中
38
+ results_per_thread[threading.current_thread()].append( port)
39
39
except Exception as err:
40
40
print (err)
41
41
finally :
42
42
conn.close()
43
43
44
44
def main_handler (event , context ):
45
- global Tport, ip,ports
46
-
45
+ global ip, ports
46
+ results_per_thread.clear()
47
47
ip = event[" queryString" ][" ip" ]
48
48
ports = event[" queryString" ][" port" ].split(" ," )
49
49
50
- with ThreadPoolExecutor(max_workers = 20 ) as executor:
50
+ with ThreadPoolExecutor(max_workers = 20 ) as executor:
51
51
executor.map(Scan, ports)
52
52
53
- with lock:
54
- a = Tport[1 :]
55
- Tport = " "
53
+ # 合并所有线程的结果
54
+ open_ports = []
55
+ for thread_results in results_per_thread.values():
56
+ open_ports.extend(thread_results)
57
+
58
+ # 将端口列表转换为逗号分隔的字符串
59
+ a = " ," .join(str (port) for port in open_ports)
56
60
return a
57
61
```
58
62
0 commit comments