-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns.go
151 lines (134 loc) Β· 3.49 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
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
package main
// SPDX-License-Identifier: WTFPL
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/miekg/dns"
)
const (
SOA = `wd.ip.wtf SOA wd-ns.ip.wtf dns-admin.oo.fail 42 42 42 42 42`
NS = `wd.ip.wtf NS wd-ns.ip.wtf`
TXT = ` TXT ""`
)
var (
flagListen = flag.String("listen", ":5300", "DNS listen address ([host]:port)")
)
// MustNewRR is a shortcut to dns.NewRR that panics on error.
func MustNewRR(s string) dns.RR {
r, err := dns.NewRR(s)
if err != nil {
panic(err)
}
return r
}
var wordles = map[string]string{
"0": "zeros",
"1": "amaze",
"2": "magic",
"3": "house",
"4": "cloud",
"5": "nodes",
"6": "paint",
"7": "disco",
"8": "hedge",
"9": "poems",
"10": "flair",
"11": "batch",
"12": "stout",
"13": "candy",
"14": "larva",
"15": "maple",
"16": "ladle",
"17": "bacon",
"18": "snake",
"19": "chart",
"20": "oasis",
"21": "space",
"22": "basic",
"23": "slope",
"24": "great",
"25": "first",
"26": "rhino",
"27": "screw",
"28": "force",
"29": "water",
"30": "hyper",
"31": "cakes",
"example": "names",
}
func dnsServe() {
dns.HandleFunc("wd.ip.wtf", func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
rname := strings.Split(r.Question[0].Name, ".")
name := strings.TrimSuffix(r.Question[0].Name, ".")
day := fmt.Sprintf("%d", 1+((12 + time.Now().UTC().Day()) % 31))
if len(rname) == 4 && strings.ToLower(name) == "wd.ip.wtf" && r.Question[0].Qtype == dns.TypeNS {
m.Answer = []dns.RR{MustNewRR(NS)}
} else if len(rname) == 4 && strings.ToLower(name) == "wd.ip.wtf" && r.Question[0].Qtype == dns.TypeSOA {
m.Answer = []dns.RR{MustNewRR(SOA)}
} else if len(rname[0]) != 5 && len(rname) <= 5 {
rr := MustNewRR(name + TXT)
txt := rr.(*dns.TXT)
txt.Txt[0] = `Welcome to Wordle over DNS! Today's puzzle is #` + day + `: <guess>.` + day + `.wd.ip.wtf`
rr2 := MustNewRR(name + TXT)
txt2 := rr2.(*dns.TXT)
txt2.Txt = []string{
`This shell function makes it easier to play`,
`wd() { dig +short txt $1.` + day + `.wd.ip.wtf | perl -pe's/\\([0-9]{1,3})/chr$1/eg'; }`}
m.Answer = []dns.RR{rr, rr2}
} else if len(rname[0]) == 5 && len(rname) <= 5 {
rr := MustNewRR(name + " CNAME " + rname[0] + "." + day + ".wd.ip.wtf.")
m.Answer = []dns.RR{rr}
} else if word, ok := wordles[rname[1]]; ok {
result := wordle(strings.ToLower(rname[0]), word)
if result == "" || !checkGuessValid(rname[0]) {
result = fmt.Sprintf("Your guess must be a valid word and %d letters.", len(word))
}
rr := MustNewRR(name + TXT)
txt := rr.(*dns.TXT)
txt.Txt[0] = result
m.Answer = []dns.RR{rr}
if result == "π©π©π©π©π©" {
rr := MustNewRR(name + TXT)
txt := rr.(*dns.TXT)
txt.Txt = []string{
"π₯³ππ₯³ππͺ
",
}
m.Answer = append(m.Answer, rr)
}
} else {
m.Rcode = dns.RcodeNameError
m.Ns = []dns.RR{MustNewRR(SOA)}
}
w.WriteMsg(m)
})
go func() {
srv := &dns.Server{Addr: *flagListen, Net: "udp"}
if err := srv.ListenAndServe(); err != nil {
log.Fatalf("Failed to set udp listener %s\n", err.Error())
}
}()
go func() {
srv := &dns.Server{Addr: *flagListen, Net: "tcp"}
if err := srv.ListenAndServe(); err != nil {
log.Fatalf("Failed to set tcp listener %s\n", err.Error())
}
}()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
log.Fatalf("Signal (%v) received, stopping\n", s)
}
func main() {
flag.Parse()
dictLoad(5)
dnsServe()
}