Skip to content

Instantly share code, notes, and snippets.

@soutar
Last active November 14, 2022 20:14
Show Gist options
  • Save soutar/22a1e1df23147d5ba1f4 to your computer and use it in GitHub Desktop.
Save soutar/22a1e1df23147d5ba1f4 to your computer and use it in GitHub Desktop.
Redirect HTTP traffic on Internet Sharing to a local Charles proxy
#!/usr/bin/env node
var options = require('minimist')(process.argv.slice(2), { default: {
cport: 8888
}});
var disable = options.disable || options.d;
var enable = options.enable || options.e;
var status = options.s || options.status;
var child = require('child_process');
// -s or --status to list active rules
if (status) {
child.exec('sudo pfctl -s nat -i bridge100', function (error, stdout, stderror) {
console.log(stdout || stderror);
});
return;
}
if (enable) {
var pfrules = [
'rdr pass on bridge100 inet proto tcp from any to any port 80 -> 127.0.0.1 port {charles_port}',
];
pfrules.forEach(function (pfrule) {
pfrule = pfrule.replace(/{charles_port}/g, options.cport);
child.exec('echo "' + pfrule + '" | sudo pfctl -ef -');
});
console.log('HTTP & HTTPS traffic on Internet Sharing network now redirecting to 127.0.0.1:' + options.cport);
return;
} else if (disable) {
console.log('Cleared all rules on the Internet Sharing network');
child.exec('sudo pfctl -F nat -i bridge100');
return;
}
console.log('Usage: -e to enable, -d to disable or -s for status');
@RajanPunja
Copy link

Hi There im trying to run this script in combination with the two other reference pages and haven't been successful. Any help would be appreciated

https://blog.adobe.com/en/publish/2019/07/01/10-easy-steps-to-connecting-over-the-top-ott-devices-to-a-proxy.html#gs.5cn9an
https://blog.teamtreehouse.com/install-node-js-npm-mac

Error
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
at Function.Module._load (node:internal/modules/cjs/loader:772:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Object. (/Users/rajan.punja/Desktop/proxy.js:2:15)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:816:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/rajan.punja/Desktop/proxy.js' ]
}

@acotilla91
Copy link

This is only redirecting HTTP traffic, not HTTPS right? The port 443 is not being acknowledged here.

I tried just simply adding another rule, but that didn't work.
'rdr pass on bridge100 inet proto tcp from any to any port 443 -> 127.0.0.1 port {charles_port}'

Any idea how to capture HTTPS traffic?

@edgarsanleo
Copy link

This is only redirecting HTTP traffic, not HTTPS right? The port 443 is not being acknowledged here.

I tried just simply adding another rule, but that didn't work. 'rdr pass on bridge100 inet proto tcp from any to any port 443 -> 127.0.0.1 port {charles_port}'

Any idea how to capture HTTPS traffic?

The script is prepared to catch a single pf rule.
So in order to add both http and https, they need to be together in the same string.

var pfrules = [ 'rdr pass on bridge100 inet proto tcp from any to any port 80 -> 127.0.0.1 port {charles_port} \n rdr pass on bridge100 inet proto tcp from any to any port 443 -> 127.0.0.1 port {charles_port}' ];

@BC-DavidAguilar
Copy link

I can't seem to get logs from my Roku device when I try to capture HTTPS traffic.

I constantly receive 503 errors with "Invalid first line in request".

What could I be missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment