-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (38 loc) · 1012 Bytes
/
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
package main
import (
"dnsproxy/pkg/localip"
"log"
"github.com/miekg/dns"
)
func main() {
localip.Getip()
// Create a DNS handler function
dnsHandler := func(w dns.ResponseWriter, r *dns.Msg) {
// Create a UDP client to communicate with the upstream DNS server
client := &dns.Client{Net: "udp"}
// Configure the upstream DNS server address and port
upstreamAddr := "8.8.8.8:53"
// Send the DNS query to the upstream DNS server
response, _, err := client.Exchange(r, upstreamAddr)
if err != nil {
log.Printf("Failed to send DNS query: %s", err.Error())
return
}
// Send the DNS response back to the client
err = w.WriteMsg(response)
if err != nil {
log.Printf("Failed to send DNS response: %s", err.Error())
}
}
// Start the DNS server
server := &dns.Server{
Addr: ":53",
Net: "udp",
Handler: dns.HandlerFunc(dnsHandler),
}
log.Printf("DNS proxy server listening on :53...")
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}