-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
318 lines (287 loc) · 10.7 KB
/
Copy pathserver.go
File metadata and controls
318 lines (287 loc) · 10.7 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package httpserver
import (
"encoding/json"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/uleroboticsgroup/Secdocker/docker-utilities"
)
var dockerEngineVersion = "v1.24"
// RunOpts is the specification of the package of the Docker API
type RunOpts struct {
Hostname string `json:"Hostname"`
Domainname string `json:"Domainname"`
User string `json:"User"`
AttachStdin bool `json:"AttachStdin"`
AttachStdout bool `json:"AttachStdout"`
AttachStderr bool `json:"AttachStderr"`
Tty bool `json:"Tty"`
OpenStdin bool `json:"OpenStdin"`
StdinOnce bool `json:"StdinOnce"`
Env []string `json:"Env"`
Cmd []string `json:"Cmd"`
Entrypoint string `json:"Entrypoint,omitempty"`
Image string `json:"Image"`
Labels map[string]string `json:"Labels"`
Volumes map[string]struct{} `json:"Volumes"`
WorkingDir string `json:"WorkingDir"`
NetworkDisabled bool `json:"NetworkDisabled"`
MacAddress string `json:"MacAddress"`
ExposedPorts map[string]struct{} `json:"ExposedPorts"`
StopSignal string `json:"StopSignal"`
StopTimeout int `json:"StopTimeout"`
HostConfig struct {
Binds []string `json:"Binds"`
Links []string `json:"Links"`
Memory int `json:"Memory"`
MemorySwap int `json:"MemorySwap"`
MemoryReservation int `json:"MemoryReservation"`
KernelMemory int `json:"KernelMemory"`
NanoCPUs int `json:"NanoCPUs"`
CPUPercent int `json:"CpuPercent"`
CPUShares int `json:"CpuShares"`
CPUPeriod int `json:"CpuPeriod"`
CPURealtimePeriod int `json:"CpuRealtimePeriod"`
CPURealtimeRuntime int `json:"CpuRealtimeRuntime"`
CPUQuota int `json:"CpuQuota"`
CpusetCpus string `json:"CpusetCpus"`
CpusetMems string `json:"CpusetMems"`
MaximumIOps int `json:"MaximumIOps"`
MaximumIOBps int `json:"MaximumIOBps"`
BlkioWeight int `json:"BlkioWeight"`
BlkioWeightDevice []struct {
Path string `json:"Path"`
Weight int `json:"Weight"`
} `json:"BlkioWeightDevice"`
BlkioDeviceReadBps []struct {
Path string `json:"Path"`
Rate int `json:"Rate"`
} `json:"BlkioDeviceReadBps"`
BlkioDeviceReadIOps []struct {
Path string `json:"Path"`
Rate int `json:"Rate"`
} `json:"BlkioDeviceReadIOps"`
BlkioDeviceWriteBps []struct {
Path string `json:"Path"`
Rate int `json:"Rate"`
} `json:"BlkioDeviceWriteBps"`
BlkioDeviceWriteIOps []struct {
Path string `json:"Path"`
Rate int `json:"Rate"`
} `json:"BlkioDeviceWriteIOps"`
DeviceRequests []struct {
Driver string `json:"Driver"`
Count int `json:"Count"`
DeviceIDs []string `json:"DeviceIDs"`
Capabilities [][]string `json:"Capabilities"`
Options map[string]string `json:"Options"`
} `json:"DeviceRequests"`
MemorySwappiness int `json:"MemorySwappiness"`
OomKillDisable bool `json:"OomKillDisable"`
OomScoreAdj int `json:"OomScoreAdj"`
PidMode string `json:"PidMode"`
PidsLimit int `json:"PidsLimit"`
PortBindings map[string][]struct {
HostIP string `json:"HostIp"`
HostPort string `json:"HostPort"`
} `json:"PortBindings"`
PublishAllPorts bool `json:"PublishAllPorts"`
Privileged bool `json:"Privileged"`
ReadonlyRootfs bool `json:"ReadonlyRootfs"`
DNS []string `json:"Dns"`
DNSOptions []string `json:"DnsOptions"`
DNSSearch []string `json:"DnsSearch"`
VolumesFrom []string `json:"VolumesFrom"`
CapAdd []string `json:"CapAdd"`
CapDrop []string `json:"CapDrop"`
GroupAdd []string `json:"GroupAdd"`
RestartPolicy struct {
Name string `json:"Name"`
MaximumRetryCount int `json:"MaximumRetryCount"`
} `json:"RestartPolicy"`
AutoRemove bool `json:"AutoRemove"`
NetworkMode string `json:"NetworkMode"`
Devices []interface{} `json:"Devices"`
Ulimits []struct {
Name string `json:"Name"`
Soft int `json:"Soft"`
Hard int `json:"Hard"`
} `json:"Ulimits"`
LogConfig struct {
Type string `json:"Type"`
Config struct {
} `json:"Config"`
} `json:"LogConfig"`
SecurityOpt []string `json:"SecurityOpt"`
StorageOpt map[string]string `json:"StorageOpt"`
CgroupParent string `json:"CgroupParent"`
VolumeDriver string `json:"VolumeDriver"`
ShmSize int `json:"ShmSize"`
} `json:"HostConfig"`
NetworkingConfig struct {
EndpointsConfig map[string]struct {
IPAMConfig struct {
IPv4Address string `json:"IPv4Address"`
IPv6Address string `json:"IPv6Address"`
LinkLocalIPs []string `json:"LinkLocalIPs"`
} `json:"IPAMConfig"`
Links []string `json:"Links"`
Aliases []string `json:"Aliases"`
NetworkID string `json:"NetworkID"`
EndpointID string `json:"EndpointID"`
Gateway string `json:"Gateway"`
IPAdress string `json:"IPAdress"`
IPPrefixLen int `json:"IPPrefixLen"`
IPv6Gateway string `json:"IPv6Gateway"`
GlobalIPv6Address string `json:"GlobalIPv6Address"`
GlobalIPv6PrefixLen int64 `json:"GlobalIPv6PrefixLen"`
MadAddress string `json:"MadAddress"`
DriverOpts map[string]string `json:"DriverOpts"`
} `json:"EndpointsConfig"`
} `json:"NetworkingConfig"`
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func createOptsFromAPIData(rawOpts RunOpts) docker.ContainerOpts {
containerOpts := docker.ContainerOpts{}
containerOpts.Mounts = rawOpts.HostConfig.Binds
containerOpts.Env = rawOpts.Env
containerOpts.Entrypoint = rawOpts.Entrypoint
containerOpts.Image = rawOpts.Image
containerOpts.SecurityPolicies = rawOpts.HostConfig.SecurityOpt
containerOpts.User = rawOpts.User
containerOpts.Privileged = rawOpts.HostConfig.Privileged
for _, v := range rawOpts.HostConfig.PortBindings {
for _, item := range v {
if item.HostIP != "" {
containerOpts.Ports = append(containerOpts.Ports, item.HostIP+":"+item.HostPort)
} else {
containerOpts.Ports = append(containerOpts.Ports, item.HostPort)
}
}
}
return containerOpts
}
func createRunDataFromOpts(rawOpts RunOpts, opts docker.ContainerOpts) RunOpts {
rawOpts.HostConfig.Binds = opts.Mounts
rawOpts.Env = opts.Env
rawOpts.Entrypoint = opts.Entrypoint
rawOpts.Image = opts.Image
rawOpts.HostConfig.SecurityOpt = opts.SecurityPolicies
rawOpts.User = opts.User
rawOpts.HostConfig.Privileged = opts.Privileged
if len(rawOpts.Cmd) == 0 {
rawOpts.Cmd = nil
}
return rawOpts
}
// ProcessCreateContainer handles a new http request to create a new container
func ProcessCreateContainer(req *http.Request) []byte {
log.Info("New create request received: " + req.Method + " " + req.RequestURI)
var rawOpts RunOpts
body, err := ioutil.ReadAll(req.Body)
checkErr(err)
if len(body) == 0 {
rawOpts.Image = req.URL.Query()["fromImage"][0] + ":" + req.URL.Query()["tag"][0]
} else {
err = json.Unmarshal(body, &rawOpts)
checkErr(err)
}
if opts := createOptsFromAPIData(rawOpts); docker.ProcessAPICreateRequest(opts) {
log.Info("Request is valid")
opts := docker.AddGeneralRestrictions(opts)
finalData := createRunDataFromOpts(rawOpts, opts)
rawDataToSend, err := json.Marshal(finalData)
checkErr(err)
return rawDataToSend
} else {
log.Info("Request is not valid")
}
return []byte{}
}
/*
#########################################
## THIS CODE ONLY ACCEPTS HTTP CONNECTION.
## Docker CLI uses special TCP packets to work properly
#########################################
func sendToAPI(method, url string, data []byte) *http.Response {
//https://docs.docker.com/engine/api/v1.40/#
dockerAPI := config.LoadConfig().DockerAPI
client := &http.Client{Timeout: 3 * time.Second}
if dockerAPI[0] == '/' {
fd := func(proto, addr string) (conn net.Conn, err error) {
return net.Dial("unix", dockerAPI)
}
tr := &http.Transport{
Dial: fd,
}
client = &http.Client{Transport: tr, Timeout: 3 * time.Second}
url = "http://docker" + url
} else {
url = dockerAPI + url
}
fmt.Println(method, url)
var resp *http.Response
if method == "POST" || method == "PUT" {
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
checkErr(err)
req.Header.Add("Content-Type", "application/json")
req.Header.Del("User-Agent")
fmt.Println(req.Header.Get("User-Agent"))
req.Header.Add("User-Agent", "Docker-Client/19.03.6")
fmt.Println(req.Header.Get("User-Agent"))
resp, err = client.Do(req)
checkErr(err)
} else {
req, err := http.NewRequest(method, url, nil)
checkErr(err)
req.Header.Set("User-Agent", "Docker-Client/19.03.6 (linux)")
resp, err = client.Do(req)
checkErr(err)
}
body, err := ioutil.ReadAll(resp.Body)
checkErr(err)
if resp.StatusCode > 250 { // 201 for container creation
log.Error(string(body))
}
return resp
}
func other(w http.ResponseWriter, req *http.Request) {
log.Info("New request received: " + req.Method + " " + req.URL.String())
body, err := ioutil.ReadAll(req.Body)
fmt.Println("New request received: " + req.Method + " " + req.URL.String())
fmt.Println(string(body))
checkErr(err)
serveReverseProxy(w, req, body)
}
func Start() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/containers/create", processCreateContainer)
r.HandleFunc("/v{id}/containers/create", processCreateContainer)
//r.HandleFunc("/v{id}/containers/{key}/wait", ProductHandler)
r.PathPrefix("/").HandlerFunc(other)
http.Handle("/", r)
fmt.Println("Listening on port 8999")
log.Fatal(http.ListenAndServe(":8999", nil))
return r
return r
}
// Serve a reverse proxy for a given url
func serveReverseProxy(res http.ResponseWriter, req *http.Request, body []byte) {
// parse the url
url, _ := url.Parse("http://localhost:2376")
// create the reverse proxy
proxy := httputil.NewSingleHostReverseProxy(url)
// Update the headers to allow for SSL redirection
req.URL.Host = url.Host
req.URL.Scheme = url.Scheme
//req.Header.Set("X-Forwarded-Host", req.Header.Get("Host"))
req.Host = url.Host
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
// Note that ServeHttp is non blocking and uses a go routine under the hood
proxy.ServeHTTP(res, req)
}
*/