-
Notifications
You must be signed in to change notification settings - Fork 0
/
entries.go
239 lines (201 loc) · 5.33 KB
/
entries.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
232
233
234
235
236
237
238
239
package main
import (
"fmt"
"regexp"
"strings"
"github.com/docker/docker/api/types"
)
const (
labelPrefix string = "ghosts"
defaultCategory string = "apps"
)
type entriesManager struct {
docker docker
config config
}
type segment struct {
Hosts []string
Paths []string
Port string
Proto string
}
type entry struct {
Segments map[string]segment
IP string
NetworkID string
Name string
Category []string
Description string
Logo string
Auth bool
NoWeb bool
NoHosts bool
NoNetAutoConnect bool
Direct bool
WebDirect bool
}
func newEntriesManager(docker docker, config config) entriesManager {
return entriesManager{docker, config}
}
func (em entriesManager) get(ids ...string) ([]entry, error) {
var entries []entry
containers, err := em.docker.getContainers(ids...)
if err != nil {
return entries, err
}
for _, container := range containers {
entry := entry{}
// Skip if no segments
entry.Segments = parseSegments(container)
if len(entry.Segments) == 0 {
continue
}
// Take the IP of the first network and the network ID
for _, n := range container.NetworkSettings.Networks {
entry.IP = n.IPAddress
entry.NetworkID = n.NetworkID
break
}
// Name
entry.Name = "unknown"
if val, ok := container.Labels[fmt.Sprintf("%s.name", labelPrefix)]; ok {
entry.Name = val
} else if len(container.Names) > 0 {
entry.Name = strings.TrimPrefix(container.Names[0], "/")
}
// Auth
entry.Auth = false
if val, ok := container.Labels[fmt.Sprintf("%s.auth", labelPrefix)]; ok && val == "true" {
entry.Auth = true
}
// Category
entry.Category = []string{defaultCategory}
if val, ok := container.Labels[fmt.Sprintf("%s.category", labelPrefix)]; ok {
val = strings.ToLower(val)
entry.Category = strings.Split(val, ",")
}
// Logo
if val, ok := container.Labels[fmt.Sprintf("%s.logo", labelPrefix)]; ok {
entry.Logo = val
}
// Description
if val, ok := container.Labels[fmt.Sprintf("%s.description", labelPrefix)]; ok {
entry.Description = val
}
// No Web
entry.NoWeb = false
if val, ok := container.Labels[fmt.Sprintf("%s.noweb", labelPrefix)]; ok && val == "true" {
entry.NoWeb = true
}
// No Hosts
entry.NoHosts = false
if val, ok := container.Labels[fmt.Sprintf("%s.nohosts", labelPrefix)]; ok && val == "true" {
entry.NoHosts = true
}
// No Net Auto Connect
entry.NoNetAutoConnect = false
if val, ok := container.Labels[fmt.Sprintf("%s.nonetautoconnect", labelPrefix)]; ok && val == "true" {
entry.NoNetAutoConnect = true
}
// Direct
entry.Direct = false
if val, ok := container.Labels[fmt.Sprintf("%s.direct", labelPrefix)]; ok && val == "true" {
entry.Direct = true
}
// Web Direct
entry.WebDirect = false
if val, ok := container.Labels[fmt.Sprintf("%s.webdirect", labelPrefix)]; ok && val == "true" {
entry.WebDirect = true
}
entries = append(entries, entry)
}
return entries, nil
}
func parseSegments(container types.Container) map[string]segment {
segments := make(map[string]segment)
regex := fmt.Sprintf("%s\\.([a-zA-Z0-9_-]+)\\.", labelPrefix)
rHosts := regexp.MustCompile(fmt.Sprint(regex, "host"))
rPaths := regexp.MustCompile(fmt.Sprint(regex, "path"))
rPort := regexp.MustCompile(fmt.Sprint(regex, "port"))
rProto := regexp.MustCompile(fmt.Sprint(regex, "proto"))
hostsMap := make(map[string][]string)
pathsMap := make(map[string][]string)
portMap := make(map[string]string)
protoMap := make(map[string]string)
for key, value := range container.Labels {
// Segment Hosts
if match := rHosts.FindStringSubmatch(key); match != nil {
name := match[1]
for _, host := range strings.Split(value, ",") {
hostsMap[name] = append(hostsMap[name], host)
}
}
// Segment Paths
if match := rPaths.FindStringSubmatch(key); match != nil {
name := match[1]
for _, path := range strings.Split(value, ",") {
pathsMap[name] = append(pathsMap[name], path)
}
}
// Segment port
if match := rPort.FindStringSubmatch(key); match != nil {
name := match[1]
portMap[name] = value
}
// Segment proto
if match := rProto.FindStringSubmatch(key); match != nil {
name := match[1]
protoMap[name] = value
}
// Default Hosts
if key == fmt.Sprintf("%s.host", labelPrefix) {
for _, u := range strings.Split(value, ",") {
hostsMap[""] = append(hostsMap[""], u)
}
}
// Default Paths
if key == fmt.Sprintf("%s.path", labelPrefix) {
for _, u := range strings.Split(value, ",") {
pathsMap[""] = append(pathsMap[""], u)
}
}
// Default Port
if key == fmt.Sprintf("%s.port", labelPrefix) {
portMap[""] = value
}
// Default Proto
if key == fmt.Sprintf("%s.proto", labelPrefix) {
protoMap[""] = value
}
}
// Take the first port exposed
defaultPort := "80"
for _, p := range container.Ports {
defaultPort = fmt.Sprint(p.PrivatePort)
break
}
// Bind to create segments
for name, hosts := range hostsMap {
s := segment{Hosts: hosts}
// Bind paths
if paths, ok := pathsMap[name]; ok {
s.Paths = paths
} else {
s.Paths = []string{"/"}
}
// Bind port
if port, ok := portMap[name]; ok {
s.Port = port
} else {
s.Port = defaultPort
}
// Bind proto
if proto, ok := protoMap[name]; ok {
s.Proto = proto
} else {
s.Proto = "http"
}
segments[name] = s
}
return segments
}