Skip to content

Commit 7cf3af9

Browse files
freQniKMathNodes
authored andcommitted
Update wait_for_tunnel helper. Update wait_for_port helper. Update connect() for new helpers. Add Quad9 and localhost to hardcoded DNS servers. Update unified v2ray handler
1 parent 317612f commit 7cf3af9

4 files changed

Lines changed: 51 additions & 50 deletions

File tree

src/cli/v2ray.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import psutil
2727
from typedef.konstants import ConfParams
2828
import threading
29+
import threading
2930

3031
# ---------------------------------------------------------------------------
3132
# V2RayHandler – one class per platform, selected at the bottom of this

src/cli/wallet.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ def fetch_credentials(self, address, session_id, type, conndesc):
829829
return None, None, None
830830

831831
def write_wireguard_config(self, response, decode, wgkey, conndesc, iface):
832+
CONFIG = MeileConfig.read_configuration(MeileConfig.CONFFILE)
833+
user_dns = CONFIG['network'].get('dns', '9.9.9.9')
832834
if len(decode) < 100:
833835
self.connected = {
834836
"v2ray_pid": None,
@@ -884,7 +886,7 @@ def write_wireguard_config(self, response, decode, wgkey, conndesc, iface):
884886
config.set("Interface", "PrivateKey", wgkey.privkey)
885887
config.set(
886888
"Interface", "DNS",
887-
",".join(["127.0.0.1", "1.0.0.1", "1.1.1.1"])
889+
",".join([user_dns, "1.0.0.1", "1.1.1.1"])
888890
)
889891

890892
config.add_section("Peer")
@@ -1224,46 +1226,39 @@ def connect(self,
12241226
if pltfrm == Arch.LINUX:
12251227
child = pexpect.spawn(f"pkexec sh -c 'ip link delete {iface}; wg-quick up {config_file}'")
12261228
child.expect(pexpect.EOF)
1227-
ok = wait_for_tunnel_iface(iface=["wg99", "utun3"], timeout=300)
1228-
if ok is None:
1229-
self.connected = {"v2ray_pid" : None,
1230-
"result": False,
1231-
"status" : "Error bringing up wireguard interface",
1232-
"session_id" : session_id}
1233-
return
1229+
12341230
elif pltfrm == Arch.OSX:
12351231
connectBASH = [sentinel_connect_bash]
12361232
proc2 = subprocess.Popen(connectBASH)
12371233
proc2.wait(timeout=30)
12381234
pid2 = proc2.pid
12391235
proc_out, proc_err = proc2.communicate()
1240-
ok = wait_for_tunnel_iface(iface=["wg99", "utun3"], timeout=300)
1241-
if ok is None:
1242-
self.connected = {"v2ray_pid" : None,
1243-
"result": False,
1244-
"status" : "Error bringing up wireguard interface",
1245-
"session_id" : session_id}
1246-
return
1236+
12471237
elif pltfrm == Arch.WINDOWS:
12481238
wgup = [gsudo, MeileConfig.WIREGUARD_BIN, "/installtunnelservice", config_file]
12491239
wg_process = subprocess.Popen(wgup)
1250-
ok = wait_for_tunnel_iface(iface=["wg99", "utun3"], timeout=300)
1251-
if ok is None:
1252-
self.connected = {"v2ray_pid" : None,
1253-
"result": False,
1254-
"status" : "Error bringing up wireguard interface",
1255-
"session_id" : session_id}
1256-
return
12571240

1258-
if psutil.net_if_addrs().get(iface) or psutil.net_if_addrs().get("utun3"):
1241+
1242+
#if psutil.net_if_addrs().get(iface) or psutil.net_if_addrs().get("utun3"):
1243+
tuniface = wait_for_tunnel_iface(iface=[iface, "utun3"], timeout=30)
1244+
if tuniface is not None:
12591245
self.connected = {"v2ray_pid" : None,
12601246
"result": True,
1261-
"status" : iface,
1247+
"status" : tuniface,
12621248
"session_id" : session_id}
12631249
conndesc.write("Checking network connection...\n")
12641250
conndesc.flush()
1265-
sleep(1)
1266-
self.get_ip_address()
1251+
sleep(2)
1252+
if self.get_ip_address():
1253+
self.connected = {"v2ray_pid" : None,
1254+
"result": True,
1255+
"status" : tuniface,
1256+
"session_id" : session_id}
1257+
else:
1258+
self.connected = {"v2ray_pid" : None,
1259+
"result": False,
1260+
"status" : "Error establising connection to internet. Try a differnt node.",
1261+
"session_id" : session_id}
12671262
sleep(1)
12681263
conndesc.close()
12691264
return
@@ -1303,7 +1298,8 @@ def connect(self,
13031298
if pltfrm == Arch.OSX:
13041299
chdir(MeileConfig.BASEDIR)
13051300
return
1306-
1301+
1302+
'''
13071303
if pltfrm != Arch.OSX:
13081304
for iface in psutil.net_if_addrs().keys():
13091305
if "tun" in iface:
@@ -1317,12 +1313,16 @@ def connect(self,
13171313
"session_id" : session_id}
13181314
print(self.connected)
13191315
tuniface = True
1320-
1321-
if tuniface is True:
1322-
print(self.connected)
1316+
'''
1317+
1318+
tuniface = wait_for_tunnel_iface(iface=["tun", "utun3", "utun123"], timeout=30)
1319+
1320+
1321+
if tuniface is not None:
1322+
print("Tunnel interface is up:", tuniface)
13231323
conndesc.write("Checking network connection...\n")
13241324
conndesc.flush()
1325-
sleep(1)
1325+
sleep(3.3)
13261326
if self.get_ip_address():
13271327
self.connected = {"v2ray_pid" : v2ray_handler.v2ray_pid,
13281328
"result": True,

src/helpers/helpers.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import ipaddress
33
import socket
44
import time
5+
import psutil
56

67

78
def format_byte_size(size, decimals=2, binary_system=True):
@@ -45,31 +46,28 @@ def natural_gateway(ip_with_prefix: str) -> str:
4546
net64 = ipaddress.ip_network(f"{iface.ip.exploded}/64", strict=False)
4647
return str(ipaddress.ip_address(int(net64.network_address) + 1))
4748

48-
def wait_for_port(host, port, timeout=300, poll=0.2):
49-
deadline = time.monotonic() + timeout
50-
while time.monotonic() < deadline:
51-
try:
52-
with socket.create_connection((host, port), timeout=0.5):
49+
def wait_for_port(host, port, timeout=120, poll=0.2):
50+
deadline = time.monotonic() + timeout
51+
while time.monotonic() < deadline:
52+
for c in psutil.net_connections(kind="tcp"):
53+
if c.status == psutil.CONN_LISTEN and c.laddr.port == port:
54+
# optional: also match host
55+
if host in ("0.0.0.0", "", c.laddr.ip):
5356
return True
54-
except OSError:
55-
time.sleep(poll)
56-
return False
57+
time.sleep(poll)
58+
return False
5759

58-
def wait_for_tunnel_iface(iface=None, timeout=300, poll=0.2):
60+
def wait_for_tunnel_iface(iface=None, timeout=30, poll=0.2):
5961
if not iface:
6062
raise ValueError("iface must be a non-empty list")
6163

62-
wanted = set(iface)
6364
deadline = time.monotonic() + timeout
6465

6566
while time.monotonic() < deadline:
66-
present = {name for _, name in socket.if_nameindex()}
67-
found = wanted & present
68-
if found:
69-
# if multiple matched, prefer the order the caller gave
70-
for name in iface:
71-
if name in found:
72-
return name
67+
for tunface in psutil.net_if_addrs().keys():
68+
for intface in iface:
69+
if tunface.startswith(intface):
70+
return tunface
7371
time.sleep(poll)
7472

7573
return None

src/typedef/konstants.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class HTTParams():
8383
"https://cache.noncompliance.org/api/public/card/bc75f719-db4a-44b8-9688-f5793742a203/query/json"
8484
]
8585

86-
DNSSERVERS = [{'Country' : 'Mexico-1' , 'ip' : '207.248.224.71'},
86+
DNSSERVERS = [{'Country' : 'DNSCrypt' , 'ip' : '127.0.0.1'},
87+
{'Country' : 'Mexico-1' , 'ip' : '207.248.224.71'},
8788
{'Country' : 'Mexico-2' , 'ip' : '200.56.98.145'},
8889
{'Country' : 'U.S.-1' , 'ip' : '75.150.197.154'},
8990
{'Country' : 'U.S.-2' , 'ip' : '98.101.194.137'},
@@ -92,7 +93,8 @@ class HTTParams():
9293
{'Country' : 'Japan' , 'ip' : '153.156.93.5'},
9394
{'Country' : 'Russia' , 'ip' : '77.88.8.8'},
9495
{'Country' : 'Indonesia' , 'ip' : '103.184.98.227'},
95-
{'Country' : 'Cloudflare' , 'ip' : '1.1.1.1'}
96+
{'Country' : 'Cloudflare' , 'ip' : '1.1.1.1'},
97+
{'Country' : 'Quad9' , 'ip' : '9.9.9.9'}
9698
]
9799

98100

0 commit comments

Comments
 (0)