WIP better automatic proxy conf
This commit is contained in:
parent
2daa043840
commit
69d1556a1d
@ -74,6 +74,7 @@
|
|||||||
"js-cookie": "3.0.1",
|
"js-cookie": "3.0.1",
|
||||||
"js-yaml": "4.1.0",
|
"js-yaml": "4.1.0",
|
||||||
"jsonwebtoken": "8.5.1",
|
"jsonwebtoken": "8.5.1",
|
||||||
|
"mustache": "^4.2.0",
|
||||||
"node-forge": "1.2.1",
|
"node-forge": "1.2.1",
|
||||||
"svelte-kit-cookie-session": "2.1.2",
|
"svelte-kit-cookie-session": "2.1.2",
|
||||||
"tailwindcss-scrollbar": "^0.1.0",
|
"tailwindcss-scrollbar": "^0.1.0",
|
||||||
|
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
@ -35,6 +35,7 @@ specifiers:
|
|||||||
js-yaml: 4.1.0
|
js-yaml: 4.1.0
|
||||||
jsonwebtoken: 8.5.1
|
jsonwebtoken: 8.5.1
|
||||||
lint-staged: 12.3.4
|
lint-staged: 12.3.4
|
||||||
|
mustache: ^4.2.0
|
||||||
node-forge: 1.2.1
|
node-forge: 1.2.1
|
||||||
postcss: 8.4.6
|
postcss: 8.4.6
|
||||||
prettier: 2.5.1
|
prettier: 2.5.1
|
||||||
@ -70,6 +71,7 @@ dependencies:
|
|||||||
js-cookie: 3.0.1
|
js-cookie: 3.0.1
|
||||||
js-yaml: 4.1.0
|
js-yaml: 4.1.0
|
||||||
jsonwebtoken: 8.5.1
|
jsonwebtoken: 8.5.1
|
||||||
|
mustache: 4.2.0
|
||||||
node-forge: 1.2.1
|
node-forge: 1.2.1
|
||||||
svelte-kit-cookie-session: 2.1.2
|
svelte-kit-cookie-session: 2.1.2
|
||||||
tailwindcss-scrollbar: 0.1.0_tailwindcss@3.0.23
|
tailwindcss-scrollbar: 0.1.0_tailwindcss@3.0.23
|
||||||
@ -4091,6 +4093,14 @@ packages:
|
|||||||
msgpackr-extract: 1.0.15
|
msgpackr-extract: 1.0.15
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
/mustache/4.2.0:
|
||||||
|
resolution:
|
||||||
|
{
|
||||||
|
integrity: sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==
|
||||||
|
}
|
||||||
|
hasBin: true
|
||||||
|
dev: false
|
||||||
|
|
||||||
/nan/2.15.0:
|
/nan/2.15.0:
|
||||||
resolution:
|
resolution:
|
||||||
{
|
{
|
||||||
|
@ -16,6 +16,7 @@ model Setting {
|
|||||||
maxPort Int @default(9100)
|
maxPort Int @default(9100)
|
||||||
proxyPassword String
|
proxyPassword String
|
||||||
proxyUser String
|
proxyUser String
|
||||||
|
proxyHash String?
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
}
|
}
|
||||||
|
130
src/lib/haproxy/configuration.ts
Normal file
130
src/lib/haproxy/configuration.ts
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
import { dev } from '$app/env';
|
||||||
|
import got from 'got';
|
||||||
|
import mustache from 'mustache';
|
||||||
|
import crypto from 'crypto';
|
||||||
|
|
||||||
|
import * as db from '$lib/database';
|
||||||
|
import { checkContainer, checkHAProxy } from '.';
|
||||||
|
import { getDomain } from '$lib/common';
|
||||||
|
|
||||||
|
const url = dev ? 'http://localhost:5555' : 'http://coolify-haproxy:5555';
|
||||||
|
|
||||||
|
let template = `#coolhash={{hash}}
|
||||||
|
program api
|
||||||
|
command /usr/bin/dataplaneapi -f /usr/local/etc/haproxy/dataplaneapi.hcl --userlist haproxy-dataplaneapi
|
||||||
|
no option start-on-reload
|
||||||
|
|
||||||
|
global
|
||||||
|
stats socket /var/run/api.sock user haproxy group haproxy mode 660 level admin expose-fd listeners
|
||||||
|
log stdout format raw local0 debug
|
||||||
|
|
||||||
|
defaults
|
||||||
|
mode http
|
||||||
|
log global
|
||||||
|
timeout http-request 60s
|
||||||
|
timeout connect 10s
|
||||||
|
timeout client 60s
|
||||||
|
timeout server 60s
|
||||||
|
|
||||||
|
userlist haproxy-dataplaneapi
|
||||||
|
user admin insecure-password "\${HAPROXY_PASSWORD}"
|
||||||
|
|
||||||
|
frontend http
|
||||||
|
mode http
|
||||||
|
bind :80
|
||||||
|
bind :443 ssl crt /usr/local/etc/haproxy/ssl/ alpn h2,http/1.1
|
||||||
|
acl is_certbot path_beg /.well-known/acme-challenge/
|
||||||
|
{{#applications}}
|
||||||
|
{{#isHttps}}
|
||||||
|
http-request redirect scheme https code ${
|
||||||
|
dev ? 302 : 301
|
||||||
|
} if { hdr(host) -i {{domain}} } !{ ssl_fc }
|
||||||
|
{{/isHttps}}
|
||||||
|
http-request redirect location {{{redirectValue}}} code ${
|
||||||
|
dev ? 302 : 301
|
||||||
|
} if { req.hdr(host) -i {{redirectTo}} }
|
||||||
|
{{/applications}}
|
||||||
|
use_backend backend-certbot if is_certbot
|
||||||
|
use_backend %[req.hdr(host),lower]
|
||||||
|
|
||||||
|
frontend stats
|
||||||
|
bind *:8404
|
||||||
|
stats enable
|
||||||
|
stats uri /
|
||||||
|
stats refresh 5s
|
||||||
|
stats admin if TRUE
|
||||||
|
stats auth "\${HAPROXY_USERNAME}:\${HAPROXY_PASSWORD}"
|
||||||
|
|
||||||
|
backend backend-certbot
|
||||||
|
mode http
|
||||||
|
server certbot host.docker.internal:9080
|
||||||
|
|
||||||
|
{{#applications}}
|
||||||
|
|
||||||
|
backend {{domain}}
|
||||||
|
option forwardfor
|
||||||
|
server {{id}} {{id}}:{{port}}
|
||||||
|
{{/applications}}
|
||||||
|
`;
|
||||||
|
export async function haproxyInstance() {
|
||||||
|
const { proxyPassword } = await db.listSettings();
|
||||||
|
return got.extend({
|
||||||
|
prefixUrl: url,
|
||||||
|
username: 'admin',
|
||||||
|
password: proxyPassword
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function configureHAProxy() {
|
||||||
|
const haproxy = await haproxyInstance();
|
||||||
|
await checkHAProxy(haproxy);
|
||||||
|
const data = {
|
||||||
|
applications: [],
|
||||||
|
services: []
|
||||||
|
};
|
||||||
|
const applications = await db.prisma.application.findMany({
|
||||||
|
include: { destinationDocker: true }
|
||||||
|
});
|
||||||
|
for (const application of applications) {
|
||||||
|
const {
|
||||||
|
fqdn,
|
||||||
|
id,
|
||||||
|
port,
|
||||||
|
destinationDocker: { engine }
|
||||||
|
} = application;
|
||||||
|
const isRunning = await checkContainer(engine, id);
|
||||||
|
if (isRunning) {
|
||||||
|
const domain = getDomain(fqdn);
|
||||||
|
const isHttps = fqdn.startsWith('https://');
|
||||||
|
const isWWW = fqdn.includes('www.');
|
||||||
|
const redirectValue = `${isHttps ? 'https://' : 'http://'}${domain}%[capture.req.uri]`;
|
||||||
|
data.applications.push({
|
||||||
|
id,
|
||||||
|
port,
|
||||||
|
domain,
|
||||||
|
isHttps,
|
||||||
|
redirectValue,
|
||||||
|
redirectTo: isWWW ? domain : 'www.' + domain
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const output = mustache.render(template, data);
|
||||||
|
const newHash = crypto.createHash('md5').update(JSON.stringify(template)).digest('hex');
|
||||||
|
const { proxyHash, id } = await db.listSettings();
|
||||||
|
console.log(proxyHash, newHash);
|
||||||
|
if (proxyHash !== newHash) {
|
||||||
|
await db.prisma.setting.update({ where: { id }, data: { proxyHash: newHash } });
|
||||||
|
console.log('HAProxy configuration changed, updating...');
|
||||||
|
await haproxy.post(`v2/services/haproxy/configuration/raw`, {
|
||||||
|
searchParams: {
|
||||||
|
skip_version: true
|
||||||
|
},
|
||||||
|
body: output,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'text/plain'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log('HAProxy configuration is up to date');
|
||||||
|
}
|
||||||
|
}
|
@ -214,306 +214,306 @@ export async function checkProxyConfigurations() {
|
|||||||
console.log(error.response.body);
|
console.log(error.response.body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export async function configureHAProxy(
|
// export async function configureHAProxy(
|
||||||
haproxy,
|
// haproxy,
|
||||||
transactionId,
|
// transactionId,
|
||||||
fqdn,
|
// fqdn,
|
||||||
id,
|
// id,
|
||||||
port,
|
// port,
|
||||||
containerRunning,
|
// containerRunning,
|
||||||
engine
|
// engine
|
||||||
) {
|
// ) {
|
||||||
const domain = getDomain(fqdn);
|
// const domain = getDomain(fqdn);
|
||||||
const isHttps = fqdn.startsWith('https://');
|
// const isHttps = fqdn.startsWith('https://');
|
||||||
const isWWW = fqdn.includes('www.');
|
// const isWWW = fqdn.includes('www.');
|
||||||
const redirectValue = `${isHttps ? 'https://' : 'http://'}${domain}%[capture.req.uri]`;
|
// const redirectValue = `${isHttps ? 'https://' : 'http://'}${domain}%[capture.req.uri]`;
|
||||||
const contTest = `{ req.hdr(host) -i ${isWWW ? domain.replace('www.', '') : `www.${domain}`} }`;
|
// const contTest = `{ req.hdr(host) -i ${isWWW ? domain.replace('www.', '') : `www.${domain}`} }`;
|
||||||
|
|
||||||
// console.log({ fqdn, domain, id, port, containerRunning, isHttps, isWWW });
|
// // console.log({ fqdn, domain, id, port, containerRunning, isHttps, isWWW });
|
||||||
|
|
||||||
if (!containerRunning) {
|
// if (!containerRunning) {
|
||||||
try {
|
// try {
|
||||||
await haproxy.get(`v2/services/haproxy/configuration/backends/${domain}`).json();
|
// await haproxy.get(`v2/services/haproxy/configuration/backends/${domain}`).json();
|
||||||
console.log('removing', domain);
|
// console.log('removing', domain);
|
||||||
transactionId = await getNextTransactionId();
|
// transactionId = await getNextTransactionId();
|
||||||
await haproxy
|
// await haproxy
|
||||||
.delete(`v2/services/haproxy/configuration/backends/${domain}`, {
|
// .delete(`v2/services/haproxy/configuration/backends/${domain}`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId
|
// transaction_id: transactionId
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
if (error?.response?.body) {
|
// if (error?.response?.body) {
|
||||||
const json = JSON.parse(error.response.body);
|
// const json = JSON.parse(error.response.body);
|
||||||
if (json.code === 400 && json.message.includes('could not resolve address')) {
|
// if (json.code === 400 && json.message.includes('could not resolve address')) {
|
||||||
await stopCoolifyProxy(engine);
|
// await stopCoolifyProxy(engine);
|
||||||
await startCoolifyProxy(engine);
|
// await startCoolifyProxy(engine);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
//
|
// //
|
||||||
}
|
// }
|
||||||
try {
|
// try {
|
||||||
let rules: any;
|
// let rules: any;
|
||||||
// Force SSL off
|
// // Force SSL off
|
||||||
rules = await haproxy
|
// rules = await haproxy
|
||||||
.get(`v2/services/haproxy/configuration/http_request_rules`, {
|
// .get(`v2/services/haproxy/configuration/http_request_rules`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
if (rules.data.length > 0) {
|
// if (rules.data.length > 0) {
|
||||||
const rule = rules.data.find((rule) =>
|
// const rule = rules.data.find((rule) =>
|
||||||
rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`)
|
// rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`)
|
||||||
);
|
// );
|
||||||
if (rule) {
|
// if (rule) {
|
||||||
if (!transactionId) transactionId = await getNextTransactionId();
|
// if (!transactionId) transactionId = await getNextTransactionId();
|
||||||
await haproxy
|
// await haproxy
|
||||||
.delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
|
// .delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId,
|
// transaction_id: transactionId,
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Force WWW off
|
// // Force WWW off
|
||||||
rules = await haproxy
|
// rules = await haproxy
|
||||||
.get(`v2/services/haproxy/configuration/http_request_rules`, {
|
// .get(`v2/services/haproxy/configuration/http_request_rules`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
if (rules.data.length > 0) {
|
// if (rules.data.length > 0) {
|
||||||
const rule = rules.data.find((rule) => rule.redir_value.includes(redirectValue));
|
// const rule = rules.data.find((rule) => rule.redir_value.includes(redirectValue));
|
||||||
if (rule) {
|
// if (rule) {
|
||||||
if (!transactionId) transactionId = await getNextTransactionId();
|
// if (!transactionId) transactionId = await getNextTransactionId();
|
||||||
await haproxy
|
// await haproxy
|
||||||
.delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
|
// .delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId,
|
// transaction_id: transactionId,
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
console.log(error);
|
// console.log(error);
|
||||||
//
|
// //
|
||||||
} finally {
|
// } finally {
|
||||||
try {
|
// try {
|
||||||
if (transactionId) return transactionId;
|
// if (transactionId) return transactionId;
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
if (error?.response?.body) {
|
// if (error?.response?.body) {
|
||||||
const json = JSON.parse(error.response.body);
|
// const json = JSON.parse(error.response.body);
|
||||||
if (json.code === 400 && json.message.includes('could not resolve address')) {
|
// if (json.code === 400 && json.message.includes('could not resolve address')) {
|
||||||
await stopCoolifyProxy(engine);
|
// await stopCoolifyProxy(engine);
|
||||||
await startCoolifyProxy(engine);
|
// await startCoolifyProxy(engine);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return;
|
// return;
|
||||||
} else {
|
// } else {
|
||||||
let serverConfigured = false;
|
// let serverConfigured = false;
|
||||||
let backendAvailable: any = null;
|
// let backendAvailable: any = null;
|
||||||
try {
|
// try {
|
||||||
backendAvailable = await haproxy
|
// backendAvailable = await haproxy
|
||||||
.get(`v2/services/haproxy/configuration/backends/${domain}`)
|
// .get(`v2/services/haproxy/configuration/backends/${domain}`)
|
||||||
.json();
|
// .json();
|
||||||
const server: any = await haproxy
|
// const server: any = await haproxy
|
||||||
.get(`v2/services/haproxy/configuration/servers/${id}`, {
|
// .get(`v2/services/haproxy/configuration/servers/${id}`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
backend: domain
|
// backend: domain
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
|
|
||||||
if (backendAvailable && server) {
|
// if (backendAvailable && server) {
|
||||||
// Very sophisticated way to check if the server is already configured in proxy
|
// // Very sophisticated way to check if the server is already configured in proxy
|
||||||
if (backendAvailable.data.forwardfor.enabled === 'enabled') {
|
// if (backendAvailable.data.forwardfor.enabled === 'enabled') {
|
||||||
if (backendAvailable.data.name === domain) {
|
// if (backendAvailable.data.name === domain) {
|
||||||
if (server.data.check === 'disabled') {
|
// if (server.data.check === 'disabled') {
|
||||||
if (server.data.address === id) {
|
// if (server.data.address === id) {
|
||||||
if (server.data.port === port) {
|
// if (server.data.port === port) {
|
||||||
serverConfigured = true;
|
// serverConfigured = true;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
//
|
// //
|
||||||
console.log(error);
|
// console.log(error);
|
||||||
}
|
// }
|
||||||
if (serverConfigured) {
|
// if (serverConfigured) {
|
||||||
console.log('server configured', domain);
|
// console.log('server configured', domain);
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (backendAvailable) {
|
// if (backendAvailable) {
|
||||||
if (!transactionId) transactionId = await getNextTransactionId();
|
// if (!transactionId) transactionId = await getNextTransactionId();
|
||||||
await haproxy
|
// await haproxy
|
||||||
.delete(`v2/services/haproxy/configuration/backends/${domain}`, {
|
// .delete(`v2/services/haproxy/configuration/backends/${domain}`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId
|
// transaction_id: transactionId
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
}
|
// }
|
||||||
try {
|
// try {
|
||||||
console.log('adding ', domain);
|
// console.log('adding ', domain);
|
||||||
if (!transactionId) transactionId = await getNextTransactionId();
|
// if (!transactionId) transactionId = await getNextTransactionId();
|
||||||
await haproxy.post('v2/services/haproxy/configuration/backends', {
|
// await haproxy.post('v2/services/haproxy/configuration/backends', {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId
|
// transaction_id: transactionId
|
||||||
},
|
// },
|
||||||
json: {
|
// json: {
|
||||||
'init-addr': 'last,libc,none',
|
// 'init-addr': 'last,libc,none',
|
||||||
forwardfor: { enabled: 'enabled' },
|
// forwardfor: { enabled: 'enabled' },
|
||||||
name: domain
|
// name: domain
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
await haproxy.post('v2/services/haproxy/configuration/servers', {
|
// await haproxy.post('v2/services/haproxy/configuration/servers', {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId,
|
// transaction_id: transactionId,
|
||||||
backend: domain
|
// backend: domain
|
||||||
},
|
// },
|
||||||
json: {
|
// json: {
|
||||||
address: id,
|
// address: id,
|
||||||
check: 'disabled',
|
// check: 'disabled',
|
||||||
name: id,
|
// name: id,
|
||||||
port: port
|
// port: port
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
let rules: any;
|
// let rules: any;
|
||||||
|
|
||||||
// Force SSL off
|
// // Force SSL off
|
||||||
rules = await haproxy
|
// rules = await haproxy
|
||||||
.get(`v2/services/haproxy/configuration/http_request_rules`, {
|
// .get(`v2/services/haproxy/configuration/http_request_rules`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
if (rules.data.length > 0) {
|
// if (rules.data.length > 0) {
|
||||||
const rule = rules.data.find((rule) =>
|
// const rule = rules.data.find((rule) =>
|
||||||
rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`)
|
// rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`)
|
||||||
);
|
// );
|
||||||
if (rule) {
|
// if (rule) {
|
||||||
await haproxy
|
// await haproxy
|
||||||
.delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
|
// .delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId,
|
// transaction_id: transactionId,
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// Generate SSL && force SSL on
|
// // Generate SSL && force SSL on
|
||||||
if (isHttps) {
|
// if (isHttps) {
|
||||||
await letsEncrypt(domain, id, false);
|
// await letsEncrypt(domain, id, false);
|
||||||
rules = await haproxy
|
// rules = await haproxy
|
||||||
.get(`v2/services/haproxy/configuration/http_request_rules`, {
|
// .get(`v2/services/haproxy/configuration/http_request_rules`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
let nextRule = 0;
|
// let nextRule = 0;
|
||||||
if (rules.data.length > 0) {
|
// if (rules.data.length > 0) {
|
||||||
const rule = rules.data.find((rule) =>
|
// const rule = rules.data.find((rule) =>
|
||||||
rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`)
|
// rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`)
|
||||||
);
|
// );
|
||||||
if (rule) return;
|
// if (rule) return;
|
||||||
nextRule = rules.data[rules.data.length - 1].index + 1;
|
// nextRule = rules.data[rules.data.length - 1].index + 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
await haproxy
|
// await haproxy
|
||||||
.post(`v2/services/haproxy/configuration/http_request_rules`, {
|
// .post(`v2/services/haproxy/configuration/http_request_rules`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId,
|
// transaction_id: transactionId,
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
},
|
// },
|
||||||
json: {
|
// json: {
|
||||||
index: nextRule,
|
// index: nextRule,
|
||||||
cond: 'if',
|
// cond: 'if',
|
||||||
cond_test: `{ hdr(host) -i ${domain} } !{ ssl_fc }`,
|
// cond_test: `{ hdr(host) -i ${domain} } !{ ssl_fc }`,
|
||||||
type: 'redirect',
|
// type: 'redirect',
|
||||||
redir_type: 'scheme',
|
// redir_type: 'scheme',
|
||||||
redir_value: 'https',
|
// redir_value: 'https',
|
||||||
redir_code: dev ? 302 : 301
|
// redir_code: dev ? 302 : 301
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
}
|
// }
|
||||||
|
|
||||||
// WWW redirect on
|
// // WWW redirect on
|
||||||
rules = await haproxy
|
// rules = await haproxy
|
||||||
.get(`v2/services/haproxy/configuration/http_request_rules`, {
|
// .get(`v2/services/haproxy/configuration/http_request_rules`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
let nextRule = 0;
|
// let nextRule = 0;
|
||||||
if (rules.data.length > 0) {
|
// if (rules.data.length > 0) {
|
||||||
const rule = rules.data.find((rule) => rule.redir_value.includes(redirectValue));
|
// const rule = rules.data.find((rule) => rule.redir_value.includes(redirectValue));
|
||||||
if (rule) return;
|
// if (rule) return;
|
||||||
nextRule = rules.data[rules.data.length - 1].index + 1;
|
// nextRule = rules.data[rules.data.length - 1].index + 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
await haproxy
|
// await haproxy
|
||||||
.post(`v2/services/haproxy/configuration/http_request_rules`, {
|
// .post(`v2/services/haproxy/configuration/http_request_rules`, {
|
||||||
searchParams: {
|
// searchParams: {
|
||||||
transaction_id: transactionId,
|
// transaction_id: transactionId,
|
||||||
parent_name: 'http',
|
// parent_name: 'http',
|
||||||
parent_type: 'frontend'
|
// parent_type: 'frontend'
|
||||||
},
|
// },
|
||||||
json: {
|
// json: {
|
||||||
index: nextRule,
|
// index: nextRule,
|
||||||
cond: 'if',
|
// cond: 'if',
|
||||||
cond_test: contTest,
|
// cond_test: contTest,
|
||||||
type: 'redirect',
|
// type: 'redirect',
|
||||||
redir_type: 'location',
|
// redir_type: 'location',
|
||||||
redir_value: redirectValue,
|
// redir_value: redirectValue,
|
||||||
redir_code: dev ? 302 : 301
|
// redir_code: dev ? 302 : 301
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.json();
|
// .json();
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
console.log(error);
|
// console.log(error);
|
||||||
} finally {
|
// } finally {
|
||||||
try {
|
// try {
|
||||||
if (transactionId) return transactionId;
|
// if (transactionId) return transactionId;
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
if (error?.response?.body) {
|
// if (error?.response?.body) {
|
||||||
const json = JSON.parse(error.response.body);
|
// const json = JSON.parse(error.response.body);
|
||||||
if (json.code === 400 && json.message.includes('could not resolve address')) {
|
// if (json.code === 400 && json.message.includes('could not resolve address')) {
|
||||||
await stopCoolifyProxy(engine);
|
// await stopCoolifyProxy(engine);
|
||||||
await startCoolifyProxy(engine);
|
// await startCoolifyProxy(engine);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
export async function configureCoolifyProxyOff(fqdn) {
|
export async function configureCoolifyProxyOff(fqdn) {
|
||||||
const domain = getDomain(fqdn);
|
const domain = getDomain(fqdn);
|
||||||
|
@ -1,109 +1,104 @@
|
|||||||
import * as db from '$lib/database';
|
import { configureHAProxy } from '$lib/haproxy/configuration';
|
||||||
import { getDomain } from '$lib/common';
|
|
||||||
import {
|
|
||||||
checkContainer,
|
|
||||||
checkHAProxy,
|
|
||||||
checkProxyConfigurations,
|
|
||||||
configureCoolifyProxyOn,
|
|
||||||
configureHAProxy,
|
|
||||||
forceSSLOnApplication,
|
|
||||||
haproxyInstance,
|
|
||||||
setWwwRedirection,
|
|
||||||
startCoolifyProxy,
|
|
||||||
startHttpProxy
|
|
||||||
} from '$lib/haproxy';
|
|
||||||
|
|
||||||
export default async function () {
|
export default async function () {
|
||||||
const haproxy = await haproxyInstance();
|
|
||||||
await checkHAProxy(haproxy);
|
|
||||||
let transactionId;
|
|
||||||
try {
|
try {
|
||||||
await checkProxyConfigurations();
|
return await configureHAProxy();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error.response.body || error);
|
||||||
}
|
}
|
||||||
try {
|
// const haproxy = await haproxyInstance();
|
||||||
const applications = await db.prisma.application.findMany({
|
// await checkHAProxy(haproxy);
|
||||||
include: { destinationDocker: true }
|
// const transactionId = await getNextTransactionId();
|
||||||
});
|
// let executeTransaction = {
|
||||||
|
// applications: false,
|
||||||
for (const application of applications) {
|
// services: false
|
||||||
const {
|
// }
|
||||||
fqdn,
|
// try {
|
||||||
id,
|
// await checkProxyConfigurations();
|
||||||
port,
|
// } catch (error) {
|
||||||
destinationDocker: { engine }
|
// console.log(error);
|
||||||
} = application;
|
// }
|
||||||
const containerRunning = await checkContainer(engine, id);
|
// try {
|
||||||
transactionId = await configureHAProxy(
|
// const applications = await db.prisma.application.findMany({
|
||||||
haproxy,
|
// include: { destinationDocker: true }
|
||||||
transactionId,
|
// });
|
||||||
fqdn,
|
|
||||||
id,
|
// for (const application of applications) {
|
||||||
port,
|
// const {
|
||||||
containerRunning,
|
// fqdn,
|
||||||
engine
|
// id,
|
||||||
);
|
// port,
|
||||||
}
|
// destinationDocker: { engine }
|
||||||
|
// } = application;
|
||||||
const services = await db.prisma.service.findMany({
|
// const containerRunning = await checkContainer(engine, id);
|
||||||
include: {
|
// executeTransaction.applications = await configureHAProxy(
|
||||||
destinationDocker: true,
|
// haproxy,
|
||||||
minio: true,
|
// transactionId,
|
||||||
plausibleAnalytics: true,
|
// fqdn,
|
||||||
vscodeserver: true,
|
// id,
|
||||||
wordpress: true
|
// port,
|
||||||
}
|
// containerRunning,
|
||||||
});
|
// engine
|
||||||
|
// );
|
||||||
for (const service of services) {
|
// }
|
||||||
const {
|
|
||||||
fqdn,
|
// const services = await db.prisma.service.findMany({
|
||||||
id,
|
// include: {
|
||||||
type,
|
// destinationDocker: true,
|
||||||
destinationDocker: { engine }
|
// minio: true,
|
||||||
} = service;
|
// plausibleAnalytics: true,
|
||||||
console.log({ fqdn, id, type, engine });
|
// vscodeserver: true,
|
||||||
const found = db.supportedServiceTypesAndVersions.find((a) => a.name === type);
|
// wordpress: true
|
||||||
if (found) {
|
// }
|
||||||
console.log(found);
|
// });
|
||||||
const port = found.ports.main;
|
|
||||||
const publicPort = service[type]?.publicPort;
|
// for (const service of services) {
|
||||||
const containerRunning = await checkContainer(engine, id);
|
// const {
|
||||||
console.log(containerRunning);
|
// fqdn,
|
||||||
transactionId = await configureHAProxy(
|
// id,
|
||||||
haproxy,
|
// type,
|
||||||
transactionId,
|
// destinationDocker: { engine }
|
||||||
fqdn,
|
// } = service;
|
||||||
id,
|
// const found = db.supportedServiceTypesAndVersions.find((a) => a.name === type);
|
||||||
port,
|
// if (found) {
|
||||||
containerRunning,
|
// console.log(found);
|
||||||
engine
|
// const port = found.ports.main;
|
||||||
);
|
// const publicPort = service[type]?.publicPort;
|
||||||
if (publicPort) {
|
// const containerRunning = await checkContainer(engine, id);
|
||||||
const containerFound = await checkContainer(
|
// executeTransaction.services = await configureHAProxy(
|
||||||
service.destinationDocker.engine,
|
// haproxy,
|
||||||
`haproxy-for-${publicPort}`
|
// transactionId,
|
||||||
);
|
// fqdn,
|
||||||
if (!containerFound) {
|
// id,
|
||||||
await startHttpProxy(service.destinationDocker, id, publicPort, 9000);
|
// port,
|
||||||
}
|
// containerRunning,
|
||||||
}
|
// engine
|
||||||
}
|
// );
|
||||||
}
|
// if (publicPort) {
|
||||||
console.log(transactionId);
|
// const containerFound = await checkContainer(
|
||||||
if (transactionId) await haproxy.put(`v2/services/haproxy/transactions/${transactionId}`);
|
// service.destinationDocker.engine,
|
||||||
// Check Coolify FQDN and configure proxy if needed
|
// `haproxy-for-${publicPort}`
|
||||||
// const { fqdn } = await db.listSettings();
|
// );
|
||||||
// if (fqdn) {
|
// if (!containerFound) {
|
||||||
// const domain = getDomain(fqdn);
|
// await startHttpProxy(service.destinationDocker, id, publicPort, 9000);
|
||||||
// await startCoolifyProxy('/var/run/docker.sock');
|
// }
|
||||||
// await configureCoolifyProxyOn(fqdn);
|
// }
|
||||||
// await setWwwRedirection(fqdn);
|
// }
|
||||||
// const isHttps = fqdn.startsWith('https://');
|
// }
|
||||||
// if (isHttps) await forceSSLOnApplication(domain);
|
// if (executeTransaction.applications || executeTransaction.services) {
|
||||||
|
// await haproxy.put(`v2/services/haproxy/transactions/${transactionId}`);
|
||||||
|
// }
|
||||||
|
// // Check Coolify FQDN and configure proxy if needed
|
||||||
|
// // const { fqdn } = await db.listSettings();
|
||||||
|
// // if (fqdn) {
|
||||||
|
// // const domain = getDomain(fqdn);
|
||||||
|
// // await startCoolifyProxy('/var/run/docker.sock');
|
||||||
|
// // await configureCoolifyProxyOn(fqdn);
|
||||||
|
// // await setWwwRedirection(fqdn);
|
||||||
|
// // const isHttps = fqdn.startsWith('https://');
|
||||||
|
// // if (isHttps) await forceSSLOnApplication(domain);
|
||||||
|
// // }
|
||||||
|
// } catch (error) {
|
||||||
|
// throw error;
|
||||||
// }
|
// }
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user