0

I am trying to connect to wss(proxy) with self-signed certificate using wscat and browser but it giving me errors.

  • https running on 8443 with certificate cert.pem
  • proxy running on 8080 with secure true

Things I have tried to make sure my secure server is running properly.

  • I can reach https://localhost:8443 and receive "hello from a secure world"
  • I can connect to wss://localhost:8443 with wscat wscat -c wss://localhost:8443 --ca cert.pem and it works

Errors I get:

  • I cannot reach the proxy https://localhost:8080 from browser. I get This site can’t provide a secure connection and 500 status code
  • I cannot connect to wss://localhost:8080 with wscat -c wss://localhost:8080 --ca cert.pem I get error: write EPROTO 140266887743360:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:

What I think the issue is that my proxy server is unable to take the cert.pem and pass it to the https server. I have looked everywhere but I can't find how to connect to wss(proxy) with a self-signed certificate. I can't supress the

/server

const app = express()
app.use('/', function (req, res) {
  res.writeHead(200);
  res.end("hello from a secure world\n");
})

export const server = https.createServer({
  cert: fs.readFileSync(path.resolve(__dirname, 'cert.pem'), 'utf-8'),
  ca: fs.readFileSync(path.resolve(__dirname, 'cert.pem'), 'utf-8'),
  key: fs.readFileSync(path.resolve(__dirname, 'server.key'), 'utf-8')
}, app)

const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws) {
  console.log("connected");
  ws.on('message', function incoming(message) {
      console.log('received: %s', message);
      ws.send('hello from server!, the time is: ' + timestamp());
    });
});

/Proxy

const wsProxy = createProxyMiddleware('/', {
    target: `https://localhost:8443`,
    changeOrigin: true,
    secure: true,
    ws: true,
    ssl: {
        cert: fs.readFileSync(path.resolve(__dirname, 'cert.pem')),
    }
});

const app = express();
app.use(wsProxy);

const proxy = app.listen(8080)    
proxy.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
azlan_909
  • 1
  • 2
  • 3

1 Answers1

0

Okay, it turned out that I was missing something crucial there. There wasn't really a "proxy websocket" I was confusing https proxy with websocket proxy. Once I made sense of that it solved my problem. I had to create a websocket with using https server(with cert and key) then I could just connect to the wss with the same cert and key :)

azlan_909
  • 1
  • 2
  • 3