-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipbridge.py
More file actions
1359 lines (1160 loc) · 44.7 KB
/
clipbridge.py
File metadata and controls
1359 lines (1160 loc) · 44.7 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
ClipBridge - Cross-platform clipboard synchronization
https://github.com/FrancoLionti/ClipBridge
"""
__version__ = "2.0.4"
import argparse
import json
import os
import signal
import socket
import subprocess
import sys
import threading
import time
from collections.abc import Mapping
from datetime import datetime
from pathlib import Path
from typing import Optional
# ============================================================
# LINUX: Prevent ghost windows from clipboard access
# ============================================================
if sys.platform.startswith('linux'):
# Force xclip/xsel backend instead of GTK/Qt which can create windows
os.environ.setdefault('PYPERCLIP_BACKEND', 'xclip')
# Prevent GTK from creating windows
os.environ.setdefault('GDK_BACKEND', 'x11')
# Suppress GTK accessibility warnings
os.environ.setdefault('NO_AT_BRIDGE', '1')
# Hide from desktop/taskbar
os.environ.setdefault('SDL_VIDEODRIVER', 'dummy')
import pyperclip
import requests
from flask import Flask, request, abort, Response
# ============================================================
# CONFIGURATION
# ============================================================
SCRIPT_DIR = Path(__file__).resolve().parent
def _path_looks_installed(package_root: Path) -> bool:
"""True when the package lives under site-packages/dist-packages (pip install)."""
if getattr(sys, "frozen", False):
return True # PyInstaller / frozen: never write beside the exe
parts = package_root.parts
return "site-packages" in parts or "dist-packages" in parts
def _user_default_config_file(environ: Mapping[str, str]) -> Path:
"""Per-user config path (used when ClipBridge is installed as a Python package)."""
if sys.platform.startswith("win"):
base_raw = environ.get("LOCALAPPDATA", "").strip()
base = Path(base_raw).expanduser() if base_raw else Path.home() / "AppData" / "Local"
return base / "clipbridge" / "config.json"
if sys.platform == "darwin":
home = Path.home()
return home / "Library" / "Application Support" / "clipbridge" / "config.json"
xdg = (environ.get("XDG_CONFIG_HOME") or "").strip()
root = Path(xdg).expanduser() if xdg else Path.home() / ".config"
return root / "clipbridge" / "config.json"
def resolve_config_file(
package_root: Path,
environ: Optional[Mapping[str, str]] = None,
) -> Path:
"""Where to store config.json: env override → repo-local (dev clone) → XDG-ish user dir."""
envmap = environ if environ is not None else os.environ
raw = (envmap.get("CLIPBRIDGE_CONFIG") or "").strip()
if raw:
p = Path(raw).expanduser()
if not p.is_absolute():
p = Path.cwd() / p
if p.is_dir():
return p / "config.json"
return p
if not _path_looks_installed(package_root):
return package_root / "config.json"
return _user_default_config_file(envmap)
CONFIG_FILE = resolve_config_file(SCRIPT_DIR)
def _interactive_pid_path() -> Path:
"""File written while ``clipbridge client on`` runs; removed on clean exit."""
return CONFIG_FILE.parent / "interactive_client.pid"
def _interactive_pid_record() -> None:
path = _interactive_pid_path()
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(str(os.getpid()), encoding="utf-8")
def _interactive_pid_clear() -> None:
try:
_interactive_pid_path().unlink(missing_ok=True)
except OSError:
pass
def _process_exists(pid: int) -> bool:
if pid <= 0:
return False
if sys.platform == "win32":
try:
out = subprocess.run(
["tasklist", "/FI", f"PID eq {pid}"],
capture_output=True,
text=True,
timeout=15,
check=False,
).stdout
except OSError:
return False
if not out or "no tasks" in out.lower():
return False
return str(pid) in out
try:
os.kill(pid, 0)
except ProcessLookupError:
return False
except PermissionError:
return True
return True
def _pid_cmdline_for_verify(pid: int) -> str:
if sys.platform.startswith("linux"):
try:
raw = Path(f"/proc/{pid}/cmdline").read_bytes()
return raw.replace(b"\0", b" ").decode("utf-8", errors="replace")
except OSError:
return ""
if sys.platform == "win32":
try:
out = subprocess.run(
[
"powershell",
"-NoProfile",
"-Command",
f"(Get-CimInstance Win32_Process -Filter "
f"'ProcessId = {pid}' -ErrorAction SilentlyContinue)"
".CommandLine",
],
capture_output=True,
text=True,
timeout=15,
check=False,
).stdout.strip()
return out
except FileNotFoundError:
pass
except OSError:
pass
return ""
def _pid_targets_clipbridge(pid: int) -> bool:
hay = _pid_cmdline_for_verify(pid).lower()
if not hay:
return False
return "clipbridge" in hay or "clipbridge.py" in hay
def stop_remote_interactive_client() -> None:
"""Tell a running ``clipbridge client on`` (same machine, same config dir) to stop.
Sends SIGINT on Unix (same as Ctrl+C in the REPL). On Windows uses ``taskkill`` without ``/F``.
"""
path = _interactive_pid_path()
if not path.is_file():
log("❌ No interactive client PID file — nothing to stop. Start one with: clipbridge client on")
return
try:
pid = int(path.read_text(encoding="utf-8").strip())
except ValueError:
log("⚠️ Clearing invalid PID file.")
path.unlink(missing_ok=True)
return
if not _process_exists(pid):
log("⚠️ Process is gone — removing stale PID file.")
path.unlink(missing_ok=True)
return
if not _pid_targets_clipbridge(pid):
log(
f"⚠️ Refusing to signal PID {pid} (does not look like ClipBridge). "
f"Delete {path} manually if it is stuck."
)
return
log(f"🛑 Stopping interactive client (PID {pid})…")
try:
if sys.platform == "win32":
subprocess.run(
["taskkill", "/PID", str(pid)],
capture_output=True,
timeout=45,
check=False,
)
else:
os.kill(pid, signal.SIGINT)
except ProcessLookupError:
log("👋 Interactive client already exited.")
except Exception as e:
log(f"❌ Could not signal process: {e}")
return
path.unlink(missing_ok=True)
DEFAULT_CONFIG = {
"server_ip": None,
"port": 5000,
"discovery_port": 5001,
"mode": "auto",
"push_interval": 3.0,
"pull_interval": 1.5,
# Security settings
"secret_key": None, # Shared secret for authentication (None = no auth)
"encryption_enabled": False, # Encrypt clipboard data
"rate_limit": 10 # Max requests per second per IP
}
def load_config():
"""Load config from file or return defaults."""
if CONFIG_FILE.exists():
try:
with open(CONFIG_FILE, 'r') as f:
user_config = json.load(f)
return {**DEFAULT_CONFIG, **user_config}
except Exception:
pass
return DEFAULT_CONFIG
CONFIG = load_config()
PORT = CONFIG["port"]
DISCOVERY_PORT = CONFIG["discovery_port"]
PUSH_INTERVAL = CONFIG.get("push_interval", 3.0)
PULL_INTERVAL = CONFIG.get("pull_interval", 1.5)
SECRET_KEY = CONFIG.get("secret_key")
ENCRYPTION_ENABLED = bool(CONFIG.get("encryption_enabled", False) and SECRET_KEY)
RATE_LIMIT = CONFIG.get("rate_limit", 10)
DISCOVERY_MAGIC = b"CLIPBRIDGE_DISCOVER"
DISCOVERY_RESPONSE = b"CLIPBRIDGE_SERVER"
# ============================================================
# SECURITY
# ============================================================
import hashlib
import hmac
import base64
from collections import defaultdict
from functools import wraps
# Optional: cryptography for encryption
try:
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
CRYPTO_AVAILABLE = True
except ImportError:
CRYPTO_AVAILABLE = False
if ENCRYPTION_ENABLED:
print("WARNING: cryptography not installed, encryption disabled")
print(" Install with: pip install cryptography")
ENCRYPTION_ENABLED = False
class SecurityManager:
"""Handles authentication and encryption for ClipBridge."""
def __init__(self, secret_key, encryption_enabled=False):
self.secret_key = secret_key.encode() if secret_key else None
self.encryption_enabled = bool(encryption_enabled and CRYPTO_AVAILABLE and self.secret_key)
self.fernet = None
if self.encryption_enabled:
# Derive encryption key from secret using PBKDF2
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=b'clipbridge_salt_v1', # Fixed salt for simplicity
iterations=100000,
)
key = base64.urlsafe_b64encode(kdf.derive(self.secret_key))
self.fernet = Fernet(key)
def sign(self, data):
"""Create HMAC signature for data."""
if not self.secret_key:
return ""
return hmac.new(self.secret_key, data.encode(), hashlib.sha256).hexdigest()
def verify(self, data, signature):
"""Verify HMAC signature."""
if not self.secret_key:
return True # No auth configured
expected = self.sign(data)
return hmac.compare_digest(expected, signature)
def encrypt(self, plaintext):
"""Encrypt data if encryption is enabled."""
if not self.encryption_enabled:
return plaintext
try:
result = self.fernet.encrypt(plaintext.encode()).decode()
return result
except Exception as e:
print(f"⚠️ Encryption error: {e}")
return plaintext
def decrypt(self, ciphertext):
"""Decrypt data if encryption is enabled."""
if not self.encryption_enabled:
return ciphertext
try:
return self.fernet.decrypt(ciphertext.encode()).decode()
except Exception as e:
print(f"⚠️ Decryption error: {e}")
# Return as-is if decryption fails (might be unencrypted)
return ciphertext
# Initialize security manager
security = SecurityManager(SECRET_KEY, ENCRYPTION_ENABLED)
# Stable ciphertext for a given (rev, plaintext) so long-poll clients don't see a new token every request.
_enc_wire_lock = threading.Lock()
_enc_wire_cache_key = None # (rev, plain)
_enc_wire_cache_val = None
def _payload_looks_fernet_token(data: str) -> bool:
"""Heuristic: Fernet wire tokens are long urlsafe-base64 strings (version 0x80 → often 'gAAAA…')."""
if not data:
return False
s = data.strip()
if len(s) < 32:
return False
if not s.startswith("gAAAA"):
return False
allowed = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=")
return all(c in allowed for c in s)
def _encrypted_payload_for_response(plain_text: str, rev: int) -> str:
"""Return wire payload; encryption is stable until (rev, plaintext) changes."""
global _enc_wire_cache_key, _enc_wire_cache_val
if not security.encryption_enabled or app.config.get("TESTING", False):
return plain_text
key = (rev, plain_text)
with _enc_wire_lock:
if key == _enc_wire_cache_key:
return _enc_wire_cache_val
_enc_wire_cache_key = key
_enc_wire_cache_val = security.encrypt(plain_text)
return _enc_wire_cache_val
def ensure_crypto_if_encryption_configured():
"""Exit with a clear message if config requests encryption but cryptography is missing."""
if not bool(CONFIG.get("encryption_enabled", False) and CONFIG.get("secret_key")):
return
if CRYPTO_AVAILABLE:
return
print(
"ERROR: config.json has encryption_enabled=true but the 'cryptography' package is not installed.\n"
" Fix: pip install cryptography (use the same venv as ClipBridge)\n"
" Or set encryption_enabled to false on this machine.",
file=sys.stderr,
)
sys.exit(1)
# Rate limiting
request_counts = defaultdict(list)
def rate_limit_check(ip):
"""Check if IP has exceeded rate limit."""
import time
now = time.time()
# Clean old entries
request_counts[ip] = [t for t in request_counts[ip] if now - t < 1.0]
# Check limit
if len(request_counts[ip]) >= RATE_LIMIT:
return False
request_counts[ip].append(now)
return True
def require_auth(f):
"""Decorator to require authentication on endpoints."""
@wraps(f)
def decorated(*args, **kwargs):
# Skip rate limiting in test mode
if not app.config.get('TESTING', False):
# Rate limit check
client_ip = request.remote_addr
if not rate_limit_check(client_ip):
return "Rate limit exceeded", 429
# Auth check (skip if no secret configured or in test mode)
if security.secret_key and not app.config.get('TESTING', False):
auth_header = request.headers.get('X-ClipBridge-Auth', '')
timestamp = request.headers.get('X-ClipBridge-Time', '0')
# Verify timestamp is recent (within 60 seconds)
try:
req_time = float(timestamp)
if abs(time.time() - req_time) > 60:
return "Request expired", 401
except ValueError:
return "Invalid timestamp", 401
# Verify signature
data_to_sign = f"{timestamp}:{request.path}"
if not security.verify(data_to_sign, auth_header):
return "Unauthorized", 401
return f(*args, **kwargs)
return decorated
# ============================================================
# CLIPBOARD ABSTRACTION (Linux-safe)
# ============================================================
def _linux_get_clipboard():
"""Get clipboard on Linux using xclip directly (less intrusive than pyperclip)."""
try:
# Try xclip first (X11)
result = subprocess.run(
['xclip', '-selection', 'clipboard', '-o'],
capture_output=True, text=True, timeout=1
)
if result.returncode == 0:
return result.stdout
except FileNotFoundError:
pass
except subprocess.TimeoutExpired:
pass
except Exception:
pass
try:
# Try wl-paste for Wayland
result = subprocess.run(
['wl-paste', '--no-newline'],
capture_output=True, text=True, timeout=1
)
if result.returncode == 0:
return result.stdout
except FileNotFoundError:
pass
except subprocess.TimeoutExpired:
pass
except Exception:
pass
# Fallback to pyperclip
return pyperclip.paste()
def _linux_set_clipboard(text):
"""Set clipboard on Linux using xclip directly."""
try:
# Try xclip first (X11)
process = subprocess.Popen(
['xclip', '-selection', 'clipboard'],
stdin=subprocess.PIPE
)
process.communicate(input=text.encode('utf-8'), timeout=1)
if process.returncode == 0:
return True
except FileNotFoundError:
pass
except subprocess.TimeoutExpired:
pass
except Exception:
pass
try:
# Try wl-copy for Wayland
process = subprocess.Popen(
['wl-copy'],
stdin=subprocess.PIPE
)
process.communicate(input=text.encode('utf-8'), timeout=1)
if process.returncode == 0:
return True
except FileNotFoundError:
pass
except subprocess.TimeoutExpired:
pass
except Exception:
pass
# Fallback to pyperclip
pyperclip.copy(text)
return True
def clipboard_get():
"""Cross-platform clipboard get."""
if sys.platform.startswith('linux'):
return _linux_get_clipboard()
return pyperclip.paste()
def clipboard_set(text):
"""Cross-platform clipboard set."""
if sys.platform.startswith('linux'):
return _linux_set_clipboard(text)
pyperclip.copy(text)
return True
def _clip_sync_equal(a, b):
"""Whether two clipboard strings are the same for sync (avoids xclip/Wayland newline quirks)."""
sa = "" if a is None else a
sb = "" if b is None else b
if sa == sb:
return True
return sa.rstrip("\r\n") == sb.rstrip("\r\n")
# ============================================================
# EVENT-BASED CLIPBOARD MONITORING (Linux)
# ============================================================
def _has_clipnotify():
"""Check if clipnotify is installed."""
try:
result = subprocess.run(['which', 'clipnotify'], capture_output=True)
return result.returncode == 0
except Exception:
return False
def _wait_for_clipboard_change(stop_event=None):
"""Block until clipboard changes (Linux only, requires clipnotify).
If ``stop_event`` is set, the waiting ``clipnotify`` child is terminated and
this returns False so the client loop can exit promptly (e.g. interactive mode).
Returns True if clipboard changed, False on error or stop.
"""
if stop_event and stop_event.is_set():
return False
try:
proc = subprocess.Popen(
["clipnotify"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
while True:
if stop_event and stop_event.is_set():
proc.terminate()
try:
proc.wait(timeout=2)
except subprocess.TimeoutExpired:
proc.kill()
return False
code = proc.poll()
if code is not None:
return code == 0
time.sleep(0.2)
except FileNotFoundError:
return False
except Exception:
return False
# Global flag to track if we can use event-based monitoring
USE_CLIPNOTIFY = sys.platform.startswith('linux') and _has_clipnotify()
# ============================================================
# LOGGING
# ============================================================
_log_lock = threading.Lock()
def log(msg):
ts = datetime.now().strftime("%H:%M:%S")
line = f"[{ts}] {msg}"
with _log_lock:
print(line, flush=True)
# ============================================================
# AUTO-DISCOVERY
# ============================================================
def get_local_ip():
"""Get the local IP address of this machine."""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "127.0.0.1"
def discovery_responder():
"""Server: Respond to UDP discovery broadcasts."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind(("", DISCOVERY_PORT))
log(f"📡 Discovery responder listening on UDP:{DISCOVERY_PORT}")
while True:
data, addr = sock.recvfrom(1024)
if data == DISCOVERY_MAGIC:
log(f"🔍 Discovery request from {addr[0]}")
response = DISCOVERY_RESPONSE + b":" + get_local_ip().encode()
sock.sendto(response, addr)
except Exception as e:
log(f"⚠️ Discovery responder error: {e}")
finally:
sock.close()
def discover_server(timeout=5):
"""Client: Send UDP broadcast to find server."""
log("🔍 Searching for ClipBridge server on network...")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.settimeout(timeout)
try:
# Send broadcast
sock.sendto(DISCOVERY_MAGIC, ("<broadcast>", DISCOVERY_PORT))
# Wait for response
data, addr = sock.recvfrom(1024)
if data.startswith(DISCOVERY_RESPONSE):
server_ip = data.split(b":")[1].decode()
log(f"✅ Found server at {server_ip}")
return server_ip
except socket.timeout:
log("⏰ Discovery timeout - no server found")
except Exception as e:
log(f"⚠️ Discovery error: {e}")
finally:
sock.close()
return None
# ============================================================
# SERVER
# ============================================================
app = Flask(__name__)
clipboard_condition = threading.Condition()
shared_clipboard = ""
clipboard_rev = 0
last_update_source = "init"
@app.route('/push', methods=['POST'])
@require_auth
def push():
global shared_clipboard, last_update_source, clipboard_rev
incoming = request.data.decode('utf-8')
# Decrypt only if body looks like a Fernet token (mixed clients / plaintext push).
if security.encryption_enabled and not app.config.get('TESTING', False):
if _payload_looks_fernet_token(incoming):
decrypted = security.decrypt(incoming)
if decrypted != incoming:
incoming = decrypted
log("🔓 Decrypted incoming data")
with clipboard_condition:
if incoming != shared_clipboard:
shared_clipboard = incoming
clipboard_rev += 1
last_update_source = "remote"
clipboard_set(incoming)
clipboard_condition.notify_all()
log(f"📥 RECV from client: {incoming[:40].replace(chr(10), ' ')}...")
return "OK", 200
@app.route('/pull', methods=['GET'])
@require_auth
def pull():
with clipboard_condition:
data = shared_clipboard
rev = clipboard_rev
if security.encryption_enabled and not app.config.get('TESTING', False):
data = _encrypted_payload_for_response(data, rev)
return data
@app.route('/pull_wait', methods=['GET'])
@require_auth
def pull_wait():
"""Long-poll: block until shared clipboard revision advances past ``since`` (or timeout).
Query params:
since: last revision seen by client; use -1 for an immediate snapshot (no wait).
timeout: max seconds to wait (0.5–60, default 30).
Response header: X-ClipBridge-Rev — current revision after wait.
"""
try:
since = int(request.args.get("since", "-1"))
except ValueError:
since = -1
try:
timeout_s = float(request.args.get("timeout", "30"))
except ValueError:
timeout_s = 30.0
timeout_s = max(0.5, min(timeout_s, 60.0))
deadline = time.time() + timeout_s
with clipboard_condition:
if since < 0:
payload = shared_clipboard
rev_out = clipboard_rev
else:
while clipboard_rev <= since:
remaining = deadline - time.time()
if remaining <= 0:
break
clipboard_condition.wait(timeout=remaining)
payload = shared_clipboard
rev_out = clipboard_rev
if security.encryption_enabled and not app.config.get("TESTING", False):
payload = _encrypted_payload_for_response(payload, rev_out)
body = payload if isinstance(payload, (bytes, bytearray)) else (payload or "")
return Response(
body,
status=200,
headers={"X-ClipBridge-Rev": str(rev_out)},
mimetype="text/plain",
)
@app.route('/helo', methods=['GET'])
def helo():
# helo doesn't require auth (used for discovery)
return f"CLIPBRIDGE_SERVER:{get_local_ip()}", 200
def server_clipboard_monitor():
"""Monitor local clipboard and update shared state."""
global shared_clipboard, last_update_source, clipboard_rev
last_local = clipboard_get()
log("🔄 Clipboard monitor active")
while True:
try:
current = clipboard_get()
with clipboard_condition:
if current != last_local:
if last_update_source != "remote" or current != shared_clipboard:
old = shared_clipboard
shared_clipboard = current
last_update_source = "local"
if current != old:
clipboard_rev += 1
clipboard_condition.notify_all()
log(f"📋 LOCAL copy: {current[:40].replace(chr(10), ' ')}...")
last_local = current
except Exception as e:
log(f"⚠️ Monitor error: {e}")
time.sleep(0.8)
def start_server():
global shared_clipboard, clipboard_rev
ensure_crypto_if_encryption_configured()
local_ip = get_local_ip()
print("\n" + "=" * 50)
print(" CLIPBRIDGE SERVER")
print("=" * 50)
log(f"🚀 Server starting on {local_ip}:{PORT}")
# Check if port is already in use
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(('0.0.0.0', PORT))
sock.close()
except OSError:
log(f"❌ Error: Port {PORT} is already in use!")
log(" Is ClipBridge already running?")
sys.exit(1)
with clipboard_condition:
shared_clipboard = clipboard_get()
clipboard_rev = 1
clipboard_condition.notify_all()
# Start discovery responder
discovery_thread = threading.Thread(target=discovery_responder, daemon=True)
discovery_thread.start()
# Start clipboard monitor
monitor_thread = threading.Thread(target=server_clipboard_monitor, daemon=True)
monitor_thread.start()
# Silence Flask logs
import logging
logging.getLogger('werkzeug').setLevel(logging.WARNING)
log("✅ Server ready - waiting for clients...")
# Log security status
if SECRET_KEY:
log("🔒 Authentication: ENABLED")
if ENCRYPTION_ENABLED:
log("🔐 Encryption: ENABLED (AES-256)")
else:
log("⚠️ Encryption: disabled (install cryptography)")
else:
log("⚠️ Security: disabled (set secret_key in config.json)")
print("=" * 50 + "\n")
app.run(host='0.0.0.0', port=PORT, debug=False, threaded=True)
# ============================================================
# CLIENT
# ============================================================
def _make_auth_headers(path):
"""Create authentication headers for client requests."""
if not security.secret_key:
return {}
timestamp = str(time.time())
data_to_sign = f"{timestamp}:{path}"
signature = security.sign(data_to_sign)
return {
'X-ClipBridge-Auth': signature,
'X-ClipBridge-Time': timestamp
}
def _client_push(server_ip, data):
"""Push data to server with auth and encryption."""
# Encrypt if enabled
if ENCRYPTION_ENABLED:
data = security.encrypt(data)
headers = _make_auth_headers('/push')
response = requests.post(
f"http://{server_ip}:{PORT}/push",
data=data.encode('utf-8'),
headers=headers,
timeout=2
)
return response.status_code == 200
def _client_pull(server_ip):
"""Pull data from server with auth and decryption."""
headers = _make_auth_headers('/pull')
response = requests.get(
f"http://{server_ip}:{PORT}/pull",
headers=headers,
timeout=2
)
if response.status_code != 200:
return None
data = response.text
# Decrypt if enabled
if ENCRYPTION_ENABLED:
data = security.decrypt(data)
return data
def _client_pull_wait(server_ip, since_rev):
"""Block until the server's clipboard revision advances past ``since_rev`` (or timeout).
Returns ``(text, new_rev)``. ``new_rev`` is None on network/HTTP failure — caller
should retry with a short backoff. ``since_rev`` of ``-1`` always returns immediately
with the current snapshot (used for the first request).
"""
headers = _make_auth_headers('/pull_wait')
try:
response = requests.get(
f"http://{server_ip}:{PORT}/pull_wait",
params={"since": since_rev, "timeout": 30},
headers=headers,
timeout=35,
)
except requests.RequestException:
return None, None
if response.status_code != 200:
return None, None
try:
new_rev = int(response.headers.get("X-ClipBridge-Rev", ""))
except ValueError:
new_rev = None
data = response.text
if ENCRYPTION_ENABLED:
data = security.decrypt(data)
return data, new_rev
def client_sync_loop(server_ip, stop_event=None):
"""Main client loop: push local changes, pull remote changes.
Local clipboard:
Linux + clipnotify: event-based (no polling).
Otherwise: polling on ``push_interval``.
Remote clipboard: long-poll ``/pull_wait`` (blocks until the server has something
new, or a timeout). No periodic GET storm to ``/pull``.
Pass ``stop_event`` to interrupt the loop (e.g. ``clipbridge client on`` REPL ``exit``).
"""
ensure_crypto_if_encryption_configured()
if stop_event is None:
stop_event = threading.Event()
print("\n" + "=" * 50)
print(" CLIPBRIDGE CLIENT")
print("=" * 50)
log(f"🔗 Connecting to server: {server_ip}:{PORT}")
# Verify connection
connected = False
for attempt in range(3):
try:
resp = requests.get(f"http://{server_ip}:{PORT}/helo", timeout=3)
if resp.status_code == 200:
log(f"✅ Connected: {resp.text}")
connected = True
break
except Exception as e:
log(f"⚠️ Attempt {attempt+1}/3 failed: {type(e).__name__}")
time.sleep(1)
if not connected:
log("❌ Could not connect to server")
log(" Check firewall settings and server status")
return
log("✅ Sync active - Ctrl+C to stop")
# Log security status
if SECRET_KEY:
log("🔒 Authentication: ENABLED")
if ENCRYPTION_ENABLED:
log("🔐 Encryption: ENABLED (AES-256)")
else:
log("⚠️ Encryption: disabled (install cryptography)")
else:
log("⚠️ Security: disabled (no secret_key in config)")
if USE_CLIPNOTIFY:
log("🎯 Local clipboard: clipnotify (event-based, no polling)")
log("📡 Remote clipboard: long-poll /pull_wait (blocked until server updates)")
_client_loop_event_based(server_ip, stop_event)
else:
if sys.platform.startswith('linux'):
log("⚠️ clipnotify not found - local clipboard polling (run install/install_clipnotify.sh)")
log(f"ℹ️ Local clipboard poll every {PUSH_INTERVAL}s · Remote: long-poll (no pull interval)")
_client_loop_polling(server_ip, stop_event)
def _client_loop_event_based(server_ip, stop_event):
"""Linux + clipnotify: local changes via clipnotify; remote changes via long-poll."""
print("=" * 50 + "\n")
last_local = clipboard_get()
last_remote = ""
backoff = min(5.0, max(0.5, PULL_INTERVAL))
def pull_thread():
"""Background thread: block on /pull_wait until the server revision advances."""
nonlocal last_remote, last_local
last_seen_rev = -1
while not stop_event.is_set():
try:
remote_clip, new_rev = _client_pull_wait(server_ip, last_seen_rev)
if new_rev is not None:
last_seen_rev = new_rev
if remote_clip is None and new_rev is None:
time.sleep(backoff)
continue