Created
June 24, 2022 15:16
-
-
Save GJCav/7106375cf1c93fa31d0ac391e531e13d to your computer and use it in GitHub Desktop.
dns_switcher: quickly switch dns, written with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CONFIG_FILE = "dnsconfig.json" | |
| # BACKUP_FILE = "dnsbackup.json" | |
| """ config file template | |
| [ | |
| { | |
| "name": "default", | |
| "v4": null, | |
| "v6": null | |
| }, | |
| { | |
| "name": "1.1.1.1", | |
| "v4": ["1.1.1.1", "1.0.0.1"], | |
| "v6": ["2606:4700:4700::1111", "2606:4700:4700::1001"] | |
| } | |
| ] | |
| """ | |
| """ UAC 提权脚本 (pwsh) | |
| """ | |
| import ctypes | |
| import sys | |
| def isAdmin(): | |
| try: | |
| return ctypes.windll.shell32.IsUserAnAdmin() | |
| except Exception as e: | |
| print("ERROR: " + e) | |
| sys.exit(-1) | |
| if not isAdmin(): | |
| ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1) | |
| sys.exit(-2) | |
| import os.path as Path | |
| import os | |
| import json as Json | |
| from typing import Tuple | |
| from wmi import WMI | |
| curDir = Path.dirname(__file__) | |
| configFile = Path.join(curDir, CONFIG_FILE) | |
| # backupFile = Path.join(curDir, BACKUP_FILE) | |
| if not Path.exists(configFile): | |
| print("DNS config not exist, creating an empty one..") | |
| with open(configFile, "w") as f: | |
| f.write("[]") | |
| # if not Path.exists(backupFile): | |
| # with open(backupFile, "w") as f: | |
| # f.write("{}") | |
| config = [] | |
| with open(configFile, "r") as f: | |
| config = Json.load(f) | |
| # backup = {} | |
| # with open(backupFile, "r") as f: | |
| # backup = Json.load(f) | |
| class Win32NetAdapterConfig: | |
| def __init__(self, a) -> None: | |
| self.a = a | |
| def Caption(self) -> str: | |
| return self.a.Caption | |
| def Description(self) -> str: | |
| return self.a.Description | |
| def DNSServerSearchOrder(self) -> Tuple[str] or None: | |
| return self.a.DNSServerSearchOrder | |
| def setDNS(self, conf): | |
| a = self.a | |
| if "v4" in conf: | |
| r = a.SetDNSServerSearchOrder(conf["v4"] or []) | |
| if r[0] != 0: | |
| print(f"unable to set IPv4 DNS, error: {r}") | |
| if "v6" in conf: | |
| dnsList = conf["v6"] or [None] | |
| os.system( | |
| f"netsh int ipv6 set dns name={a.InterfaceIndex} " | |
| + f"source=static {dnsList[0]} validate=no" | |
| ) | |
| for i in range(1, len(dnsList)): | |
| os.system(f"netsh int ipv6 add dns name={a.InterfaceIndex} {dnsList[i]} validate=no") | |
| def selectIndex(st, ed): | |
| while True: | |
| try: | |
| a = input("Select: ") | |
| a = int(a) | |
| if st <= a <= ed: | |
| return a | |
| except: | |
| continue | |
| wSvr = WMI() | |
| adapters = wSvr.Win32_NetworkAdapterConfiguration(IPEnabled=True) | |
| adapters = [Win32NetAdapterConfig(e) for e in adapters] | |
| print("\nAvailable NICs: ") | |
| for i in range(len(adapters)): | |
| print(f" {i+1:2}. {adapters[i].Description()}") | |
| idx = selectIndex(1, len(adapters)) | |
| nic = adapters[idx-1] | |
| optionList = config | |
| print("\nAvailable DNS configs: ") | |
| for i in range(len(optionList)): | |
| print(f" {i+1:2}. {optionList[i]['name']}") | |
| idx = selectIndex(1, len(optionList)) | |
| nic.setDNS(optionList[idx-1]) | |
| os.system("ipconfig /flushdns") | |
| os.system("pause") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment