diff --git a/apps/api/prisma/migrations/20221104092223_default_redirect_proxy/migration.sql b/apps/api/prisma/migrations/20221104092223_default_redirect_proxy/migration.sql new file mode 100644 index 000000000..8584fe25c --- /dev/null +++ b/apps/api/prisma/migrations/20221104092223_default_redirect_proxy/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Setting" ADD COLUMN "proxyDefaultRedirect" TEXT; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 0a7b8552d..b2de9d23d 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -29,6 +29,7 @@ model Setting { proxyPassword String proxyUser String proxyHash String? + proxyDefaultRedirect String? isAutoUpdateEnabled Boolean @default(false) isDNSCheckEnabled Boolean @default(true) DNSServers String? diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 8c9d2e1dc..c760116b0 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -111,12 +111,13 @@ const host = '0.0.0.0'; fastify.register(cors); fastify.register(socketIO, { cors: { - origin: "http://localhost:3000" + origin: isDev ? "*" : '' } }) // To detect allowed origins // fastify.addHook('onRequest', async (request, reply) => { + // console.log(request.headers.host) // let allowedList = ['coolify:3000']; // const { ipv4, ipv6, fqdn } = await prisma.setting.findFirst({}) diff --git a/apps/api/src/routes/api/v1/settings/handlers.ts b/apps/api/src/routes/api/v1/settings/handlers.ts index f308c0314..4aa5905f0 100644 --- a/apps/api/src/routes/api/v1/settings/handlers.ts +++ b/apps/api/src/routes/api/v1/settings/handlers.ts @@ -44,16 +44,18 @@ export async function saveSettings(request: FastifyRequest, reply: maxPort, isAutoUpdateEnabled, isDNSCheckEnabled, - DNSServers + DNSServers, + proxyDefaultRedirect } = request.body const { id } = await listSettings(); await prisma.setting.update({ where: { id }, - data: { isRegistrationEnabled, dualCerts, isAutoUpdateEnabled, isDNSCheckEnabled, DNSServers, isAPIDebuggingEnabled } + data: { isRegistrationEnabled, dualCerts, isAutoUpdateEnabled, isDNSCheckEnabled, DNSServers, isAPIDebuggingEnabled, } }); if (fqdn) { await prisma.setting.update({ where: { id }, data: { fqdn } }); } + await prisma.setting.update({ where: { id }, data: { proxyDefaultRedirect } }); if (minPort && maxPort) { await prisma.setting.update({ where: { id }, data: { minPort, maxPort } }); } diff --git a/apps/api/src/routes/api/v1/settings/types.ts b/apps/api/src/routes/api/v1/settings/types.ts index 618101bba..d68a7bdd3 100644 --- a/apps/api/src/routes/api/v1/settings/types.ts +++ b/apps/api/src/routes/api/v1/settings/types.ts @@ -10,7 +10,8 @@ export interface SaveSettings { maxPort: number, isAutoUpdateEnabled: boolean, isDNSCheckEnabled: boolean, - DNSServers: string + DNSServers: string, + proxyDefaultRedirect: string } } export interface DeleteDomain { diff --git a/apps/api/src/routes/webhooks/traefik/handlers.ts b/apps/api/src/routes/webhooks/traefik/handlers.ts index 977075339..3afdc7822 100644 --- a/apps/api/src/routes/webhooks/traefik/handlers.ts +++ b/apps/api/src/routes/webhooks/traefik/handlers.ts @@ -20,12 +20,14 @@ function generateRouters(id, domain, nakedDomain, pathPrefix, isHttps, isWWW, is entrypoints: ['web'], rule: `Host(\`${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, service: `${id}`, + priority: 2, middlewares: [] } let https: any = { entrypoints: ['websecure'], rule: `Host(\`${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, service: `${id}`, + priority: 2, tls: { certresolver: 'letsencrypt' }, @@ -35,12 +37,14 @@ function generateRouters(id, domain, nakedDomain, pathPrefix, isHttps, isWWW, is entrypoints: ['web'], rule: `Host(\`www.${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, service: `${id}`, + priority: 2, middlewares: [] } let httpsWWW: any = { entrypoints: ['websecure'], rule: `Host(\`www.${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, service: `${id}`, + priority: 2, tls: { certresolver: 'letsencrypt' }, @@ -54,6 +58,7 @@ function generateRouters(id, domain, nakedDomain, pathPrefix, isHttps, isWWW, is httpWWW.middlewares.push('redirect-to-non-www'); httpsWWW.middlewares.push('redirect-to-non-www'); delete https.tls + delete httpsWWW.tls } // 3. http + www only @@ -64,6 +69,7 @@ function generateRouters(id, domain, nakedDomain, pathPrefix, isHttps, isWWW, is http.middlewares.push('redirect-to-www'); https.middlewares.push('redirect-to-www'); delete https.tls + delete httpsWWW.tls } // 5. https + non-www only if (isHttps && !isWWW) { @@ -164,6 +170,32 @@ export async function proxyConfiguration(request: FastifyRequest, remote }; try { const { id = null } = request.params; + const settings = await prisma.setting.findFirst(); + if (settings.isTraefikUsed && settings.proxyDefaultRedirect) { + traefik.http.routers['catchall'] = { + entrypoints: ["web"], + rule: "HostRegexp(`{catchall:.*}`)", + service: "noop", + priority: 1, + middlewares: ["redirect-regexp"] + } + traefik.http.middlewares['redirect-regexp'] = { + redirectregex: { + regex: '(.*)', + replacement: settings.proxyDefaultRedirect, + permanent: false + } + } + traefik.http.services['noop'] = { + loadBalancer: { + servers: [ + { + url: '' + } + ] + } + } + } const sslpath = '/etc/traefik/acme/custom'; let certificates = await prisma.certificate.findMany({ where: { team: { applications: { some: { settings: { isCustomSSL: true } } }, destinationDocker: { some: { remoteEngine: false, isCoolifyProxyUsed: true } } } } }) @@ -306,7 +338,7 @@ export async function proxyConfiguration(request: FastifyRequest, remote const port = 3000 const pathPrefix = '/' const isCustomSSL = false - + traefik.http.routers = { ...traefik.http.routers, ...generateRouters(`${id}-${port || 'default'}`, domain, nakedDomain, pathPrefix, isHttps, isWWW, dualCerts, isCustomSSL) } traefik.http.services = { ...traefik.http.services, ...generateServices(id, container, port) } } diff --git a/apps/ui/src/routes/settings/coolify.svelte b/apps/ui/src/routes/settings/coolify.svelte index f1bf8467b..cf3c21ddb 100644 --- a/apps/ui/src/routes/settings/coolify.svelte +++ b/apps/ui/src/routes/settings/coolify.svelte @@ -18,16 +18,12 @@