-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
119 lines (101 loc) · 2.45 KB
/
dns.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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"golang.org/x/text/encoding/simplifiedchinese"
)
type Charset string
const (
UTF8 = Charset("UTF-8")
GB18030 = Charset("GB18030")
)
func ConvertByte2String(byte []byte, charset Charset) string {
var str string
switch charset {
case GB18030:
var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
str = string(decodeBytes)
case UTF8:
fallthrough
default:
str = string(byte)
}
return str
}
func main() {
// 检查操作系统类型
if strings.ToUpper(os.Getenv("PROCESSOR_ARCHITECTURE")) != "AMD64" {
fmt.Println("请在 64 位操作系统上运行此程序")
os.Exit(1)
}
// 修改DNS服务器
dnsServers := []string{"8.8.8.8", "4.4.4.4"}
err := SetDNS(dnsServers)
if err != nil {
os.Exit(1)
}
// 刷新DNS缓存
err = FlushDNS()
if err != nil {
os.Exit(1)
}
time.Sleep(time.Second * 5)
}
// SetDNS 设置DNS服务器
func SetDNS(servers []string) error {
for _, conn := range GetActiveConnections() {
var cmd *exec.Cmd
var dns_name string
for k, server := range servers {
if k == 0 {
cmd = exec.Command("netsh", "interface", "ip", "set", "dns", conn, "static", server, "primary")
dns_name = "主"
} else {
cmd = exec.Command("netsh", "interface", "ip", "add", "dns", conn, server)
dns_name = "备"
}
err := cmd.Run()
if err != nil {
fmt.Printf("%s 的%sDNS服务器设置失败:%s\n", conn, dns_name, err)
return err
}
fmt.Printf("%s 的%sDNS服务器已设置为:%s\n", conn, dns_name, server)
}
}
fmt.Println("DNS服务器设置成功")
return nil
}
// FlushDNS 刷新DNS缓存
func FlushDNS() error {
cmd := exec.Command("ipconfig", "/flushdns")
err := cmd.Run()
if err != nil {
fmt.Printf("刷新dns失败:%s\n", err)
return err
}
fmt.Println("DNS缓存已刷新")
return nil
}
// GetActiveConnections 获取当前活动的网络连接名称
func GetActiveConnections() []string {
var connections []string
cmd := exec.Command("netsh", "interface", "show", "interface")
output, err := cmd.Output()
if err != nil {
return connections
}
cmdRe := ConvertByte2String(output, "GB18030")
lines := strings.Split(string(cmdRe), "\n")
for _, line := range lines {
if strings.Contains(line, "Connected") || strings.Contains(line, "已连接") {
fields := strings.Fields(line)
if len(fields) >= 4 {
connections = append(connections, fields[3])
}
}
}
return connections
}