-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
137 lines (122 loc) · 3.24 KB
/
server.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
package whoami
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
dnstap "github.com/dnstap/golang-dnstap"
"github.com/gorilla/mux"
"github.com/patrickmn/go-cache"
)
type Server struct {
bin *cache.Cache
web *http.Server
in *dnstap.FrameStreamSockInput
out *dnstap.TextOutput
mwf []mux.MiddlewareFunc
}
func NewServer() *Server {
return &Server{
bin: cache.New(30*time.Second, 2*time.Minute),
mwf: []mux.MiddlewareFunc{headerMiddleware},
}
}
func (s *Server) Write(p []byte) (n int, err error) {
str := string(p)
parts := strings.Split(str, " ")
ip := parts[2]
hostname := parts[5]
hostname = strings.Trim(hostname, `"`)
hostname = strings.TrimSuffix(hostname, ".")
hostname = strings.ToLower(hostname)
fmt.Println("DNS query for", hostname, "from", ip)
s.bin.SetDefault(hostname, ip)
return len(p), nil
}
func (s *Server) whoamiEndpoint(w http.ResponseWriter, r *http.Request) {
ip := r.Header["X-Forwarded-For"][0]
if ip == "" {
ip = r.RemoteAddr
}
host := r.Header["X-Forwarded-Host"][0]
if host == "" {
host = r.Host
}
u, _ := url.Parse("http://" + host)
h := u.Hostname()
h = strings.TrimSpace(h)
h = strings.TrimSuffix(h, ".")
h = strings.ToLower(h)
fmt.Println("HTTP request for", h, "from", ip)
for i := 0; i < 20; i++ {
body, found := s.bin.Get(h)
if found {
w.WriteHeader(http.StatusOK)
w.Write([]byte(body.(string) + "\n"))
return
}
time.Sleep(500 * time.Millisecond)
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error: no query received\n"))
}
func headerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store, max-age=0")
w.Header().Set("Access-Control-Allow-Origin", "*")
next.ServeHTTP(w, r)
})
}
func (s *Server) SetHeader(name, value string) {
s.mwf = append(s.mwf, func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(name, value)
next.ServeHTTP(w, r)
})
})
}
func (s *Server) OpenSocket(path string) {
input, err := dnstap.NewFrameStreamSockInputFromPath(path)
if err != nil {
panic(err)
}
output := dnstap.NewTextOutput(s, dnstap.TextFormat)
s.in = input
s.out = output
go output.RunOutputLoop()
go input.ReadInto(output.GetOutputChannel())
fmt.Println("dnstap socket opened at", path)
}
func (s *Server) CloseSocket() {
s.out.Close()
}
func (s *Server) Start(port string) {
router := mux.NewRouter()
router.HandleFunc("/", s.whoamiEndpoint).Methods("GET", "HEAD")
router.Use(s.mwf...)
s.web = &http.Server{
Addr: ":" + port,
Handler: router,
ReadTimeout: 30 * time.Second,
}
s.web.SetKeepAlivesEnabled(false)
go func() {
if err := s.web.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Println("HTTP server error:", err)
os.Exit(99)
}
}()
fmt.Println("HTTP server listening on", s.web.Addr)
}
func (s *Server) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
fmt.Println("Waiting up to 30 seconds for HTTP server to shutdown")
if err := s.web.Shutdown(ctx); err != nil {
fmt.Println("HTTP server shutdown error:", err)
}
fmt.Println("HTTP server gone. Ta-ta!")
}