-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor server Signed-off-by: Jari Kolehmainen <[email protected]> * add multitenancy Signed-off-by: Jari Kolehmainen <[email protected]> * cleanup Signed-off-by: Jari Kolehmainen <[email protected]> * clarify agents key Signed-off-by: Jari Kolehmainen <[email protected]>
- Loading branch information
Showing
9 changed files
with
306 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { IncomingMessage } from "http"; | ||
import WebSocket from "ws"; | ||
import { Agent } from "../agent"; | ||
import { defaultClusterId, TunnelServer } from "../server"; | ||
import { parseAuthorization, verifyAgentToken } from "../util"; | ||
|
||
export function handleAgentSocket(req: IncomingMessage, socket: WebSocket, server: TunnelServer) { | ||
if (!req.headers.authorization) { | ||
console.log("SERVER: agent did not specify authorization header, closing connection."); | ||
socket.close(4401); | ||
|
||
return; | ||
} | ||
|
||
const authorization = parseAuthorization(req.headers.authorization); | ||
|
||
if (authorization?.type !== "bearer") { | ||
console.log("SERVER: invalid agent token, closing connection."); | ||
socket.close(4403); | ||
|
||
return; | ||
} | ||
|
||
let clusterId: string; | ||
|
||
if (server.agentToken) { | ||
if (authorization.token !== server.agentToken) { | ||
console.log("SERVER: invalid agent token, closing connection."); | ||
socket.close(4403); | ||
|
||
return; | ||
} | ||
|
||
clusterId = defaultClusterId; | ||
} else { | ||
try { | ||
const tokenData = verifyAgentToken(authorization.token, server); | ||
|
||
clusterId = tokenData.sub; | ||
} catch(error) { | ||
console.error(error); | ||
console.log("SERVER: invalid agent jwt token, closing connection."); | ||
socket.close(4403); | ||
|
||
return; | ||
} | ||
} | ||
|
||
const agents = server.getAgentsForClusterId(clusterId); | ||
|
||
console.log("SERVER: agent connected"); | ||
const publicKey = Buffer.from(req.headers["x-bored-publickey"]?.toString() || "", "base64").toString("utf-8"); | ||
const agent = new Agent(socket, publicKey); | ||
|
||
agents.push(agent); | ||
|
||
socket.on("close", () => { | ||
console.log("SERVER: agent disconnected"); | ||
const index = agents.findIndex((agent) => agent.socket === socket); | ||
|
||
if (index !== -1) { | ||
agents.splice(index, 1); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Agent } from "../agent"; | ||
import { defaultClusterId, TunnelServer } from "../server"; | ||
import { parseAuthorization, verifyClientToken } from "../util"; | ||
|
||
export function handleClientPublicKey(req: IncomingMessage, res: ServerResponse, server: TunnelServer) { | ||
if (!req.headers.authorization) { // old agent | ||
handleClientDefaultPublicKey(res, server); | ||
|
||
return; | ||
} | ||
|
||
const authorization = parseAuthorization(req.headers.authorization); | ||
|
||
if (!authorization || !authorization.token) { | ||
res.writeHead(403); | ||
res.end(); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
const tokenData = verifyClientToken(authorization?.token, server); | ||
const agents = server.getAgentsForClusterId(tokenData.clusterId); | ||
|
||
respondWithAgentPublicKey(res, agents); | ||
} catch(error) { | ||
res.writeHead(4403); | ||
res.end(); | ||
} | ||
|
||
|
||
return; | ||
} | ||
|
||
function handleClientDefaultPublicKey(res: ServerResponse, server: TunnelServer) { | ||
const agents = server.getAgentsForClusterId(defaultClusterId); | ||
|
||
respondWithAgentPublicKey(res, agents); | ||
} | ||
|
||
function respondWithAgentPublicKey(res: ServerResponse, agents: Agent[]) { | ||
if (agents.length === 0) { | ||
res.writeHead(404); | ||
res.end(); | ||
|
||
return; | ||
} | ||
|
||
res.writeHead(200); | ||
res.write(agents[0].publicKey); | ||
res.end(); | ||
} |
Oops, something went wrong.