Commit 51d764f4 authored by Jan-Stefan Janetzky's avatar Jan-Stefan Janetzky

removing logging and adding cookie support

Signed-off-by: Jan-Stefan Janetzky's avatarJan-Stefan Janetzky <git@gottz.de>
parent a8de827e
......@@ -35,8 +35,16 @@ const {ArgumentParser} = argparse;
help: "target host:port (port relative to server. required. otherwise starts server)"
});
parser.addArgument(["-c", "--cookie"], {
help: "specify a cookie file for authenticating to your backend"
});
const args = parser.parseArgs();
if (args.cookie) args.cookie = (await readFileAsync(args.cookie, "utf8")).trim();
console.log(args.cookie);
(await import(args.server === null || args.target === null ? "./server.mjs" : "./client.mjs")).default(args);
})();
import websocket from "websocket";
const {client: WSClient} = websocket;
import http from "http";
import net from "net";
export default async args => {
const [tPort, tHost] = args.target.split(":").reverse();
const server = net.createServer(sock => {
console.log(`${new Date()} Received connection`);
//console.log(`${new Date()} Received connection`);
const client = new WSClient();
let ready = false;
......@@ -25,7 +24,7 @@ export default async args => {
}));
queue.forEach(buffer => con.sendBytes(buffer));
queue = null;
console.log("connected");
//console.log("connected");
con.on("close", () => sock.end());
sock.on("end", () => {con.close()});
......@@ -34,10 +33,12 @@ export default async args => {
con.on("message", data => sock.write(data.binaryData));
});
client.connect(args.server);
client.connect(args.server, undefined, undefined, args.cookie ? {
"Cookie": args.cookie
} : undefined);
sock.on("data", data => {
console.log(`${new Date()} received browser data ${data.length}`);
//console.log(`${new Date()} received browser data ${data.length}`);
ready ? con.sendBytes(data) : queue.push(data);
});
......
......@@ -6,7 +6,7 @@ import net from "net";
export default async args => {
const server = http.createServer((req, res) => {
console.log(`${new Date()} Received request for ${req.url}`);
//console.log(`${new Date()} Received request for ${req.url}`);
res.writeHead(404);
res.end();
});
......@@ -21,9 +21,9 @@ export default async args => {
});
ws.on("connect", con => {
console.log(`${new Date()} new connection`);
//console.log(`${new Date()} new connection`);
con.once("message", msg => {
console.log(`received message ${msg.utf8Data} of type ${msg.type}`);
//console.log(`received message ${msg.utf8Data} of type ${msg.type}`);
if (msg.type !== "utf8") {
con.close();
return;
......@@ -36,7 +36,7 @@ export default async args => {
let queue = [];
const client = net.createConnection(config.port, config.host, () => {
console.log("ready");
//console.log("ready");
ready = true;
queue.forEach(buffer => client.write(buffer));
queue = null;
......@@ -44,16 +44,17 @@ export default async args => {
client.on("data", data => con.sendBytes(data));
client.on("end", () => con.close());
client.on("error", e => {
console.dir(e);
con.close();
//console.dir(e);
});
con.on("message", data => {
console.log(`${new Date()} received data ${data.binaryData.length}`);
//console.log(`${new Date()} received data ${data.binaryData.length}`);
ready ? client.write(data.binaryData) : queue.push(data.binaryData);
});
con.on("close", () => {
console.log("closed a socket");
//console.log("closed a socket");
client.end();
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment