Skip to content

Commit

Permalink
Fix #102. Complete DNSCryptProxy integration in Windows. Beta4 tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
freQniK committed Aug 1, 2024
1 parent a9ab182 commit f62f02f
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 3 deletions.
102 changes: 102 additions & 0 deletions src/adapters/DNSCryptproxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import subprocess
from typedef.konstants import Arch, ConfParams
from os import chdir, path
import platform
import multiprocessing
from multiprocessing import Process
from time import sleep

class HandleDNSCryptProxy():
dnscrypt_start = """
$dnscrypt_proxy_executable = "%s"
$dnscrypt_proxy_config = "%s"
Start-Process -FilePath $dnscrypt_proxy_executable -ArgumentList "-config `"$dnscrypt_proxy_config`"" -NoNewWindow -Wait
"""
dnscrypt_stop = 'Stop-Process -Name "dnscrypt-proxy" -Force'

dns_ps1 = """$interfaces = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }
foreach ($interface in $interfaces) {
Set-DnsClientServerAddress -InterfaceIndex $interface.ifIndex -ServerAddresses %s
Set-DnsClientServerAddress -InterfaceIndex $interface.ifIndex -ServerAddresses %s
}
Get-DnsClientServerAddress
"""
lh = "127.0.0.1"
lhv6 = "::1"
cf = "1.1.1.1"
cfv6 = "2606:4700:4700::1111"
dnscrypt_pid = 0
dnscryptcmd = None

def __init__(self, default: str = "1.1.1.1", defaultv6: str = "2606:4700:4700::1111"):
self.default = default
self.defaultv6 = defaultv6
pass

def fork_dnscrypt(self):
dnscryptcmd = f"gsudo.exe powershell.exe {self.dnscryptcmd}"
process = subprocess.Popen(dnscryptcmd, shell=True,close_fds=True)
print(f"DNSCryptProxy: {process.pid}")
self.dnscrypt_pid = process.pid

# Be sure to run in thread or concurent.futures
def dnscrypt(self, state: bool = False):
chdir(ConfParams.BASEBINDIR)
dnscrypt_proxy_executable = path.join(ConfParams.BASEBINDIR, 'dnscrypt-proxy.exe')
dnscrypt_proxy_config = path.join(ConfParams.KEYRINGDIR, 'dnscrypt-proxy.toml')
pltfrm = platform.system()
if state == True:
if pltfrm == Arch.WINDOWS:
dnscrypt_start = self.dnscrypt_start % (dnscrypt_proxy_executable, dnscrypt_proxy_config)
dns_bat = self.dns_ps1 % (self.lh, self.lhv6)
print(dns_bat)
print(dnscrypt_start)
#dnscryptcmd = f'"{dnscrypt_proxy_executable}" -config "{dnscrypt_proxy_config}"'
with open("change_dns.ps1", "w") as DNSFILE:
DNSFILE.write(f"{dns_bat}\n")
DNSFILE.write(f"{dnscrypt_start}")


print("Startin DNScrypt-proxy...", end=' ')
self.dnscryptcmd = path.join(ConfParams.BASEBINDIR, "change_dns.ps1")
multiprocessing.get_context('spawn')
fork = Process(target=self.fork_dnscrypt)
fork.run()
sleep(1.5)


# TODO: Linux/osx


else:
if pltfrm == Arch.WINDOWS:
kill_dns_crypt = "change_dns.ps1"
if self.default != self.cf:
dns_bat = self.dns_ps1 % (self.default, self.defaultv6)
else:
dns_bat = self.dns_ps1 % (self.cf, self.cfv6)
with open(kill_dns_crypt, "w") as DNSFILE:
DNSFILE.write(f"{dns_bat}\n")
DNSFILE.write(f"{self.dnscrypt_stop}")


print(dns_bat)
print(self.dnscrypt_stop)
print("Killing dnscrypt-proxy.exe...")
self.dnscryptcmd = path.join(ConfParams.BASEBINDIR, "change_dns.ps1")
end_daemon_cmd = [ConfParams.GSUDO, "powershell.exe", self.dnscryptcmd]
proc2 = subprocess.Popen(end_daemon_cmd, shell=True)
proc2.wait(timeout=30)
proc_out,proc_err = proc2.communicate()

#dnscmd = [ConfParams.GSUDO, path.join(ConfParams.BASEBINDIR, "change_dns.bat")]
#process = subprocess.Popen(dnscmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.dnscrypt_pid = 0
# TODO: Linux/osx

chdir(ConfParams.KEYRINGDIR)



Empty file modified src/bin/localhost.pem
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions src/helpers/bandwidth.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import timedelta, datetime
import psutil
import re

def compute_consumed_data(consumed):
if "GB" in consumed:
Expand Down
2 changes: 2 additions & 0 deletions src/typedef/konstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Arch():
class ConfParams():
PATH = environ['PATH']
KEYRINGDIR = path.join(path.expanduser('~'), '.meile-gui')
BASEBINDIR = path.join(KEYRINGDIR, "bin")
GSUDO = path.join(BASEBINDIR, "gsudo.exe")
BASEDIR = path.join(path.expanduser('~'), '.sentinelcli')
WALLETINFO = path.join(KEYRINGDIR, "infos.txt")
SUBSCRIBEINFO = path.join(KEYRINGDIR, "subscribe.infos")
Expand Down
57 changes: 54 additions & 3 deletions src/ui/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from cli.v2ray import V2RayHandler
from fiat.stripe_pay import scrtsxx
from adapters.ChangeDNS import ChangeDNS
from adapters.DNSCryptproxy import HandleDNSCryptProxy as dcp
from helpers.helpers import format_byte_size
from helpers.bandwidth import compute_consumed_data, compute_consumed_hours, init_GetConsumedWhileConnected, GetConsumedWhileConnected

Expand Down Expand Up @@ -339,6 +340,7 @@ class MainWindow(Screen):
ip = ""
CONNECTED = None
warpd = False
dnscrypt = False
warpd_disconnected = True
NodeTree = None
SubResult = None
Expand Down Expand Up @@ -686,11 +688,42 @@ def menu_callback(self, selection):
self.start_dnscrypt()
elif selection == self.MenuOptions[4]:
self.disconnect_from_node()
if self.dnscrypt:
dnsproxy = dcp()
dnsproxy.dnscrypt(state=False)
sys.exit(0)

@delayable
def start_dnscrypt(self):
pass

dnsproxy = dcp()
if not self.dnscrypt:
self.add_loading_popup("Starting DNSCryptProxy with user selected resolvers...")
yield 1.3
t = Thread(target=lambda: dnsproxy.dnscrypt(state=True))
t.start()

while t.is_alive():
print(".", end="")
yield 0.5

self.dnscrypt = True
self.remove_loading_widget(None)
self.display_dnscrypt_success(dnsproxy.dnscrypt_pid)

else:
self.add_loading_popup("Terminating DNSCryptProxy...")
yield 1.3

t = Thread(target=lambda: dnsproxy.dnscrypt(state=False))
t.start()

while t.is_alive():
print(".", end="")
yield 0.5

self.dnscrypt = False
self.remove_loading_widget(None)

def build(self, dt):
# Check to build Map
self.build_meile_map()
Expand Down Expand Up @@ -866,7 +899,25 @@ def display_warp_success(self):
MDRaisedButton(
text="Okay",
theme_text_color="Custom",
text_color=(1,1,1,1),
text_color=MeileColors.BLACK,
on_release=self.remove_loading_widget
),
],
)
self.dialog.open()


@mainthread
def display_dnscrypt_success(self, pid):

self.dialog = MDDialog(
text=f"You are now using DoH (DNS-over-HTTPS) and your DNS traffic is encrypted from prying eyes. {pid}",
md_bg_color=get_color_from_hex(MeileColors.BLACK),
buttons=[
MDRaisedButton(
text="Okay",
theme_text_color="Custom",
text_color=MeileColors.BLACK,
on_release=self.remove_loading_widget
),
],
Expand Down

0 comments on commit f62f02f

Please sign in to comment.