-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbtcmesh_server_gui.py
More file actions
1366 lines (1154 loc) · 52.2 KB
/
Copy pathbtcmesh_server_gui.py
File metadata and controls
1366 lines (1154 loc) · 52.2 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
"""
BTCMesh Server GUI - Kivy-based graphical interface for running and monitoring
the BTCMesh relay server.
This GUI wraps the btcmesh_server module, providing visual status displays,
activity logging, and server controls.
"""
import logging
import os
import shutil
import threading
import queue
import re
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.spinner import Spinner
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.popup import Popup
from kivy.graphics import Color, Rectangle
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.properties import BooleanProperty
from kivy.core.clipboard import Clipboard
# Import shared GUI components
from core.gui_common import (
# Colors
COLOR_PRIMARY,
COLOR_SUCCESS,
COLOR_ERROR,
COLOR_WARNING,
COLOR_BG,
COLOR_BG_LIGHT,
COLOR_DISCONNECTED,
COLOR_MAINNET,
COLOR_TESTNET,
COLOR_SIGNET,
COLOR_SECUNDARY,
# Classes
ConnectionState,
StatusLog,
# Functions
get_log_color,
create_separator,
create_section_label,
create_title,
create_action_button,
create_clear_button,
create_status_row,
create_input_row,
create_toggle_button,
create_refresh_button,
create_popup_button,
create_popup_inline_button,
)
# Import server module
import btcmesh_server
from core.logger_setup import server_logger
from core.config_loader import load_app_config
from core.transaction_history import TransactionHistory
# Set window size for desktop
Window.size = (550, 920)
# Meshtastic connection states (text only - description is in separate label)
STATE_MESHTASTIC_DISCONNECTED = ConnectionState('Not connected', COLOR_DISCONNECTED)
STATE_MESHTASTIC_CONNECTED = ConnectionState('Connected', COLOR_SUCCESS)
STATE_MESHTASTIC_FAILED = ConnectionState('Connection failed', COLOR_ERROR)
# Bitcoin RPC connection states (text only - description is in separate label)
STATE_RPC_DISCONNECTED = ConnectionState('Not connected', COLOR_DISCONNECTED)
STATE_RPC_CONNECTED = ConnectionState('Connected', COLOR_SUCCESS)
STATE_RPC_FAILED = ConnectionState('Connection failed', COLOR_ERROR)
# Device selection constants
DEVICE_AUTO_DETECT = "Auto-detect"
DEVICE_SCANNING = "Scanning..."
DEVICE_NO_DEVICES = "No devices found"
# Path to .env file (same as config_loader.py)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
DOTENV_PATH = os.path.join(PROJECT_ROOT, ".env")
class QueueLogHandler(logging.Handler):
"""Custom log handler that sends log records to a queue for GUI display."""
def __init__(self, result_queue):
super().__init__()
self.result_queue = result_queue
def emit(self, record):
try:
msg = self.format(record)
level = record.levelno
self.result_queue.put(('log', msg, level))
except Exception:
self.handleError(record)
def parse_log_for_status(message: str, level: int = None): # noqa: ARG001
"""Parse a log message and return status update if applicable.
Returns:
Tuple of (status_type, data) or None if no status update needed.
"""
# Bitcoin RPC status
if "Connected to Bitcoin Core RPC node successfully" in message:
# Extract host from "Host: localhost:8332" or "Host: *.onion"
host_match = re.search(r'Host: ([^,]+)', message)
host = host_match.group(1).strip() if host_match else None
# Extract Tor status from "Tor: True" or "Tor: False"
tor_match = re.search(r'Tor: (True|False)', message)
is_tor = tor_match.group(1) == 'True' if tor_match else False
# Extract chain from "Chain: main" or "Chain: testnet4" etc.
chain_match = re.search(r'Chain: (\w+)', message)
chain = chain_match.group(1) if chain_match else None
return ('rpc_connected', {'host': host, 'is_tor': is_tor, 'chain': chain})
if "Failed to connect to Bitcoin Core RPC node" in message:
# Extract error message after the colon
match = re.search(r'Failed to connect to Bitcoin Core RPC node: (.+?)(?:\. |$)', message)
error = match.group(1) if match else "Connection failed"
return ('rpc_failed', error)
# Meshtastic status
if "Meshtastic interface initialized successfully" in message:
# Extract device path from "Device: /dev/ttyUSB0"
device_match = re.search(r'Device: ([^,]+)', message)
device = device_match.group(1).strip() if device_match else None
# Validate device - reject invalid values like "None", "?", empty strings
if device in (None, '', '?', 'None'):
device = None
# Extract node ID from "My Node Num: !abcdef12"
node_match = re.search(r'My Node Num: (!?[0-9a-fA-F]+)', message)
node_id = node_match.group(1) if node_match else None
# If we don't have a valid node_id, treat as failed connection
if not node_id or not node_id.startswith('!'):
return ('meshtastic_failed', "Invalid device info")
return ('meshtastic_connected', {'node_id': node_id, 'device': device})
if "Failed to initialize Meshtastic interface" in message:
return ('meshtastic_failed', "Could not initialize Meshtastic")
if "Meshtastic interface created but could not retrieve device info" in message:
return ('meshtastic_failed', "No device connected")
if "No Meshtastic device found" in message:
return ('meshtastic_failed', "No device found")
# Server running status
if "Registered Meshtastic message handler. Waiting for messages" in message:
return ('server_started', None)
if "Stop signal received. Shutting down" in message:
return ('server_stopping', None)
if "Closing Meshtastic interface" in message:
return ('server_stopped', None)
# PubSub errors
if "PubSub library not found" in message:
return ('pubsub_error', "PubSub library not found")
return None
class BTCMeshServerGUI(BoxLayout):
"""Main server GUI widget."""
is_running = BooleanProperty(False)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = 'vertical'
self.padding = 15
self.spacing = 20
# Server state
self._stop_event = threading.Event()
self._server_thread = None
self._log_handler = None
# Thread-safe result queue for communication
self.result_queue = queue.Queue()
# Set background color
with self.canvas.before:
Color(*COLOR_BG)
self.rect = Rectangle(size=self.size, pos=self.pos)
self.bind(size=self._update_rect, pos=self._update_rect)
self._build_ui()
# Start polling for results from background threads
Clock.schedule_interval(self._process_results, 0.1)
def _update_rect(self, *args):
"""Update background rectangle on resize."""
self.rect.pos = self.pos
self.rect.size = self.size
def _build_ui(self):
"""Build the GUI layout."""
# Title
self.add_widget(create_title('BTCMesh Relay Server'))
# Orange separator line after title
self.add_widget(create_separator())
# Connection status section
self.add_widget(create_section_label('Connection Status:'))
self._build_status_section()
# Orange separator line after status section
self.add_widget(create_separator())
# Active Sessions section
self.add_widget(create_section_label('Active Sessions:'))
self._build_active_sessions_section()
# Orange separator line after active sessions section
self.add_widget(create_separator())
# Bitcoin RPC Settings section
self.add_widget(create_section_label('Bitcoin RPC Settings:'))
self._build_rpc_settings()
# Orange separator line after RPC settings section
self.add_widget(create_separator())
# Meshtastic Device Settings section
self.add_widget(create_section_label('Meshtastic Settings:'))
self._build_meshtastic_settings()
# Orange separator line after Meshtastic settings
self.add_widget(create_separator())
# Reassembly timeout setting
self.add_widget(create_section_label('Server Settings:'))
self._build_timeout_settings()
# Orange separator line after timeout settings
self.add_widget(create_separator())
# Server controls
self._build_controls_section()
# Activity log section
self.add_widget(create_section_label('Activity Log:'))
# Status log (scrollable)
self.status_log = StatusLog(size_hint_y=1)
self.add_widget(self.status_log)
# Clear log button
self.clear_btn = create_clear_button(self.on_clear_pressed)
self.add_widget(self.clear_btn)
# Initial log message
self.status_log.add_message("Server GUI initialized. Click 'Start Server' to begin.", COLOR_PRIMARY)
def _build_status_section(self):
"""Build the connection status section."""
status_section = BoxLayout(orientation='vertical', size_hint_y=None, height=95, spacing=5)
# Network badge row (mainnet/testnet/signet)
network_row, self.network_label = create_status_row(
'Network:',
'--',
initial_color=COLOR_DISCONNECTED,
bold_value=True
)
status_section.add_widget(network_row)
# Bitcoin RPC status row
rpc_row, self.rpc_label = create_status_row(
'Bitcoin RPC:',
'Not connected',
initial_color=COLOR_DISCONNECTED
)
status_section.add_widget(rpc_row)
# Meshtastic status row
meshtastic_row, self.meshtastic_label = create_status_row(
'Meshtastic:',
'Not connected',
initial_color=COLOR_DISCONNECTED
)
status_section.add_widget(meshtastic_row)
self.add_widget(status_section)
def _build_active_sessions_section(self):
"""Build the active sessions display section with ScrollView.
Displays up to 3 sessions without scrolling (~85px), scrolls for more.
Session count is always visible outside the scroll area.
"""
# Container with left padding for indentation (matches other sections)
sessions_section = BoxLayout(
orientation='vertical',
size_hint_y=None,
height=115, # count label (25) + spacing (5) + scroll (85)
padding=[10, 0, 0, 0], # left indent
spacing=5
)
# Session count label (always visible, outside ScrollView)
self.session_count_label = Label(
text='0 active sessions',
size_hint_y=None,
height=25,
color=COLOR_DISCONNECTED,
halign='left',
valign='middle',
)
self.session_count_label.bind(size=lambda inst, val: setattr(inst, 'text_size', val))
sessions_section.add_widget(self.session_count_label)
# Fixed-height ScrollView to contain sessions
# Height: 3 sessions * 25px + 2 * spacing(2) + padding = ~85px
self.sessions_scroll = ScrollView(
size_hint_y=None,
height=85,
do_scroll_x=False,
bar_width=8,
bar_color=COLOR_PRIMARY,
bar_inactive_color=(0.5, 0.5, 0.3, 0.3),
)
# Inner container that grows with content
self.sessions_container = BoxLayout(
orientation='vertical',
size_hint_y=None,
spacing=2
)
# Bind height to minimum height needed for children
self.sessions_container.bind(minimum_height=self.sessions_container.setter('height'))
# Initial "no sessions" label (shown when count is 0)
self.no_sessions_label = Label(
text='No active sessions',
size_hint_y=None,
height=30,
color=COLOR_DISCONNECTED,
halign='left',
valign='middle',
)
self.no_sessions_label.bind(size=lambda inst, val: setattr(inst, 'text_size', val))
self.sessions_container.add_widget(self.no_sessions_label)
# Dict to track session display widgets by session_id
self._session_widgets = {}
self.sessions_scroll.add_widget(self.sessions_container)
sessions_section.add_widget(self.sessions_scroll)
self.add_widget(sessions_section)
def _update_active_sessions(self, sessions_info):
"""Update the active sessions display with new session info.
Args:
sessions_info: List of dicts with session_id, sender, chunks_received,
total_chunks, elapsed_seconds
"""
# Update session count label (always visible)
count = len(sessions_info) if sessions_info else 0
self.session_count_label.text = f"{count} active session{'s' if count != 1 else ''}"
self.session_count_label.color = COLOR_WARNING if count > 0 else COLOR_DISCONNECTED
# Track which sessions are still active
active_session_ids = set()
if not sessions_info:
# No active sessions
if self._session_widgets:
# Clear all session widgets
for widget in self._session_widgets.values():
self.sessions_container.remove_widget(widget)
self._session_widgets.clear()
# Show "no sessions" label
if self.no_sessions_label.parent is None:
self.sessions_container.add_widget(self.no_sessions_label)
return
# Hide "no sessions" label
if self.no_sessions_label.parent is not None:
self.sessions_container.remove_widget(self.no_sessions_label)
# Update or create widgets for each session
for session in sessions_info:
session_id = session['session_id']
active_session_ids.add(session_id)
# Format elapsed time
elapsed = int(session['elapsed_seconds'])
elapsed_str = f"{elapsed}s"
# Format session display text
session_text = (
f"[{session_id}] from {session['sender']}: "
f"{session['chunks_received']}/{session['total_chunks']} chunks, "
f"{elapsed_str} ago"
)
if session_id in self._session_widgets:
# Update existing widget
self._session_widgets[session_id].text = session_text
else:
# Create new widget
session_label = Label(
text=session_text,
size_hint_y=None,
height=25,
color=COLOR_WARNING,
halign='left',
valign='middle',
)
session_label.bind(size=lambda inst, val: setattr(inst, 'text_size', val))
self._session_widgets[session_id] = session_label
self.sessions_container.add_widget(session_label)
# Remove widgets for sessions that are no longer active
sessions_to_remove = set(self._session_widgets.keys()) - active_session_ids
for session_id in sessions_to_remove:
widget = self._session_widgets.pop(session_id)
self.sessions_container.remove_widget(widget)
def _build_rpc_settings(self):
"""Build the Bitcoin RPC settings input section."""
import os
# Load defaults from environment
load_app_config()
default_host = os.getenv("BITCOIN_RPC_HOST", "127.0.0.1")
default_port = os.getenv("BITCOIN_RPC_PORT", "8332")
default_user = os.getenv("BITCOIN_RPC_USER", "")
default_password = os.getenv("BITCOIN_RPC_PASSWORD", "")
settings_container = BoxLayout(orientation='vertical', size_hint_y=None, height=225, spacing=5)
# Row 1: Host input with Show/Hide toggle
host_row, self.rpc_host_input = create_input_row(
'Host:', default_host, hint_text='127.0.0.1 or xyz.onion', password=True
)
# Show/Hide host toggle button
self.show_host_btn = create_toggle_button('Show')
self.show_host_btn.bind(on_press=self._toggle_host_visibility)
host_row.add_widget(self.show_host_btn)
settings_container.add_widget(host_row)
# Row 2: Port input
port_row, self.rpc_port_input = create_input_row(
'Port:', default_port, hint_text='8332', input_filter='int'
)
# Spacer to align with host row (Show button = 60px)
port_row.add_widget(Widget(size_hint_x=None, width=60))
settings_container.add_widget(port_row)
# Row 3: User input
user_row, self.rpc_user_input = create_input_row(
'User:', default_user, hint_text='rpcuser'
)
# Spacer to align with host row
user_row.add_widget(Widget(size_hint_x=None, width=60))
settings_container.add_widget(user_row)
# Row 4: Password input with Show/Hide toggle
pass_row, self.rpc_password_input = create_input_row(
'Password:', default_password, hint_text='password', password=True
)
# Show/Hide password toggle button
self.show_password_btn = create_toggle_button('Show')
self.show_password_btn.bind(on_press=self._toggle_password_visibility)
pass_row.add_widget(self.show_password_btn)
settings_container.add_widget(pass_row)
# add spacer
settings_container.add_widget(Widget(size_hint_y=None, height=5))
# Row 5: Test Connection button
test_row = BoxLayout(orientation='horizontal', size_hint_y=None, height=40, spacing=5)
# Left spacer to center button
test_row.add_widget(Widget(size_hint_x=0.3))
self.test_connection_btn = Button(
text='Test Connection',
size_hint_x=0.4,
background_color=COLOR_BG_LIGHT,
background_normal='',
)
self.test_connection_btn.bind(on_press=self._on_test_connection)
test_row.add_widget(self.test_connection_btn)
# Right spacer
test_row.add_widget(Widget(size_hint_x=0.3))
settings_container.add_widget(test_row)
self.add_widget(settings_container)
def _build_meshtastic_settings(self):
"""Build the Meshtastic device settings section."""
import os
# Load default from environment
load_app_config()
default_device = os.getenv("MESHTASTIC_SERIAL_PORT", "")
settings_container = BoxLayout(orientation='horizontal', size_hint_y=None, height=40, spacing=5)
# Device label
device_label = Label(
text='Device:',
size_hint_x=None,
halign='right',
valign='middle',
color=COLOR_SECUNDARY,
)
device_label.bind(texture_size=lambda inst, val: setattr(inst, 'width', val[0] + 15))
settings_container.add_widget(device_label)
# Device dropdown/spinner
self.device_spinner = Spinner(
text=default_device if default_device else DEVICE_AUTO_DETECT,
values=[DEVICE_AUTO_DETECT],
size_hint_x=0.75,
background_color=COLOR_BG_LIGHT,
background_normal='',
color=COLOR_SECUNDARY,
)
settings_container.add_widget(self.device_spinner)
# Scan button
self.scan_btn = create_refresh_button('Scan')
self.scan_btn.bind(on_press=self._on_scan_devices)
settings_container.add_widget(self.scan_btn)
self.add_widget(settings_container)
def _build_timeout_settings(self):
"""Build the reassembly timeout settings section."""
import os
# Load default from environment, fallback to 300 seconds
load_app_config()
env_timeout = os.getenv("REASSEMBLY_TIMEOUT_SECONDS", "")
default_timeout = env_timeout if env_timeout else "300"
# Use create_input_row for consistent styling
timeout_row, self.timeout_input = create_input_row(
'Reassembly Timeout:',
default_timeout,
hint_text='seconds',
input_filter='int',
input_size_hint_x=0.3,
)
# Add a spacer to balance the row
timeout_row.add_widget(Widget(size_hint_x=0.4))
self.add_widget(timeout_row)
def _set_timeout_settings_enabled(self, enabled: bool):
"""Enable or disable timeout settings."""
self.timeout_input.disabled = not enabled
def _build_controls_section(self):
"""Build the server control buttons section."""
controls_section = BoxLayout(orientation='horizontal', size_hint_y=None, height=50, spacing=10)
self.start_btn = create_action_button('Start Server')
self.start_btn.bind(on_press=self.on_start_pressed)
controls_section.add_widget(self.start_btn)
self.stop_btn = create_action_button('Stop Server', color=COLOR_ERROR, disabled=True)
self.stop_btn.bind(on_press=self.on_stop_pressed)
controls_section.add_widget(self.stop_btn)
self.save_btn = create_action_button('Save Settings', color=COLOR_BG_LIGHT)
self.save_btn.bind(on_press=self._on_save_settings)
controls_section.add_widget(self.save_btn)
self.history_btn = create_action_button('History', color=COLOR_BG_LIGHT)
self.history_btn.bind(on_press=self._on_history_pressed)
controls_section.add_widget(self.history_btn)
self.add_widget(controls_section)
def _on_scan_devices(self, instance):
"""Scan for available Meshtastic devices."""
self.device_spinner.text = DEVICE_SCANNING
self.scan_btn.disabled = True
def scan_thread():
from core.meshtastic_utils import scan_meshtastic_devices
devices = scan_meshtastic_devices()
self.result_queue.put(('devices_found', devices))
threading.Thread(target=scan_thread, daemon=True).start()
def _set_meshtastic_settings_enabled(self, enabled: bool):
"""Enable or disable Meshtastic device settings."""
self.device_spinner.disabled = not enabled
self.scan_btn.disabled = not enabled
def _set_rpc_settings_enabled(self, enabled: bool):
"""Enable or disable all RPC settings inputs."""
self.rpc_host_input.disabled = not enabled
self.rpc_port_input.disabled = not enabled
self.rpc_user_input.disabled = not enabled
self.rpc_password_input.disabled = not enabled
self.test_connection_btn.disabled = not enabled
def _toggle_host_visibility(self, instance):
"""Toggle host field visibility."""
self.rpc_host_input.password = not self.rpc_host_input.password
self.show_host_btn.text = 'Hide' if not self.rpc_host_input.password else 'Show'
def _toggle_password_visibility(self, instance):
"""Toggle password field visibility."""
self.rpc_password_input.password = not self.rpc_password_input.password
self.show_password_btn.text = 'Hide' if not self.rpc_password_input.password else 'Show'
def _on_test_connection(self, instance):
"""Test the RPC connection with current settings."""
host = self.rpc_host_input.text.strip()
port = self.rpc_port_input.text.strip()
user = self.rpc_user_input.text.strip()
password = self.rpc_password_input.text
# Basic validation
if not host:
self.status_log.add_message("Test failed: Host is required", COLOR_ERROR)
return
if not port:
self.status_log.add_message("Test failed: Port is required", COLOR_ERROR)
return
if not user:
self.status_log.add_message("Test failed: User is required", COLOR_ERROR)
return
if not password:
self.status_log.add_message("Test failed: Password is required", COLOR_ERROR)
return
self.status_log.add_message("Testing RPC connection...", COLOR_WARNING)
self.test_connection_btn.disabled = True
self.start_btn.disabled = True
def test_thread():
try:
# Check for Tor requirement
is_tor = host.endswith('.onion')
if is_tor:
# Validate Tor is available on port 9050
import socket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex(('127.0.0.1', 9050))
sock.close()
if result != 0:
self.result_queue.put(('test_connection_result', False,
'Tor service not reachable on port 9050'))
return
except Exception as e:
self.result_queue.put(('test_connection_result', False,
f'Failed to check Tor: {e}'))
return
# Test RPC connection
from core.rpc_client import BitcoinRPCClient
config = {
'host': host,
'port': int(port),
'user': user,
'password': password,
}
client = BitcoinRPCClient(config)
chain = client.chain
tor_suffix = ' via Tor' if is_tor else ''
self.result_queue.put(('test_connection_result', True,
f'Connected to {chain} network{tor_suffix}'))
except Exception as e:
self.result_queue.put(('test_connection_result', False, str(e)))
threading.Thread(target=test_thread, daemon=True).start()
def _on_save_settings(self, instance):
"""Save current settings to .env file.
Preserves existing comments and unrelated variables.
Creates a backup (.env.bak) before overwriting.
"""
# Collect current settings from GUI
settings = {
'BITCOIN_RPC_HOST': self.rpc_host_input.text.strip(),
'BITCOIN_RPC_PORT': self.rpc_port_input.text.strip(),
'BITCOIN_RPC_USER': self.rpc_user_input.text.strip(),
'BITCOIN_RPC_PASSWORD': self.rpc_password_input.text, # Don't strip password
'REASSEMBLY_TIMEOUT_SECONDS': self.timeout_input.text.strip(),
}
# Handle Meshtastic device - only save if not Auto-detect
selected_device = self.device_spinner.text
if selected_device and selected_device not in (DEVICE_AUTO_DETECT, DEVICE_SCANNING, DEVICE_NO_DEVICES):
settings['MESHTASTIC_SERIAL_PORT'] = selected_device
else:
# Mark for removal if currently set
settings['MESHTASTIC_SERIAL_PORT'] = None
try:
# Read existing .env content (preserving structure)
existing_lines = []
existing_keys = set()
if os.path.exists(DOTENV_PATH):
# Create backup before modifying
backup_path = DOTENV_PATH + '.bak'
shutil.copy2(DOTENV_PATH, backup_path)
with open(DOTENV_PATH, 'r') as f:
for line in f:
stripped = line.strip()
# Check if this line sets a variable we want to update
if stripped and not stripped.startswith('#') and '=' in stripped:
key = stripped.split('=', 1)[0].strip()
if key in settings:
existing_keys.add(key)
value = settings[key]
if value is None:
# Skip this line (remove the setting)
continue
# Replace the value, preserving the key format
existing_lines.append(f'{key}={value}\n')
continue
# Keep the line as-is (comments, empty lines, other vars)
existing_lines.append(line)
# Add any new settings that weren't in the file
for key, value in settings.items():
if key not in existing_keys and value is not None:
existing_lines.append(f'{key}={value}\n')
# Write updated content
with open(DOTENV_PATH, 'w') as f:
f.writelines(existing_lines)
self.status_log.add_message(
f"Settings saved to {DOTENV_PATH}", COLOR_SUCCESS)
# Security note for password
if settings.get('BITCOIN_RPC_PASSWORD'):
self.status_log.add_message(
"Note: Password is stored in plain text in .env file", COLOR_WARNING)
except PermissionError:
self.status_log.add_message(
f"Permission denied: Cannot write to {DOTENV_PATH}", COLOR_ERROR)
except Exception as e:
self.status_log.add_message(
f"Failed to save settings: {e}", COLOR_ERROR)
def on_start_pressed(self, instance):
"""Handle Start Server button press."""
# Validate required fields before starting
host = self.rpc_host_input.text.strip()
port = self.rpc_port_input.text.strip()
user = self.rpc_user_input.text.strip()
password = self.rpc_password_input.text
if not host or not port or not user or not password:
self.status_log.add_message(
"Cannot start: Please fill in all RPC settings fields", COLOR_ERROR)
return
# Validate timeout (must be positive integer)
timeout_text = self.timeout_input.text.strip()
if not timeout_text:
self.status_log.add_message(
"Cannot start: Reassembly timeout is required", COLOR_ERROR)
return
try:
reassembly_timeout = int(timeout_text)
if reassembly_timeout <= 0:
raise ValueError()
except ValueError:
self.status_log.add_message(
"Cannot start: Reassembly timeout must be a positive integer", COLOR_ERROR)
return
self.status_log.add_message("Starting server...", COLOR_WARNING)
self.start_btn.disabled = True
self.save_btn.disabled = True
self._set_rpc_settings_enabled(False)
self._set_meshtastic_settings_enabled(False)
self._set_timeout_settings_enabled(False)
# Build RPC config from GUI inputs
rpc_config = {
'host': host,
'port': int(port),
'user': user,
'password': password,
}
# Get selected device (None for auto-detect)
selected_device = self.device_spinner.text
serial_port = None if selected_device == DEVICE_AUTO_DETECT else selected_device
# Reset stop event
self._stop_event.clear()
# Set up log handler to capture server logs with timestamps
self._log_handler = QueueLogHandler(self.result_queue)
self._log_handler.setFormatter(logging.Formatter('[%(asctime)s] %(message)s', datefmt='%H:%M:%S'))
server_logger.addHandler(self._log_handler)
# Session update callback - puts session info in result queue
def session_callback(sessions_info):
self.result_queue.put(('active_sessions', sessions_info))
# Start server in background thread
def run_server():
try:
btcmesh_server.main(stop_event=self._stop_event, rpc_config=rpc_config,
serial_port=serial_port, reassembly_timeout=reassembly_timeout,
session_update_callback=session_callback)
except Exception as e:
self.result_queue.put(('init_error', str(e), logging.ERROR))
finally:
# Signal that server has stopped (in case main() exits without stop signal)
if not self._stop_event.is_set():
self.result_queue.put(('server_stopped', None, logging.INFO))
self._server_thread = threading.Thread(target=run_server, daemon=True)
self._server_thread.start()
def on_stop_pressed(self, instance):
"""Handle Stop Server button press."""
self.status_log.add_message("Stopping server...", COLOR_WARNING)
self.stop_btn.disabled = True
# Signal server to stop
self._stop_event.set()
def _process_results(self, dt):
"""Process results from background threads (called by Clock)."""
try:
while True:
result = self.result_queue.get_nowait()
self._handle_result(result)
except queue.Empty:
pass
def _handle_result(self, result):
"""Handle a single result from the queue."""
result_type = result[0]
data = result[1] if len(result) > 1 else None
level = result[2] if len(result) > 2 else logging.INFO
if result_type == 'log':
# Parse log message for status updates
status_update = parse_log_for_status(data, level)
if status_update:
self._apply_status_update(status_update)
# Display log message with appropriate color
color = get_log_color(level, data)
self.status_log.add_message(data, color)
elif result_type in ('rpc_connected', 'rpc_failed', 'meshtastic_connected',
'meshtastic_failed', 'server_started', 'server_stopped',
'server_stopping', 'pubsub_error', 'init_error'):
self._apply_status_update((result_type, data))
elif result_type == 'test_connection_result':
# data is success (bool), level is message string
success = data
message = level # Third element contains the message
self.test_connection_btn.disabled = False
self.start_btn.disabled = False
if success:
self.status_log.add_message(f"RPC test successful: {message}", COLOR_SUCCESS)
else:
self.status_log.add_message(f"RPC test failed: {message}", COLOR_ERROR)
elif result_type == 'devices_found':
devices = data
self.scan_btn.disabled = False
if devices:
# Add Auto-detect as first option, then found devices
self.device_spinner.values = [DEVICE_AUTO_DETECT] + devices
if len(devices) == 1:
# Auto-select single device
self.device_spinner.text = devices[0]
self.status_log.add_message(f"Found device: {devices[0]}", COLOR_SUCCESS)
else:
# Multiple devices - keep current selection or show first
if self.device_spinner.text == DEVICE_SCANNING:
self.device_spinner.text = DEVICE_AUTO_DETECT
self.status_log.add_message(
f"Found {len(devices)} devices", COLOR_SUCCESS)
else:
self.device_spinner.values = [DEVICE_AUTO_DETECT]
self.device_spinner.text = DEVICE_AUTO_DETECT
self.status_log.add_message("No Meshtastic devices found", COLOR_WARNING)
elif result_type == 'active_sessions':
# Update the active sessions display
self._update_active_sessions(data)
def _apply_status_update(self, status_update):
"""Apply a status update to the GUI."""
status_type, data = status_update
if status_type == 'rpc_connected':
# data is dict with 'host', 'is_tor', and 'chain' keys
host = data.get('host') if isinstance(data, dict) else None
is_tor = data.get('is_tor', False) if isinstance(data, dict) else False
chain = data.get('chain') if isinstance(data, dict) else None
if host:
tor_badge = " [Tor]" if is_tor else ""
self.rpc_label.text = f"Connected ({host}){tor_badge}"
else:
self.rpc_label.text = STATE_RPC_CONNECTED.text
self.rpc_label.color = STATE_RPC_CONNECTED.color
# Update network badge based on chain
if chain:
# Map chain values to display names and colors
chain_display_map = {
'main': ('MAINNET', COLOR_MAINNET),
'test': ('TESTNET3', COLOR_TESTNET),
'testnet4': ('TESTNET4', COLOR_TESTNET),
'signet': ('SIGNET', COLOR_SIGNET),
}
display_name, color = chain_display_map.get(chain, (chain.upper(), COLOR_TESTNET))
self.network_label.text = display_name
self.network_label.color = color
elif status_type == 'rpc_failed':
self.rpc_label.text = STATE_RPC_FAILED.text
self.rpc_label.color = STATE_RPC_FAILED.color
elif status_type == 'meshtastic_connected':
# data is dict with 'node_id' and 'device' keys
node_id = data.get('node_id', 'Unknown') if isinstance(data, dict) else data
device = data.get('device') if isinstance(data, dict) else None
if device:
self.meshtastic_label.text = f"Connected ({node_id}) on {device}"
else:
self.meshtastic_label.text = f"Connected ({node_id})"
self.meshtastic_label.color = STATE_MESHTASTIC_CONNECTED.color
elif status_type == 'meshtastic_failed':
self.meshtastic_label.text = STATE_MESHTASTIC_FAILED.text
self.meshtastic_label.color = STATE_MESHTASTIC_FAILED.color
# Re-enable start button and settings on failure
self.start_btn.disabled = False
self.save_btn.disabled = False
self._set_rpc_settings_enabled(True)
self._set_meshtastic_settings_enabled(True)
self._set_timeout_settings_enabled(True)
self._cleanup_log_handler()
elif status_type == 'pubsub_error':
self.start_btn.disabled = False
self.save_btn.disabled = False
self._set_rpc_settings_enabled(True)
self._set_meshtastic_settings_enabled(True)
self._set_timeout_settings_enabled(True)
self._cleanup_log_handler()
elif status_type == 'server_started':
self.is_running = True
self.stop_btn.disabled = False
elif status_type == 'server_stopping':
# Server is in the process of stopping
pass
elif status_type == 'server_stopped':
self.is_running = False
self.meshtastic_label.text = STATE_MESHTASTIC_DISCONNECTED.text
self.meshtastic_label.color = STATE_MESHTASTIC_DISCONNECTED.color
self.rpc_label.text = STATE_RPC_DISCONNECTED.text
self.rpc_label.color = STATE_RPC_DISCONNECTED.color
self.network_label.text = '--'
self.network_label.color = COLOR_DISCONNECTED
# Clear active sessions display
self._update_active_sessions([])
# Note: stop_btn is set first because in tests, mocked buttons may be same object
self.stop_btn.disabled = True
self.start_btn.disabled = False
self.save_btn.disabled = False
self._set_rpc_settings_enabled(True)
self._set_meshtastic_settings_enabled(True)
self._set_timeout_settings_enabled(True)
self._cleanup_log_handler()