Skip to content

Instantly share code, notes, and snippets.

@d3x0r
Last active April 11, 2018 22:53
Show Gist options
  • Save d3x0r/168b9fc4a74d3fcf425b15b009fdc844 to your computer and use it in GitHub Desktop.
Save d3x0r/168b9fc4a74d3fcf425b15b009fdc844 to your computer and use it in GitHub Desktop.
Resource; index.html, and server testWssHttp.js

with node > 7

npm install sack.vfs
node testWssHttps.js

opens a server on port 8080, and 8081

should replace /*should be valid IP*/ in testWssHttps.js with a valid IP that it will be serving... (not localhost or 127.0.0.1)

Connect to https://<server IP>:8080/ This loads the index.html shown above, which connects back to two wss connections.

These connections show up in 'unknown/canceled' but cannot get the certificates from them The connection to the second results in net::ERR_CERT_AUTHORITY_INVALID instead of like ERR_CERT_NAME_BAD, because the cert on the second connection has '127.0.0.1' as the cert name. Which is why the other IP needs to be set to an IP to serve on and connect to....

the TLS interface exposed through sack.vfs is just a thing wrapper on OpenSSL linked to node.

the network and disk is custom code; it tries to stick to its own directory by referencing '.' prepended to the URL; but this is just a test, and not meant to be fully featured.

<HTML>
<HEAD>
<BODY>
Doing Initial wss connect...
</BODY>
<SCRIPT>
var ws = new WebSocket( "wss://" + location.host + "/"
, "test" );
ws.onopen = function (){
var out = document.createElement("p");
out.innerText = "connected(1)";
document.body.appendChild( out );
var ws = new WebSocket( "wss://" + location.hostname + ":8081/"
, "test2" );
ws.onopen = function (){
var out = document.createElement("p");
out.innerText = "connected(2)";
document.body.appendChild( out );
}
}
</SCRIPT>
</HTML>
var sack = require( "sack.vfs" );
var disk = sack.Volume();
var keys = [ sack.TLS.genkey( 1024 ), sack.TLS.genkey( 1024 ), sack.TLS.genkey( 1024, "password" ) ];
var certRoot = sack.TLS.gencert( { key:keys[0]
, country:"US"
, state:"NV"
, locality:"Las Vegas"
, org:"Freedom Collective", unit:"Tests", name:"Root Cert", serial: 1001 } )
console.log( sack.TLS );
var signer = ( sack.TLS.signreq( {
request: sack.TLS.genreq( { key:keys[1]
, country:"US", state:"NV", locality:"Las Vegas"
, org:"Freedom Collective", unit:"Tests"
, name:"CA Cert", serial: 1002 } )
, signer: certRoot, serial: 1003, key:keys[0] } ) );
var cert = sack.TLS.signreq( {
request: sack.TLS.genreq( { key:keys[2], password:"password"
, country:"US", state:"NV", locality:"Las Vegas"
, org:"Freedom Collective", unit:"Tests", name:"Cert", serial: 1004
, subject: { DNS:["test.nowhere"], IP:["/*should be valid IP*/"] }
} )
, signer: signer, serial: 1005, key:keys[1] } );
var cert2 = sack.TLS.signreq( {
request: sack.TLS.genreq( { key:keys[2], password:"password"
, country:"US", state:"NV", locality:"Las Vegas"
, org:"Freedom Collective", unit:"Tests", name:"Cert", serial: 1004
, subject: { IP:["127.0.0.1"] }
} )
, signer: signer, serial: 1005, key:keys[1] } );
var server = sack.WebSocket.Server( { port: 8080, cert : cert, ca : signer+certRoot, key: keys[2], passphrase:"password" } )
var server2 = sack.WebSocket.Server( { port: 8081, cert : cert, ca : signer, key: keys[2], passphrase:"password" } )
console.log( "serving on 8080,8081" );
server.onrequest( function( req, res ) {
console.log( "Received request:", req.url );
if( req.url.endsWith( ".html" ) || req.url == "/" ) {
res.writeHead( 200 );
if( req.url == "/" )
req.url = "/index.html";
var x = disk.read( "." + req.url );
console.log( "Read:", x, "." + req.url );
res.end( x );
} else {
res.writeHead( 404 );
res.end();
}
} );
server.onaccept( function ( protocols, resource ) {
console.log( "Connection received with : ", protocols, " path:", resource );
if( process.argv[2] == "1" )
this.reject();
else
this.accept();
//this.accept( protocols );
} );
server.onconnect( function (ws) {
//console.log( "Connect:", ws );
ws.onmessage( function( msg ) {
console.log( "Received data:", msg );
ws.send( msg );
//ws.close();
} );
ws.onclose( function() {
console.log( "Remote closed" );
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment