Initial hp working?

This commit is contained in:
Owen 2025-02-22 11:20:56 -05:00
parent a9a9391b39
commit e4c5be4350
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
6 changed files with 49 additions and 16 deletions

View file

@ -1,2 +1,3 @@
export * from "./getConfig"; export * from "./getConfig";
export * from "./receiveBandwidth"; export * from "./receiveBandwidth";
export * from "./updateHolePunch";

View file

@ -35,6 +35,8 @@ export async function updateHolePunch(
} }
const { olmId, newtId, ip, port, timestamp } = parsedParams.data; const { olmId, newtId, ip, port, timestamp } = parsedParams.data;
logger.debug(`Got hole punch with ip: ${ip}, port: ${port} for olmId: ${olmId} or newtId: ${newtId}`);
if (olmId) { if (olmId) {
const [olm] = await db const [olm] = await db

View file

@ -34,6 +34,7 @@ internalRouter.use("/gerbil", gerbilRouter);
gerbilRouter.post("/get-config", gerbil.getConfig); gerbilRouter.post("/get-config", gerbil.getConfig);
gerbilRouter.post("/receive-bandwidth", gerbil.receiveBandwidth); gerbilRouter.post("/receive-bandwidth", gerbil.receiveBandwidth);
gerbilRouter.post("/update-hole-punch", gerbil.updateHolePunch);
// Badger routes // Badger routes
const badgerRouter = Router(); const badgerRouter = Router();

View file

@ -21,7 +21,6 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
logger.debug(JSON.stringify(message.data)); logger.debug(JSON.stringify(message.data));
logger.debug("Handling Newt get config message!"); logger.debug("Handling Newt get config message!");
if (!newt) { if (!newt) {
@ -67,7 +66,7 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
.update(sites) .update(sites)
.set({ .set({
publicKey, publicKey,
endpoint, // endpoint,
address, address,
listenPort listenPort
}) })
@ -82,8 +81,8 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
const [siteRes] = await db const [siteRes] = await db
.update(sites) .update(sites)
.set({ .set({
publicKey, publicKey
endpoint // endpoint
}) })
.where(eq(sites.siteId, siteId)) .where(eq(sites.siteId, siteId))
.returning(); .returning();
@ -101,13 +100,22 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
.from(clients) .from(clients)
.where(eq(clients.siteId, siteId)); .where(eq(clients.siteId, siteId));
const now = new Date().getTime() / 1000;
const peers = await Promise.all( const peers = await Promise.all(
clientsRes.map(async (client) => { clientsRes
return { .filter((client) => {
publicKey: client.pubKey, if (client.lastHolePunch && now - client.lastHolePunch > 6) {
allowedIps: [client.subnet] logger.warn("Client last hole punch is too old");
}; return;
}) }
})
.map(async (client) => {
return {
publicKey: client.pubKey,
allowedIps: [client.subnet],
endpoint: client.endpoint
};
})
); );
const configResponse = { const configResponse = {
@ -162,9 +170,11 @@ async function getNextAvailableSubnet(): Promise<string> {
async function getNextAvailablePort(): Promise<number> { async function getNextAvailablePort(): Promise<number> {
// Get all existing ports from exitNodes table // Get all existing ports from exitNodes table
const existingPorts = await db.select({ const existingPorts = await db
listenPort: sites.listenPort, .select({
}).from(sites); listenPort: sites.listenPort
})
.from(sites);
// Find the first available port between 1024 and 65535 // Find the first available port between 1024 and 65535
let nextPort = config.getRawConfig().newt.start_port; let nextPort = config.getRawConfig().newt.start_port;
@ -174,7 +184,7 @@ async function getNextAvailablePort(): Promise<number> {
} }
nextPort++; nextPort++;
if (nextPort > 65535) { if (nextPort > 65535) {
throw new Error('No available ports remaining in space'); throw new Error("No available ports remaining in space");
} }
} }

View file

@ -6,6 +6,7 @@ import { sendToClient } from '../ws';
export async function addPeer(siteId: number, peer: { export async function addPeer(siteId: number, peer: {
publicKey: string; publicKey: string;
allowedIps: string[]; allowedIps: string[];
endpoint: string;
}) { }) {
const [site] = await db.select().from(sites).where(eq(sites.siteId, siteId)).limit(1); const [site] = await db.select().from(sites).where(eq(sites.siteId, siteId)).limit(1);

View file

@ -56,6 +56,23 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
return; return;
} }
// make sure we hand endpoints for both the site and the client and the lastHolePunch is not too old
if (!site.endpoint || !client.endpoint) {
logger.warn("Site or client has no endpoint or listen port");
return;
}
const now = new Date().getTime() / 1000;
if (site.lastHolePunch && now - site.lastHolePunch > 6) {
logger.warn("Site last hole punch is too old");
return;
}
if (client.lastHolePunch && now - client.lastHolePunch > 6) {
logger.warn("Client last hole punch is too old");
return;
}
await db await db
.update(clients) .update(clients)
.set({ .set({
@ -77,14 +94,15 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
// add the peer to the exit node // add the peer to the exit node
await addPeer(site.siteId, { await addPeer(site.siteId, {
publicKey: publicKey, publicKey: publicKey,
allowedIps: [client.subnet] allowedIps: [client.subnet],
endpoint: client.endpoint
}); });
return { return {
message: { message: {
type: "olm/wg/connect", type: "olm/wg/connect",
data: { data: {
endpoint: `${site.endpoint}:${site.listenPort}`, endpoint: site.endpoint,
publicKey: site.publicKey, publicKey: site.publicKey,
serverIP: site.address!.split("/")[0], serverIP: site.address!.split("/")[0],
tunnelIP: client.subnet tunnelIP: client.subnet