This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
162 lines (138 loc) · 4.42 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
153
154
155
156
157
158
159
160
161
162
package main
import (
"log"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/Luzifer/dockerproxy/sni"
"github.com/Luzifer/go_helpers/accessLogger"
"github.com/elazarl/goproxy"
"github.com/Luzifer/dockerproxy/auth"
_ "github.com/Luzifer/dockerproxy/auth/basic"
)
type dockerProxy struct {
proxy *goproxy.ProxyHttpServer
}
type redirectRewriter struct{}
func (r redirectRewriter) HandleResp(resp *http.Response, ctx *goproxy.ProxyCtx) bool {
if resp == nil {
return false
}
_, ok := resp.Header["Location"]
return ok
}
func redirectRewriterRewrite(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
loc, _ := resp.Location()
if host, ok := proxyConfiguration.Domains[loc.Host]; ok && host.ForceSSL && loc.Scheme == "http" {
loc.Scheme = "https"
resp.Header.Set("Location", loc.String())
}
return resp
}
func newDockerProxy() *dockerProxy {
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest().HandleConnect(goproxy.AlwaysReject)
proxy.OnResponse(redirectRewriter{}).DoFunc(redirectRewriterRewrite)
// We are not really a proxy but act as a HTTP(s) server who delivers remote pages
proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
proxy.ServeHTTP(w, req)
})
rand.Seed(time.Now().UnixNano())
return &dockerProxy{
proxy: proxy,
}
}
func (d *dockerProxy) ServeHTTP(res http.ResponseWriter, r *http.Request) {
d.shieldOwnHosts(d.httpLog(d.proxy)).ServeHTTP(res, r)
}
func (d *dockerProxy) getCertificates() []sni.Certificates {
var certs []sni.Certificates
for _, domain := range proxyConfiguration.Domains {
if domain.SSL.Cert != "" {
certs = append(certs, sni.Certificates{
CertFile: domain.SSL.Cert,
KeyFile: domain.SSL.Key,
})
}
}
return certs
}
func (d *dockerProxy) normalizeRemoteAddr(remoteAddress string) string {
idx := strings.LastIndex(remoteAddress, ":")
if idx != -1 {
remoteAddress = remoteAddress[0:idx]
if remoteAddress[0] == '[' && remoteAddress[len(remoteAddress)-1] == ']' {
remoteAddress = remoteAddress[1 : len(remoteAddress)-1]
}
}
return remoteAddress
}
func (d *dockerProxy) httpLog(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
al := accessLogger.New(w)
start := time.Now()
handler.ServeHTTP(al, r)
duration := float64(time.Since(start)) / float64(time.Microsecond)
requestCount.WithLabelValues(
strings.ToLower(r.Method),
strconv.FormatInt(int64(al.StatusCode), 10),
).Inc()
responseSize.Observe(float64(al.Size))
requestDuration.Observe(duration)
log.Printf("%s %s \"%s %s\" %d %d \"%s\"",
d.normalizeRemoteAddr(r.RemoteAddr),
r.Host,
r.Method, r.URL.RequestURI(),
al.StatusCode, al.Size,
r.Header.Get("User-Agent"),
)
})
}
func (d *dockerProxy) shieldOwnHosts(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
slug := ""
// Host is defined and slug has been found
if host, ok := proxyConfiguration.Domains[req.Host]; ok {
slug = host.Slug
if host.ForceSSL && req.TLS == nil {
req.URL.Scheme = "https"
req.URL.Host = req.Host
http.Redirect(w, req, req.URL.String(), 301)
return
}
if proxyConfiguration.Domains[req.Host].Authentication.Type != "" {
authHandler, err := auth.GetAuthHandler(proxyConfiguration.Domains[req.Host].Authentication.Type)
if err != nil {
http.Error(w, "Authentication system is misconfigured for this host.", http.StatusInternalServerError)
log.Printf("AuthSystemError: %s\n", err)
return
}
ok, err := authHandler(proxyConfiguration.Domains[req.Host].Authentication.Config, w, req)
if err != nil {
http.Error(w, "Authentication system threw an error.", http.StatusInternalServerError)
log.Printf("AuthSystemError: %s\n", err)
return
}
if !ok {
http.Error(w, "Unauthorized.", http.StatusUnauthorized)
return
}
}
}
// Host is a generic host
if strings.HasSuffix(req.Host, proxyConfiguration.Generic) {
slug = strings.Replace(req.Host, proxyConfiguration.Generic, "", -1)
}
// We found a valid slug before?
if target, ok := (*containers)[slug]; ok && slug != "" {
req.URL.Scheme = "http"
req.URL.Host = target[rand.Intn(len(target))]
req.Header.Add("X-Forwarded-For", d.normalizeRemoteAddr(req.RemoteAddr))
handler.ServeHTTP(w, req)
} else {
http.Error(w, "This host is currently not available", 503)
}
})
}