-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
231 lines (218 loc) · 6.57 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/gorilla/mux"
"github.com/kyokomi/emoji"
"github.com/namsral/flag"
)
type upStream struct {
name string
handle *http.Client
}
func (r upStream) pass() func(res http.ResponseWriter, req *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(res, "400 Bad request ; only GET allowed.", 400)
return
}
param := ""
if len(req.URL.RawQuery) > 0 {
param = "?" + req.URL.RawQuery
}
body, _ := r.get("http://docker"+req.URL.Path+param, res)
fmt.Fprintf(res, "%s", body)
}
}
func (r upStream) get(url string, res http.ResponseWriter) ([]byte, error) {
req, err := r.handle.Get(url)
if err != nil {
return nil, err
}
defer req.Body.Close()
contentType := req.Header.Get("Content-type")
if contentType != "" {
res.Header().Set("Content-type", contentType)
}
return ioutil.ReadAll(req.Body)
}
func newProxySocket(socket string) upStream {
stream := upStream{name: socket}
stream.handle = &http.Client{
Transport: &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
conn, err := net.Dial("unix", socket)
return conn, err
},
},
}
return stream
}
type stringSlice []string
func (s *stringSlice) String() string {
return fmt.Sprintf("%d", *s)
}
func (s *stringSlice) Set(value string) error {
log.Println(emoji.Sprint(":heavy_check_mark:") + "Allowing endpoint " + value)
*s = append(*s, value)
return nil
}
var allowedOptions = []string{
"containers", "images", "networks", "volumes",
"services", "tasks", "events", "info", "version",
"ping", "events", "auth", "secrets", "build", "commit",
"configs", "distribution", "exec", "nodes", "plugins",
"session", "swarm", "system",
}
case "events":
m.HandleFunc("/events", upstream.pass())
case "auth":
m.HandleFunc("/auth", upstream.pass())
case "secrets":
m.HandleFunc("/secrets", upstream.pass())
case "build":
m.HandleFunc("/build", upstream.pass())
case "commit":
m.HandleFunc("/commit", upstream.pass())
case "configs":
m.HandleFunc("/configs", upstream.pass())
case "distribution":
m.HandleFunc("/distribution", upstream.pass())
case "exec":
m.HandleFunc("/exec", upstream.pass())
case "nodes":
m.HandleFunc("/nodes", upstream.pass())
case "plugins":
m.HandleFunc("/plugins", upstream.pass())
case "session":
m.HandleFunc("/session", upstream.pass())
case "swarm":
m.HandleFunc("/swarm", upstream.pass())
case "system":
m.HandleFunc("/system", upstream.pass())
func main() {
fmt.Println("############################################")
fmt.Println("############# Docker Proxy ACL #############")
fmt.Println("############# by Quentin McGaw #############")
fmt.Println("############### Give some " + emoji.Sprint(":heart:") + "at ###############")
fmt.Println("# github.com/qdm12/docker-proxy-acl-alpine #")
fmt.Print("##############################################\n\n")
fs := flag.NewFlagSetWithEnvPrefix(os.Args[0], "GO", 0)
var (
allowed stringSlice
filename = fs.String("filename", "/tmp/docker-proxy-acl/docker.sock", "Location of socket file")
)
fs.Var(&allowed, "a", "Allowed location pattern prefix")
fs.Parse(os.Args[1:])
if len(allowed) < 1 {
log.Fatal(emoji.Sprint(":x:") + "Need at least 1 argument for -a: [" + strings.Join(allowedOptions, ",") + "]")
}
var ok bool
for i := range allowed {
ok = false
for j := range allowedOptions {
if allowed[i] == allowedOptions[j] {
ok = true
break
}
}
if !ok {
log.Fatal(emoji.Sprint(":x:") + "Argument " + allowed[i] + " not recognized")
}
}
var routers [2]*mux.Router
routers[0] = mux.NewRouter()
routers[1] = routers[0].PathPrefix("/{version:[v][0-9]+[.][0-9]+}").Subrouter()
upstream := newProxySocket("/var/run/docker.sock")
for i := range allowed {
log.Println(emoji.Sprint(":registered:") + "Registering " + allowed[i] + " handlers...")
for _, m := range routers {
switch allowed[i] {
case "containers":
containers := m.PathPrefix("/containers").Subrouter()
containers.HandleFunc("/json", upstream.pass())
containers.HandleFunc("/{name}/json", upstream.pass())
case "images":
images := m.PathPrefix("/images").Subrouter()
images.HandleFunc("/json", upstream.pass())
images.HandleFunc("/{name}/json", upstream.pass())
images.HandleFunc("/{name}/history", upstream.pass())
case "volumes":
m.HandleFunc("/volumes", upstream.pass())
m.HandleFunc("/volumes/{name}", upstream.pass())
case "networks":
m.HandleFunc("/networks", upstream.pass())
m.HandleFunc("/networks/{name}", upstream.pass())
case "services":
m.HandleFunc("/services", upstream.pass())
m.HandleFunc("/services/{name}", upstream.pass())
case "tasks":
m.HandleFunc("/tasks", upstream.pass())
m.HandleFunc("/tasks/{name}", upstream.pass())
case "events":
m.HandleFunc("/events", upstream.pass())
case "version":
m.HandleFunc("/version", upstream.pass())
case "info":
m.HandleFunc("/info", upstream.pass())
case "ping":
m.HandleFunc("/_ping", upstream.pass())
case "events":
m.HandleFunc("/events", upstream.pass())
case "auth":
m.HandleFunc("/auth", upstream.pass())
case "secrets":
m.HandleFunc("/secrets", upstream.pass())
case "build":
m.HandleFunc("/build", upstream.pass())
case "commit":
m.HandleFunc("/commit", upstream.pass())
case "configs":
m.HandleFunc("/configs", upstream.pass())
case "distribution":
m.HandleFunc("/distribution", upstream.pass())
case "exec":
m.HandleFunc("/exec", upstream.pass())
case "nodes":
m.HandleFunc("/nodes", upstream.pass())
case "plugins":
m.HandleFunc("/plugins", upstream.pass())
case "session":
m.HandleFunc("/session", upstream.pass())
case "swarm":
m.HandleFunc("/swarm", upstream.pass())
case "system":
m.HandleFunc("/system", upstream.pass())
}
}
}
http.Handle("/", routers[0])
listener, err := net.Listen("unix", *filename)
if err != nil {
log.Fatal(err)
}
os.Chmod(*filename, 0666)
// Looking up group ids coming up for Go 1.7
// https://github.com/golang/go/issues/2617
log.Println("Listening on " + *filename + emoji.Sprint(" :ear:"))
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
go waitForSignal(sigc, listener)
err = http.Serve(listener, nil)
if err != nil {
log.Fatal(err)
}
}
func waitForSignal(c chan os.Signal, listener net.Listener) {
sig := <-c
log.Println(emoji.Sprint(":heavy_exclamation_mark:") + "Caught signal '" + sig.String() + "', shutting down")
listener.Close()
os.Exit(0)
}