-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
992 lines (927 loc) · 43.2 KB
/
server.py
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
from flask import Flask, request, jsonify, render_template_string, redirect, url_for
from werkzeug.utils import secure_filename
import subprocess
import re
import os
import json
import time
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/tmp/'
ALLOWED_EXTENSIONS = {'bin'}
# Define the list of set and get commands
SET_COMMANDS = [
'country_region', 'ssid', 'channel', 'rts_threshold', 'frag_threshold', 'key_mgmt', 'wpa_psk',
'bssid_filter', 'freq_range', 'bss_bw', 'tx_bw', 'tx_mcs', 'acs', 'bgrssi', 'chan_list', 'mode', 'paired_stas',
'pairing', 'beacon_int', 'radio_onoff', 'join_group', 'ether_type', 'txpower', 'agg_cnt', 'ps_connect',
'bss_max_idle', 'wkio_mode', 'loaddef', 'disassoc_sta', 'dtim_period', 'ps_mode', 'aplost_time', 'unpair',
'auto_chswitch', 'mcast_key', 'reassoc_wkhost', 'wakeup_io', 'dbginfo', 'sysdbg', 'primary_chan', 'autosleep_time',
'super_pwr', 'r_ssid', 'r_psk', 'auto_save', 'pair_autostop', 'dcdc13', 'acktmo', 'pa_pwrctl_dis', 'dhcpc',
'wkdata_save', 'mcast_txparam', 'reset_sta', 'ant_auto', 'ant_sel', 'wkhost_reason', 'macfilter', 'atcmd', 'roaming',
'ap_hide', 'max_txcnt', 'assert_holdup', 'ap_psmode', 'dupfilter', 'dis_1v1m2u', 'dis_psconnect', 'reset',
'heartbeat', 'heartbeat_resp', 'wakeup_data', 'wakeup', 'custmgmt', 'mgmtframe', 'wkdata_mask', 'driverdata',
'freqinfo', 'blenc', 'sleep', 'hwscan', 'user_edca', 'fix_txrate', 'nav_max', 'clr_nav', 'cca_param', 'tx_modgain',
'rts_duration', 'disable_print', 'conn_paironly', 'diffcust_conn', 'wait_psmode', 'standby', 'ap_chansw', 'cca_ce',
'rtc', 'apep_padding', 'watchdog', 'retry_fallback_cnt', 'fallback_mcs', 'xosc', 'freq_cali_period', 'cust_drvdata',
'max_txdelay', 'heartbeat_int', 'atcmd'
]
GET_COMMANDS = [
'mode', 'sta_list', 'scan_list', 'ssid', 'bssid', 'wpa_psk', 'txpower', 'agg_cnt', 'bss_bw', 'chan_list', 'freq_range',
'key_mgmt', 'battery_level', 'module_type', 'disassoc_reason', 'ant_sel', 'wkreason', 'wkdata_buff', 'temperature',
'conn_state', 'sta_count', 'txq_param', 'nav', 'rtc', 'bgrssi', 'center_freq', 'acs_result', 'reason_code', 'status_code',
'dhcpc_result', 'xosc', 'freq_offset', 'fwinfo', 'stainfo', 'signal'
]
CONFIG_DIR = '/etc/'
LAST_SETTINGS_FILE = os.path.join(CONFIG_DIR, 'last_settings.json')
CURRENT_SETTINGS_FILE = os.path.join(CONFIG_DIR, 'hgicf.conf')
BACKUP_SETTINGS_FILE = os.path.join(CONFIG_DIR, 'hgicf-backup.conf')
DEFAULT_SETTINGS_FILE = os.path.join(CONFIG_DIR, 'hgicf-template.conf')
WIFI_SSID_FILE = '/boot/wifi.ssid'
WIFI_PASS_FILE = '/boot/wifi.pass'
MODE_FILE = os.path.join(CONFIG_DIR, 'mode.conf')
MODES = ['hgpriv', 'libnetat']
DEFAULT_MODE = 'hgpriv'
# Command remapping table for libnetat
COMMAND_REMAP = {
'signal': 'rssi'
}
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Taixin Tool</title>
<link rel="stylesheet" href="/static/bootstrap.min.css">
<script src="/static/chart.min.js"></script>
<script src="/static/moment.min.js"></script>
<script src="/static/chartjs-adapter-moment.min.js"></script>
<style>
body {
padding-top: 50px;
}
.container {
max-width: 700px;
}
.form-group {
margin-bottom: 1.5rem;
}
.nav-tabs {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center">Taixin Tool</h1>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#home" data-toggle="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#station-settings" data-toggle="tab">Station Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#quick-pair" data-toggle="tab">Quick Pair</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#network-settings" data-toggle="tab">Network Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#system" data-toggle="tab">System</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#wifi-settings" data-toggle="tab">WiFi Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#site-survey" data-toggle="tab">Site Survey</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="home">
<h2>Current Settings</h2>
<p><strong>SSID:</strong> {{ current_settings['ssid'] }}</p>
<p><strong>BSSID:</strong> {{ current_settings['bssid'] }}</p>
<p><strong>Tx Power:</strong> {{ current_settings['txpower'] }}</p>
<p><strong>BSS BW:</strong> {{ current_settings['bss_bw'] }}</p>
<p><strong>Connection State:</strong> <span id="conn_state">{{ current_settings['conn_state'] }}</span></p>
<form id="commandForm">
<div class="form-group">
<label for="commandType">Select Command Type:</label>
<select class="form-control" id="commandType" name="commandType" onchange="updateCommands()">
<option value="get">Get</option>
<option value="set">Set</option>
</select>
</div>
<div class="form-group">
<label for="command">Select Command:</label>
<select class="form-control" id="command" name="command" onchange="checkSignal()"></select>
</div>
<div class="form-group" id="valueGroup">
<label for="value">Value (for Set commands only):</label>
<input type="text" class="form-control" id="value" name="value">
</div>
<div class="form-group" id="intervalGroup" style="display: none;">
<label for="interval">Refresh Interval (seconds):</label>
<input type="number" class="form-control" id="interval" name="interval" value="5" min="1">
</div>
<button type="button" class="btn btn-primary" onclick="sendCommand()">Send</button>
</form>
<h2>Response:</h2>
<pre id="response" class="bg-light p-3 border rounded"></pre>
<canvas id="signalChart" width="400" height="200" class="my-4"></canvas>
</div>
<div class="tab-pane fade" id="station-settings">
<h2>Station Settings</h2>
<form id="stationSettingsForm">
<div class="form-group">
<label for="ssid">SSID:</label>
<input type="text" class="form-control" id="ssid" name="ssid">
</div>
<div class="form-group">
<label for="super_pwr">Super Power:</label>
<input type="text" class="form-control" id="super_pwr" name="super_pwr">
</div>
<div class="form-group">
<label for="wpa_psk">WPA PSK:</label>
<input type="text" class="form-control" id="wpa_psk" name="wpa_psk">
</div>
<div class="form-group">
<label for="key_mgmt">Key Management:</label>
<select class="form-control" id="key_mgmt" name="key_mgmt" onchange="validateKeyMgmt()">
<option value="WPA-PSK">WPA-PSK</option>
<option value="NONE">NONE</select>
</div>
<div class="form-group">
<label for="tx_mcs">Tx MCS:</label>
<input type="text" class="form-control" id="tx_mcs" name="tx_mcs">
</div>
<div class="form-group">
<label for="freq_range">Frequency Range:</label>
<input type="text" class="form-control" id="freq_range" name="freq_range">
</div>
<div class="form-group">
<label for="bss_bw">BSS BW:</label>
<select class="form-control" id="bss_bw" name="bss_bw">
<option value="1">1 MHz</option>
<option value="2">2 MHz</option>
<option value="4">4 MHz</option>
<option value="8">8 MHz</option>
</select>
</div>
<div class="form-group">
<label for="mode">Mode:</label>
<select class="form-control" id="mode" name="mode" onchange="toggleApStaFields()">
<option value="ap">Access Point</option>
<option value="sta">Station</option>
<option value="group">Broadcast</option>
<option value="apsta">Access Point/Station</option>
</select>
</div>
<div class="form-group" id="r_ssid_group" style="display: none;">
<label for="r_ssid">Remote SSID:</label>
<input type="text" class="form-control" id="r_ssid" name="r_ssid">
</div>
<div class="form-group" id="r_psk_group" style="display: none;">
<label for="r_psk">Remote PSK:</label>
<input type="text" class="form-control" id="r_psk" name="r_psk">
</div>
<button type="button" class="btn btn-primary" onclick="saveStationSettings()">Save Settings</button>
<button type="button" class="btn btn-warning" onclick="restoreDefaults()">Restore Defaults</button>
</form>
</div>
<div class="tab-pane fade" id="quick-pair">
<h2>Quick Pair</h2>
<button type="button" class="btn btn-primary" onclick="quickPair()">Start Pairing</button>
<h3>Pairing Details</h3>
<pre id="pairingDetails" class="bg-light p-3 border rounded"></pre>
</div>
<div class="tab-pane fade" id="network-settings">
<h2>Network Settings</h2>
<form id="networkSettingsForm">
<div class="form-group">
<label for="ip_address">IP Address:</label>
<input type="text" class="form-control" id="ip_address" name="ip_address">
</div>
<div class="form-group">
<label for="netmask">Netmask:</label>
<input type="text" class="form-control" id="netmask" name="netmask">
</div>
<div class="form-group">
<label for="gateway">Gateway:</label>
<input type="text" class="form-control" id="gateway" name="gateway">
</div>
<button type="button" class="btn btn-primary" onclick="saveNetworkSettings()">Save Network Settings</button>
</form>
<h2>Current Network Settings</h2>
<p><strong>IP Address:</strong> <span id="current_ip_address">{{ current_network_settings['ip_address'] }}</span></p>
<p><strong>Netmask:</strong> <span id="current_netmask">{{ current_network_settings['netmask'] }}</span></p>
<p><strong>Gateway:</strong> <span id="current_gateway">{{ current_network_settings['gateway'] }}</span></p>
</div>
<div class="tab-pane fade" id="system">
<h2>System</h2>
<button type="button" class="btn btn-danger" onclick="rebootSystem()">Reboot</button>
<h3>Run Command</h3>
<form id="systemCommandForm">
<div class="form-group">
<label for="syscommand">Command:</label>
<input type="text" class="form-control" id="syscommand" name="syscommand">
</div>
<button type="button" class="btn btn-primary" onclick="runSystemCommand()">Run</button>
</form>
<h3>Command Output:</h3>
<pre id="commandOutput" class="bg-light p-3 border rounded"></pre>
<h3>Firmware Upload</h3>
<form action="/upload_firmware" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="firmware">Choose firmware file:</label>
<input type="file" class="form-control" id="firmware" name="firmware">
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
<h3>Mode Setting</h3>
<form id="modeSettingForm">
<div class="form-group">
<label for="mode_select">Mode:</label>
<select class="form-control" id="mode_select" name="mode_select" onchange="changeMode()">
<option value="hgpriv">hgpriv</option>
<option value="libnetat">libnetat</option>
</select>
</div>
</form>
</div>
<div class="tab-pane fade" id="wifi-settings">
<h2>WiFi Settings</h2>
<form id="wifiSettingsForm">
<div class="form-group">
<label for="wifi_ssid">SSID:</label>
<input type="text" class="form-control" id="wifi_ssid" name="wifi_ssid">
</div>
<div class="form-group">
<label for="wifi_pass">Password:</label>
<input type="text" class="form-control" id="wifi_pass" name="wifi_pass">
</div>
<button type="button" class="btn btn-primary" onclick="saveWiFiSettings()">Save WiFi Settings</button>
</form>
</div>
<div class="tab-pane fade" id="site-survey">
<h2>Site Survey</h2>
<button type="button" class="btn btn-primary" onclick="runSiteSurvey()">Run Site Survey</button>
<h3>Survey Results</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>BSSID</th>
<th>SSID</th>
<th>Encryption</th>
<th>Frequency</th>
<th>Signal</th>
</tr>
</thead>
<tbody id="surveyResults">
</tbody>
</table>
</div>
</div>
</div>
<script>
const setCommands = {{ set_commands | tojson }};
const getCommands = {{ get_commands | tojson }};
let rssiInterval;
let signalChart;
let signalData = [];
let connStateInterval;
function updateCommands() {
const commandType = document.getElementById('commandType').value;
const commandSelect = document.getElementById('command');
commandSelect.innerHTML = '';
const commands = commandType === 'get' ? getCommands : setCommands;
commands.forEach(cmd => {
const option = document.createElement('option');
option.value = cmd;
option.textContent = cmd;
commandSelect.appendChild(option);
});
document.getElementById('valueGroup').style.display = commandType === 'set' ? 'block' : 'none';
}
function checkSignal() {
const command = document.getElementById('command').value;
const intervalGroup = document.getElementById('intervalGroup');
if (command === 'signal' || command === 'rssi') {
intervalGroup.style.display = 'block';
setupChart();
} else {
intervalGroup.style.display = 'none';
clearInterval(rssiInterval);
if (signalChart) {
signalChart.destroy();
signalChart = null;
}
}
}
function sendCommand() {
const commandType = document.getElementById('commandType').value;
const command = document.getElementById('command').value;
const value = document.getElementById('value').value;
const interval = document.getElementById('interval').value;
const url = `/command?cmd=${commandType}¶m=${encodeURIComponent(command)}${commandType === 'set' ? '&value=' + encodeURIComponent(value) : ''}`;
if (command === 'signal' || command === 'rssi') {
clearInterval(rssiInterval);
fetchAndUpdateSignal(url);
rssiInterval = setInterval(() => fetchAndUpdateSignal(url), interval * 1000);
} else {
fetch(url)
.then(response => response.json())
.then(data => {
const responseText = data.response.replace(/^RESP:\\d+\\s*/, '');
document.getElementById('response').innerText = responseText;
});
}
}
function fetchAndUpdateSignal(url) {
fetch(url)
.then(response => response.json())
.then(data => {
const responseText = data.response.replace(/^RESP:\\d+\\s*/, '').trim();
document.getElementById('response').innerText = responseText;
const matches = responseText.match(/-?\\d+/);
if (matches) {
const rssi = parseInt(matches[0]);
const now = new Date();
signalData.push({x: now, y: rssi});
if (signalData.length > 20) {
signalData.shift();
}
if (signalChart) {
signalChart.update();
}
}
});
}
function setupChart() {
const ctx = document.getElementById('signalChart').getContext('2d');
signalChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Signal',
data: signalData,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
},
options: {
scales: {
x: {
type: 'time',
time: {
unit: 'second'
}
},
y: {
beginAtZero: true
}
}
}
});
}
function validateKeyMgmt() {
const keyMgmt = document.getElementById('key_mgmt').value;
const wpaPskInput = document.getElementById('wpa_psk');
if (keyMgmt === 'WPA-PSK') {
wpaPskInput.setAttribute('maxlength', '64');
} else {
wpaPskInput.removeAttribute('maxlength');
}
}
function toggleApStaFields() {
const mode = document.getElementById('mode').value;
const rSsidGroup = document.getElementById('r_ssid_group');
const rPskGroup = document.getElementById('r_psk_group');
if (mode === 'apsta') {
rSsidGroup.style.display = 'block';
rPskGroup.style.display = 'block';
} else {
rSsidGroup.style.display = 'none';
rPskGroup.style.display = 'none';
}
}
function loadStationSettings() {
fetch('/load_station_settings')
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
document.getElementById('ssid').value = data.settings.ssid || '';
document.getElementById('super_pwr').value = data.settings.super_pwr || '';
document.getElementById('wpa_psk').value = data.settings.wpa_psk || '';
document.getElementById('key_mgmt').value = data.settings.key_mgmt || 'NONE';
document.getElementById('tx_mcs').value = data.settings.tx_mcs || '';
document.getElementById('freq_range').value = data.settings.freq_range || '';
document.getElementById('bss_bw').value = data.settings.bss_bw || '1';
document.getElementById('mode').value = data.settings.mode || 'ap';
document.getElementById('r_ssid').value = data.settings.r_ssid || '';
document.getElementById('r_psk').value = data.settings.r_psk || '';
toggleApStaFields();
}
});
}
function saveStationSettings() {
const settings = {
ssid: document.getElementById('ssid').value,
super_pwr: document.getElementById('super_pwr').value,
wpa_psk: document.getElementById('wpa_psk').value,
key_mgmt: document.getElementById('key_mgmt').value,
tx_mcs: document.getElementById('tx_mcs').value,
freq_range: document.getElementById('freq_range').value,
bss_bw: document.getElementById('bss_bw').value,
mode: document.getElementById('mode').value,
r_ssid: document.getElementById('r_ssid').value,
r_psk: document.getElementById('r_psk').value
};
if (settings.key_mgmt === 'WPA-PSK' && settings.wpa_psk.length !== 64) {
alert('WPA PSK must be exactly 64 characters long.');
return;
}
if (settings.mode === 'apsta' && (!settings.r_ssid || (settings.r_psk && settings.r_psk.length !== 64))) {
alert('Remote SSID must be set for AP/STA mode, and Remote PSK must be 64 characters long if set.');
return;
}
fetch('/station_settings', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(settings)
}).then(response => response.json())
.then(data => {
if (data.status === 'success') {
if (confirm('Settings saved successfully. Would you like to reboot now?')) {
rebootSystem();
} else {
alert('Settings saved successfully.');
}
} else {
alert('Failed to save settings.');
}
});
}
function restoreDefaults() {
fetch('/restore_defaults', { method: 'POST' })
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
alert('Defaults restored successfully. Please save the settings to apply.');
} else {
alert('Failed to restore defaults.');
}
});
}
function saveNetworkSettings() {
const settings = {
ip_address: document.getElementById('ip_address').value,
netmask: document.getElementById('netmask').value,
gateway: document.getElementById('gateway').value
};
fetch('/network_settings', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(settings)
}).then(response => response.json())
.then(data => {
if (data.status === 'success') {
alert('Network settings saved successfully.');
document.getElementById('current_ip_address').innerText = settings.ip_address;
document.getElementById('current_netmask').innerText = settings.netmask;
document.getElementById('current_gateway').innerText = settings.gateway;
} else {
alert('Failed to save network settings.');
}
});
}
function quickPair() {
fetch('/quick_pair', { method: 'POST' })
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
fetch('/command?cmd=get¶m=ssid')
.then(response => response.json())
.then(ssidData => {
const ssid = ssidData.response.replace(/^RESP:\\d+\\s*/, '').trim();
fetch('/command?cmd=get¶m=wpa_psk')
.then(response => response.json())
.then(wpaPskData => {
const wpa_psk = wpaPskData.response.replace(/^RESP:\\d+\\s*/, '').trim();
document.getElementById('pairingDetails').innerText = `SSID: ${ssid}\\nWPA PSK: ${wpa_psk}`;
});
});
} else {
alert('Failed to start pairing.');
}
});
}
function rebootSystem() {
fetch('/reboot', { method: 'POST' })
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
alert('System is rebooting.');
} else {
alert('Failed to reboot system.');
}
});
}
function runSystemCommand() {
const syscommand = document.getElementById('syscommand').value;
fetch('/run_command', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ command: syscommand })
}).then(response => response.json())
.then(data => {
if (data.status === 'success') {
document.getElementById('commandOutput').innerText = data.output;
} else {
document.getElementById('commandOutput').innerText = 'Failed to run command.';
}
});
}
function updateConnState() {
fetch('/command?cmd=get¶m=conn_state')
.then(response => response.json())
.then(data => {
const connState = parseInt(data.response.replace(/^RESP:\\d+\\s*/, '').trim());
const connStateElement = document.getElementById('conn_state');
switch (connState) {
case 0:
connStateElement.textContent = 'DISCONNECTED';
connStateElement.style.color = 'red';
break;
case 1:
connStateElement.textContent = 'DISABLED';
connStateElement.style.color = 'red';
break;
case 2:
connStateElement.textContent = 'INACTIVE';
connStateElement.style.color = 'red';
break;
case 3:
connStateElement.textContent = 'SCANNING';
connStateElement.style.color = 'blue';
break;
case 4:
connStateElement.textContent = 'AUTHENTICATING';
connStateElement.style.color = 'blue';
break;
case 5:
connStateElement.textContent = 'ASSOCIATING';
connStateElement.style.color = 'blue';
break;
case 6:
connStateElement.textContent = 'ASSOCIATED';
connStateElement.style.color = 'green';
break;
case 7:
connStateElement.textContent = '4-WAY-HANDSHAKE';
connStateElement.style.color = 'blue';
break;
case 8:
connStateElement.textContent = '4-WAY-GROUP-HANDSHAKE';
connStateElement.style.color = 'blue';
break;
case 9:
connStateElement.textContent = 'CONNECTED TO AP';
connStateElement.style.color = 'green';
break;
default:
connStateElement.textContent = data.response;
connStateElement.style.color = 'black';
break;
}
});
}
function loadWiFiSettings() {
fetch('/load_wifi_settings')
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
document.getElementById('wifi_ssid').value = data.ssid || '';
document.getElementById('wifi_pass').value = data.pass || '';
}
});
}
function saveWiFiSettings() {
const settings = {
ssid: document.getElementById('wifi_ssid').value,
pass: document.getElementById('wifi_pass').value
};
fetch('/save_wifi_settings', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(settings)
}).then(response => response.json())
.then(data => {
if (data.status === 'success') {
if (confirm('WiFi settings saved successfully. Would you like to reboot now?')) {
rebootSystem();
} else {
alert('WiFi settings saved successfully.');
}
} else {
alert('Failed to save WiFi settings.');
}
});
}
function runSiteSurvey() {
fetch('/command?cmd=get¶m=scan_list')
.then(response => response.json())
.then(data => {
const responseText = data.response.replace(/^RESP:\\d+\\s*/, '').trim();
const surveyResults = document.getElementById('surveyResults');
surveyResults.innerHTML = ''; // Clear previous results
const lines = responseText.split('\\n').slice(1); // Ignore the first line
lines.forEach(line => {
const [bssid, ssid, encryption, frequency, signal] = line.split('\t');
const row = document.createElement('tr');
row.innerHTML = `<td>${bssid}</td><td>${ssid}</td><td>${encryption}</td><td>${frequency}</td><td>${signal}</td>`;
surveyResults.appendChild(row);
});
});
}
function loadMode() {
fetch('/load_mode')
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
document.getElementById('mode_select').value = data.mode;
}
});
}
function changeMode() {
const mode = document.getElementById('mode_select').value;
fetch('/switch_mode', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ mode })
}).then(response => response.json())
.then(data => {
if (data.status === 'success') {
alert('Mode switched successfully.');
} else {
alert('Failed to switch mode.');
}
});
}
document.addEventListener('DOMContentLoaded', updateCommands);
document.addEventListener('DOMContentLoaded', validateKeyMgmt);
document.addEventListener('DOMContentLoaded', toggleApStaFields);
document.addEventListener('DOMContentLoaded', loadStationSettings);
document.addEventListener('DOMContentLoaded', loadWiFiSettings);
document.addEventListener('DOMContentLoaded', loadMode);
document.addEventListener('DOMContentLoaded', () => {
updateConnState();
connStateInterval = setInterval(updateConnState, 5000);
sendCommand('get', 'signal');
});
</script>
<script src="/static/jquery-3.5.1.min.js"></script>
<script src="/static/bootstrap.bundle.min.js"></script>
</body>
</html>
"""
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def run_command(command):
try:
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).decode()
output = re.sub(r'^RESP:\d+\s*', '', output)
except subprocess.CalledProcessError as e:
output = e.output.decode()
return output
def run_libnetat_command(command, cmd_type):
if cmd_type == 'get':
command = f"at+{command}?"
elif cmd_type == 'set':
command = f"at+{command}"
try:
output = subprocess.check_output(f"/sbin/libnetat hg0 {command}", shell=True, stderr=subprocess.STDOUT).decode()
if 'valid cmds:' in output:
return "Invalid command in libnetat mode"
# Extract the actual value from the response
match = re.search(r'\+\w+:(.+?)\nOK', output, re.DOTALL)
if match:
return match.group(1).strip()
return output.strip()
except subprocess.CalledProcessError as e:
return e.output.decode()
def load_last_settings():
if os.path.exists(LAST_SETTINGS_FILE):
with open(LAST_SETTINGS_FILE, 'r') as f:
return json.load(f)
return {}
def save_settings(settings):
with open(LAST_SETTINGS_FILE, 'w') as f:
json.dump(settings, f)
def apply_settings(settings):
mode = load_mode()
for key, value in settings.items():
if mode == 'hgpriv':
command = f"/sbin/hgpriv hg0 set {key}={value}"
run_command(command)
elif mode == 'libnetat':
command = f"{key}={value}"
run_libnetat_command(command, 'set')
def get_current_network_settings():
ip_address = run_command("ifconfig hg0 | grep 'inet ' | awk '{print $2}'").strip()
netmask = run_command("ifconfig hg0 | grep 'inet ' | awk '{print $4}'").strip()
gateway = run_command("ip route | grep default | awk '{print $3}'").strip()
return {'ip_address': ip_address, 'netmask': netmask, 'gateway': gateway}
def set_network_settings(ip_address, netmask, gateway):
run_command(f"ifconfig hg0 {ip_address} netmask {netmask}")
run_command(f"route add default gw {gateway}")
def load_station_settings():
settings = {}
if os.path.exists(CURRENT_SETTINGS_FILE):
with open(CURRENT_SETTINGS_FILE, 'r') as f:
for line in f:
if '=' in line:
key, value = line.strip().split('=', 1)
settings[key] = value.replace('\n', '')
return settings
def save_station_settings_file(settings):
backup_station_settings()
with open(CURRENT_SETTINGS_FILE, 'w') as f:
for key, value in settings.items():
f.write(f"{key}={value}\n")
def backup_station_settings():
if os.path.exists(CURRENT_SETTINGS_FILE):
with open(CURRENT_SETTINGS_FILE, 'r') as src, open(BACKUP_SETTINGS_FILE, 'w') as dst:
dst.write(src.read())
def restore_defaults():
if os.path.exists(DEFAULT_SETTINGS_FILE):
with open(DEFAULT_SETTINGS_FILE, 'r') as src, open(CURRENT_SETTINGS_FILE, 'w') as dst:
dst.write(src.read())
def load_wifi_settings():
ssid = ''
password = ''
if os.path.exists(WIFI_SSID_FILE):
with open(WIFI_SSID_FILE, 'r') as f:
ssid = f.read().strip()
if os.path.exists(WIFI_PASS_FILE):
with open(WIFI_PASS_FILE, 'r') as f:
password = f.read().strip()
return ssid, password
def save_wifi_settings(ssid, password):
with open(WIFI_SSID_FILE, 'w') as f:
f.write(ssid + '\n')
with open(WIFI_PASS_FILE, 'w') as f:
f.write(password + '\n')
def load_mode():
if os.path.exists(MODE_FILE):
with open(MODE_FILE, 'r') as f:
return f.read().strip()
return DEFAULT_MODE
def save_mode(mode):
with open(MODE_FILE, 'w') as f:
f.write(mode + '\n')
@app.route('/')
def index():
mode = load_mode()
if mode == 'hgpriv':
current_settings = {param: run_command(f"/sbin/hgpriv hg0 get {param}").strip() for param in ['ssid', 'bssid', 'txpower', 'bss_bw', 'conn_state']}
elif mode == 'libnetat':
current_settings = {param: run_libnetat_command(f"{param}", 'get').strip() for param in ['ssid', 'bssid', 'txpower', 'bss_bw', 'conn_state']}
current_network_settings = get_current_network_settings()
return render_template_string(HTML_TEMPLATE, set_commands=SET_COMMANDS, get_commands=GET_COMMANDS, current_settings=current_settings, current_network_settings=current_network_settings)
@app.route('/command', methods=['GET'])
def handle_command():
cmd_type = request.args.get('cmd')
param = request.args.get('param')
value = request.args.get('value')
mode = load_mode()
if mode == 'hgpriv':
if cmd_type == 'get':
command = f"/sbin/hgpriv hg0 get {param}"
elif cmd_type == 'set':
command = f"/sbin/hgpriv hg0 set {param}={value}"
else:
return jsonify({"response": "Invalid command type"})
output = run_command(command)
elif mode == 'libnetat':
# Remap command if necessary
if param in COMMAND_REMAP:
param = COMMAND_REMAP[param]
if cmd_type == 'get':
command = f"{param}?"
elif cmd_type == 'set':
command = f"{param}={value}"
else:
return jsonify({"response": "Invalid command type"})
output = run_libnetat_command(command, cmd_type)
else:
return jsonify({"response": "Invalid mode"})
return jsonify({"response": output})
@app.route('/station_settings', methods=['POST'])
def save_station_settings():
settings = request.json
save_station_settings_file(settings)
save_settings(settings)
return jsonify({"status": "success"})
@app.route('/restore_defaults', methods=['POST'])
def handle_restore_defaults():
restore_defaults()
return jsonify({"status": "success"})
@app.route('/network_settings', methods=['POST'])
def save_network_settings():
settings = request.json
ip_address = settings.get('ip_address')
netmask = settings.get('netmask')
gateway = settings.get('gateway')
if ip_address and netmask and gateway:
set_network_settings(ip_address, netmask, gateway)
return jsonify({"status": "success"})
return jsonify({"status": "failure"})
@app.route('/load_station_settings', methods=['GET'])
def handle_load_station_settings():
settings = load_station_settings()
return jsonify({"status": "success", "settings": settings})
@app.route('/apply_last_settings', methods=['GET'])
def apply_last_settings():
settings = load_last_settings()
apply_settings(settings)
return jsonify({"status": "success"})
@app.route('/quick_pair', methods=['POST'])
def quick_pair():
mode = load_mode()
if mode == 'hgpriv':
run_command("/sbin/hgpriv hg0 set pairing=1")
time.sleep(20)
run_command("/sbin/hgpriv hg0 set pairing=0")
elif mode == 'libnetat':
run_libnetat_command("pairing=1", 'set')
time.sleep(20)
run_libnetat_command("pairing=0", 'set')
return jsonify({"status": "success"})
@app.route('/reboot', methods=['POST'])
def reboot_system():
run_command("reboot")
return jsonify({"status": "success"})
@app.route('/run_command', methods=['POST'])
def run_system_command():
command = request.json.get('syscommand')
if command:
output = run_command(command)
return jsonify({"status": "success", "output": output})
return jsonify({"status": "failure"})
@app.route('/load_wifi_settings', methods=['GET'])
def handle_load_wifi_settings():
ssid, password = load_wifi_settings()
return jsonify({"status": "success", "ssid": ssid, "pass": password})
@app.route('/save_wifi_settings', methods=['POST'])
def handle_save_wifi_settings():
settings = request.json
ssid = settings.get('ssid')
password = settings.get('pass')
if ssid and password:
save_wifi_settings(ssid, password)
return jsonify({"status": "success"})
return jsonify({"status": "failure"})
@app.route('/upload_firmware', methods=['POST'])
def upload_firmware():
if 'firmware' not in request.files:
return redirect(request.url)
file = request.files['firmware']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
with open(filepath, 'rb') as f:
firmware_data = f.read()
with open('/proc/hgicf/ota', 'wb') as f:
f.write(firmware_data)
time.sleep(30)
run_command("reboot")
return jsonify({"status": "success"})
return jsonify({"status": "failure"})
@app.route('/switch_mode', methods=['POST'])
def switch_mode():
mode = request.json.get('mode')
if mode in MODES:
save_mode(mode)
return jsonify({"status": "success"})
return jsonify({"status": "failure"})
@app.route('/load_mode', methods=['GET'])
def load_mode_route():
mode = load_mode()
return jsonify({"status": "success", "mode": mode})
if __name__ == '__main__':
last_settings = load_last_settings()
if last_settings:
apply_settings(last_settings)
app.run(host='0.0.0.0', port=8080)