-
Notifications
You must be signed in to change notification settings - Fork 8
/
as112.go
163 lines (147 loc) · 4.69 KB
/
as112.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* A name server for the AS112 sink system. See http://www.as112.net/
Stephane Bortzmeyer <[email protected]>
Example of use:
grong -servername "grong.cloud.as112.test" -- -email toto.example.net -hostname me.as112.net -location "In the cloud"
*/
package responder
import (
"regexp"
"strings"
"fmt"
"os"
"./types"
"./myflag"
)
const as112Regexp = "(168\\.192\\.in-addr\\.arpa|154\\.169\\.in-addr\\.arpa|16\\.172\\.in-addr\\.arpa|17\\.172\\.in-addr\\.arpa|18\\.172\\.in-addr\\.arpa|19\\.172\\.in-addr\\.arpa|20\\.172\\.in-addr\\.arpa|21\\.172\\.in-addr\\.arpa|22\\.172\\.in-addr\\.arpa|23\\.172\\.in-addr\\.arpa|24\\.172\\.in-addr\\.arpa|25\\.172\\.in-addr\\.arpa|26\\.172\\.in-addr\\.arpa|27\\.172\\.in-addr\\.arpa|28\\.172\\.in-addr\\.arpa|29\\.172\\.in-addr\\.arpa|30\\.172\\.in-addr\\.arpa|31\\.172\\.in-addr\\.arpa|10\\.in-addr\\.arpa)$"
const defaultTTL = 3600
var (
as112Domain = regexp.MustCompile("^" + as112Regexp)
as112SubDomain = regexp.MustCompile("\\." + as112Regexp)
// Answers to "TXT hostname.as112.net"
hostnameAnswers = [...]string{
"Unknown location on Earth.",
"GRONG, name server written in Go.",
"See http://as112.net/ for more information.",
}
// Name servers of AS112, currently two
as112nameServers = [...]string{
"blackhole-1.iana.org",
"blackhole-2.iana.org",
}
hostnamesoa = types.SOArecord{
Mname: "NOT-CONFIGURED-use-hostname-option.as112.example.net", // Put the real hostname with the -hostname command-line option. We do not use -server which has lightly different semantics.
Rname: "UNKNOWN-use-email-option.as112.example.net", // Put your email address (with @ replaced by .) with the -email command-line option
Serial: 2003030100,
Refresh: 3600,
Retry: 600,
Expire: 2592000,
Minimum: 15,
}
as112soa = types.SOArecord{
Mname: "prisoner.iana.org",
Rname: "hostmaster.root-servers.org",
Serial: 2002040800,
Refresh: 1800,
Retry: 900,
Expire: 604800,
Minimum: 604800,
}
)
func nsRecords(domain string) (result []types.RR) {
result = make([]types.RR, len(as112nameServers))
for i, text := range as112nameServers {
result[i] = types.RR{
Name: domain,
TTL: defaultTTL,
Type: types.NS,
Class: types.IN,
Data: types.Encode(text),
}
}
return
}
func soaRecord(domain string, soa types.SOArecord) (result types.RR) {
result = types.RR{
Name: domain,
TTL: defaultTTL,
Type: types.SOA,
Class: types.IN,
Data: types.EncodeSOA(soa),
}
return
}
func Respond(query types.DNSquery, config map[string]interface{}) (result types.DNSresponse) {
result.Ansection = nil
qname := strings.ToLower(query.Qname)
if query.Qclass == types.IN {
switch {
case as112Domain.Match([]byte(qname)):
result.Responsecode = types.NOERROR
switch {
case query.Qtype == types.NS:
result.Ansection = nsRecords(query.Qname)
case query.Qtype == types.SOA:
result.Ansection = make([]types.RR, 1)
result.Ansection[0] = soaRecord(query.Qname, as112soa)
default:
// Do nothing
}
case as112SubDomain.Match([]byte(qname)):
result.Responsecode = types.NXDOMAIN
// TODO: send the proper SOA in the authority section (so we
// must find which domain matched)
case qname == "hostname.as112.net":
result.Responsecode = types.NOERROR
switch query.Qtype { // TODO: handle ANY qtypes
case types.TXT:
result.Ansection = make([]types.RR, len(hostnameAnswers))
for i, text := range hostnameAnswers {
result.Ansection[i] = types.RR{
Name: query.Qname,
TTL: defaultTTL,
Type: types.TXT,
Class: types.IN,
Data: types.ToTXT(text),
}
}
case types.NS:
result.Ansection = nsRecords(query.Qname)
case types.SOA:
result.Ansection = []types.RR{soaRecord(query.Qname, hostnamesoa)}
default:
// Do nothing
}
default:
result.Responsecode = types.SERVFAIL
}
} else {
result.Responsecode = types.SERVFAIL
}
return result
}
func Init(firstoption int) {
flag.Reinit(firstoption)
helpptr := flag.Bool("help", false, "Displays usage instructions")
emailptr := flag.String("email", "",
"Set the email address of the manager for this server (in DNS format, with . instead of @)")
locationptr := flag.String("location", "",
"Set the location of this server, for instance \"ALIX exchange point in Somewhere, Somestate\"")
hostnameptr := flag.String("hostname", "",
"Set the official host name for this server")
flag.Parse()
help := *helpptr
if help {
fmt.Printf("Usage of the AS112 responder:\n")
flag.PrintDefaults()
os.Exit(0)
}
if *emailptr != "" {
hostnamesoa.Rname = *emailptr
}
if *locationptr != "" {
hostnameAnswers[0] = *locationptr
}
if *hostnameptr != "" {
hostnamesoa.Mname = *hostnameptr
}
}