-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathproxy.go
More file actions
158 lines (131 loc) · 3.72 KB
/
proxy.go
File metadata and controls
158 lines (131 loc) · 3.72 KB
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
package main
import (
"fmt"
"net/http"
"sort"
"strings"
"time"
"github.com/kcmerrill/shutdown.go"
log "github.com/sirupsen/logrus"
"rsc.io/letsencrypt"
)
// Store all of our endpoints
var endpoints map[string]*Endpoint
var endpointkeys sort.StringSlice
// 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)
log.WithFields(
log.Fields{
"Request": r.Host,
"IP": r.RemoteAddr,
"Forwarded": endpoint,
}).Info("New Request")
// One quick sanity check before sending it on it's way
if _, exists := endpoints[endpoint]; exists {
endpoints[endpoint].Proxy.ServeHTTP(w, r)
} else {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("Error 502 - Bad Gateway"))
}
}
// FetchProxyStart creates and starts the proxy
func FetchProxyStart(httpPort int, secured, healthChecks bool, healthCheckURL, defaultEndpoint string) {
log.WithFields(
log.Fields{
"port": httpPort,
}).Info("Starting fetch proxy")
// Start our healthchecks
if healthChecks {
go HealthChecks(healthCheckURL)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
passThrough(w, r, defaultEndpoint)
})
if !secured {
// 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()
}
} else {
// start our letsencrypt SSL goodies
var m letsencrypt.Manager
if err := m.CacheFile("letsencrypt.cache"); err != nil {
log.Fatal(err)
shutdown.Now()
}
log.Fatal(m.Serve())
}
}
// AddSite adds a new website to the proxy to be forwarded
func AddSite(base, address string, healthChecks bool, healthCheckURL 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 + "-" + time.Now().Format("2006-01-02T15:04:05.000")
// Add new endpoint
ep, err := NewEndpoint(base, address, healthChecks, healthCheckURL)
if err == nil {
// If it doesn't exist ...
log.WithFields(log.Fields{
"url": address,
"registered": base,
"urlbase": urlbase,
}).Info("Registered endpoint")
endpoints[key] = ep
endpointkeys = append(endpointkeys, key)
sort.Sort(sort.Reverse(endpointkeys))
return nil
}
return err
}
// HealthChecks starts the background process for __all__ site health checks
func HealthChecks(healthCheckURL string) {
for {
<-time.After(10 * time.Second)
for key := range endpoints {
go endpoints[key].HealthCheck(healthCheckURL)
}
}
}
// 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)
}