Skip to content

Commit

Permalink
Refactor TunnelServer (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Jari Kolehmainen <[email protected]>
  • Loading branch information
jakolehm authored Apr 14, 2021
1 parent 89cfe7e commit b5d9de3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
19 changes: 18 additions & 1 deletion src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import WebSocket from "ws";
import { BoredMplexClient } from "bored-mplex";
import { BoredMplex, BoredMplexClient } from "bored-mplex";

export class Agent {
public socket: WebSocket;
Expand All @@ -23,6 +23,23 @@ export class Agent {

addClient(socket: WebSocket) {
this.clients.push(socket);

const mplex = new BoredMplex((stream) => {
const agentStream = this.openStream();

stream.pipe(agentStream);
agentStream.pipe(stream);

stream.on("finish", () => agentStream.end());
agentStream.on("finish", () => stream.end());
});

const duplex = WebSocket.createWebSocketStream(socket);

duplex.pipe(mplex).pipe(duplex);
duplex.on("unpipe", () => {
socket.close(4410);
});
}

removeClient(socket: WebSocket) {
Expand Down
36 changes: 3 additions & 33 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Agent } from "./agent";
import { Socket } from "net";
import { URL } from "url";
import * as jwt from "jsonwebtoken";
import { BoredMplex } from "bored-mplex";
import { parseAuthorization } from "./util";

export class TunnelServer {
private agentToken = "";
Expand Down Expand Up @@ -107,19 +107,6 @@ export class TunnelServer {
}
}

parseAuthorization(authHeader: string) {
const authorization = authHeader.split(" ");

if (authorization.length !== 2) {
return null;
}

return {
type: authorization[0].toLowerCase(),
token: authorization[1]
};
}

handleAgentSocket(req: IncomingMessage, socket: WebSocket) {
if (!req.headers.authorization) {
console.log("SERVER: agent did not specify authorization header, closing connection.");
Expand All @@ -128,7 +115,7 @@ export class TunnelServer {
return;
}

const authorization = this.parseAuthorization(req.headers.authorization);
const authorization = parseAuthorization(req.headers.authorization);

if (authorization?.type !== "bearer" || authorization.token !== this.agentToken) {
console.log("SERVER: invalid agent token, closing connection.");
Expand Down Expand Up @@ -162,7 +149,7 @@ export class TunnelServer {
return;
}

const authorization = this.parseAuthorization(req.headers.authorization);
const authorization = parseAuthorization(req.headers.authorization);

if (authorization?.type !== "bearer") {
console.log("SERVER: invalid client token, closing connection.");
Expand Down Expand Up @@ -195,22 +182,5 @@ export class TunnelServer {
return;
}
agent.addClient(socket);

const mplex = new BoredMplex((stream) => {
const agentStream = agent.openStream();

stream.pipe(agentStream);
agentStream.pipe(stream);

stream.on("finish", () => agentStream.end());
agentStream.on("finish", () => stream.end());
});

const duplex = WebSocket.createWebSocketStream(socket);

duplex.pipe(mplex).pipe(duplex);
duplex.on("unpipe", () => {
socket.close(4410);
});
}
}
13 changes: 13 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

export function parseAuthorization(authHeader: string) {
const authorization = authHeader.split(" ");

if (authorization.length !== 2) {
return null;
}

return {
type: authorization[0].toLowerCase(),
token: authorization[1]
};
}

0 comments on commit b5d9de3

Please sign in to comment.