This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
forked from stripe/smokescreen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smokescreen.go
193 lines (163 loc) · 4.19 KB
/
smokescreen.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/DataDog/datadog-go/statsd"
"github.com/icub3d/graceful"
"github.com/stripe/go-einhorn/einhorn"
"gopkg.in/elazarl/goproxy.v1"
)
var privateNetworks []net.IPNet
var connectTimeout time.Duration
var track *statsd.Client
func init() {
var err error
privateNetworkStrings := []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"fc00::/7",
}
privateNetworks = make([]net.IPNet, len(privateNetworkStrings))
for i, netstring := range privateNetworkStrings {
_, net, err := net.ParseCIDR(netstring)
if err != nil {
log.Fatal(err)
}
privateNetworks[i] = *net
}
track, err = statsd.New("127.0.0.1:8200")
if err != nil {
log.Fatal(err)
}
track.Namespace = "smokescreen."
}
func isPrivateNetwork(ip net.IP) bool {
if !ip.IsGlobalUnicast() {
return true
}
for _, net := range privateNetworks {
if net.Contains(ip) {
return true
}
}
return false
}
func safeResolve(network, addr string) (string, error) {
track.Count("resolver.attempts_total", 1, []string{}, 0.3)
resolved, err := net.ResolveTCPAddr(network, addr)
if err != nil {
track.Count("resolver.errors_total", 1, []string{}, 0.3)
return "", err
}
if isPrivateNetwork(resolved.IP) {
track.Count("resolver.illegal_total", 1, []string{}, 0.3)
return "", fmt.Errorf("host %s resolves to illegal IP %s",
addr, resolved.IP)
}
return resolved.String(), nil
}
func dial(network, addr string) (net.Conn, error) {
resolved, err := safeResolve(network, addr)
if err != nil {
return nil, err
}
return net.DialTimeout(network, resolved, connectTimeout)
}
func errorResponse(req *http.Request, err error) *http.Response {
resp := goproxy.NewResponse(req,
goproxy.ContentTypeText,
http.StatusServiceUnavailable,
err.Error()+"\n")
resp.ProtoMajor = req.ProtoMajor
resp.ProtoMinor = req.ProtoMinor
resp.Header.Add("X-Smokescreen-Error", err.Error())
return resp
}
func buildProxy() *goproxy.ProxyHttpServer {
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = true
proxy.Tr.Dial = dial
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
ctx.Logf("Received HTTP proxy request: "+
"remote=%#v host=%#v url=%#v",
ctx.Req.RemoteAddr,
ctx.Req.Host,
ctx.Req.RequestURI)
return req, nil
})
proxy.OnRequest().HandleConnectFunc(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
ctx.Logf("Received CONNECT proxy request: "+
"remote=%#v host=%#v",
ctx.Req.RemoteAddr,
host)
resolved, err := safeResolve("tcp", host)
if err != nil {
ctx.Resp = errorResponse(ctx.Req, err)
return goproxy.RejectConnect, ""
}
return goproxy.OkConnect, resolved
})
proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if resp == nil && ctx.Error != nil {
resp = errorResponse(ctx.Req, ctx.Error)
}
return resp
})
return proxy
}
func findListener(defaultPort int) (net.Listener, error) {
if einhorn.IsWorker() {
listener, err := einhorn.GetListener(0)
if err != nil {
return nil, err
}
err = einhorn.Ack()
return listener, err
} else {
return net.Listen("tcp", fmt.Sprintf(":%d", defaultPort))
}
}
func main() {
var port int
var maintenanceFile string
flag.IntVar(&port, "port", 4750, "Port to bind on")
flag.DurationVar(&connectTimeout, "timeout",
time.Duration(10)*time.Second, "Time to wait while connecting")
flag.StringVar(&maintenanceFile, "maintenance", "",
"Flag file for maintenance. chmod to 000 to put into maintenance mode")
flag.Parse()
proxy := buildProxy()
listener, err := findListener(port)
if err != nil {
log.Fatal(err)
}
kill := make(chan os.Signal, 1)
var handler http.Handler = proxy
if maintenanceFile != "" {
handler = &HealthcheckMiddleware{
App: handler,
MaintenanceFile: maintenanceFile,
}
}
server := graceful.NewServer(&http.Server{
Handler: handler,
})
go func() {
<-kill
server.Close()
}()
signal.Notify(kill, syscall.SIGUSR2, syscall.SIGTERM)
err = server.Serve(listener)
if !strings.HasSuffix(err.Error(), "use of closed network connection") {
log.Fatal(err)
}
}