Skip to content

Commit 650d5f9

Browse files
committed
Fix a couple of go vet issues
1 parent 3869853 commit 650d5f9

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

COPYING

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2023 David Leadbeater <http://©.st>
1+
Copyright David Leadbeater <http://©.st>
22

33
Permission to use, copy, modify, and/or distribute this software for any
44
purpose with or without fee is hereby granted.

cowsay.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"math/rand"
5+
"math/rand/v2"
66
"net"
77
"net/http"
88
"strings"
@@ -47,7 +47,7 @@ func cowsay(w http.ResponseWriter, req *http.Request, rConn *RecordingConn) {
4747
remoteAddr := rConn.RemoteAddr().(*net.TCPAddr)
4848
w.Write([]byte("\x1bc"))
4949
basicTmpl := basicCow
50-
if rand.Intn(9) <= 1 {
50+
if rand.IntN(9) <= 1 {
5151
basicTmpl = basicTux
5252
}
5353
w.Write([]byte(cowsayText(0, "What the fuck is my IP address?", basicTmpl, "")))
@@ -63,7 +63,7 @@ func cowsay(w http.ResponseWriter, req *http.Request, rConn *RecordingConn) {
6363
ip = v4.String()
6464
proto = "v4"
6565
}
66-
if rand.Intn(3) > 1 {
66+
if rand.IntN(3) > 1 {
6767
w.Write([]byte(cowsayText(0, fmt.Sprintf("It's fucking %v", ip), ipCow, proto)))
6868
} else {
6969
w.Write([]byte(cowsayText(50, fmt.Sprintf("It's fucking %v", ip), ipBCow, proto)))

dns.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/signal"
1010
"strings"
11+
"sync"
1112
"syscall"
1213
"time"
1314

@@ -22,18 +23,25 @@ type DNSData struct {
2223

2324
var (
2425
dnsListen = flag.String("dns-listen", ":8053", "Port to listen on for DNS server")
25-
dnsIP = flag.String("dns-public-ip", "", "Public IP to return")
26+
dnsIP = flag.String("dns-public-ip", "", "Public IP to return")
2627
)
2728

28-
var dnsMap = map[string]DNSData{}
29+
var (
30+
dnsMap = map[string]DNSData{}
31+
dnsMapMu sync.RWMutex
32+
)
2933

3034
func dnsHandler(w http.ResponseWriter, req *http.Request) {
3135
host := strings.TrimSuffix(req.Host, ".")
3236

3337
w.Header().Add("Access-Control-Allow-Origin", "*")
3438
w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS, HEAD")
3539

36-
if m, ok := dnsMap[host]; ok {
40+
dnsMapMu.RLock()
41+
m, ok := dnsMap[host]
42+
dnsMapMu.RUnlock()
43+
44+
if ok {
3745
err := json.NewEncoder(w).Encode(m)
3846
if err != nil {
3947
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -81,11 +89,13 @@ func dnsServe() {
8189
m.Answer = []dns.RR{rrx}
8290
} else {
8391
name := strings.TrimSuffix(r.Question[0].Name, ".")
92+
dnsMapMu.Lock()
8493
dnsMap[name] = DNSData{
8594
IP: w.RemoteAddr().String(),
8695
EdnsSubnet: subnet,
8796
Expire: time.Now().Add(2 * time.Minute),
8897
}
98+
dnsMapMu.Unlock()
8999
rr := MustNewRR(name + myAddrA)
90100
m.Answer = []dns.RR{rr}
91101
}
@@ -100,6 +110,22 @@ func dnsServe() {
100110
return
101111
}
102112

113+
// Periodically clean up expired DNS entries
114+
go func() {
115+
ticker := time.NewTicker(1 * time.Minute)
116+
defer ticker.Stop()
117+
for range ticker.C {
118+
now := time.Now()
119+
dnsMapMu.Lock()
120+
for k, v := range dnsMap {
121+
if now.After(v.Expire) {
122+
delete(dnsMap, k)
123+
}
124+
}
125+
dnsMapMu.Unlock()
126+
}
127+
}()
128+
103129
go func() {
104130
srv := &dns.Server{Addr: *dnsListen, Net: "udp"}
105131
if err := srv.ListenAndServe(); err != nil {
@@ -114,7 +140,7 @@ func dnsServe() {
114140
}
115141
}()
116142

117-
sig := make(chan os.Signal)
143+
sig := make(chan os.Signal, 1)
118144
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
119145
s := <-sig
120146
log.Fatalf("Signal (%v) received, stopping\n", s)

0 commit comments

Comments
 (0)