-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (109 loc) · 3.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"net"
"os/exec"
"strings"
"syscall"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
type Server struct {
PreferredDNS string
AlternateDNS string
}
var (
Shecan = Server{PreferredDNS: "178.22.122.100", AlternateDNS: "185.51.200.2"}
Online403 = Server{PreferredDNS: "10.202.10.202", AlternateDNS: "10.202.10.102"}
Electro = Server{PreferredDNS: "78.157.42.100", AlternateDNS: "78.157.42.101"}
CurrentPref string
CurrentAlter string
)
const (
AdapterName = "Wi-Fi"
)
func main() {
a := app.New()
w := a.NewWindow("SpiritDns")
pW := widget.NewLabel("...")
aW := widget.NewLabel("...")
SetDnsText(pW, aW)
shecanBtn := widget.NewButton("Shecan", func() {
setDNS(AdapterName, Shecan.PreferredDNS, Shecan.AlternateDNS)
SetDnsText(pW, aW)
})
online403Btn := widget.NewButton("403", func() {
setDNS(AdapterName, Online403.PreferredDNS, Online403.AlternateDNS)
SetDnsText(pW, aW)
})
electro := widget.NewButton("Electro", func() {
setDNS(AdapterName, Electro.PreferredDNS, Electro.AlternateDNS)
SetDnsText(pW, aW)
})
clear := widget.NewButton("Clear Dns", func() {
setDNS(AdapterName, "", "")
SetDnsText(pW, aW)
})
dns := container.New(layout.NewGridLayout(2), pW, aW)
continer := container.New(layout.NewGridLayout(1), shecanBtn, online403Btn, electro, clear, dns)
w.CenterOnScreen()
w.Resize(fyne.Size{250, 200})
w.SetContent(continer)
w.ShowAndRun()
}
func setDNS(adapterName, preferredDNS, alternateDNS string) error {
cmd := exec.Command("netsh", "interface", "ipv4", "set", "dns", `name="`+adapterName+`"`, "source=static", "address="+preferredDNS)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
cmd.Stdin = nil
cmd.Stdout = nil
cmd.Stderr = nil
if err := cmd.Run(); err != nil {
return fmt.Errorf("error setting preferred DNS: %v", err)
}
cmd = exec.Command("netsh", "interface", "ipv4", "add", "dns", `name="`+adapterName+`"`, "address="+alternateDNS, "index=2")
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
cmd.Stdin = nil
cmd.Stdout = nil
cmd.Stderr = nil
if err := cmd.Run(); err != nil {
return fmt.Errorf("error setting alternate DNS: %v", err)
}
return nil
}
func getDNSAddressesForAdapter(adapterName string) (preferredDNS, alternateDNS string, err error) {
cmd := exec.Command("netsh", "interface", "ipv4", "show", "dnsservers", `name="`+adapterName+`"`)
output, err := cmd.CombinedOutput()
if err != nil {
return "", "", err
}
outputStr := string(output)
lines := strings.Split(outputStr, "\n")
for i, line := range lines {
if i == 2 {
s := strings.Replace(line, "Statically Configured DNS Servers:", " ", 1)
preferredDNS = strings.TrimSpace(s)
}
if i == 3 {
alternateDNS = strings.TrimSpace(line)
}
}
return preferredDNS, alternateDNS, nil
}
func SetDnsText(pW, aW *widget.Label) {
pI, aI, err := getDNSAddressesForAdapter(AdapterName)
if err != nil {
fmt.Errorf("error getting DNS: %v", err)
}
ipP := net.ParseIP(pI)
ipA := net.ParseIP(aI)
if ipP != nil && ipA != nil {
pW.SetText(pI)
aW.SetText(aI)
} else {
pW.SetText("empty")
aW.SetText("empty")
}
}