mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-01 08:34:53 +02:00
Newt working
This commit is contained in:
parent
f9e0c33368
commit
0670d48ac7
7 changed files with 112 additions and 53 deletions
|
@ -43,9 +43,7 @@ export async function deletePeer(exitNodeId: number, publicKey: string) {
|
||||||
throw new Error(`Exit node with ID ${exitNodeId} is not reachable`);
|
throw new Error(`Exit node with ID ${exitNodeId} is not reachable`);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const response = await axios.delete(`${exitNode.reachableAt}/peer`, {
|
const response = await axios.delete(`${exitNode.reachableAt}/peer?public_key=${encodeURIComponent(publicKey)}`);
|
||||||
data: { publicKey } // Send public key in request body
|
|
||||||
});
|
|
||||||
logger.info('Peer deleted successfully:', response.data.status);
|
logger.info('Peer deleted successfully:', response.data.status);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {
|
||||||
generateSessionToken,
|
generateSessionToken,
|
||||||
} from "@server/auth";
|
} from "@server/auth";
|
||||||
import { createNewtSession } from "@server/auth/newt";
|
import { createNewtSession } from "@server/auth/newt";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
|
||||||
export const createNewtBodySchema = z.object({});
|
export const createNewtBodySchema = z.object({});
|
||||||
|
|
||||||
|
@ -24,6 +25,13 @@ export type CreateNewtResponse = {
|
||||||
secret: string;
|
secret: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const createNewtSchema = z
|
||||||
|
.object({
|
||||||
|
newtId: z.string(),
|
||||||
|
secret: z.string()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
export async function createNewt(
|
export async function createNewt(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
|
@ -31,8 +39,25 @@ export async function createNewt(
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
const parsedBody = createNewtSchema.safeParse(req.body);
|
||||||
|
if (!parsedBody.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedBody.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { newtId, secret } = parsedBody.data;
|
||||||
|
|
||||||
|
if (!req.userOrgRoleId) {
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.FORBIDDEN, "User does not have a role")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// generate a newtId and secret
|
// generate a newtId and secret
|
||||||
const secret = generateId(48);
|
|
||||||
const secretHash = await hash(secret, {
|
const secretHash = await hash(secret, {
|
||||||
memoryCost: 19456,
|
memoryCost: 19456,
|
||||||
timeCost: 2,
|
timeCost: 2,
|
||||||
|
@ -40,8 +65,6 @@ export async function createNewt(
|
||||||
parallelism: 1,
|
parallelism: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
const newtId = generateId(15);
|
|
||||||
|
|
||||||
await db.insert(newts).values({
|
await db.insert(newts).values({
|
||||||
newtId: newtId,
|
newtId: newtId,
|
||||||
secretHash,
|
secretHash,
|
||||||
|
|
|
@ -4,8 +4,6 @@ import { exitNodes, resources, sites, targets } from "@server/db/schema";
|
||||||
import { eq, inArray } from "drizzle-orm";
|
import { eq, inArray } from "drizzle-orm";
|
||||||
import { addPeer, deletePeer } from "../gerbil/peers";
|
import { addPeer, deletePeer } from "../gerbil/peers";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { findNextAvailableCidr } from "@server/utils/ip";
|
|
||||||
import { exit } from "process";
|
|
||||||
|
|
||||||
export const handleRegisterMessage: MessageHandler = async (context) => {
|
export const handleRegisterMessage: MessageHandler = async (context) => {
|
||||||
const { message, newt, sendToClient } = context;
|
const { message, newt, sendToClient } = context;
|
||||||
|
@ -28,13 +26,18 @@ export const handleRegisterMessage: MessageHandler = async (context) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// const [site] = await db
|
|
||||||
// .select()
|
|
||||||
// .from(sites)
|
|
||||||
// .where(eq(sites.siteId, siteId))
|
|
||||||
// .limit(1);
|
|
||||||
|
|
||||||
const [site] = await db
|
const [site] = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.siteId, siteId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!site || !site.exitNodeId) {
|
||||||
|
logger.warn("Site not found or does not have exit node");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [updatedSite] = await db
|
||||||
.update(sites)
|
.update(sites)
|
||||||
.set({
|
.set({
|
||||||
pubKey: publicKey
|
pubKey: publicKey
|
||||||
|
@ -43,11 +46,6 @@ export const handleRegisterMessage: MessageHandler = async (context) => {
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
|
|
||||||
if (!site || !site.exitNodeId) {
|
|
||||||
logger.warn("Site not found or does not have exit node");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [exitNode] = await db
|
const [exitNode] = await db
|
||||||
.select()
|
.select()
|
||||||
.from(exitNodes)
|
.from(exitNodes)
|
||||||
|
@ -100,10 +98,10 @@ export const handleRegisterMessage: MessageHandler = async (context) => {
|
||||||
message: {
|
message: {
|
||||||
type: "newt/wg/connect",
|
type: "newt/wg/connect",
|
||||||
data: {
|
data: {
|
||||||
endpoint: exitNode.endpoint,
|
endpoint: `${exitNode.endpoint}:${exitNode.listenPort}`,
|
||||||
publicKey: exitNode.publicKey,
|
publicKey: exitNode.publicKey,
|
||||||
serverIP: exitNode.address,
|
serverIP: exitNode.address.split("/")[0],
|
||||||
tunnelIP: site.subnet,
|
tunnelIP: site.subnet.split("/")[0],
|
||||||
targets: {
|
targets: {
|
||||||
udp: udpTargets,
|
udp: udpTargets,
|
||||||
tcp: tcpTargets,
|
tcp: tcpTargets,
|
||||||
|
|
|
@ -10,6 +10,9 @@ import { eq, and } from "drizzle-orm";
|
||||||
import { getUniqueSiteName } from "@server/db/names";
|
import { getUniqueSiteName } from "@server/db/names";
|
||||||
import { addPeer } from "../gerbil/peers";
|
import { addPeer } from "../gerbil/peers";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
|
import { hash } from "@node-rs/argon2";
|
||||||
|
import { newts } from "@server/db/schema";
|
||||||
|
import moment from "moment";
|
||||||
|
|
||||||
const createSiteParamsSchema = z.object({
|
const createSiteParamsSchema = z.object({
|
||||||
orgId: z.string(),
|
orgId: z.string(),
|
||||||
|
@ -22,10 +25,14 @@ const createSiteSchema = z
|
||||||
subdomain: z.string().min(1).max(255).optional(),
|
subdomain: z.string().min(1).max(255).optional(),
|
||||||
pubKey: z.string().optional(),
|
pubKey: z.string().optional(),
|
||||||
subnet: z.string(),
|
subnet: z.string(),
|
||||||
|
newtId: z.string().optional(),
|
||||||
|
secret: z.string().optional(),
|
||||||
type: z.string(),
|
type: z.string(),
|
||||||
})
|
})
|
||||||
.strict();
|
.strict();
|
||||||
|
|
||||||
|
export type CreateSiteBody = z.infer<typeof createSiteSchema>;
|
||||||
|
|
||||||
export type CreateSiteResponse = {
|
export type CreateSiteResponse = {
|
||||||
name: string;
|
name: string;
|
||||||
siteId: number;
|
siteId: number;
|
||||||
|
@ -49,7 +56,8 @@ export async function createSite(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, type, exitNodeId, pubKey, subnet } = parsedBody.data;
|
const { name, type, exitNodeId, pubKey, subnet, newtId, secret } =
|
||||||
|
parsedBody.data;
|
||||||
|
|
||||||
const parsedParams = createSiteParamsSchema.safeParse(req.params);
|
const parsedParams = createSiteParamsSchema.safeParse(req.params);
|
||||||
if (!parsedParams.success) {
|
if (!parsedParams.success) {
|
||||||
|
@ -80,7 +88,8 @@ export async function createSite(
|
||||||
type,
|
type,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (pubKey) {
|
if (pubKey && type == "wireguard") {
|
||||||
|
// we dont add the pubKey for newts because the newt will generate it
|
||||||
payload = {
|
payload = {
|
||||||
...payload,
|
...payload,
|
||||||
pubKey,
|
pubKey,
|
||||||
|
@ -114,19 +123,34 @@ export async function createSite(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pubKey) {
|
// add the peer to the exit node
|
||||||
// add the peer to the exit node
|
if (type == "newt") {
|
||||||
if (type == "newt") {
|
const secretHash = await hash(secret!, {
|
||||||
await addPeer(exitNodeId, {
|
memoryCost: 19456,
|
||||||
publicKey: pubKey,
|
timeCost: 2,
|
||||||
allowedIps: [subnet],
|
outputLen: 32,
|
||||||
});
|
parallelism: 1,
|
||||||
} else if (type == "wireguard") {
|
});
|
||||||
await addPeer(exitNodeId, {
|
|
||||||
publicKey: pubKey,
|
await db.insert(newts).values({
|
||||||
allowedIps: [],
|
newtId: newtId!,
|
||||||
});
|
secretHash,
|
||||||
|
siteId: newSite.siteId,
|
||||||
|
dateCreated: moment().toISOString(),
|
||||||
|
});
|
||||||
|
} else if (type == "wireguard") {
|
||||||
|
if (!pubKey) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
"Public key is required for wireguard sites"
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
await addPeer(exitNodeId, {
|
||||||
|
publicKey: pubKey,
|
||||||
|
allowedIps: [],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return response(res, {
|
return response(res, {
|
||||||
|
@ -142,7 +166,7 @@ export async function createSite(
|
||||||
status: HttpCode.CREATED,
|
status: HttpCode.CREATED,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error);
|
throw error;
|
||||||
return next(
|
return next(
|
||||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
);
|
);
|
||||||
|
|
|
@ -7,6 +7,7 @@ import HttpCode from "@server/types/HttpCode";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { findNextAvailableCidr } from "@server/utils/ip";
|
import { findNextAvailableCidr } from "@server/utils/ip";
|
||||||
|
import { generateId } from "@server/auth";
|
||||||
|
|
||||||
export type PickSiteDefaultsResponse = {
|
export type PickSiteDefaultsResponse = {
|
||||||
exitNodeId: number;
|
exitNodeId: number;
|
||||||
|
@ -16,6 +17,8 @@ export type PickSiteDefaultsResponse = {
|
||||||
listenPort: number;
|
listenPort: number;
|
||||||
endpoint: string;
|
endpoint: string;
|
||||||
subnet: string;
|
subnet: string;
|
||||||
|
newtId: string;
|
||||||
|
newtSecret: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function pickSiteDefaults(
|
export async function pickSiteDefaults(
|
||||||
|
@ -60,6 +63,9 @@ export async function pickSiteDefaults(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newtId = generateId(15);
|
||||||
|
const secret = generateId(48);
|
||||||
|
|
||||||
return response<PickSiteDefaultsResponse>(res, {
|
return response<PickSiteDefaultsResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
exitNodeId: exitNode.exitNodeId,
|
exitNodeId: exitNode.exitNodeId,
|
||||||
|
@ -69,6 +75,8 @@ export async function pickSiteDefaults(
|
||||||
listenPort: exitNode.listenPort,
|
listenPort: exitNode.listenPort,
|
||||||
endpoint: exitNode.endpoint,
|
endpoint: exitNode.endpoint,
|
||||||
subnet: newSubnet,
|
subnet: newSubnet,
|
||||||
|
newtId,
|
||||||
|
newtSecret: secret,
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
error: false,
|
error: false,
|
||||||
|
|
|
@ -91,7 +91,7 @@ export async function createTarget(
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure the target is within the site subnet
|
// make sure the target is within the site subnet
|
||||||
if (!isIpInCidr(targetData.ip, site.subnet!)) {
|
if (site.type == "wireguard" && !isIpInCidr(targetData.ip, site.subnet!)) {
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(
|
||||||
HttpCode.BAD_REQUEST,
|
HttpCode.BAD_REQUEST,
|
||||||
|
|
|
@ -29,7 +29,7 @@ import {
|
||||||
} from "@app/components/Credenza";
|
} from "@app/components/Credenza";
|
||||||
import { useOrgContext } from "@app/hooks/useOrgContext";
|
import { useOrgContext } from "@app/hooks/useOrgContext";
|
||||||
import { useParams, useRouter } from "next/navigation";
|
import { useParams, useRouter } from "next/navigation";
|
||||||
import { PickSiteDefaultsResponse } from "@server/routers/site";
|
import { CreateSiteBody, PickSiteDefaultsResponse } from "@server/routers/site";
|
||||||
import { generateKeypair } from "../[niceId]/components/wireguardConfig";
|
import { generateKeypair } from "../[niceId]/components/wireguardConfig";
|
||||||
import CopyTextBox from "@app/components/CopyTextBox";
|
import CopyTextBox from "@app/components/CopyTextBox";
|
||||||
import { Checkbox } from "@app/components/ui/checkbox";
|
import { Checkbox } from "@app/components/ui/checkbox";
|
||||||
|
@ -43,8 +43,8 @@ import {
|
||||||
import { formatAxiosError } from "@app/lib/utils";
|
import { formatAxiosError } from "@app/lib/utils";
|
||||||
|
|
||||||
const method = [
|
const method = [
|
||||||
{ label: "Wireguard", value: "wg" },
|
|
||||||
{ label: "Newt", value: "newt" },
|
{ label: "Newt", value: "newt" },
|
||||||
|
{ label: "Wireguard", value: "wireguard" },
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const accountFormSchema = z.object({
|
const accountFormSchema = z.object({
|
||||||
|
@ -56,14 +56,14 @@ const accountFormSchema = z.object({
|
||||||
.max(30, {
|
.max(30, {
|
||||||
message: "Name must not be longer than 30 characters.",
|
message: "Name must not be longer than 30 characters.",
|
||||||
}),
|
}),
|
||||||
method: z.enum(["wg", "newt"]),
|
method: z.enum(["wireguard", "newt"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
type AccountFormValues = z.infer<typeof accountFormSchema>;
|
type AccountFormValues = z.infer<typeof accountFormSchema>;
|
||||||
|
|
||||||
const defaultValues: Partial<AccountFormValues> = {
|
const defaultValues: Partial<AccountFormValues> = {
|
||||||
name: "",
|
name: "",
|
||||||
method: "wg",
|
method: "newt",
|
||||||
};
|
};
|
||||||
|
|
||||||
type CreateSiteFormProps = {
|
type CreateSiteFormProps = {
|
||||||
|
@ -124,13 +124,22 @@ export default function CreateSiteForm({ open, setOpen }: CreateSiteFormProps) {
|
||||||
|
|
||||||
async function onSubmit(data: AccountFormValues) {
|
async function onSubmit(data: AccountFormValues) {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
if (!siteDefaults || !keypair) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let payload: CreateSiteBody = {
|
||||||
|
name: data.name,
|
||||||
|
subnet: siteDefaults.subnet,
|
||||||
|
exitNodeId: siteDefaults.exitNodeId,
|
||||||
|
pubKey: keypair.publicKey,
|
||||||
|
type: data.method,
|
||||||
|
};
|
||||||
|
if (data.method === "newt") {
|
||||||
|
payload.secret = siteDefaults.newtSecret;
|
||||||
|
payload.newtId = siteDefaults.newtId;
|
||||||
|
}
|
||||||
const res = await api
|
const res = await api
|
||||||
.put(`/org/${orgId}/site/`, {
|
.put(`/org/${orgId}/site/`, payload)
|
||||||
name: data.name,
|
|
||||||
subnet: siteDefaults?.subnet,
|
|
||||||
exitNodeId: siteDefaults?.exitNodeId,
|
|
||||||
pubKey: keypair?.publicKey,
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
toast({
|
toast({
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
|
@ -165,8 +174,7 @@ Endpoint = ${siteDefaults.endpoint}:${siteDefaults.listenPort}
|
||||||
PersistentKeepalive = 5`
|
PersistentKeepalive = 5`
|
||||||
: "";
|
: "";
|
||||||
|
|
||||||
const newtConfig = `curl -fsSL https://get.docker.com -o get-docker.sh
|
const newtConfig = `newt --id ${siteDefaults?.newtId} --secret ${siteDefaults?.newtSecret}`;
|
||||||
sh get-docker.sh`;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -236,7 +244,7 @@ sh get-docker.sh`;
|
||||||
<SelectValue placeholder="Select method" />
|
<SelectValue placeholder="Select method" />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
<SelectItem value="wg">
|
<SelectItem value="wireguard">
|
||||||
WireGuard
|
WireGuard
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
<SelectItem value="newt">
|
<SelectItem value="newt">
|
||||||
|
@ -255,10 +263,10 @@ sh get-docker.sh`;
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="max-w-md">
|
<div className="max-w-md">
|
||||||
{form.watch("method") === "wg" &&
|
{form.watch("method") === "wireguard" &&
|
||||||
!isLoading ? (
|
!isLoading ? (
|
||||||
<CopyTextBox text={wgConfig} />
|
<CopyTextBox text={wgConfig} />
|
||||||
) : form.watch("method") === "wg" &&
|
) : form.watch("method") === "wireguard" &&
|
||||||
isLoading ? (
|
isLoading ? (
|
||||||
<p>
|
<p>
|
||||||
Loading WireGuard
|
Loading WireGuard
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue