From 9e3ba295ea03113af9076dc23706d84d3b0cef4d Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 22 Jul 2022 20:48:04 +0000 Subject: [PATCH] feat: ipv4 and ipv6 --- .../20220722203927_ipaddress/migration.sql | 3 +++ apps/api/prisma/schema.prisma | 2 ++ apps/api/src/index.ts | 22 ++++++++++++++++--- apps/api/src/routes/api/v1/base/index.ts | 5 ++++- apps/ui/src/lib/store.ts | 4 ++++ apps/ui/src/routes/__layout.svelte | 2 ++ .../[id]/_Databases/_Databases.svelte | 2 +- 7 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 apps/api/prisma/migrations/20220722203927_ipaddress/migration.sql diff --git a/apps/api/prisma/migrations/20220722203927_ipaddress/migration.sql b/apps/api/prisma/migrations/20220722203927_ipaddress/migration.sql new file mode 100644 index 000000000..21d45ada3 --- /dev/null +++ b/apps/api/prisma/migrations/20220722203927_ipaddress/migration.sql @@ -0,0 +1,3 @@ +-- AlterTable +ALTER TABLE "Setting" ADD COLUMN "ipv4" TEXT; +ALTER TABLE "Setting" ADD COLUMN "ipv6" TEXT; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 72c926f3f..37f306b70 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -11,6 +11,8 @@ datasource db { model Setting { id String @id @default(cuid()) fqdn String? @unique + ipv4 String? + ipv6 String? isRegistrationEnabled Boolean @default(false) dualCerts Boolean @default(false) minPort Int @default(9000) diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index e302df635..6a9666027 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -5,7 +5,7 @@ import env from '@fastify/env'; import cookie from '@fastify/cookie'; import path, { join } from 'path'; import autoLoad from '@fastify/autoload'; -import { asyncExecShell, isDev, prisma } from './lib/common'; +import { asyncExecShell, isDev, listSettings, prisma } from './lib/common'; import { scheduler } from './lib/scheduler'; declare module 'fastify' { @@ -101,10 +101,10 @@ fastify.listen({ port, host }, async (err: any, address: any) => { process.exit(1); } console.log(`Coolify's API is listening on ${host}:${port}`); - await initServer() + await initServer(); await scheduler.start('deployApplication'); await scheduler.start('cleanupStorage'); - await scheduler.start('checkProxies') + await scheduler.start('checkProxies'); // Check if no build is running @@ -130,8 +130,24 @@ fastify.listen({ port, host }, async (err: any, address: any) => { if (!scheduler.workers.has('deployApplication')) await scheduler.start('deployApplication'); } }); + await getIPAddress(); }); +async function getIPAddress() { + const { publicIpv4, publicIpv6 } = await import('public-ip') + try { + const settings = await listSettings(); + if (!settings.ipv4) { + const ipv4 = await publicIpv4({ timeout: 2000 }) + await prisma.setting.update({ where: { id: settings.id }, data: { ipv4 } }) + } + if (!settings.ipv6) { + const ipv6 = await publicIpv6({ timeout: 2000 }) + await prisma.setting.update({ where: { id: settings.id }, data: { ipv6 } }) + } + + } catch (error) { } +} async function initServer() { try { await asyncExecShell(`docker network create --attachable coolify`); diff --git a/apps/api/src/routes/api/v1/base/index.ts b/apps/api/src/routes/api/v1/base/index.ts index 9eaa8c40a..76735a032 100644 --- a/apps/api/src/routes/api/v1/base/index.ts +++ b/apps/api/src/routes/api/v1/base/index.ts @@ -1,10 +1,13 @@ import { FastifyPluginAsync } from 'fastify'; -import { errorHandler, version } from '../../../../lib/common'; +import { errorHandler, listSettings, version } from '../../../../lib/common'; const root: FastifyPluginAsync = async (fastify): Promise => { fastify.get('/', async () => { + const settings = await listSettings() try { return { + ipv4: settings.ipv4, + ipv6: settings.ipv6, version, whiteLabeled: process.env.COOLIFY_WHITE_LABELED === 'true', whiteLabeledIcon: process.env.COOLIFY_WHITE_LABELED_ICON, diff --git a/apps/ui/src/lib/store.ts b/apps/ui/src/lib/store.ts index 096be2e52..8b7a8d830 100644 --- a/apps/ui/src/lib/store.ts +++ b/apps/ui/src/lib/store.ts @@ -1,6 +1,8 @@ import { writable, readable, type Writable, type Readable } from 'svelte/store'; interface AppSession { + ipv4: string | null, + ipv6: string | null, version: string | null, userId: string | null, teamId: string | null, @@ -17,6 +19,8 @@ interface AppSession { } export const loginEmail: Writable = writable() export const appSession: Writable = writable({ + ipv4: null, + ipv6: null, version: null, userId: null, teamId: null, diff --git a/apps/ui/src/routes/__layout.svelte b/apps/ui/src/routes/__layout.svelte index a6932eae0..d25437d2c 100644 --- a/apps/ui/src/routes/__layout.svelte +++ b/apps/ui/src/routes/__layout.svelte @@ -65,6 +65,8 @@