-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdynamic-connector.py
More file actions
54 lines (44 loc) · 1.5 KB
/
dynamic-connector.py
File metadata and controls
54 lines (44 loc) · 1.5 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
import threading
import time
from flask import Flask, request
import socket
app = Flask(__name__)
# Set of active targets (tuple of IP and port)
targets = set()
targets_lock = threading.Lock()
def connect_to_target(ip, port, timeout=1):
"""Connect to target via TCP socket"""
try:
with socket.create_connection((ip, int(port)), timeout=timeout):
print(f"[✓] Created connection {ip}:{port}")
except Exception as e:
print(f"[✗] Failed to connect {ip}:{port} - {e}")
def connector():
"""Background thread to continuously connect to targets"""
while True:
with targets_lock:
current_targets = list(targets)
for ip, port in current_targets:
connect_to_target(ip, port)
time.sleep(2)
@app.route('/')
def handle_request():
action = request.args.get('action')
ip = request.args.get('ip')
port = request.args.get('port')
if not all([action, ip, port]):
return "Missing required parameters: action, ip, port", 400
target = (ip, int(port))
with targets_lock:
if action == 'open':
targets.add(target)
return f"Opened {ip}:{port}", 200
elif action == 'close':
targets.discard(target)
return f"Closed {ip}:{port}", 200
else:
return f"Invalid action '{action}'", 400
if __name__ == '__main__':
threading.Thread(target=connector, daemon=True).start()
# Start the Flask server
app.run(host='127.0.0.1', port=8181)