mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-19 00:40:40 +02:00
Make org subnet optional
This commit is contained in:
parent
1dff9baa61
commit
021bc073a2
6 changed files with 31 additions and 4 deletions
|
@ -22,7 +22,7 @@ export const domains = pgTable("domains", {
|
||||||
export const orgs = pgTable("orgs", {
|
export const orgs = pgTable("orgs", {
|
||||||
orgId: varchar("orgId").primaryKey(),
|
orgId: varchar("orgId").primaryKey(),
|
||||||
name: varchar("name").notNull(),
|
name: varchar("name").notNull(),
|
||||||
subnet: varchar("subnet").notNull()
|
subnet: varchar("subnet")
|
||||||
});
|
});
|
||||||
|
|
||||||
export const orgDomains = pgTable("orgDomains", {
|
export const orgDomains = pgTable("orgDomains", {
|
||||||
|
|
|
@ -16,7 +16,7 @@ export const domains = sqliteTable("domains", {
|
||||||
export const orgs = sqliteTable("orgs", {
|
export const orgs = sqliteTable("orgs", {
|
||||||
orgId: text("orgId").primaryKey(),
|
orgId: text("orgId").primaryKey(),
|
||||||
name: text("name").notNull(),
|
name: text("name").notNull(),
|
||||||
subnet: text("subnet").notNull(),
|
subnet: text("subnet")
|
||||||
});
|
});
|
||||||
|
|
||||||
export const userDomains = sqliteTable("userDomains", {
|
export const userDomains = sqliteTable("userDomains", {
|
||||||
|
|
|
@ -240,6 +240,14 @@ export async function getNextAvailableClientSubnet(
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const [org] = await db.select().from(orgs).where(eq(orgs.orgId, orgId));
|
const [org] = await db.select().from(orgs).where(eq(orgs.orgId, orgId));
|
||||||
|
|
||||||
|
if (!org) {
|
||||||
|
throw new Error(`Organization with ID ${orgId} not found`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!org.subnet) {
|
||||||
|
throw new Error(`Organization with ID ${orgId} has no subnet defined`);
|
||||||
|
}
|
||||||
|
|
||||||
const existingAddressesSites = await db
|
const existingAddressesSites = await db
|
||||||
.select({
|
.select({
|
||||||
address: sites.address
|
address: sites.address
|
||||||
|
@ -279,7 +287,7 @@ export async function getNextAvailableOrgSubnet(): Promise<string> {
|
||||||
.from(orgs)
|
.from(orgs)
|
||||||
.where(isNotNull(orgs.subnet));
|
.where(isNotNull(orgs.subnet));
|
||||||
|
|
||||||
const addresses = existingAddresses.map((org) => org.subnet);
|
const addresses = existingAddresses.map((org) => org.subnet!);
|
||||||
|
|
||||||
let subnet = findNextAvailableCidr(
|
let subnet = findNextAvailableCidr(
|
||||||
addresses,
|
addresses,
|
||||||
|
|
|
@ -120,6 +120,15 @@ export async function createClient(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!org.subnet) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
`Organization with ID ${orgId} has no subnet defined`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (!isIpInCidr(subnet, org.subnet)) {
|
if (!isIpInCidr(subnet, org.subnet)) {
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(
|
||||||
|
|
|
@ -129,6 +129,15 @@ export async function createSite(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!org.subnet) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
`Organization with ID ${orgId} has no subnet defined`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let updatedAddress = null;
|
let updatedAddress = null;
|
||||||
if (address) {
|
if (address) {
|
||||||
if (!isValidIP(address)) {
|
if (!isValidIP(address)) {
|
||||||
|
|
|
@ -86,7 +86,7 @@ export default async function migration() {
|
||||||
ALTER TABLE "domains" ADD COLUMN "tries" integer DEFAULT 0 NOT NULL;
|
ALTER TABLE "domains" ADD COLUMN "tries" integer DEFAULT 0 NOT NULL;
|
||||||
ALTER TABLE "exitNodes" ADD COLUMN "maxConnections" integer;
|
ALTER TABLE "exitNodes" ADD COLUMN "maxConnections" integer;
|
||||||
ALTER TABLE "newt" ADD COLUMN "version" varchar;
|
ALTER TABLE "newt" ADD COLUMN "version" varchar;
|
||||||
ALTER TABLE "orgs" ADD COLUMN "subnet" varchar NOT NULL;
|
ALTER TABLE "orgs" ADD COLUMN "subnet" varchar;
|
||||||
ALTER TABLE "sites" ADD COLUMN "address" varchar;
|
ALTER TABLE "sites" ADD COLUMN "address" varchar;
|
||||||
ALTER TABLE "sites" ADD COLUMN "endpoint" varchar;
|
ALTER TABLE "sites" ADD COLUMN "endpoint" varchar;
|
||||||
ALTER TABLE "sites" ADD COLUMN "publicKey" varchar;
|
ALTER TABLE "sites" ADD COLUMN "publicKey" varchar;
|
||||||
|
@ -112,6 +112,7 @@ export default async function migration() {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Unable to migrate database schema");
|
console.log("Unable to migrate database schema");
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`${version} migration complete`);
|
console.log(`${version} migration complete`);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue