-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (111 loc) · 2.66 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
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"strings"
"github.com/miekg/dns"
)
type Config struct {
DomainIPMap map[string]string `json:"domain_ip_map"`
}
var domainMap map[string]string
func main() {
parseConfig()
startDNSServer()
}
func parseConfig() {
file, err := ioutil.ReadFile("config.json")
if err != nil {
fmt.Println("Error reading configuration file:", err)
return
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
fmt.Println("Error parsing configuration file:", err)
return
}
domainMap = config.DomainIPMap
}
func startDNSServer() {
addr, err := net.ResolveUDPAddr("udp", ":53")
if err != nil {
fmt.Println("Error resolving UDP address:", err)
return
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
fmt.Println("Error listening on UDP:", err)
return
}
fmt.Println("DNS Proxy Server started")
for {
buffer := make([]byte, 1024*1024*1024)
n, addr, err := conn.ReadFromUDP(buffer)
if err != nil {
fmt.Println("Error reading from UDP:", err)
continue
}
data := make([]byte, n)
copy(data, buffer[:n])
go handleDNSQuery(data, conn, addr)
}
}
func handleDNSQuery(query []byte, conn *net.UDPConn, addr *net.UDPAddr) {
msg := new(dns.Msg)
err := msg.Unpack(query)
if err != nil {
fmt.Println("Error unpacking DNS query:", err)
return
}
domain := msg.Question[0].Name
domain = strings.TrimSuffix(domain, ".")
//fmt.Println("Domain:", domain)
ip, ok := domainMap[domain]
if ok {
response := new(dns.Msg)
response.SetReply(msg)
response.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{Name: dns.Fqdn(domain), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600},
A: net.ParseIP(ip),
},
}
responseBytes, err := response.Pack()
if err != nil {
fmt.Println("Error packing DNS response:", err)
return
}
_, err = conn.WriteToUDP(responseBytes, addr)
if err != nil {
fmt.Println("Error sending DNS response:", err)
return
}
} else {
dnsConfig, _ := dns.ClientConfigFromFile("resolv.conf")
dnsConn, err := net.Dial("udp", dnsConfig.Servers[0]+":53")
if err != nil {
fmt.Println("Error connecting to default DNS server:", err)
return
}
defer dnsConn.Close()
_, err = dnsConn.Write(query)
if err != nil {
fmt.Println("Error sending DNS query to default DNS server:", err)
return
}
responseBytes := make([]byte, 1024*100)
n, err := dnsConn.Read(responseBytes)
if err != nil {
fmt.Println("Error receiving DNS response from default DNS server:", err)
return
}
_, err = conn.WriteToUDP(responseBytes[:n], addr)
if err != nil {
fmt.Println("Error sending DNS response:", err)
return
}
}
}