-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traefik.go
274 lines (254 loc) · 7.18 KB
/
traefik.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
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
package main
import (
"fmt"
"github.com/imdario/mergo"
"gopkg.in/yaml.v3"
)
type GlobalStruct struct {
CheckNewVersion bool `yaml:"checkNewVersion"`
SendAnonymousUsage bool `yaml:"sendAnonymousUsage"`
}
type EntryPointHttpStruct struct {
Address string `yaml:"address"`
}
type HttpTlsDomainsStruct struct {
Main string `yaml:"main"`
Sans []string `yaml:"sans"`
}
type TlsStruct struct {
CertResolver string `yaml:"certResolver,omitempty"`
Domains []HttpTlsDomainsStruct `yaml:"domains"`
}
type HttpsHttpStruct struct {
Tls TlsStruct
}
type ForwardedHeadersStruct struct {
TrustedIPs []string `yaml:"trustedIPs,omitempty"`
}
type EntryPointHttpsStruct struct {
Address string `yaml:"address"`
ForwardedHeaders ForwardedHeadersStruct `yaml:"forwardedHeaders,omitempty"`
Http HttpsHttpStruct `yaml:"http"`
}
type EntryPointPing struct {
Address string `yaml:"address"`
}
type EntryPoints struct {
Http EntryPointHttpStruct `yaml:"http"`
Https EntryPointHttpsStruct `yaml:"https"`
Ping EntryPointPing `yaml:"ping"`
}
type ExperimentalStruct struct {
Hub bool `yaml:"hub"`
}
type HubTlsStruct struct {
Insecure bool `yaml:"insecure"`
}
type HubStruct struct {
Tls HubTlsStruct `yaml:"tls"`
}
type PrometheusStruct struct {
AddRoutersLabels bool `yaml:"addRoutersLabels"`
}
type MetricsStruct struct {
Prometheus PrometheusStruct `yaml:"prometheus"`
}
type ApiStruct struct {
Insecure bool `yaml:"insecure"`
Dashboard bool `yaml:"dashboard"`
}
type PingStruct struct {
EntryPoint string `yaml:"entryPoint"`
}
type LogStruct struct {
Level string `yaml:"level"`
FilePath string `yaml:"filePath"`
}
type AccessLogFiltersStruct struct {
StatusCodes []string `yaml:"statusCodes"`
}
type AccessLogStruct struct {
FilePath string `yaml:"filePath"`
BufferingSize int `yaml:"bufferingSize"`
Filters AccessLogFiltersStruct `yaml:"filters"`
}
type ProvidersDockerStruct struct {
Endpoint string `yaml:"endpoint"`
ExposedByDefault bool `yaml:"exposedByDefault"`
Network string `yaml:"network"`
SwarmMode bool `yaml:"swarmMode"`
}
type ProvidersFileStruct struct {
Directory string `yaml:"directory"`
Watch bool `yaml:"watch"`
}
type ProvidersStruct struct {
Docker ProvidersDockerStruct `yaml:"docker"`
File ProvidersFileStruct `yaml:"file"`
}
type DnsChallengeStruct struct {
Provider string `yaml:"provider,omitempty"`
Resolvers []string `yaml:"resolvers,omitempty"`
DelayBeforeCheck string `yaml:"delayBeforeCheck,omitempty"`
}
type CertAcmeStruct struct {
Email string `yaml:"email,omitempty"`
Storage string `yaml:"storage,omitempty"`
DnsChallenge DnsChallengeStruct `yaml:"dnsChallenge,omitempty"`
}
type CertDnsCloudflareStruct struct {
Acme CertAcmeStruct `yaml:"acme,omitempty"`
}
type CertificatesResolversStruct struct {
DnsCloudflare CertDnsCloudflareStruct `yaml:"dns-cloudflare,omitempty"`
}
type TraefikConfig struct {
Global GlobalStruct `yaml:"global"`
EntryPoints EntryPoints `yaml:"entryPoints"`
Experimental ExperimentalStruct `yaml:"experimental,omitempty"`
Hub HubStruct `yaml:"hub"`
Metrics MetricsStruct `yaml:"metrics"`
Api ApiStruct `yaml:"api"`
Ping PingStruct `yaml:"ping"`
Log LogStruct `yaml:"log"`
AccessLog AccessLogStruct `yaml:"accessLog"`
Providers ProvidersStruct `yaml:"providers"`
CertificatesResolvers CertificatesResolversStruct `yaml:"certificatesResolvers,omitempty"`
}
/*
Create traefik config file
*/
func createTraefikFile() {
entryPointsHttps := EntryPointHttpsStruct{
Address: ":443",
Http: HttpsHttpStruct{
Tls: TlsStruct{
Domains: []HttpTlsDomainsStruct{
{
Main: "traefik." + configRootDomain,
Sans: []string{
"*." + configRootDomain,
},
},
},
},
},
}
certificatesResolvers := CertificatesResolversStruct{}
if configUseCloudflare == true {
entryPointsHttpsCloudflare := EntryPointHttpsStruct{
Http: HttpsHttpStruct{
Tls: TlsStruct{
CertResolver: "dns-cloudflare",
},
},
ForwardedHeaders: ForwardedHeadersStruct{
TrustedIPs: []string{
"173.245.48.0/20",
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"141.101.64.0/18",
"108.162.192.0/18",
"190.93.240.0/20",
"188.114.96.0/20",
"197.234.240.0/22",
"198.41.128.0/17",
"162.158.0.0/15",
"104.16.0.0/13",
"104.24.0.0/14",
"172.64.0.0/13",
"131.0.72.0/22",
},
},
}
certificatesResolversCloudflare := CertificatesResolversStruct{
DnsCloudflare: CertDnsCloudflareStruct{
Acme: CertAcmeStruct{
Email: configCloudflareEmail,
Storage: "/etc/traefik/acme.json",
DnsChallenge: DnsChallengeStruct{
Provider: "cloudflare",
Resolvers: []string{"1.1.1.1:53", "1.0.0.1:54"},
DelayBeforeCheck: "90",
},
},
},
}
mergo.Merge(&entryPointsHttps, entryPointsHttpsCloudflare)
mergo.Merge(&certificatesResolvers, certificatesResolversCloudflare)
}
traefikConfig := TraefikConfig{
Global: GlobalStruct{
CheckNewVersion: true,
SendAnonymousUsage: false,
},
EntryPoints: EntryPoints{
Http: EntryPointHttpStruct{
Address: ":80",
},
Https: entryPointsHttps,
Ping: EntryPointPing{
Address: ":8081",
},
},
Experimental: ExperimentalStruct{
Hub: configUseTraefikHub,
},
Hub: HubStruct{
Tls: HubTlsStruct{
Insecure: false,
},
},
Metrics: MetricsStruct{
Prometheus: PrometheusStruct{
AddRoutersLabels: true,
},
},
Api: ApiStruct{
Insecure: false,
Dashboard: true,
},
Ping: PingStruct{
EntryPoint: "ping",
},
Log: LogStruct{
Level: "WARN",
FilePath: "/etc/traefik/traefik.log",
},
AccessLog: AccessLogStruct{
FilePath: "/etc/traefik/access.log",
BufferingSize: 100,
Filters: AccessLogFiltersStruct{
StatusCodes: []string{"400-499"},
},
},
Providers: ProvidersStruct{
Docker: ProvidersDockerStruct{
Endpoint: "tcp://socket-proxy:2375",
ExposedByDefault: false,
Network: "traefik-proxy",
SwarmMode: false,
},
File: ProvidersFileStruct{
Directory: "/etc/traefik/rules",
Watch: true,
},
},
CertificatesResolvers: certificatesResolvers,
}
yamlData, err := yaml.Marshal(&traefikConfig)
if err != nil {
fmt.Printf("Error while Marshaling. %v", err)
}
writeFile("AppData/traefik-proxy", "acme.json", []byte(""), 0600)
writeFile("AppData/traefik-proxy", "traefik.yml", yamlData, 0644)
if configUseCloudflare == true {
writeFile("secrets", "cf_email", []byte(configCloudflareEmail), 0600)
writeFile("secrets", "cf_api_key", []byte(configCloudflareApiKey), 0600)
}
if configIsLocalEnvironment == true {
traefikTlsDynamicConfig := createTraefikTlsFile()
writeFile("AppData/traefik-proxy/rules", "local-tls.yml", traefikTlsDynamicConfig, 0644)
}
}