-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathunitree_tunnel_manager.py
1173 lines (988 loc) · 45.2 KB
/
unitree_tunnel_manager.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
992
993
994
995
996
997
998
999
1000
import requests
import json
from typing import Dict, List, Tuple
import urllib3
import sqlite3
import os
import sys
import time
from datetime import datetime, timedelta
import threading
import queue
import termios
import tty
import readline
import atexit
import select
import socket
from concurrent.futures import ThreadPoolExecutor, as_completed
import csv
from ipwhois import IPWhois
from time import sleep
from contextlib import contextmanager
from queue import Queue, Empty
from pprint import pprint
# Disable SSL warning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class KeyboardListener:
def __init__(self):
self.should_stop = False
def __enter__(self):
self.old_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
return self
def __exit__(self, *args):
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.old_settings)
def check_key(self):
if sys.stdin.read(1) == 'q':
self.should_stop = True
class RateLimiter:
def __init__(self, requests_per_minute=15):
self.requests_per_minute = requests_per_minute
self.requests = []
def wait_if_needed(self):
"""Wait if we're over the rate limit"""
now = datetime.now()
# Remove requests older than 1 minute
self.requests = [req_time for req_time in self.requests
if now - req_time < timedelta(minutes=1)]
if len(self.requests) >= self.requests_per_minute:
# Calculate sleep time
oldest_request = min(self.requests)
sleep_time = 60 - (now - oldest_request).seconds
if sleep_time > 0:
print(f"\nRate limit reached, waiting {sleep_time} seconds...")
time.sleep(sleep_time)
self.requests.append(now)
class DatabaseConnectionPool:
"""Thread-safe SQLite connection pool implementation"""
def __init__(self, database, max_connections=5):
self.database = database
self.max_connections = max_connections
self.connections = Queue(maxsize=max_connections)
self.lock = threading.Lock()
self.local = threading.local()
self._fill_pool()
def _fill_pool(self):
"""Initialize the connection pool"""
for _ in range(self.max_connections):
self.connections.put(None) # Reserve slots but don't create connections yet
def _create_connection(self):
"""Create a new database connection for the current thread"""
conn = sqlite3.connect(self.database)
conn.row_factory = sqlite3.Row
return conn
@contextmanager
def get_connection(self):
"""Get a thread-local connection with context management"""
# Check if we already have a connection for this thread
if not hasattr(self.local, 'connection'):
# Get a slot from the pool
try:
self.connections.get(timeout=5) # Just get a slot
self.local.connection = self._create_connection()
except Empty:
raise RuntimeError("Could not get a database connection from the pool")
try:
yield self.local.connection
self.local.connection.commit() # Commit any pending transactions
except Exception as e:
self.local.connection.rollback() # Rollback on error
raise e
def close_all(self):
"""Close all connections in the pool"""
with self.lock:
# Close the thread-local connection if it exists
if hasattr(self.local, 'connection'):
self.local.connection.close()
delattr(self.local, 'connection')
# Clear the pool
while not self.connections.empty():
try:
self.connections.get_nowait()
except Empty:
break
class Database:
def __init__(self, db_path="tunnels.db"):
self.db_path = db_path
self.pool = DatabaseConnectionPool(db_path)
self.init_db()
def init_db(self):
"""Initialize database schema"""
with self.pool.get_connection() as conn:
# Add enhanced lookup fields
try:
# Fields matching IP-API response
conn.execute("ALTER TABLE machines ADD COLUMN dns_hostname TEXT")
conn.execute("ALTER TABLE machines ADD COLUMN dns_status TEXT")
conn.execute("ALTER TABLE machines ADD COLUMN dns_country TEXT")
conn.execute("ALTER TABLE machines ADD COLUMN dns_city TEXT")
conn.execute("ALTER TABLE machines ADD COLUMN dns_org TEXT")
conn.execute("ALTER TABLE machines ADD COLUMN dns_isp TEXT")
conn.execute("ALTER TABLE machines ADD COLUMN dns_asn TEXT")
conn.execute("ALTER TABLE machines ADD COLUMN dns_is_mobile INTEGER")
conn.execute("ALTER TABLE machines ADD COLUMN dns_is_proxy INTEGER")
conn.execute("ALTER TABLE machines ADD COLUMN dns_is_hosting INTEGER")
conn.execute("ALTER TABLE machines ADD COLUMN dns_last_lookup TEXT")
except sqlite3.OperationalError:
# Columns might already exist
pass
conn.execute("""
CREATE TABLE IF NOT EXISTS machines (
id TEXT PRIMARY KEY,
name TEXT,
os TEXT,
ip TEXT,
public_ip TEXT,
mac TEXT,
version TEXT,
is_active INTEGER,
active_time TEXT,
add_time TEXT,
last_updated TEXT,
dns_hostname TEXT,
dns_status TEXT,
dns_country TEXT,
dns_city TEXT,
dns_org TEXT,
dns_isp TEXT,
dns_asn TEXT,
dns_is_mobile INTEGER,
dns_is_proxy INTEGER,
dns_is_hosting INTEGER,
dns_last_lookup TEXT
)
""")
def update_machines(self, machines: List[Dict]):
current_time = datetime.now().isoformat()
with self.pool.get_connection() as conn:
for machine in machines:
try:
conn.execute("""
INSERT OR REPLACE INTO machines
(id, name, os, ip, public_ip, mac, version, is_active,
active_time, add_time, last_updated)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
machine['id'],
machine['name'],
machine.get('os', ''),
machine.get('ip', ''),
machine.get('publicIP', ''),
machine.get('mac', ''),
machine.get('version', ''),
machine.get('isActive', 0),
machine.get('activetime', ''),
machine.get('addtime', ''),
current_time
))
except sqlite3.Error as e:
print(f"\nError updating machine {machine.get('id', 'unknown')}: {str(e)}")
continue
def get_machines_for_display(self, page: int, page_size: int, filters: Dict = None) -> Tuple[List[Dict], int]:
"""Get paginated and filtered machines"""
query = "SELECT * FROM machines"
params = []
if filters:
conditions = []
if 'm' in filters:
conditions.append("name LIKE ?")
params.append(f"%{filters['m']}%")
if 'i' in filters:
conditions.append("(ip LIKE ? OR public_ip LIKE ?)")
params.extend([f"%{filters['i']}%", f"%{filters['i']}%"])
if 's' in filters:
state_map = {'active': 1, 'inactive': 0}
if filters['s'].lower() in state_map:
conditions.append("is_active = ?")
params.append(state_map[filters['s'].lower()])
if conditions:
query += " WHERE " + " AND ".join(conditions)
# Get total count
count_query = f"SELECT COUNT(*) as count FROM ({query})"
with self.pool.get_connection() as conn:
total = conn.execute(count_query, params).fetchone()['count']
# Add pagination
query += " ORDER BY active_time DESC NULLS LAST LIMIT ? OFFSET ?"
params.extend([page_size, page * page_size])
results = conn.execute(query, params).fetchall()
return [dict(row) for row in results], total
def analyze_organizations(self):
"""Analyze organizations via enhanced IP lookups"""
print("\nAnalyzing organizations via IP lookups...")
# Get IPs needing lookup
with self.pool.get_connection() as conn:
new_ips = conn.execute("""
SELECT DISTINCT public_ip
FROM machines
WHERE public_ip != ''
AND public_ip IS NOT NULL
AND dns_last_lookup IS NULL
""").fetchall()
new_ips = {row['public_ip'] for row in new_ips}
if not new_ips:
print("No new IPs to analyze.")
return
print(f"Processing {len(new_ips)} IP addresses...")
results = self.batch_reverse_dns_lookup(list(new_ips))
current_time = datetime.now().isoformat()
# Update DNS information
with self.pool.get_connection() as conn:
for result in results:
conn.execute("""
UPDATE machines
SET dns_hostname = ?,
dns_status = ?,
dns_country = ?,
dns_city = ?,
dns_org = ?,
dns_isp = ?,
dns_asn = ?,
dns_is_mobile = ?,
dns_is_proxy = ?,
dns_is_hosting = ?,
dns_last_lookup = ?
WHERE public_ip = ?
""", (
result['hostname'],
result['status'],
result['country'],
result['city'],
result['org'],
result['isp'],
result['asn'],
result['is_mobile'],
result['is_proxy'],
result['is_hosting'],
current_time,
result['ip']
))
def __del__(self):
"""Cleanup connection pool on deletion"""
self.pool.close_all()
def get_whois_org_info(self, ip: str) -> Dict:
"""Get organization information using ipwhois as fallback"""
try:
obj = IPWhois(ip)
result = obj.lookup_rdap(depth=1)
return {
'org': result.get('network', {}).get('name'),
'asn': f"AS{result.get('asn')}",
'asn_description': result.get('asn_description')
}
except Exception:
return {'org': None, 'asn': None, 'asn_description': None}
def batch_reverse_dns_lookup(self, ips: List[str]) -> List[Dict]:
"""Perform batch IP lookup using WHOIS first, then ip-api.com as fallback"""
if not ips:
return []
batch_size = 100
results = []
rate_limiter = RateLimiter(requests_per_minute=15)
total_batches = (len(ips) + batch_size - 1) // batch_size
for batch_num, i in enumerate(range(0, len(ips), batch_size)):
batch = list(ips)[i:i + batch_size]
rate_limiter.wait_if_needed()
batch_results = []
for ip in batch:
# Try WHOIS first
whois_info = self.get_whois_org_info(ip)
result = {
'status': 'success',
'country': None,
'city': None,
'org': whois_info['org'],
'isp': None,
'as': whois_info['asn'],
'mobile': False,
'proxy': False,
'hosting': False
}
# If WHOIS didn't provide org info, try ip-api
if not whois_info['org']:
try:
fields = ["status", "country", "city", "org", "isp", "as",
"mobile", "proxy", "hosting"]
response = requests.post(
'http://ip-api.com/batch',
json=[{"query": ip, "fields": fields}],
timeout=10
)
if response.status_code == 429:
print("\nRate limit exceeded, waiting for reset...")
time.sleep(60)
continue
api_result = response.json()[0]
# Update missing fields from ip-api
result.update({
'status': api_result.get('status'),
'country': api_result.get('country'),
'city': api_result.get('city'),
'org': api_result.get('org') or result['org'],
'isp': api_result.get('isp'),
'as': api_result.get('as') or result['as'],
'mobile': api_result.get('mobile', False),
'proxy': api_result.get('proxy', False),
'hosting': api_result.get('hosting', False)
})
except Exception as e:
print(f"\nIP-API lookup failed for {ip}: {str(e)}")
batch_results.append(result)
# Store results for this batch
for ip, result in zip(batch, batch_results):
results.append({
'ip': ip,
'hostname': socket.getfqdn(ip),
'status': result.get('status'),
'country': result.get('country'),
'city': result.get('city'),
'org': result.get('org'),
'isp': result.get('isp'),
'asn': result.get('as'),
'is_mobile': 1 if result.get('mobile') else 0,
'is_proxy': 1 if result.get('proxy') else 0,
'is_hosting': 1 if result.get('hosting') else 0
})
progress = int((batch_num + 1) / total_batches * 50)
percentage = int((batch_num + 1) / total_batches * 100)
print(f"\rProgress: [{'=' * progress}{' ' * (50-progress)}] "
f"{percentage}% "
f"({(batch_num + 1) * batch_size}/{len(ips)})", end='', flush=True)
print() # New line after progress bar
return results
class ZhexiAPI:
def __init__(self, api_key: str, base_url: str = "https://yz.zhexi.tech"):
self.api_key = api_key
self.base_url = base_url
self.session = requests.Session()
self.session.verify = False
self.db = Database()
self.update_queue = queue.Queue()
self.is_updating = False
def _make_request(self, endpoint: str, params: Dict = None, method: str = "GET") -> Dict:
if params is None:
params = {}
url = f"{self.base_url}/api/v2/{endpoint}?apikey={self.api_key}"
if method == "GET":
response = self.session.get(url, params=params)
elif method == "POST":
response = self.session.post(url, json=params)
ret = response.json()
# pprint(ret)
return ret
def start_background_update(self):
if not self.is_updating:
self.is_updating = True
thread = threading.Thread(target=self._background_update)
thread.daemon = True
thread.start()
def start_online_machines_update(self):
"""Start background thread to only update online machines"""
if not self.is_updating:
self.is_updating = True
thread = threading.Thread(target=self._update_online_machines)
thread.daemon = True
thread.start()
def _background_update(self):
page_size = 10
from_page = 0
# First request to get total count
response = self._make_request("machine/list", params={"size": page_size, "from": from_page})
total = response.get('total', 0)
machines_processed = 0
while machines_processed < total:
response = self._make_request("machine/list", params={"size": page_size, "from": from_page})
machines = response.get('data', [])
if not machines:
break
self.db.update_machines(machines)
machines_processed += len(machines)
from_page += 1
# Calculate progress percentage
progress = int((machines_processed / total) * 50)
percentage = int((machines_processed / total) * 100)
# Create progress bar
progress_bar = f"\rUpdating machines: [{'=' * progress}{' ' * (50-progress)}] {percentage}% ({machines_processed}/{total})"
self.update_queue.put(progress_bar)
# Verify final count
with sqlite3.connect(self.db.db_path) as conn:
final_count = conn.execute("SELECT COUNT(*) FROM machines").fetchone()[0]
self.update_queue.put(f"\rUpdate complete! Total machines in database: {final_count} \n")
self.is_updating = False
def _update_online_machines(self):
"""
Update only machines that are currently online.
This is more efficient than updating all machines.
Process:
1. Fetch only online machines from the API
2. If successful, mark all machines as offline in the database
3. Update online machines in the database
"""
page_size = 10
from_page = 0
current_time = datetime.now().isoformat()
# Step 1: First check if we can access the API and get online machines
self.update_queue.put("\rFetching online machines from API...\n")
# Initial API request to get total count of online machines
try:
response = self._make_request("machine/list", params={"size": page_size, "from": from_page, "online": 1})
total = response.get('total', 0)
if 'error' in response and response.get('error') != 0:
self.update_queue.put(f"\rAPI Error: {response.get('info', 'Unknown error')}\n")
self.is_updating = False
return
self.update_queue.put(f"\rFound {total} online machines. Preparing to update...\n")
# If we got here, the API is responsive, so we can safely collect all online machines
online_machines = []
machines_processed = 0
from_page = 0
while machines_processed < total:
response = self._make_request("machine/list", params={"size": page_size, "from": from_page, "online": 1})
machines = response.get('data', [])
if not machines:
break
online_machines.extend(machines)
machines_processed += len(machines)
from_page += 1
# Calculate progress percentage for fetching
progress = int((machines_processed / total) * 50) if total > 0 else 50
percentage = int((machines_processed / total) * 100) if total > 0 else 100
# Create progress bar
progress_bar = f"\rFetching online machines: [{'=' * progress}{' ' * (50-progress)}] {percentage}% ({machines_processed}/{total})"
self.update_queue.put(progress_bar)
# Step 2: Now that we have all online machines, mark all machines offline first
with self.db.pool.get_connection() as conn:
conn.execute("UPDATE machines SET is_active = 0, last_updated = ?", (current_time,))
self.update_queue.put("\rMarked all machines as offline. Now updating online machines...\n")
# Step 3: Update the online machines in the database
if online_machines:
self.db.update_machines(online_machines)
# Verify final count of online machines
with sqlite3.connect(self.db.db_path) as conn:
online_count = conn.execute("SELECT COUNT(*) FROM machines WHERE is_active = 1").fetchone()[0]
total_count = conn.execute("SELECT COUNT(*) FROM machines").fetchone()[0]
self.update_queue.put(f"\rUpdate complete! Online machines: {online_count}/{total_count} \n")
else:
self.update_queue.put(f"\rNo online machines found. All machines marked as offline.\n")
except Exception as e:
self.update_queue.put(f"\rError during update: {str(e)}\n")
self.is_updating = False
def list_valid_templates(self, tunnel_type: int, template_type: int = None, bandwidth: int = None) -> Dict:
"""
Get list of valid tunnel templates
Args:
tunnel_type (int): Required. Tunnel type (0: TCP, 1: HTTP, 2: HTTPS, 5: UDP)
template_type (int, optional): Template type
0: Random port (custom)
3: Same port (custom)
254: 5 Yuan permanent free tunnel
255: Free tunnel
bandwidth (int, optional): Template bandwidth (1,2,4,8,12,16,20,24 Mbps)
"""
params = {
"tunnelType": tunnel_type,
"size": 10 # Maximum allowed by API
}
if template_type is not None:
params["type"] = template_type
if bandwidth is not None:
params["bandwidth"] = bandwidth
return self._make_request("mapping/valid", params)
def create_tunnel(self, machine_id: str, template_id: str = None, port: int = None) -> Dict:
"""Create an SSH tunnel for a specific machine
Args:
machine_id: ID of the target machine
template_id: Optional. If provided, use this template.
If None, create a new template.
"""
# Step 1: If no template_id provided, create a new tunnel template
if not template_id:
expiry_date = (datetime.now() + timedelta(days=7)).strftime('%Y-%m-%d %H:%M:%S')
template_params = {
"amount": 1,
"bandwidth": 4,
"concurrency": 50,
"expired_at": expiry_date
}
template_response = self._make_request("order/tunnel", params=template_params)
if template_response.get('error', 1) != 0:
return template_response
template_id = template_response.get('data', {}).get('id')
if not template_id:
return {"error": 1, "info": "Failed to create tunnel template"}
print(f"Created new tunnel template: {template_id}")
# Step 2: Create the actual tunnel mapping
params = {
"machineID": machine_id,
"templet": template_id,
"type": 0, # TCP
"name": f"d_{machine_id}",
"ip": "127.0.0.1",
"port": port,
}
return self._make_request("mapping/tunnel", params=params, method="POST")
class InteractiveShell:
def __init__(self, api):
self.api = api
self.page_size = 20
self.current_page = 0
self.keyboard_listener = None
self.commands = {
'p': self.print_tunnels,
'print': self.print_tunnels,
'n': self.next_page,
'next': self.next_page,
'prev': self.prev_page,
'b': self.prev_page,
'back': self.prev_page,
'u': self.update_data,
'update': self.update_data,
'help': self.show_help,
'h': self.show_help,
'exit': self.exit,
'e': self.exit,
'quit': self.exit,
'q': self.exit,
't': self.list_templates,
'templates': self.list_templates,
'ssh': self.create_ssh_access,
'connect': self.create_tunnel_to_port,
'analyze': self.analyze_organizations,
'dns': self.analyze_organizations,
'export': self.export_to_csv,
'csv': self.export_to_csv,
'online': self.update_online_machines,
'o': self.update_online_machines,
}
self.running = True
self.current_filters = {}
# Setup command history
self.histfile = os.path.join(os.path.expanduser("~"), ".tunnel_history")
try:
readline.read_history_file(self.histfile)
readline.set_history_length(1000)
except FileNotFoundError:
pass
# Register the history file to be saved on exit
atexit.register(readline.write_history_file, self.histfile)
# Set up tab completion
readline.parse_and_bind('tab: complete')
readline.set_completer(self.complete)
def complete(self, text, state):
"""Tab completion function"""
options = [cmd for cmd in self.commands.keys() if cmd.startswith(text)]
if state < len(options):
return options[state]
else:
return None
def show_help(self, *args):
print("\nAvailable commands:")
print(" p, print [filter] - Print machine list (optional filters)")
print(" Filters:")
print(" -m <machine_name> - Filter by machine name")
print(" -i <ip> - Filter by IP or Public IP")
print(" -mac <mac> - Filter by MAC address")
print(" -s <state> - Filter by state (active/inactive)")
print(" n, next - Show next page")
print(" b, back, prev - Show previous page")
print(" u, update - Update all machine data")
print(" o, online - Update only online machines (more efficient)")
print(" h, help - Show this help")
print(" q, quit, e, exit - Exit the program")
print(" t, templates - List valid tunnel templates")
print(" Options:")
print(" -type <0,1,2,5> - Tunnel type (TCP/HTTP/HTTPS/UDP)")
print(" -t <0,3,254,255> - Template type")
print(" -b <1-24> - Bandwidth in Mbps")
print(" ssh <machine_name> - Create SSH tunnel for a machine")
print(" connect <machine_name> - Create tunnel for a machine to specific port")
print(" Options:")
print(" -port <port number> - Specific port number (mandatory)")
print(" analyze, dns - Perform reverse DNS lookup")
print(" export, csv [filename] - Export all data to CSV file")
print("\nExample: p -m raspberrypi -s active")
def parse_filters(self, args):
filters = {}
i = 0
while i < len(args):
if args[i].startswith('-'):
if i + 1 < len(args):
key = args[i][1:]
value = args[i + 1]
filters[key] = value
i += 2
else:
i += 1
else:
i += 1
return filters
def apply_filters(self, query, filters):
conditions = []
params = []
if 'm' in filters: # machine name filter
conditions.append("name LIKE ?")
params.append(f"%{filters['m']}%")
if 'i' in filters: # IP filter
conditions.append("(ip LIKE ? OR public_ip LIKE ?)")
params.extend([f"%{filters['i']}%", f"%{filters['i']}%"])
if 'mac' in filters: # MAC address filter
conditions.append("mac LIKE ?")
params.append(f"%{filters['mac']}%")
if 's' in filters: # state filter
state_map = {'active': 1, 'inactive': 0}
if filters['s'].lower() in state_map:
conditions.append("is_active = ?")
params.append(state_map[filters['s'].lower()])
if conditions:
query += " WHERE " + " AND ".join(conditions)
return query, params
def get_total_records(self, filters):
query = "SELECT COUNT(*) as count FROM machines"
query, params = self.apply_filters(query, filters)
with sqlite3.connect(self.api.db.db_path) as conn:
result = conn.execute(query, params).fetchone()
return result[0]
def print_tunnels(self, *args):
if args: # If new filters provided, reset to first page
self.current_filters = self.parse_filters(args)
self.current_page = 0
# Only print total count on initial display or filter change
with sqlite3.connect(self.api.db.db_path) as conn:
total_count = conn.execute("SELECT COUNT(*) FROM machines").fetchone()[0]
print(f"Total machines in database: {total_count}")
self.display_current_page(clear_screen=False) # Don't clear on first display
self.handle_pagination()
def display_current_page(self, clear_screen=True):
if clear_screen:
print("\033[2J\033[H", end='')
machines, total_records = self.api.db.get_machines_for_display(
page=self.current_page,
page_size=self.page_size,
filters=self.current_filters
)
total_pages = (total_records + self.page_size - 1) // self.page_size
if not machines:
print("\nNo machines found matching the filters.")
return
print(f"\n=== Current Machines (Page {self.current_page + 1}/{total_pages}) ===")
print(f"{'ID':<36} {'Name':<15} {'OS':<15} {'IP':<15} {'Public IP':<15} {'MAC':<18} {'Organization':<20} {'Status':<10} {'Last Active':<12}")
print("-" * 165)
for machine in machines:
status = "Active" if machine['is_active'] == 1 else "Inactive"
active_time = machine['active_time'].split('T')[0] if machine['active_time'] else 'N/A'
org = machine['dns_hostname'][:19] if machine['dns_hostname'] else 'N/A'
mac = machine['mac'][:17] if machine['mac'] else 'N/A'
os_val = machine['os'][:14] if machine['os'] else 'N/A'
print(f"{machine['id']:<36} {machine['name'][:14]:<15} {os_val:<15} {machine['ip'][:14]:<15} "
f"{machine['public_ip'][:14]:<15} {mac:<18} {org:<20} {status:<10} {active_time:<12}")
print(f"\nShowing {self.current_page * self.page_size + 1}-{min((self.current_page + 1) * self.page_size, total_records)} of {total_records} records")
print("Use 'n' for next page, 'b' for previous page, 'q' to return to command mode", end='\r')
def handle_pagination(self):
with KeyboardListener() as listener:
self.keyboard_listener = listener
while not listener.should_stop:
if sys.stdin in select.select([sys.stdin], [], [], 0.1)[0]:
char = sys.stdin.read(1)
if char == 'n':
self.next_page(silent=True)
elif char == 'b':
self.prev_page(silent=True)
elif char == 'q':
break
def next_page(self, *args, silent=False):
total_records = self.get_total_records(self.current_filters)
total_pages = (total_records + self.page_size - 1) // self.page_size
if self.current_page < total_pages - 1:
self.current_page += 1
if silent:
self.display_current_page()
else:
self.print_tunnels()
else:
print("\rAlready at the last page.", end='')
def prev_page(self, *args, silent=False):
if self.current_page > 0:
self.current_page -= 1
if silent:
self.display_current_page()
else:
self.print_tunnels()
else:
print("\rAlready at the first page.", end='')
def update_data(self, *args):
print("\nStarting data update...")
self.api.start_background_update()
while self.api.is_updating:
try:
status = self.api.update_queue.get(timeout=0.5)
print(status, end='', flush=True)
except queue.Empty:
continue
# After update is complete, automatically show active machines
print("\n\nShowing active machines:")
self.current_filters = {'s': ''}
# self.current_page = 0 # Reset to first page
# self.print_tunnels()
def update_online_machines(self, *args):
"""Update only machines that are currently online"""
print("\nStarting online machines update...")
self.api.start_online_machines_update()
while self.api.is_updating:
try:
status = self.api.update_queue.get(timeout=0.5)
print(status, end='', flush=True)
except queue.Empty:
continue
# After update is complete, automatically show active machines
print("\n\nShowing active machines:")
self.current_filters = {'s': 'active'} # Set filter for active machines
self.current_page = 0 # Reset to first page
self.print_tunnels()
self.current_filters = {'s': ''} # Reset filter
def exit(self, *args):
self.running = False
return True
def run(self):
print("\nWelcome to Bin4rys Unitree Tunnel Manager")
print("Type 'help' for available commands")
while self.running:
try:
cmd = input("\n> ").strip().split()
if not cmd:
continue
command = cmd[0].lower()
args = cmd[1:]
if command in self.commands:
self.commands[command](*args)
else:
print(f"Unknown command: {command}")
print("Type 'help' for available commands")
except KeyboardInterrupt:
print("\nUse 'exit' or 'quit' to exit properly")
except Exception as e:
print(f"Error: {e}")
def list_templates(self, *args):
"""List valid tunnel templates with optional filtering"""
if not args:
print("\nError: Tunnel type is required")
print("Usage: t -type <0,1,2,5> [-t <template_type>] [-b <bandwidth>]")
print("\nTunnel types:")
print(" 0: TCP")
print(" 1: HTTP")
print(" 2: HTTPS")
print(" 5: UDP")
return
# Parse arguments
filters = self.parse_filters(args)
if 'type' not in filters:
print("\nError: Tunnel type (-type) is required")
return
try:
tunnel_type = int(filters['type'])
template_type = int(filters['t']) if 't' in filters else None
bandwidth = int(filters['b']) if 'b' in filters else None
except ValueError:
print("\nError: All parameters must be numbers")
return
response = self.api.list_valid_templates(tunnel_type, template_type, bandwidth)
if response.get('error', 1) != 0:
print(f"\nError fetching templates: {response.get('info', 'Unknown error')}")
return
templates = response.get('data', [])
total = response.get('total', 0)
if not templates:
print("\nNo templates found matching the criteria")
return
print(f"\n=== Valid Templates (Total: {total}) ===")
print(f"{'ID':<36} {'Type':<8} {'Bandwidth':<10} {'Concurrent':<10} {'Domain':<8} {'Expires':<12}")
print("-" * 90)
for template in templates:
template_type = {
0: "Random",
3: "Same",
254: "5¥ Free",
255: "Free"
}.get(template.get('type'), "Unknown")
print(f"{template['id']:<36} "
f"{template_type:<8} "
f"{template.get('bandwidth', 'N/A'):<10} "
f"{template.get('concurrency', 'N/A'):<10} "
f"{('Yes' if template.get('domain') == 1 else 'No'):<8} "
f"{template.get('expiresIn', 'N/A')} days")
def create_ssh_access(self, *args):
"""Create an SSH tunnel for a specific machine"""
if not args:
print("\nError: Machine ID is required")
print("Usage: ssh <machine_id>")
return
# Parse machine ID and options
machine_id = args[0]
template_id = None
# Find the machine
query = "SELECT * FROM machines WHERE id = ?"
with sqlite3.connect(self.api.db.db_path) as conn:
conn.row_factory = sqlite3.Row
machine = conn.execute(query, [machine_id]).fetchone()
if not machine:
print(f"\nNo machine found with ID: {machine_id}")
return
print("\nFetching available TCP templates...")
response = self.api.list_valid_templates(
tunnel_type=0, # TCP
)
# print(response)
# return
if response.get('error', 1) != 0:
print(f"\nError fetching templates: {response.get('info', 'Unknown error')}")
return
templates = response.get('data', [])
if not templates:
print("\nNo valid TCP templates found. Will create a new template.")
else:
template_id = templates[0]['id'] # Use first available template
# Create the tunnel