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
122 lines (104 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
package main
import (
"log"
"math/rand"
"net/http"
"strings"
"time"
"github.com/Luzifer/dockerproxy/sni"
"github.com/elazarl/goproxy"
"github.com/Luzifer/dockerproxy/auth"
_ "github.com/Luzifer/dockerproxy/auth/basic"
)
type DockerProxy struct {
proxy *goproxy.ProxyHttpServer
}
func NewDockerProxy() *DockerProxy {
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest().HandleConnect(goproxy.AlwaysReject)
// 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(remote_addr string) string {
idx := strings.LastIndex(remote_addr, ":")
if idx != -1 {
remote_addr = remote_addr[0:idx]
if remote_addr[0] == '[' && remote_addr[len(remote_addr)-1] == ']' {
remote_addr = remote_addr[1 : len(remote_addr)-1]
}
}
return remote_addr
}
func (d *DockerProxy) httpLog(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s %s", d.normalizeRemoteAddr(r.RemoteAddr), r.Method, r.Host, r.URL)
handler.ServeHTTP(w, r)
})
}
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)
}
})
}