-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (129 loc) · 6.25 KB
/
index.js
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
const fs = require('fs')
const yaml = require('js-yaml')
const fetch = require('node-fetch-npm')
const express = require('express')
const app = express()
app.use(express.json())
const port = 8081
app.listen(port, () => {
console.log(`Rest API listening on port ${port}`)
})
async function GetTraefikConfig() {
let Traefik_Coolify
await fetch('http://localhost:3000/webhooks/traefik/main.json')
.then(response => response.json())
.then(data => {
Traefik_Coolify = data
})
.catch(async (error) => {
await fetch('http://10.0.0.176:3000/webhooks/traefik/main.json')
.then(response => response.json())
.then(data => {
Traefik_Coolify = data
})
})
let Traefik_EasyPanel = yaml.load(fs.readFileSync('/etc/easypanel/traefik/config/main.yaml', {encoding: 'utf-8'}))
let Traefik_VPN = yaml.load(fs.readFileSync('/etc/traefik/traefik-vpn.yml', {encoding: 'utf-8'}))
let Traefik_WEB = yaml.load(fs.readFileSync('/etc/traefik/traefik-web.yml', {encoding: 'utf-8'}))
console.log(Traefik_Coolify)
console.log(Traefik_EasyPanel)
console.log(Traefik_VPN)
console.log(Traefik_WEB)
let Traefik_Compiled = {
http: {
routers: {},
services: {},
middlewares: {}
},
tcp: {
routers: {},
services: {}
}
}
console.log('-'.repeat(100))
for (const router in Traefik_Coolify['http']['routers']) {
let router_data = Traefik_Coolify['http']['routers'][router]
indexWeb = router_data['entrypoints'].indexOf('web')
if (indexWeb != -1) router_data['entrypoints'][indexWeb] = 'http'
indexWebSecure = router_data['entrypoints'].indexOf('websecure')
if (indexWebSecure != -1) router_data['entrypoints'][indexWebSecure] = 'https'
if (router_data['rule'].includes('Host(`www.')) continue
//indexRedirectHttps = router_data['middlewares'].indexOf('redirect-to-https')
//if (indexRedirectHttps != -1) router_data['middlewares'].splice(indexRedirectHttps)
Traefik_Compiled['http']['routers'][router] = router_data
console.log(router, Traefik_Compiled['http']['routers'][router])
}
for (const router in Traefik_EasyPanel['http']['routers']) {
let router_data = Traefik_EasyPanel['http']['routers'][router]
if (router_data['service'] == 'error-pages') continue
if (router_data['tls']) router_data['tls'] = { certresolver: 'letsencrypt' }
if (router_data['rule'].includes('.easypanel.host`)')) continue
if (router.includes('easypanel-ip')) continue
//indexRedirectHttps = router_data['middlewares'].indexOf('redirect-to-https')
//if (indexRedirectHttps != -1) router_data['middlewares'].splice(indexRedirectHttps)
Traefik_Compiled['http']['routers'][router] = router_data
console.log(router, Traefik_Compiled['http']['routers'][router])
}
for (const router in Traefik_VPN['tcp']['routers']) {
let router_data = Traefik_VPN['tcp']['routers'][router]
Traefik_Compiled['tcp']['routers'][router] = router_data
console.log(router, Traefik_Compiled['tcp']['routers'][router])
}
for (const router in Traefik_WEB['http']['routers']) {
let router_data = Traefik_WEB['http']['routers'][router]
router_data['tls'] = { certresolver: 'letsencrypt' }
Traefik_Compiled['http']['routers'][router] = router_data
console.log(router, Traefik_Compiled['http']['routers'][router])
}
console.log(Object.keys(Traefik_Compiled['http']['routers']).length)
console.log(Object.keys(Traefik_Compiled['tcp']['routers']).length)
console.log('-'.repeat(100))
for (const service in Traefik_Coolify['http']['services']) {
let service_data = Traefik_Coolify['http']['services'][service]
Traefik_Compiled['http']['services'][service] = service_data
console.log(service, Traefik_Compiled['http']['services'][service])
}
for (const service in Traefik_EasyPanel['http']['services']) {
let service_data = Traefik_EasyPanel['http']['services'][service]
if (service == 'error-pages') continue
Traefik_Compiled['http']['services'][service] = service_data
console.log(service, Traefik_Compiled['http']['services'][service])
}
for (const service in Traefik_VPN['tcp']['services']) {
let service_data = Traefik_VPN['tcp']['services'][service]
Traefik_Compiled['tcp']['services'][service] = service_data
console.log(service, Traefik_Compiled['tcp']['services'][service])
}
for (const service in Traefik_WEB['http']['services']) {
let service_data = Traefik_WEB['http']['services'][service]
Traefik_Compiled['http']['services'][service] = service_data
console.log(service, Traefik_Compiled['http']['services'][service])
}
console.log(Object.keys(Traefik_Compiled['http']['services']).length)
console.log(Object.keys(Traefik_Compiled['tcp']['services']).length)
console.log('-'.repeat(100))
for (const middleware in Traefik_Coolify['http']['middlewares']) {
let middleware_data = Traefik_Coolify['http']['middlewares'][middleware]
if (middleware.includes('-www')) continue
if (middleware == 'redirect-to-https') continue
if (middleware == 'redirect-to-http') continue
Traefik_Compiled['http']['middlewares'][middleware] = middleware_data
console.log(middleware)
}
for (const middleware in Traefik_EasyPanel['http']['middlewares']) {
let middleware_data = Traefik_EasyPanel['http']['middlewares'][middleware]
if (middleware == 'error-pages') continue
//if (middleware == 'redirect-to-https') continue
Traefik_Compiled['http']['middlewares'][middleware] = middleware_data
console.log(middleware)
}
console.log(Object.keys(Traefik_Compiled['http']['middlewares']).length)
if (Object.keys(Traefik_Compiled['http']['middlewares']).length == 0) delete Traefik_Compiled['http']['middlewares']
console.log(Traefik_Compiled)
return Traefik_Compiled
}
GetTraefikConfig()
app.get('/', async (req, res) => {
console.log('-'.repeat(100))
res.status(200).json(await GetTraefikConfig())
})