-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
160 lines (145 loc) · 3.63 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bytes"
"context"
"fmt"
"log"
"net"
"net/http"
"sync"
"time"
)
func getOutboundIPs(dest string) []net.IP {
conn, err := net.Dial("udp", fmt.Sprintf("%s:53", dest))
if err != nil {
return []net.IP{}
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return []net.IP{localAddr.IP}
}
func localNamesFromBind(ctx context.Context, addr net.Addr) ([]string, string) {
lhs, port, err := net.SplitHostPort(addr.String())
if err != nil {
log.Printf("Could not split hostport: %s", err)
return nil, ""
}
lh := net.ParseIP(lhs)
lookups := []net.IP{}
isEmpty := false
if lh.Equal(net.IPv4zero) {
isEmpty = true
lookups = append(lookups, getOutboundIPs("127.0.0.1")...)
lookups = append(lookups, getOutboundIPs("8.8.8.8")...)
}
if lh.Equal(net.IPv6zero) {
isEmpty = true
lookups = append(lookups, getOutboundIPs("127.0.0.1")...)
lookups = append(lookups, getOutboundIPs("8.8.8.8")...)
lookups = append(lookups, getOutboundIPs("[::1]")...)
lookups = append(lookups, getOutboundIPs("[2001:4860:4860::8888]")...)
}
if !isEmpty {
lookups = append(lookups, lh)
}
names := map[string]struct{}{}
for _, l := range lookups {
hns, err := new(net.Resolver).LookupAddr(ctx, l.String())
if err != nil {
log.Printf("Could not look up '%s': %s", l, err)
}
for _, n := range hns {
names[n] = struct{}{}
}
}
n := make([]string, len(names))
i := 0
for k := range names {
n[i] = k
i++
}
return n, port
}
func listen(ctx context.Context, addr string, hostnames []string) error {
log.Printf("Binding to %s\n", addr)
l, err := (&(net.ListenConfig{})).Listen(ctx, "tcp", addr)
if err != nil {
return err
}
log.Printf("Listening on http://%s\n", l.Addr())
bindNames, port := localNamesFromBind(ctx, l.Addr())
hostnames = append(hostnames, bindNames...)
printed := map[string]struct{}{}
for _, hn := range hostnames {
_, ok := printed[hn]
if ok {
continue
}
printed[hn] = struct{}{}
lps := fmt.Sprintf(":%s", port)
if lps == ":80" {
lps = ""
}
log.Printf("Resolvable at http://%s%s\n", hn, lps)
}
s := &http.Server{Addr: l.Addr().String()}
go func() {
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
s.Shutdown(ctx)
}()
return s.Serve(l)
}
type bufWriter struct {
Code int
headers http.Header
Body bytes.Buffer
once sync.Once
}
func (b *bufWriter) Header() http.Header {
b.once.Do(func() {
b.headers = make(http.Header)
})
return b.headers
}
func (b *bufWriter) WriteHeader(status int) {
b.Code = status
}
func (b *bufWriter) Write(d []byte) (int, error) {
return b.Body.Write(d)
}
func (b *bufWriter) WriteTo(d http.ResponseWriter, skipHeaders bool) (int, error) {
h := d.Header()
for key, val := range b.headers {
h[key] = val
}
if !skipHeaders {
d.WriteHeader(b.Code)
}
return d.Write(b.Body.Bytes())
}
var _ http.ResponseWriter = &bufWriter{}
func httpErrorWrap(reportf func(w *bufWriter, r *http.Request, err error), f func(w http.ResponseWriter, r *http.Request) error) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
bw := &bufWriter{}
err := f(bw, r)
reportf(bw, r, err)
if err != nil {
http.Error(w, fmt.Sprintf("500 Internal Server Error\n\n%s", err.Error()), http.StatusInternalServerError)
if bw.Body.Len() > 0 {
w.Write([]byte("\n\n\nOutput buffer prior to error:\n\n"))
bw.WriteTo(w, true)
}
return
}
bw.WriteTo(w, false)
}
}
func getCookie(r *http.Request, name string, def string) (string, bool) {
cookie, err := r.Cookie(name)
if err != nil {
return def, false
}
return cookie.Value, true
}