mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-31 16:14:46 +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", {
|
||||
orgId: varchar("orgId").primaryKey(),
|
||||
name: varchar("name").notNull(),
|
||||
subnet: varchar("subnet").notNull()
|
||||
subnet: varchar("subnet")
|
||||
});
|
||||
|
||||
export const orgDomains = pgTable("orgDomains", {
|
||||
|
|
|
@ -16,7 +16,7 @@ export const domains = sqliteTable("domains", {
|
|||
export const orgs = sqliteTable("orgs", {
|
||||
orgId: text("orgId").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
subnet: text("subnet").notNull(),
|
||||
subnet: text("subnet")
|
||||
});
|
||||
|
||||
export const userDomains = sqliteTable("userDomains", {
|
||||
|
|
|
@ -240,6 +240,14 @@ export async function getNextAvailableClientSubnet(
|
|||
): Promise<string> {
|
||||
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
|
||||
.select({
|
||||
address: sites.address
|
||||
|
@ -279,7 +287,7 @@ export async function getNextAvailableOrgSubnet(): Promise<string> {
|
|||
.from(orgs)
|
||||
.where(isNotNull(orgs.subnet));
|
||||
|
||||
const addresses = existingAddresses.map((org) => org.subnet);
|
||||
const addresses = existingAddresses.map((org) => org.subnet!);
|
||||
|
||||
let subnet = findNextAvailableCidr(
|
||||
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)) {
|
||||
return next(
|
||||
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;
|
||||
if (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 "exitNodes" ADD COLUMN "maxConnections" integer;
|
||||
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 "endpoint" varchar;
|
||||
ALTER TABLE "sites" ADD COLUMN "publicKey" varchar;
|
||||
|
@ -112,6 +112,7 @@ export default async function migration() {
|
|||
} catch (e) {
|
||||
console.log("Unable to migrate database schema");
|
||||
console.log(e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
console.log(`${version} migration complete`);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue