Repository navigation
Expand file tree
/
Copy pathagent_windows.py
More file actions
246 lines (219 loc) · 9.49 KB
/
Copy pathagent_windows.py
File metadata and controls
246 lines (219 loc) · 9.49 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3
"""
SyncBridge Windows Agent
Syncs clipboard, mirrors notifications, handles remote shell, file transfers.
Usage (PowerShell):
$env:SYNCBRIDGE_SERVER="http://192.168.1.100:5000"
$env:SYNCBRIDGE_TOKEN="syncbridge-token-2024"
$env:SYNCBRIDGE_NAME="my-windows-pc"
python agent_windows.py
Dependencies:
pip install requests pywin32 win10toast plyer
"""
import os, sys, uuid, time, signal, platform, subprocess, threading, ctypes
import requests
# ─── Config ──────────────────────────────────────────────────────────────────
SERVER = os.environ.get('SYNCBRIDGE_SERVER', 'http://localhost:5000')
TOKEN = os.environ.get('SYNCBRIDGE_TOKEN', 'syncbridge-token-2024')
NAME = os.environ.get('SYNCBRIDGE_NAME', platform.node())
DEVICE_ID = os.environ.get('SYNCBRIDGE_ID', str(uuid.uuid4()))
POLL = int(os.environ.get('SYNCBRIDGE_POLL', 5))
HEADERS = {'X-Auth-Token': TOKEN, 'Content-Type': 'application/json'}
running = True
# ─── Windows Clipboard (win32) ────────────────────────────────────────────────
try:
import win32clipboard
import win32con
HAS_WIN32 = True
except ImportError:
HAS_WIN32 = False
def _get_clip_win32():
if not HAS_WIN32:
return ''
try:
win32clipboard.OpenClipboard()
try:
data = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
return data.strip()
finally:
win32clipboard.CloseClipboard()
except: return ''
def _set_clip_win32(text):
if not HAS_WIN32:
return
try:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text)
win32clipboard.CloseClipboard()
except: pass
def _get_clip_powershell():
"""Fallback clipboard get via PowerShell."""
try:
out = subprocess.check_output(
['powershell', '-Command', 'Get-Clipboard'],
capture_output=True, text=True, timeout=3)
return out.stdout.strip()
except: return ''
def _set_clip_powershell(text):
"""Fallback clipboard set via PowerShell."""
try:
subprocess.run(
['powershell', '-Command', f'Set-Clipboard -Value "{text}"'],
capture_output=True, timeout=3)
except: pass
def get_clipboard():
return _get_clip_win32() if HAS_WIN32 else _get_clip_powershell()
def set_clipboard(text):
if HAS_WIN32:
_set_clip_win32(text)
else:
_set_clip_powershell(text)
# ─── Notifications ────────────────────────────────────────────────────────────
try:
from plyer import notification as plyer_notif
HAS_PLYER = True
except ImportError:
HAS_PLYER = False
def _notify_win(title, body, app=''):
prefix = f"[{app}] " if app else ""
full_title = f"{prefix}{title}"
if HAS_PLYER:
try:
plyer_notif.notify(
title=full_title,
message=body,
app_name='SyncBridge',
timeout=5,
)
return
except: pass
# Ultimate fallback — message box (blocking, use sparingly)
try:
ctypes.windll.user32.MessageBoxW(0, body, full_title, 0x40)
except: pass
# ─── Helpers ──────────────────────────────────────────────────────────────────
def api(method, path, **kw):
try:
r = getattr(requests, method)(f"{SERVER}{path}",
headers=HEADERS, timeout=10, **kw)
return r.json() if r.content else {}
except Exception as e:
log(f"API error: {e}")
return {}
def log(msg):
print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
# ─── Registration & Heartbeat ─────────────────────────────────────────────────
def register():
log(f"Registering as '{NAME}' ({DEVICE_ID})")
res = api('post', '/api/register', json={
'device_id': DEVICE_ID,
'name': NAME,
'type': 'windows',
'os': platform.platform(),
})
log(f"Server: {res}")
def heartbeat_loop():
while running:
api('post', '/api/heartbeat', json={'device_id': DEVICE_ID})
time.sleep(20)
# ─── Clipboard Loop ───────────────────────────────────────────────────────────
last_clip = ''
def clipboard_loop():
global last_clip
while running:
try:
cur = get_clipboard()
if cur and cur != last_clip:
last_clip = cur
api('post', '/api/clipboard', json={'content': cur, 'source': NAME})
log(f"[CLIP] → server: {cur[:60]}")
remote = api('get', '/api/clipboard')
if (remote and remote.get('content')
and remote.get('source') != NAME
and remote['content'] != last_clip):
set_clipboard(remote['content'])
last_clip = remote['content']
log(f"[CLIP] ← {remote.get('source')}: {remote['content'][:60]}")
except Exception as e:
log(f"[CLIP] error: {e}")
time.sleep(POLL)
# ─── Notification Loop ────────────────────────────────────────────────────────
def notification_loop():
seen = set()
while running:
try:
notifs = api('get', '/api/notifications')
if isinstance(notifs, list):
for n in notifs:
if n['id'] not in seen:
seen.add(n['id'])
if n.get('source') != NAME:
_notify_win(n.get('title',''), n.get('body',''), n.get('app',''))
log(f"[NOTIF] {n.get('app')} — {n.get('title')}")
except Exception as e:
log(f"[NOTIF] error: {e}")
time.sleep(POLL)
# ─── Shell Loop ───────────────────────────────────────────────────────────────
def shell_loop():
while running:
try:
cmds = api('get', f'/api/shell/poll?device_id={DEVICE_ID}')
if isinstance(cmds, list):
for cmd_obj in cmds:
rid = cmd_obj['id']
command = cmd_obj['command']
log(f"[SHELL] exec: {command}")
try:
res = subprocess.run(
command, shell=True, capture_output=True,
text=True, timeout=30, encoding='utf-8',
errors='replace')
api('post', '/api/shell/result', json={
'request_id': rid,
'output': res.stdout,
'error': res.stderr,
'exit_code': res.returncode,
'device': NAME,
})
except subprocess.TimeoutExpired:
api('post', '/api/shell/result', json={
'request_id': rid,
'output': '', 'error': 'Timeout (30s)', 'exit_code': -1,
'device': NAME,
})
except Exception as e:
log(f"[SHELL] error: {e}")
time.sleep(POLL)
# ─── File Helper ──────────────────────────────────────────────────────────────
def upload_file(path):
with open(path, 'rb') as fh:
r = requests.post(f"{SERVER}/api/files/upload",
headers={'X-Auth-Token': TOKEN},
files={'file': fh},
data={'source': NAME})
return r.json()
# ─── Main ─────────────────────────────────────────────────────────────────────
def stop(*_):
global running
running = False
log("Stopping agent...")
sys.exit(0)
def main():
signal.signal(signal.SIGINT, stop)
signal.signal(signal.SIGTERM, stop)
register()
threads = [
threading.Thread(target=heartbeat_loop, daemon=True, name='heartbeat'),
threading.Thread(target=clipboard_loop, daemon=True, name='clipboard'),
threading.Thread(target=notification_loop, daemon=True, name='notifications'),
threading.Thread(target=shell_loop, daemon=True, name='shell'),
]
for t in threads:
t.start()
log(f"Started {t.name} thread")
log(f"Agent running. Server={SERVER} Poll={POLL}s")
while running:
time.sleep(1)
if __name__ == '__main__':
main()