-
Notifications
You must be signed in to change notification settings - Fork 1
/
pingtrace.go
75 lines (56 loc) · 1.5 KB
/
pingtrace.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
package main
import (
"flag"
"fmt"
"go.ntppool.org/pingtrace/handlers"
gorillahandlers "github.com/gorilla/handlers"
"github.com/rs/cors"
"log"
"net/http"
"net/netip"
"os"
)
var (
listen = flag.String("listen", "127.0.0.1:8060", "listen address")
privateNets []netip.Prefix
)
// A version string that can be set with
//
// -ldflags "-X main.Version VERSION"
//
// at compile-time.
var Version string
func init() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: pingtrace [-listen=<listen>]")
flag.PrintDefaults()
}
log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile)
pn := []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"}
for _, p := range pn {
ipnet, err := netip.ParsePrefix(p)
if err != nil {
panic(err)
}
privateNets = append(privateNets, ipnet)
}
}
func setupMux(hdl *handlers.Handlers) http.Handler {
mux := http.NewServeMux()
rateh := handlers.NewRateHandler(mux, NewLimiter(10))
mux.Handle("/ntp/", rateh.Wrap(hdl.NTPHandler))
mux.Handle("/traceroute/", rateh.Wrap(hdl.TracerouteHandler))
mux.HandleFunc("/traceroute/checkqueue", rateh.CheckQueue)
return cors.New(cors.Options{AllowedOrigins: []string{"*.pool.ntp.org", "*.ntppool.org"}}).Handler(mux)
}
func main() {
flag.Parse()
hdl := handlers.NewHandlers()
hdl.PrivateNets = privateNets
h := gorillahandlers.LoggingHandler(os.Stdout, setupMux(hdl))
log.Printf("Listening to '%s'", *listen)
err := http.ListenAndServe(*listen, h)
if err != nil {
log.Printf("listen: %s", err)
}
}