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
/
main.go
140 lines (122 loc) · 3.28 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/Luzifer/dockerproxy/sni"
"github.com/fsouza/go-dockerclient"
"gopkg.in/elazarl/goproxy.v1"
)
func loadConfig(configFile *string) config {
file, e := ioutil.ReadFile(*configFile)
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
var cfg config
err := json.Unmarshal(file, &cfg)
if err != nil {
fmt.Printf("JSON error: %v\n", err)
os.Exit(1)
}
return cfg
}
func collectDockerContainer(cfg *config) map[string]string {
result := make(map[string]string)
for dockerHostPrivate, dockerHost := range cfg.Docker.Hosts {
endpoint := fmt.Sprintf("tcp://%s:%d", dockerHostPrivate, cfg.Docker.Port)
client, _ := docker.NewClient(endpoint)
containers, _ := client.ListContainers(docker.ListContainersOptions{})
for _, apiContainer := range containers {
container, _ := client.InspectContainer(apiContainer.ID)
currentEnv := make(map[string]string)
for _, envVar := range container.Config.Env {
var k, v string
unpack(strings.Split(envVar, "="), &k, &v)
currentEnv[k] = v
}
if slug, ok := currentEnv["ROUTER_SLUG"]; ok {
port := currentEnv["ROUTER_PORT"]
result[slug] = fmt.Sprintf("%s:%s", dockerHost, port)
}
}
}
return result
}
func unpack(s []string, vars ...*string) {
for i, str := range s {
*vars[i] = str
}
}
func main() {
var configFile = flag.String("configfile", "./config.json", "Location of the configuration file")
flag.Parse()
proxy := goproxy.NewProxyHttpServer()
cfg := loadConfig(configFile)
containers := collectDockerContainer(&cfg)
// 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) {
slug := ""
// Host is defined and slug has been found
if host, ok := cfg.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
}
}
// Host is a generic host
if strings.HasSuffix(req.Host, cfg.Generic) {
slug = strings.Replace(req.Host, cfg.Generic, "", -1)
}
// We found a valid slug before?
if target, ok := containers[slug]; ok {
req.URL.Scheme = "http"
req.URL.Host = target
proxy.ServeHTTP(w, req)
} else {
http.Error(w, "This host is currently not available", 502)
}
})
var certs []sni.Certificates
for _, domain := range cfg.Domains {
if domain.SSL.Cert != "" {
certs = append(certs, sni.Certificates{
CertFile: domain.SSL.Cert,
KeyFile: domain.SSL.Key,
})
}
}
httpChan := make(chan error)
httpsChan := make(chan error)
loaderChan := time.NewTicker(time.Minute).C
go func(proxy *goproxy.ProxyHttpServer) {
httpChan <- http.ListenAndServe(cfg.ListenHTTP, proxy)
}(proxy)
go func(*goproxy.ProxyHttpServer) {
httpsServer := &http.Server{
Handler: proxy,
Addr: cfg.ListenHTTPS,
}
httpsChan <- sni.ListenAndServeTLSSNI(httpsServer, certs)
}(proxy)
for {
select {
case httpErr := <-httpChan:
log.Fatal(httpErr)
case httpsErr := <-httpsChan:
log.Fatal(httpsErr)
case <-loaderChan:
cfg = loadConfig(configFile)
containers = collectDockerContainer(&cfg)
}
}
}