mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-31 08:04:54 +02:00
add client olm relationship
This commit is contained in:
parent
b9de0f8e38
commit
346f2db5fb
5 changed files with 69 additions and 31 deletions
|
@ -61,7 +61,9 @@ export const resources = sqliteTable("resources", {
|
||||||
.notNull()
|
.notNull()
|
||||||
.default(false),
|
.default(false),
|
||||||
isBaseDomain: integer("isBaseDomain", { mode: "boolean" }),
|
isBaseDomain: integer("isBaseDomain", { mode: "boolean" }),
|
||||||
applyRules: integer("applyRules", { mode: "boolean" }).notNull().default(false)
|
applyRules: integer("applyRules", { mode: "boolean" })
|
||||||
|
.notNull()
|
||||||
|
.default(false)
|
||||||
});
|
});
|
||||||
|
|
||||||
export const targets = sqliteTable("targets", {
|
export const targets = sqliteTable("targets", {
|
||||||
|
@ -114,22 +116,27 @@ export const newts = sqliteTable("newt", {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
export const olms = sqliteTable("olms", {
|
export const clients = sqliteTable("clients", {
|
||||||
olmId: text("id").primaryKey(),
|
clientId: integer("id").primaryKey({ autoIncrement: true }),
|
||||||
secretHash: text("secretHash").notNull(),
|
|
||||||
dateCreated: text("dateCreated").notNull(),
|
|
||||||
siteId: integer("siteId").references(() => sites.siteId, {
|
siteId: integer("siteId").references(() => sites.siteId, {
|
||||||
onDelete: "cascade"
|
onDelete: "cascade"
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// wgstuff
|
|
||||||
pubKey: text("pubKey"),
|
pubKey: text("pubKey"),
|
||||||
subnet: text("subnet").notNull(),
|
subnet: text("subnet").notNull(),
|
||||||
megabytesIn: integer("bytesIn"),
|
megabytesIn: integer("bytesIn"),
|
||||||
megabytesOut: integer("bytesOut"),
|
megabytesOut: integer("bytesOut"),
|
||||||
lastBandwidthUpdate: text("lastBandwidthUpdate"),
|
lastBandwidthUpdate: text("lastBandwidthUpdate"),
|
||||||
type: text("type").notNull(), // "newt" or "wireguard"
|
type: text("type").notNull(), // "olm"
|
||||||
online: integer("online", { mode: "boolean" }).notNull().default(false),
|
online: integer("online", { mode: "boolean" }).notNull().default(false)
|
||||||
|
});
|
||||||
|
|
||||||
|
export const olms = sqliteTable("olms", {
|
||||||
|
olmId: text("id").primaryKey(),
|
||||||
|
secretHash: text("secretHash").notNull(),
|
||||||
|
dateCreated: text("dateCreated").notNull(),
|
||||||
|
clientId: integer("clientId").references(() => clients.clientId, {
|
||||||
|
onDelete: "cascade"
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
export const twoFactorBackupCodes = sqliteTable("twoFactorBackupCodes", {
|
export const twoFactorBackupCodes = sqliteTable("twoFactorBackupCodes", {
|
||||||
|
@ -259,6 +266,24 @@ export const userSites = sqliteTable("userSites", {
|
||||||
.references(() => sites.siteId, { onDelete: "cascade" })
|
.references(() => sites.siteId, { onDelete: "cascade" })
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const userClients = sqliteTable("userClients", {
|
||||||
|
userId: text("userId")
|
||||||
|
.notNull()
|
||||||
|
.references(() => users.userId, { onDelete: "cascade" }),
|
||||||
|
clientId: integer("clientId")
|
||||||
|
.notNull()
|
||||||
|
.references(() => clients.clientId, { onDelete: "cascade" })
|
||||||
|
});
|
||||||
|
|
||||||
|
export const roleClients = sqliteTable("roleClients", {
|
||||||
|
roleId: integer("roleId")
|
||||||
|
.notNull()
|
||||||
|
.references(() => roles.roleId, { onDelete: "cascade" }),
|
||||||
|
clientId: integer("clientId")
|
||||||
|
.notNull()
|
||||||
|
.references(() => clients.clientId, { onDelete: "cascade" })
|
||||||
|
});
|
||||||
|
|
||||||
export const roleResources = sqliteTable("roleResources", {
|
export const roleResources = sqliteTable("roleResources", {
|
||||||
roleId: integer("roleId")
|
roleId: integer("roleId")
|
||||||
.notNull()
|
.notNull()
|
||||||
|
@ -451,3 +476,6 @@ export type ResourceAccessToken = InferSelectModel<typeof resourceAccessToken>;
|
||||||
export type ResourceWhitelist = InferSelectModel<typeof resourceWhitelist>;
|
export type ResourceWhitelist = InferSelectModel<typeof resourceWhitelist>;
|
||||||
export type VersionMigration = InferSelectModel<typeof versionMigrations>;
|
export type VersionMigration = InferSelectModel<typeof versionMigrations>;
|
||||||
export type ResourceRule = InferSelectModel<typeof resourceRules>;
|
export type ResourceRule = InferSelectModel<typeof resourceRules>;
|
||||||
|
export type Client = InferSelectModel<typeof clients>;
|
||||||
|
export type RoleClient = InferSelectModel<typeof roleClients>;
|
||||||
|
export type UserClient = InferSelectModel<typeof userClients>;
|
||||||
|
|
1
server/routers/client/index.ts
Normal file
1
server/routers/client/index.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export * from "./pickClientDefaults";
|
|
@ -1,6 +1,6 @@
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { db } from "@server/db";
|
import { db } from "@server/db";
|
||||||
import { olms, sites } from "@server/db/schema";
|
import { clients, olms, sites } from "@server/db/schema";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
@ -30,7 +30,7 @@ export type PickClientDefaultsResponse = {
|
||||||
clientSecret: string;
|
clientSecret: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function pickOlmDefaults(
|
export async function pickClientDefaults(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
|
@ -58,29 +58,39 @@ export async function pickOlmDefaults(
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure all the required fields are present
|
// make sure all the required fields are present
|
||||||
if (
|
|
||||||
!site.address ||
|
const sitesRequiredFields = z.object({
|
||||||
!site.publicKey ||
|
address: z.string(),
|
||||||
!site.listenPort ||
|
publicKey: z.string(),
|
||||||
!site.endpoint
|
listenPort: z.number(),
|
||||||
) {
|
endpoint: z.string()
|
||||||
|
});
|
||||||
|
|
||||||
|
const parsedSite = sitesRequiredFields.safeParse(site);
|
||||||
|
if (!parsedSite.success) {
|
||||||
return next(
|
return next(
|
||||||
createHttpError(HttpCode.BAD_REQUEST, "Site has no address")
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
"Unable to pick client defaults because: " +
|
||||||
|
fromError(parsedSite.error).toString()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { address, publicKey, listenPort, endpoint } = parsedSite.data;
|
||||||
|
|
||||||
const clientsQuery = await db
|
const clientsQuery = await db
|
||||||
.select({
|
.select({
|
||||||
subnet: olms.subnet
|
subnet: clients.subnet
|
||||||
})
|
})
|
||||||
.from(olms)
|
.from(clients)
|
||||||
.where(eq(olms.siteId, site.siteId));
|
.where(eq(clients.siteId, site.siteId));
|
||||||
|
|
||||||
let subnets = clientsQuery.map((client) => client.subnet);
|
let subnets = clientsQuery.map((client) => client.subnet);
|
||||||
|
|
||||||
// exclude the exit node address by replacing after the / with a site block size
|
// exclude the exit node address by replacing after the / with a site block size
|
||||||
subnets.push(
|
subnets.push(
|
||||||
site.address.replace(
|
address.replace(
|
||||||
/\/\d+$/,
|
/\/\d+$/,
|
||||||
`/${config.getRawConfig().wg_site.block_size}`
|
`/${config.getRawConfig().wg_site.block_size}`
|
||||||
)
|
)
|
||||||
|
@ -88,7 +98,7 @@ export async function pickOlmDefaults(
|
||||||
const newSubnet = findNextAvailableCidr(
|
const newSubnet = findNextAvailableCidr(
|
||||||
subnets,
|
subnets,
|
||||||
config.getRawConfig().wg_site.block_size,
|
config.getRawConfig().wg_site.block_size,
|
||||||
site.address
|
address
|
||||||
);
|
);
|
||||||
if (!newSubnet) {
|
if (!newSubnet) {
|
||||||
return next(
|
return next(
|
||||||
|
@ -105,11 +115,11 @@ export async function pickOlmDefaults(
|
||||||
return response<PickClientDefaultsResponse>(res, {
|
return response<PickClientDefaultsResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
siteId: site.siteId,
|
siteId: site.siteId,
|
||||||
address: site.address,
|
address: address,
|
||||||
publicKey: site.publicKey,
|
publicKey: publicKey,
|
||||||
name: site.name,
|
name: site.name,
|
||||||
listenPort: site.listenPort,
|
listenPort: listenPort,
|
||||||
endpoint: site.endpoint,
|
endpoint: endpoint,
|
||||||
subnet: newSubnet,
|
subnet: newSubnet,
|
||||||
clientId,
|
clientId,
|
||||||
clientSecret: secret
|
clientSecret: secret
|
|
@ -7,7 +7,7 @@ import * as target from "./target";
|
||||||
import * as user from "./user";
|
import * as user from "./user";
|
||||||
import * as auth from "./auth";
|
import * as auth from "./auth";
|
||||||
import * as role from "./role";
|
import * as role from "./role";
|
||||||
import * as olm from "./olm";
|
import * as client from "./client";
|
||||||
import * as accessToken from "./accessToken";
|
import * as accessToken from "./accessToken";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import {
|
import {
|
||||||
|
@ -100,7 +100,7 @@ authenticated.get(
|
||||||
"/site/:siteId/pick-client-defaults",
|
"/site/:siteId/pick-client-defaults",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
verifyUserHasAction(ActionsEnum.createClient),
|
verifyUserHasAction(ActionsEnum.createClient),
|
||||||
olm.pickOlmDefaults
|
client.pickClientDefaults
|
||||||
);
|
);
|
||||||
|
|
||||||
// authenticated.get(
|
// authenticated.get(
|
||||||
|
|
|
@ -1,2 +1 @@
|
||||||
export * from "./pickOlmDefaults";
|
export * from "./handleOlmRegisterMessage";
|
||||||
export * from "./handleOlmRegisterMessage";
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue