-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
152 lines (124 loc) · 3.37 KB
/
proxy.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
package main
import (
"fmt"
"net/http"
"sort"
"strings"
"github.com/kcmerrill/shutdown.go"
log "github.com/sirupsen/logrus"
)
// Store all of our endpoints
var endpoints map[string]*Endpoint
var endpointkeys sort.StringSlice
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// passThrough takes in traffic on specific port and passes it through to the appropriate endpoint
func passThrough(w http.ResponseWriter, r *http.Request, defaultEndpoint string) {
// remove www.
if strings.HasPrefix(r.Host, "www.") {
r.Host = strings.Replace(r.Host, "www.", "", 1)
}
endpoint := siteKey(r.Host, defaultEndpoint)
// One quick sanity check before sending it on it's way
if _, exists := endpoints[endpoint]; exists {
log.WithFields(
log.Fields{
"Host": r.Host,
"From": r.RemoteAddr,
"To": endpoints[endpoint].Address,
"RequestURI": r.RequestURI,
"Forwarded": endpoint,
}).Info("New Request")
endpoints[endpoint].Proxy.ServeHTTP(w, r)
} else {
log.WithFields(
log.Fields{
"Request": r.Host,
"From": r.RemoteAddr,
"Forwarded": endpoint,
}).Info("Bad Request")
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("Error 502 - Bad Gateway"))
}
}
// DProxyStart creates and starts the proxy
func DProxyStart(httpPort int, defaultEndpoint string) {
log.WithFields(
log.Fields{
"port": httpPort,
}).Info("Starting fetch proxy")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
passThrough(w, r, defaultEndpoint)
})
// Not secured, so lets just start a simple webserver
if err := http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil); err != nil {
log.Fatal(err.Error())
shutdown.Now()
}
}
// AddSite adds a new website to the proxy to be forwarded
func AddSite(base, address string) error {
// Check if endpoint already exists
for _, item := range endpoints {
if item.Registered == base && item.Address.String() == address {
return nil
}
}
// Construct the key so that you can sort by url base and time added
urlbase := base
// Remove any thing after the _ from the url
if strings.Contains(urlbase, "_") {
urlbase = urlbase[0:strings.Index(urlbase, "_")]
}
key := urlbase
// Add new endpoint
ep, err := NewEndpoint(base, address)
if err == nil {
// If it doesn't exist ...
log.WithFields(log.Fields{
"url": address,
"registered": base,
"urlbase": urlbase,
}).Info("Registered endpoint")
endpoints[key] = ep
if !stringInSlice(key, endpointkeys) {
endpointkeys = append(endpointkeys, key)
}
sort.Sort(sort.Reverse(endpointkeys))
return nil
}
return err
}
// Site key determines the endpoint to use based on the host
func siteKey(host, defaultEndpoint string) string {
registered := ""
// Grab the first key in the list that matches
for _, key := range endpointkeys {
b := endpoints[key].Registered
// Allow for multiple containers with the same url
if strings.Contains(b, "_") {
b = b[0:strings.Index(b, "_")]
}
if strings.HasPrefix(defaultEndpoint, b) && endpoints[key].Active {
defaultEndpoint = key
}
if strings.HasPrefix(host, b) && endpoints[key].Active {
registered = key
break
}
}
if registered == "" {
return defaultEndpoint
}
return registered
}
// init our maps
func init() {
endpoints = make(map[string]*Endpoint)
}