2025-06-04 12:02:07 -04:00
|
|
|
import { Target } from "@server/db";
|
2024-11-18 22:10:03 -05:00
|
|
|
import { sendToClient } from "../ws";
|
|
|
|
|
2025-02-01 18:36:12 -05:00
|
|
|
export function addTargets(
|
2025-01-28 22:26:45 -05:00
|
|
|
newtId: string,
|
|
|
|
targets: Target[],
|
2025-07-27 10:21:27 -07:00
|
|
|
protocol: string,
|
|
|
|
port: number | null = null
|
2025-02-01 18:36:12 -05:00
|
|
|
) {
|
2024-11-18 22:10:03 -05:00
|
|
|
//create a list of udp and tcp targets
|
2025-01-28 22:26:45 -05:00
|
|
|
const payloadTargets = targets.map((target) => {
|
|
|
|
return `${target.internalPort ? target.internalPort + ":" : ""}${
|
|
|
|
target.ip
|
|
|
|
}:${target.port}`;
|
|
|
|
});
|
|
|
|
|
2025-07-27 10:21:27 -07:00
|
|
|
sendToClient(newtId, {
|
2025-01-28 22:26:45 -05:00
|
|
|
type: `newt/${protocol}/add`,
|
|
|
|
data: {
|
|
|
|
targets: payloadTargets
|
|
|
|
}
|
2025-07-27 10:21:27 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const payloadTargetsResources = targets.map((target) => {
|
|
|
|
return `${port ? port + ":" : ""}${
|
|
|
|
target.ip
|
|
|
|
}:${target.port}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
sendToClient(newtId, {
|
|
|
|
type: `newt/wg/${protocol}/add`,
|
|
|
|
data: {
|
|
|
|
targets: [payloadTargetsResources[0]] // We can only use one target for WireGuard right now
|
|
|
|
}
|
|
|
|
});
|
2024-11-18 22:10:03 -05:00
|
|
|
}
|
|
|
|
|
2025-02-01 18:36:12 -05:00
|
|
|
export function removeTargets(
|
2025-01-28 22:26:45 -05:00
|
|
|
newtId: string,
|
|
|
|
targets: Target[],
|
2025-07-27 10:21:27 -07:00
|
|
|
protocol: string,
|
|
|
|
port: number | null = null
|
2025-02-01 18:36:12 -05:00
|
|
|
) {
|
2024-11-18 22:10:03 -05:00
|
|
|
//create a list of udp and tcp targets
|
2025-01-28 22:26:45 -05:00
|
|
|
const payloadTargets = targets.map((target) => {
|
|
|
|
return `${target.internalPort ? target.internalPort + ":" : ""}${
|
|
|
|
target.ip
|
|
|
|
}:${target.port}`;
|
|
|
|
});
|
|
|
|
|
2025-07-27 10:21:27 -07:00
|
|
|
sendToClient(newtId, {
|
2025-01-28 22:26:45 -05:00
|
|
|
type: `newt/${protocol}/remove`,
|
|
|
|
data: {
|
|
|
|
targets: payloadTargets
|
|
|
|
}
|
2025-07-27 10:21:27 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const payloadTargetsResources = targets.map((target) => {
|
|
|
|
return `${port ? port + ":" : ""}${
|
|
|
|
target.ip
|
|
|
|
}:${target.port}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
sendToClient(newtId, {
|
|
|
|
type: `newt/wg/${protocol}/remove`,
|
|
|
|
data: {
|
|
|
|
targets: [payloadTargetsResources[0]] // We can only use one target for WireGuard right now
|
|
|
|
}
|
|
|
|
});
|
2024-11-18 22:10:03 -05:00
|
|
|
}
|