Fixing various things

This commit is contained in:
Owen
2025-10-27 17:52:39 -07:00
parent 15d63ddffa
commit bd5cc790d6
14 changed files with 117 additions and 47 deletions

View File

@@ -1,4 +1,4 @@
import { db } from "@server/db";
import { db, dnsRecords } from "@server/db";
import { domains, exitNodes, orgDomains, orgs, resources } from "@server/db";
import config from "@server/lib/config";
import { eq, ne } from "drizzle-orm";
@@ -8,7 +8,10 @@ export async function copyInConfig() {
const endpoint = config.getRawConfig().gerbil.base_endpoint;
const listenPort = config.getRawConfig().gerbil.start_port;
if (!config.getRawConfig().flags?.disable_config_managed_domains && config.getRawConfig().domains) {
if (
!config.getRawConfig().flags?.disable_config_managed_domains &&
config.getRawConfig().domains
) {
await copyInDomains();
}
@@ -39,7 +42,7 @@ async function copyInDomains() {
domainId: key,
baseDomain: value.base_domain.toLowerCase(),
certResolver: value.cert_resolver || null,
preferWildcardCert: value.prefer_wildcard_cert || null,
preferWildcardCert: value.prefer_wildcard_cert || null
})
);
@@ -56,31 +59,79 @@ async function copyInDomains() {
if (!configDomainKeys.has(existingDomain.domainId)) {
await trx
.delete(domains)
.where(eq(domains.domainId, existingDomain.domainId))
.execute();
.where(eq(domains.domainId, existingDomain.domainId));
await trx
.delete(dnsRecords)
.where(eq(dnsRecords.domainId, existingDomain.domainId));
}
}
for (const { domainId, baseDomain, certResolver, preferWildcardCert } of configDomains) {
for (const {
domainId,
baseDomain,
certResolver,
preferWildcardCert
} of configDomains) {
if (existingDomainKeys.has(domainId)) {
await trx
.update(domains)
.set({ baseDomain, verified: true, type: "wildcard", certResolver, preferWildcardCert })
.where(eq(domains.domainId, domainId))
.execute();
} else {
await trx
.insert(domains)
.values({
domainId,
.set({
baseDomain,
configManaged: true,
type: "wildcard",
verified: true,
type: "wildcard",
certResolver,
preferWildcardCert
})
.execute();
.where(eq(domains.domainId, domainId));
// delete the dns records and add them again to ensure they are correct
await trx
.delete(dnsRecords)
.where(eq(dnsRecords.domainId, domainId));
await trx.insert(dnsRecords).values([
{
domainId,
recordType: "A",
baseDomain,
value: "Server IP Address",
verified: true
},
{
domainId,
recordType: "A",
baseDomain,
value: "Server IP Address",
verified: true
}
]);
} else {
await trx.insert(domains).values({
domainId,
baseDomain,
configManaged: true,
type: "wildcard",
verified: true,
certResolver,
preferWildcardCert
});
await trx.insert(dnsRecords).values([
{
domainId,
recordType: "A",
baseDomain,
value: "Server IP Address",
verified: true
},
{
domainId,
recordType: "A",
baseDomain,
value: "Server IP Address",
verified: true
}
]);
}
}

View File

@@ -44,7 +44,7 @@ export default async function migration() {
await db.execute(sql`
CREATE TABLE "dnsRecords" (
"id" varchar PRIMARY KEY NOT NULL,
"id" serial PRIMARY KEY NOT NULL,
"domainId" varchar NOT NULL,
"recordType" varchar NOT NULL,
"baseDomain" varchar,
@@ -108,10 +108,10 @@ export default async function migration() {
await db.execute(sql`ALTER TABLE "orgs" DROP COLUMN "settings";`);
await db.execute(sql`COMMIT`);
console.log(`Updated resource rules match value from GEOIP to COUNTRY`);
console.log("Migrated database");
} catch (e) {
await db.execute(sql`ROLLBACK`);
console.log("Unable to update resource rules match value");
console.log("Unable to migrate database");
console.log(e);
throw e;
}

View File

@@ -72,7 +72,7 @@ export default async function migration() {
db.prepare(
`
CREATE TABLE 'dnsRecords' (
'id' text PRIMARY KEY NOT NULL,
'id' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'domainId' text NOT NULL,
'recordType' text NOT NULL,
'baseDomain' text,