Skip to content

Instantly share code, notes, and snippets.

@jasonk
Last active October 7, 2015 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonk/bb781d725f2d7f0538f0 to your computer and use it in GitHub Desktop.
Save jasonk/bb781d725f2d7f0538f0 to your computer and use it in GitHub Desktop.
Monkey patch Meteor to use CA certs from the OS X keychain.
#!/bin/bash
set -e
# This script monkey-patches MeteorJS to allow it to work from behind a MITM
# proxy that forges SSL keys. You may need this to get through a corporate
# content-inspection proxy, for example, since Meteor doesn't allow you to
# specify the CA certs to use.
cd "$HOME/.meteor"
cd "$(dirname "$(readlink meteor)")"
cd tools
case "$1" in
-u) mv index.js.orig index.js; echo "Meteor unpatched"; exit ;;
-d) pwd ; exit ;;
esac
security find-certificate -ap \
/Library/Keychains/System.keychain \
/System/Library/Keychains/SystemRootCertificates.keychain \
> certs.pem
if grep -q certs index.js; then
echo "Already patched"
exit 1
fi
cp index.js index.js.orig
(
cat <<'END';
var cas = [];
var cur = '';
var certs = require('fs').readFileSync( __dirname + '/certs.pem' ).toString();
certs.split("\n").forEach( function( line ) {
cur += line + "\n";
if ( line.indexOf( '-----END' ) === 0 ) { cas.push( cur ); cur = ''; }
} );
var isObject = function isObject( obj ) {
return Object.prototype.toString.call( obj ) == "[object Object]";
};
var tls = require( 'tls' );
var _connect = tls.connect;
tls.connect = function() {
if ( isObject( arguments[0] ) ) {
arguments[0].ca = cas;
} else if ( isObject( arguments[2] ) ) {
arguments[2].ca = cas;
} else {
console.log( "arguments: %o", arguments );
throw new Error( "Invalid arguments to monkey-patched tls.connect" );
}
return _connect.apply( this, arguments );
};
END
cat index.js.orig
) > index.js
echo "Meteor patched"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment