Skip to content

Commit 8394da7

Browse files
committed
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 8182c0e commit 8394da7

5 files changed

Lines changed: 75 additions & 64 deletions

File tree

src/bin/routes.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ else
3434
ifconfig utun123 198.18.0.1 198.18.0.1 down
3535
pkill -9 tun2socks
3636
pkill -9 xray
37-
fi
37+
fi

src/cli/v2ray.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
elif sys.platform.startswith('linux'):
2626
import psutil
2727
from typedef.konstants import ConfParams
28+
import threading
2829

2930
# ---------------------------------------------------------------------------
3031
# V2RayHandler – one class per platform, selected at the bottom of this
@@ -56,6 +57,7 @@ def fork_v2ray(self):
5657

5758

5859
def start_daemon(self):
60+
'''
5961
6062
print("Starting v2ray service...")
6163
@@ -67,6 +69,32 @@ def start_daemon(self):
6769
return True
6870
else:
6971
return False
72+
'''
73+
print("Starting v2ray service...")
74+
75+
try:
76+
self.fork_v2ray()
77+
except Exception as e:
78+
print(f"[start_daemon] fork_v2ray failed: {e!r}")
79+
return False
80+
81+
result = {"ok": False}
82+
83+
def worker():
84+
try:
85+
result["ok"] = wait_for_port("127.0.0.1", 1080, timeout=120)
86+
except Exception as e:
87+
print(f"[start_daemon] worker error: {e!r}")
88+
result["ok"] = False
89+
90+
t = threading.Thread(target=worker, daemon=True)
91+
t.start()
92+
93+
while t.is_alive():
94+
print(".", end="", flush=True)
95+
time.sleep(0.3)
96+
97+
return result["ok"]
7098

7199
def kill_daemon(self):
72100
v2ray_daemon_cmd = (

src/cli/wallet.py

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,8 @@ def fetch_credentials(self, address, session_id, type, conndesc):
844844
return None, None, None
845845

846846
def write_wireguard_config(self, response, decode, wgkey, conndesc, iface):
847+
CONFIG = MeileConfig.read_configuration(MeileConfig.CONFFILE)
848+
user_dns = CONFIG['network'].get('dns', '9.9.9.9')
847849
if len(decode) < 100:
848850
self.connected = {
849851
"v2ray_pid": None,
@@ -899,7 +901,7 @@ def write_wireguard_config(self, response, decode, wgkey, conndesc, iface):
899901
config.set("Interface", "PrivateKey", wgkey.privkey)
900902
config.set(
901903
"Interface", "DNS",
902-
",".join(["127.0.0.1", "1.0.0.1", "1.1.1.1"])
904+
",".join([user_dns, "1.0.0.1", "1.1.1.1"])
903905
)
904906

905907
config.add_section("Peer")
@@ -1218,46 +1220,39 @@ def connect(self,
12181220
if pltfrm == Arch.LINUX:
12191221
child = pexpect.spawn(f"pkexec sh -c 'ip link delete {iface}; wg-quick up {config_file}'")
12201222
child.expect(pexpect.EOF)
1221-
ok = wait_for_tunnel_iface(iface=["wg99", "utun3"], timeout=300)
1222-
if ok is None:
1223-
self.connected = {"v2ray_pid" : None,
1224-
"result": False,
1225-
"status" : "Error bringing up wireguard interface",
1226-
"session_id" : session_id}
1227-
return
1223+
12281224
elif pltfrm == Arch.OSX:
12291225
connectBASH = [sentinel_connect_bash]
12301226
proc2 = subprocess.Popen(connectBASH)
12311227
proc2.wait(timeout=30)
12321228
pid2 = proc2.pid
12331229
proc_out, proc_err = proc2.communicate()
1234-
ok = wait_for_tunnel_iface(iface=["wg99", "utun3"], timeout=300)
1235-
if ok is None:
1236-
self.connected = {"v2ray_pid" : None,
1237-
"result": False,
1238-
"status" : "Error bringing up wireguard interface",
1239-
"session_id" : session_id}
1240-
return
1230+
12411231
elif pltfrm == Arch.WINDOWS:
12421232
wgup = [gsudo, MeileConfig.WIREGUARD_BIN, "/installtunnelservice", config_file]
12431233
wg_process = subprocess.Popen(wgup)
1244-
ok = wait_for_tunnel_iface(iface=["wg99", "utun3"], timeout=300)
1245-
if ok is None:
1246-
self.connected = {"v2ray_pid" : None,
1247-
"result": False,
1248-
"status" : "Error bringing up wireguard interface",
1249-
"session_id" : session_id}
1250-
return
12511234

1252-
if psutil.net_if_addrs().get(iface) or psutil.net_if_addrs().get("utun3"):
1235+
1236+
#if psutil.net_if_addrs().get(iface) or psutil.net_if_addrs().get("utun3"):
1237+
tuniface = wait_for_tunnel_iface(iface=[iface, "utun3"], timeout=30)
1238+
if tuniface is not None:
12531239
self.connected = {"v2ray_pid" : None,
12541240
"result": True,
1255-
"status" : iface,
1241+
"status" : tuniface,
12561242
"session_id" : session_id}
12571243
conndesc.write("Checking network connection...\n")
12581244
conndesc.flush()
1259-
sleep(1)
1260-
self.get_ip_address()
1245+
sleep(2)
1246+
if self.get_ip_address():
1247+
self.connected = {"v2ray_pid" : None,
1248+
"result": True,
1249+
"status" : tuniface,
1250+
"session_id" : session_id}
1251+
else:
1252+
self.connected = {"v2ray_pid" : None,
1253+
"result": False,
1254+
"status" : "Error establising connection to internet. Try a differnt node.",
1255+
"session_id" : session_id}
12611256
sleep(1)
12621257
conndesc.close()
12631258
return
@@ -1297,26 +1292,14 @@ def connect(self,
12971292
if pltfrm == Arch.OSX:
12981293
chdir(MeileConfig.BASEDIR)
12991294
return
1300-
1301-
if pltfrm != Arch.OSX:
1302-
for iface in psutil.net_if_addrs().keys():
1303-
if "tun" in iface:
1304-
tuniface = True
1305-
break
1306-
else:
1307-
if psutil.net_if_addrs().get("utun123"):
1308-
self.connected = {"v2ray_pid" : v2ray_handler.v2ray_pid,
1309-
"result": True,
1310-
"status" : "utun123",
1311-
"session_id" : session_id}
1312-
print(self.connected)
1313-
tuniface = True
1314-
1315-
if tuniface is True:
1316-
print(self.connected)
1295+
1296+
tuniface = wait_for_tunnel_iface(iface=["tun", "utun3", "utun123"], timeout=30)
1297+
1298+
if tuniface is not None:
1299+
print("Tunnel interface is up:", tuniface)
13171300
conndesc.write("Checking network connection...\n")
13181301
conndesc.flush()
1319-
sleep(1)
1302+
sleep(3.3)
13201303
if self.get_ip_address():
13211304
self.connected = {"v2ray_pid" : v2ray_handler.v2ray_pid,
13221305
"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):
@@ -30,31 +31,28 @@ def resolve_address(addr):
3031
return socket.gethostbyname(addr)
3132

3233

33-
def wait_for_port(host, port, timeout=300, poll=0.2):
34-
deadline = time.monotonic() + timeout
35-
while time.monotonic() < deadline:
36-
try:
37-
with socket.create_connection((host, port), timeout=0.5):
34+
def wait_for_port(host, port, timeout=120, poll=0.2):
35+
deadline = time.monotonic() + timeout
36+
while time.monotonic() < deadline:
37+
for c in psutil.net_connections(kind="tcp"):
38+
if c.status == psutil.CONN_LISTEN and c.laddr.port == port:
39+
# optional: also match host
40+
if host in ("0.0.0.0", "", c.laddr.ip):
3841
return True
39-
except OSError:
40-
time.sleep(poll)
41-
return False
42+
time.sleep(poll)
43+
return False
4244

43-
def wait_for_tunnel_iface(iface=None, timeout=300, poll=0.2):
45+
def wait_for_tunnel_iface(iface=None, timeout=30, poll=0.2):
4446
if not iface:
4547
raise ValueError("iface must be a non-empty list")
4648

47-
wanted = set(iface)
4849
deadline = time.monotonic() + timeout
4950

5051
while time.monotonic() < deadline:
51-
present = {name for _, name in socket.if_nameindex()}
52-
found = wanted & present
53-
if found:
54-
# if multiple matched, prefer the order the caller gave
55-
for name in iface:
56-
if name in found:
57-
return name
52+
for tunface in psutil.net_if_addrs().keys():
53+
for intface in iface:
54+
if tunface.startswith(intface):
55+
return tunface
5856
time.sleep(poll)
5957

6058
return None

src/typedef/konstants.py

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

91-
DNSSERVERS = [{'Country' : 'Mexico-1' , 'ip' : '207.248.224.71'},
91+
DNSSERVERS = [{'Country' : 'DNSCrypt' , 'ip' : '127.0.0.1'},
92+
{'Country' : 'Mexico-1' , 'ip' : '207.248.224.71'},
9293
{'Country' : 'Mexico-2' , 'ip' : '200.56.98.145'},
9394
{'Country' : 'U.S.-1' , 'ip' : '75.150.197.154'},
9495
{'Country' : 'U.S.-2' , 'ip' : '98.101.194.137'},
@@ -97,7 +98,8 @@ class HTTParams():
9798
{'Country' : 'Japan' , 'ip' : '153.156.93.5'},
9899
{'Country' : 'Russia' , 'ip' : '77.88.8.8'},
99100
{'Country' : 'Indonesia' , 'ip' : '103.184.98.227'},
100-
{'Country' : 'Cloudflare' , 'ip' : '1.1.1.1'}
101+
{'Country' : 'Cloudflare' , 'ip' : '1.1.1.1'},
102+
{'Country' : 'Quad9' , 'ip' : '9.9.9.9'}
101103
]
102104

103105

0 commit comments

Comments
 (0)