mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-23 18:56:07 +02:00
Break out hole punch
This commit is contained in:
parent
50cf284273
commit
fcc86b07ba
1 changed files with 217 additions and 219 deletions
|
@ -66,51 +66,6 @@ export async function updateHolePunch(
|
||||||
publicKey
|
publicKey
|
||||||
} = parsedParams.data;
|
} = parsedParams.data;
|
||||||
|
|
||||||
let currentSiteId: number | undefined;
|
|
||||||
let destinations: PeerDestination[] = [];
|
|
||||||
|
|
||||||
if (olmId) {
|
|
||||||
logger.debug(
|
|
||||||
`Got hole punch with ip: ${ip}, port: ${port} for olmId: ${olmId}${publicKey ? ` with exit node publicKey: ${publicKey}` : ""}`
|
|
||||||
);
|
|
||||||
|
|
||||||
const { session, olm: olmSession } =
|
|
||||||
await validateOlmSessionToken(token);
|
|
||||||
if (!session || !olmSession) {
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.UNAUTHORIZED, "Unauthorized")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (olmId !== olmSession.olmId) {
|
|
||||||
logger.warn(
|
|
||||||
`Olm ID mismatch: ${olmId} !== ${olmSession.olmId}`
|
|
||||||
);
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.UNAUTHORIZED, "Unauthorized")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [olm] = await db
|
|
||||||
.select()
|
|
||||||
.from(olms)
|
|
||||||
.where(eq(olms.olmId, olmId));
|
|
||||||
|
|
||||||
if (!olm || !olm.clientId) {
|
|
||||||
logger.warn(`Olm not found: ${olmId}`);
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.NOT_FOUND, "Olm not found")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [client] = await db
|
|
||||||
.update(clients)
|
|
||||||
.set({
|
|
||||||
lastHolePunch: timestamp
|
|
||||||
})
|
|
||||||
.where(eq(clients.clientId, olm.clientId))
|
|
||||||
.returning();
|
|
||||||
|
|
||||||
let exitNode: ExitNode | undefined;
|
let exitNode: ExitNode | undefined;
|
||||||
if (publicKey) {
|
if (publicKey) {
|
||||||
// Get the exit node by public key
|
// Get the exit node by public key
|
||||||
|
@ -130,9 +85,87 @@ export async function updateHolePunch(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const destinations = await updateAndGenerateEndpointDestinations(
|
||||||
|
olmId,
|
||||||
|
newtId,
|
||||||
|
ip,
|
||||||
|
port,
|
||||||
|
timestamp,
|
||||||
|
token,
|
||||||
|
exitNode
|
||||||
|
);
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
`Returning ${destinations.length} peer destinations for olmId: ${olmId} or newtId: ${newtId}: ${JSON.stringify(destinations, null, 2)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Return the new multi-peer structure
|
||||||
|
return res.status(HttpCode.OK).send({
|
||||||
|
destinations: destinations
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"An error occurred..."
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateAndGenerateEndpointDestinations(
|
||||||
|
olmId: string | undefined,
|
||||||
|
newtId: string | undefined,
|
||||||
|
ip: string,
|
||||||
|
port: number,
|
||||||
|
timestamp: number,
|
||||||
|
token: string,
|
||||||
|
exitNode: ExitNode
|
||||||
|
) {
|
||||||
|
let currentSiteId: number | undefined;
|
||||||
|
let destinations: PeerDestination[] = [];
|
||||||
|
|
||||||
|
if (olmId) {
|
||||||
|
logger.debug(
|
||||||
|
`Got hole punch with ip: ${ip}, port: ${port} for olmId: ${olmId}`
|
||||||
|
);
|
||||||
|
|
||||||
|
const { session, olm: olmSession } =
|
||||||
|
await validateOlmSessionToken(token);
|
||||||
|
if (!session || !olmSession) {
|
||||||
|
throw new Error("Unauthorized");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (olmId !== olmSession.olmId) {
|
||||||
|
logger.warn(`Olm ID mismatch: ${olmId} !== ${olmSession.olmId}`);
|
||||||
|
throw new Error("Unauthorized");
|
||||||
|
}
|
||||||
|
|
||||||
|
const [olm] = await db.select().from(olms).where(eq(olms.olmId, olmId));
|
||||||
|
|
||||||
|
if (!olm || !olm.clientId) {
|
||||||
|
logger.warn(`Olm not found: ${olmId}`);
|
||||||
|
throw new Error("Olm not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const [client] = await db
|
||||||
|
.update(clients)
|
||||||
|
.set({
|
||||||
|
lastHolePunch: timestamp
|
||||||
|
})
|
||||||
|
.where(eq(clients.clientId, olm.clientId))
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Get sites that are on this specific exit node and connected to this client
|
// Get sites that are on this specific exit node and connected to this client
|
||||||
const sitesOnExitNode = await db
|
const sitesOnExitNode = await db
|
||||||
.select({ siteId: sites.siteId, subnet: sites.subnet, listenPort: sites.listenPort })
|
.select({
|
||||||
|
siteId: sites.siteId,
|
||||||
|
subnet: sites.subnet,
|
||||||
|
listenPort: sites.listenPort
|
||||||
|
})
|
||||||
.from(sites)
|
.from(sites)
|
||||||
.innerJoin(clientSites, eq(sites.siteId, clientSites.siteId))
|
.innerJoin(clientSites, eq(sites.siteId, clientSites.siteId))
|
||||||
.where(
|
.where(
|
||||||
|
@ -145,7 +178,7 @@ export async function updateHolePunch(
|
||||||
// Update clientSites for each site on this exit node
|
// Update clientSites for each site on this exit node
|
||||||
for (const site of sitesOnExitNode) {
|
for (const site of sitesOnExitNode) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Updating site ${site.siteId} on exit node with publicKey: ${publicKey}`
|
`Updating site ${site.siteId} on exit node ${exitNode.exitNodeId}`
|
||||||
);
|
);
|
||||||
|
|
||||||
await db
|
await db
|
||||||
|
@ -162,13 +195,11 @@ export async function updateHolePunch(
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Updated ${sitesOnExitNode.length} sites on exit node with publicKey: ${publicKey}`
|
`Updated ${sitesOnExitNode.length} sites on exit node ${exitNode.exitNodeId}`
|
||||||
);
|
);
|
||||||
if (!client) {
|
if (!client) {
|
||||||
logger.warn(`Client not found for olm: ${olmId}`);
|
logger.warn(`Client not found for olm: ${olmId}`);
|
||||||
return next(
|
throw new Error("Client not found");
|
||||||
createHttpError(HttpCode.NOT_FOUND, "Client not found")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a list of the destinations from the sites
|
// Create a list of the destinations from the sites
|
||||||
|
@ -180,7 +211,6 @@ export async function updateHolePunch(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (newtId) {
|
} else if (newtId) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Got hole punch with ip: ${ip}, port: ${port} for newtId: ${newtId}`
|
`Got hole punch with ip: ${ip}, port: ${port} for newtId: ${newtId}`
|
||||||
|
@ -190,18 +220,14 @@ export async function updateHolePunch(
|
||||||
await validateNewtSessionToken(token);
|
await validateNewtSessionToken(token);
|
||||||
|
|
||||||
if (!session || !newtSession) {
|
if (!session || !newtSession) {
|
||||||
return next(
|
throw new Error("Unauthorized");
|
||||||
createHttpError(HttpCode.UNAUTHORIZED, "Unauthorized")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newtId !== newtSession.newtId) {
|
if (newtId !== newtSession.newtId) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Newt ID mismatch: ${newtId} !== ${newtSession.newtId}`
|
`Newt ID mismatch: ${newtId} !== ${newtSession.newtId}`
|
||||||
);
|
);
|
||||||
return next(
|
throw new Error("Unauthorized");
|
||||||
createHttpError(HttpCode.UNAUTHORIZED, "Unauthorized")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const [newt] = await db
|
const [newt] = await db
|
||||||
|
@ -211,9 +237,7 @@ export async function updateHolePunch(
|
||||||
|
|
||||||
if (!newt || !newt.siteId) {
|
if (!newt || !newt.siteId) {
|
||||||
logger.warn(`Newt not found: ${newtId}`);
|
logger.warn(`Newt not found: ${newtId}`);
|
||||||
return next(
|
throw new Error("Newt not found");
|
||||||
createHttpError(HttpCode.NOT_FOUND, "New not found")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentSiteId = newt.siteId;
|
currentSiteId = newt.siteId;
|
||||||
|
@ -230,9 +254,7 @@ export async function updateHolePunch(
|
||||||
|
|
||||||
if (!updatedSite || !updatedSite.subnet) {
|
if (!updatedSite || !updatedSite.subnet) {
|
||||||
logger.warn(`Site not found: ${newt.siteId}`);
|
logger.warn(`Site not found: ${newt.siteId}`);
|
||||||
return next(
|
throw new Error("Site not found");
|
||||||
createHttpError(HttpCode.NOT_FOUND, "Site not found")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find all clients that connect to this site
|
// Find all clients that connect to this site
|
||||||
|
@ -281,29 +303,5 @@ export async function updateHolePunch(
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
return destinations;
|
||||||
// if (destinations.length === 0) {
|
|
||||||
// logger.warn(
|
|
||||||
// `No peer destinations found for olmId: ${olmId} or newtId: ${newtId}`
|
|
||||||
// );
|
|
||||||
// return next(createHttpError(HttpCode.NOT_FOUND, "No peer destinations found"));
|
|
||||||
// }
|
|
||||||
|
|
||||||
logger.debug(
|
|
||||||
`Returning ${destinations.length} peer destinations for olmId: ${olmId} or newtId: ${newtId}: ${JSON.stringify(destinations, null, 2)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// Return the new multi-peer structure
|
|
||||||
return res.status(HttpCode.OK).send({
|
|
||||||
destinations: destinations
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error);
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.INTERNAL_SERVER_ERROR,
|
|
||||||
"An error occurred..."
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue