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 "./receiveBandwidth";
export * from "./updateHolePunch";

View file

@ -35,6 +35,8 @@ export async function updateHolePunch(
}
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) {
const [olm] = await db

View file

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

View file

@ -21,7 +21,6 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
logger.debug(JSON.stringify(message.data));
logger.debug("Handling Newt get config message!");
if (!newt) {
@ -67,7 +66,7 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
.update(sites)
.set({
publicKey,
endpoint,
// endpoint,
address,
listenPort
})
@ -82,8 +81,8 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
const [siteRes] = await db
.update(sites)
.set({
publicKey,
endpoint
publicKey
// endpoint
})
.where(eq(sites.siteId, siteId))
.returning();
@ -101,13 +100,22 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
.from(clients)
.where(eq(clients.siteId, siteId));
const now = new Date().getTime() / 1000;
const peers = await Promise.all(
clientsRes.map(async (client) => {
return {
publicKey: client.pubKey,
allowedIps: [client.subnet]
};
})
clientsRes
.filter((client) => {
if (client.lastHolePunch && now - client.lastHolePunch > 6) {
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 = {
@ -162,9 +170,11 @@ async function getNextAvailableSubnet(): Promise<string> {
async function getNextAvailablePort(): Promise<number> {
// Get all existing ports from exitNodes table
const existingPorts = await db.select({
listenPort: sites.listenPort,
}).from(sites);
const existingPorts = await db
.select({
listenPort: sites.listenPort
})
.from(sites);
// Find the first available port between 1024 and 65535
let nextPort = config.getRawConfig().newt.start_port;
@ -174,7 +184,7 @@ async function getNextAvailablePort(): Promise<number> {
}
nextPort++;
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: {
publicKey: string;
allowedIps: string[];
endpoint: string;
}) {
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;
}
// 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
.update(clients)
.set({
@ -77,14 +94,15 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
// add the peer to the exit node
await addPeer(site.siteId, {
publicKey: publicKey,
allowedIps: [client.subnet]
allowedIps: [client.subnet],
endpoint: client.endpoint
});
return {
message: {
type: "olm/wg/connect",
data: {
endpoint: `${site.endpoint}:${site.listenPort}`,
endpoint: site.endpoint,
publicKey: site.publicKey,
serverIP: site.address!.split("/")[0],
tunnelIP: client.subnet