Skip to content

Commit fa25378

Browse files
jonlucajoeferner
authored andcommitted
convert examples to typescript, extract out classes
convert examples to typescript, extract out classes tsconfig
1 parent b901de9 commit fa25378

26 files changed

+849
-717
lines changed

examples/forwardHttps.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

examples/forwardHttps.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const port = 8081;
2+
import net from "net";
3+
import assert from "assert";
4+
import Proxy from "../";
5+
const proxy = new Proxy();
6+
import { exec } from "child_process";
7+
8+
proxy.onConnect((req, socket, head) => {
9+
const host = req.url.split(":")[0];
10+
const port = req.url.split(":")[1];
11+
12+
console.log("Tunnel to", req.url);
13+
const conn = net.connect(
14+
{
15+
port,
16+
host,
17+
allowHalfOpen: true,
18+
},
19+
() => {
20+
conn.on("finish", () => {
21+
socket.destroy();
22+
});
23+
socket.on("close", () => {
24+
conn.end();
25+
});
26+
socket.write("HTTP/1.1 200 OK\r\n\r\n", "UTF-8", () => {
27+
conn.pipe(socket);
28+
socket.pipe(conn);
29+
});
30+
}
31+
);
32+
33+
conn.on("error", (err) => {
34+
filterSocketConnReset(err, "PROXY_TO_SERVER_SOCKET");
35+
});
36+
socket.on("error", (err) => {
37+
filterSocketConnReset(err, "CLIENT_TO_PROXY_SOCKET");
38+
});
39+
});
40+
41+
// Since node 0.9.9, ECONNRESET on sockets are no longer hidden
42+
function filterSocketConnReset(err, socketDescription) {
43+
if (err.errno === "ECONNRESET") {
44+
console.log(`Got ECONNRESET on ${socketDescription}, ignoring.`);
45+
} else {
46+
console.log(`Got unexpected error on ${socketDescription}`, err);
47+
}
48+
}
49+
50+
proxy.listen({ port }, () => {
51+
console.log(`Proxy server listening on ${port}`);
52+
53+
const cmd = `curl -x http://localhost:${port} https://github.com/ | grep html`;
54+
console.log(`> ${cmd}`);
55+
exec(cmd, (error, stdout, stderr) => {
56+
if (error) {
57+
console.error(`exec error: ${error}`);
58+
return;
59+
}
60+
console.log(`${stdout}`);
61+
assert(/DOCTYPE/.test(stdout));
62+
proxy.close();
63+
});
64+
});

examples/modifyGoogle.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

examples/modifyGoogle.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const port = 8081;
2+
3+
import Proxy from "../";
4+
const proxy = new Proxy();
5+
6+
proxy.onError((ctx, err, errorKind) => {
7+
// ctx may be null
8+
const url = ctx?.clientToProxyRequest?.url || "";
9+
console.error(`${errorKind} on ${url}:`, err);
10+
});
11+
12+
proxy.onRequest((ctx, callback) => {
13+
//console.log('REQUEST: http://' + ctx.clientToProxyRequest.headers.host + ctx.clientToProxyRequest.url);
14+
if (
15+
ctx.clientToProxyRequest.headers.host == "www.google.com" &&
16+
ctx.clientToProxyRequest.url.indexOf("/search") == 0
17+
) {
18+
ctx.use(Proxy.gunzip);
19+
20+
ctx.onResponseData((ctx, chunk, callback) => {
21+
chunk = new Buffer(
22+
chunk.toString().replace(/<h3.*?<\/h3>/g, "<h3>Pwned!</h3>")
23+
);
24+
return callback(null, chunk);
25+
});
26+
}
27+
return callback();
28+
});
29+
30+
proxy.onRequestData(
31+
(
32+
ctx,
33+
chunk,
34+
callback //console.log('request data length: ' + chunk.length);
35+
) => callback(null, chunk)
36+
);
37+
38+
proxy.onResponse(
39+
(
40+
ctx,
41+
callback //console.log('RESPONSE: http://' + ctx.clientToProxyRequest.headers.host + ctx.clientToProxyRequest.url);
42+
) => callback(null)
43+
);
44+
45+
proxy.onResponseData(
46+
(
47+
ctx,
48+
chunk,
49+
callback //console.log('response data length: ' + chunk.length);
50+
) => callback(null, chunk)
51+
);
52+
53+
proxy.listen({ port });
54+
console.log(`listening on ${port}`);

examples/onCertificateMissing.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/onCertificateMissing.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const port = 8081;
2+
import Proxy from "../";
3+
const proxy = new Proxy();
4+
5+
proxy.onError((ctx, err, errorKind) => {
6+
// ctx may be null
7+
const url = ctx?.clientToProxyRequest?.url || "";
8+
console.error(`${errorKind} on ${url}:`, err);
9+
});
10+
11+
proxy.onCertificateMissing = (ctx, files, callback) => {
12+
console.log('Looking for "%s" certificates', ctx.hostname);
13+
console.log('"%s" missing', ctx.files.keyFile);
14+
console.log('"%s" missing', ctx.files.certFile);
15+
16+
// Here you have the last chance to provide certificate files data
17+
// A tipical use case would be creating them on the fly
18+
//
19+
// return callback(null, {
20+
// key: keyFileData,
21+
// cert: certFileData
22+
// });
23+
};
24+
25+
proxy.listen({ port });
26+
console.log(`listening on ${port}`);

examples/onCertificateRequired.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/onCertificateRequired.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const port = 8081;
2+
import path from "path";
3+
import Proxy from "../";
4+
const proxy = new Proxy();
5+
6+
proxy.onError((ctx, err, errorKind) => {
7+
// ctx may be null
8+
const url = ctx?.clientToProxyRequest?.url || "";
9+
10+
console.error(`${errorKind} on ${url}:`, err);
11+
});
12+
13+
proxy.onCertificateRequired = (hostname, callback) =>
14+
callback(null, {
15+
keyFile: path.resolve("/ca/certs/", `${hostname}.key`),
16+
certFile: path.resolve("/ca/certs/", `${hostname}.crt`),
17+
});
18+
19+
proxy.listen({ port });
20+
console.log(`listening on ${port}`);

examples/preventRequest.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/preventRequest.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const port = 8081;
2+
3+
import Proxy from "../";
4+
const proxy = new Proxy();
5+
6+
proxy.onError((ctx, err, errorKind) => {
7+
// ctx may be null
8+
const url = ctx?.clientToProxyRequest?.url || "";
9+
10+
console.error(`${errorKind} on ${url}:`, err);
11+
});
12+
13+
proxy.onRequest((ctx, callback) => {
14+
ctx.proxyToClientResponse.end("Hacked, you cannot proceed to the website");
15+
// no callback() so proxy request is not sent to the server
16+
});
17+
18+
proxy.listen({ port });
19+
console.log(`listening on ${port}`);

0 commit comments

Comments
 (0)