MathNodes / meile-gui

Meile dVPN GUI for Linux, OS X, and Windows - Powered by the Sentinel Network
https://meile.app
GNU General Public License v3.0
34 stars 6 forks source link

[0.315 XMR BOUNTY] Make platform specific change_dns() routine #47

Closed MathNodes closed 1 year ago

MathNodes commented 1 year ago

In src/ui/widgets.py the class RecycleViewSubRow contains a routine change_dns()

    @delayable        
    def change_dns(self):
        MeileConfig = MeileGuiConfig()
        RESOLVFILE = path.join(MeileConfig.BASEDIR, "dns")
        DNSFILE = open(RESOLVFILE, 'w')
        mw = Meile.app.root.get_screen(WindowNames.MAIN_WINDOW)

        DNSFILE.write('nameserver 1.1.1.1')
        DNSFILE.flush()
        DNSFILE.close()

        yield 0.6
        if self.dialog:
            self.dialog.dismiss()
        self.add_loading_popup("DNS Resolver error... Switching to Cloudflare")
        yield 2.6

        # Linux
        dnsCMD = "pkexec bash -c 'cat %s | resolvconf -a wg99 && resolvconf -u'" % RESOLVFILE

        try: 
            dnsPROC = Popen(dnsCMD, shell=True)
            dnsPROC.wait(timeout=60)
        except TimeoutExpired as e:
            print(str(e))
            pass

        proc_out,proc_err = dnsPROC.communicate()

        yield 1.2
        mw.get_ip_address(None)
        self.remove_loading_widget()

I want the primary function of changing the DNS system resolver to be platform specific. If OS X, then handle OS X DNS Changing; if Windows - change the DNS of the adapter, if Linux - you can copy the code in this routine to use for linux.

Ideally, there should be a class called ChangeDNS with a change() routine that includes os.platform checks and properly handles the changing of the system name server.

Keep the yield, self and mw lines in this function and call the ChangeDNS class to make a system DNS change.

MathNodes commented 1 year ago

you can use the CloudFlare DNS 1.1.1.1 for all OSes

Tkd-Alex commented 1 year ago

The change dns function currently is working on all branches / for each dedicated os? We need just to wrap in a class and handle the change based on the os?

freQniK commented 1 year ago

It doesn't work on Windows and I had issues with OS X. I forgot the exact command to change DNS on OS X and im not at my box to look it up right now.

So far the code only works on Linux. You can submit the pull request to any branch and I'll merge it to the rest.

For Windows, I believe you can run a power shell command to change the DNS of the tunnel interface. You may have to query the interface first. There is a routine in sentinel.py that searches the interface names in the Windows branch.

Can't be much more help now until.I get to a computer tomorrow.

Tkd-Alex commented 1 year ago

For Windows, I believe you can run a power shell command to change the DNS of the tunnel interface. You may have to query the interface first. There is a routine in sentinel.py that searches the interface names in the Windows branch.

I can't find it :disappointed:

MathNodes commented 1 year ago

For Windows, I believe you can run a power shell command to change the DNS of the tunnel interface. You may have to query the interface first. There is a routine in sentinel.py that searches the interface names in the Windows branch.

I can't find it 😞

My bad. It was in wallet.py

for iface in psutil.net_if_addrs().keys():
  if "tun" in iface:
      TUNIFACE = True
      break

And to answer an earlier question, I think you can add the ChangeDNS class to src/adapters

Tkd-Alex commented 1 year ago

I've done a POC and the class was created under the utils folder. Btw, it's not a problem to move between the folder

The Linux version use resolv conf The Mac OSX use networksetup

For windows should be netsh 🤔

I don't know if I'm in the right direction

freQniK commented 1 year ago

Linux and OS X is right. I honestly dont know much about Windows, but I think netsh is the right command for my previous readings.

FYI, I thought I recall that for windows the DNS is set per network device. For wireguard this is the wg99 device created by th3 Wireguard exe. For v2ray, Meile creates a random tunnel interface, tunXXXX, where X is a number 0-9. It should be the only tunnel interface so that code I sent should work to properly identify the nic.

Tkd-Alex commented 1 year ago

This is what I have done: https://github.com/Tkd-Alex/meile-gui/commit/3ae5411146649b4ec577182e6f3d9179403a9528#diff-de32b1c0d23df2b47345bebc87cfcb1e2fbe9579aef5b6e6b8276bc2f07e1d4c

freQniK commented 1 year ago

Line 52 has a typo it. You have self.dn I think you mean self.dns

As for Windows I think you shouldn't execute a netsh for each interface. This will cause a lot gsudo executions which will be quite annoying to the user. Instead match the keys with

if "tun" in interface or "wg99" in interface:

Then run the Popen command

Other than that it looks really good!!!

freQniK commented 1 year ago

Change those things and make a pull request and I'll fulfill the bounty.

Thanks a ton!

freQniK commented 1 year ago

Also, networksetup for OS X can be run as the user as far as I have done it. If not I'll wrap it in an osascript and run that.

Tkd-Alex commented 1 year ago

Line 52 has a typo it. You have self.dn I think you mean self.dns

As for Windows I think you shouldn't execute a netsh for each interface. This will cause a lot gsudo executions which will be quite annoying to the user. Instead match the keys with

if "tun" in interface or "wg99" in interface:

Then run the Popen command

Other than that it looks really good!!!

We should do the same for OSX? Filter only tun and wg99?

freQniK commented 1 year ago

Yes we should. It is only tunX on OS X for either wireguard or v2ray.

Tkd-Alex commented 1 year ago

Yes we should. It is only tunX on OS X for either wireguard or v2ray.

I can't find any tunX here 😕

image

freQniK commented 1 year ago

Oh. utunX

Tkd-Alex commented 1 year ago

Oh. utunX

I'm not sure it's possible to change DNS for utun interfaces. I think the only allowed values are those shown in listinterfaces

https://developer.apple.com/forums/thread/727982

freQniK commented 1 year ago

ok. I already had this in the os/x repo. You'll want to do

networksetup -setdnsservers Wi-Fi dns

Where dns is a variable as you have (1.1.1.1 for now until I add in settings to change)

I don't have a Mac with an ethernet port so i think it is just:

networksetup -setdnsservers Ethernet dns

Would be nice to check if they are using Wi-Fi or Ethernet, but for now just place those two commands in your code. It doesn't require Admin privileges, so we good.

With that commit, I think the pull request will be complete.

freQniK commented 1 year ago

I confirmed this works on my system when connected to Meile

Tkd-Alex commented 1 year ago

ok. I already had this in the os/x repo. You'll want to do

networksetup -setdnsservers Wi-Fi dns

Where dns is a variable as you have (1.1.1.1 for now until I add in settings to change)

I don't have a Mac with an ethernet port so i think it is just:

networksetup -setdnsservers Ethernet dns

Would be nice to check if they are using Wi-Fi or Ethernet, but for now just place those two commands in your code. It doesn't require Admin privileges, so we good.

With that commit, I think the pull request will be complete.

My code firstly got all the interface and then trough iteration set the DNS for each device, we don't need to add the the two commands for WiFi and Ethernet, as you can see from my previous screen the devices in my case are WiFi, Ax-* etc

freQniK commented 1 year ago

Okay. Very good. I'll review it and merge and then send the XMR if all is gravy.