I have built a https server and got my certs all there and it seems to find them with no issues. My my issue is that any time I run the code I am getting
var proxy = new http_proxy.HttpProxy({
^
TypeError: http_proxy.HttpProxy is not a constructor
at Object.<anonymous> (C:\Users\Adam.Wolarczuk\Desktop\Projects\nodetest\server.js:11:13)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I tried to find the issue on the web but can't seem to get it to work.
Here is my code:
var http_proxy = require ('http-proxy'),
https = require('https'),
fs = require('fs');
var privateKey = fs.readFileSync("privatekey.pem").toString();
var cert = fs.readFileSync("newcert.pem").toString();
var options = {key: privateKey, cert: cert };
var proxy = new http_proxy.HttpProxy({
target:{
host: "localhost",
port: 8080
}
});
var s =https.createServer(options, function (req, res) {
console.log("Proxying!");
proxy.proxyRequest(req, res);
});
s.listen(8443);
The docs don't specify your syntax nor the library exports the module you're trying to initialize.
RTFM https://www.npmjs.com/package/http-proxy
EDIT
Even though the docs are VERY good, here's an example w/ Proxy over HTTPS node server:
var http_proxy = require ('http-proxy'),
https = require('https'),
fs = require('fs');
var privateKey = fs.readFileSync("privatekey.pem").toString();
var cert = fs.readFileSync("newcert.pem").toString();
var options = {key: privateKey, cert: cert };
var proxy = httpProxy.createServer({
ssl: {
key: privateKey,
cert
},
target: 'https://localhost:9010', // Send it anywhere
secure: true // Depends on your needs, could be false.
}).listen(443);
var s = https.createServer(options, function (req, res) {
console.log("Proxying!");
proxy.web(req, res);
});
s.listen(8443);
Related
Generated a self-signed certificate with OpenSSL and copied the certificate & the private key to the required destination folder.
To create an HTTPS server, we require two things: an SSL certificate, and Node's built-in https module.
With Node.js installed, I tried the following JavaScript to run from the command Line
TLSServer.js
var tls = require('tls');
var fs = require('fs');
var port = 8081; //3000;
var host = '127.0.0.1'; //192.168.1.135
var options = {
key: fs.readFileSync('private-key.pem'), // /path/to/private-key.pem
cert: fs.readFileSync('certificate.pem') // /path/to/certificate.pem
};
TLSClient.js
var client = tls.connect(port, host, options, function() {
console.log('connected');
if (client.authorized) {
console.log('authorized: ' + client.authorized);
client.on('data', function(data) {
client.write(data); // Just send data back to server
});
} else {
console.log('connection not authorized: ' + client.authorizationError);
}
});
Actual Output:
cmd>node TLSServer.js
openssl config failed: error:02001005:system library:fopen:Input/output error
cmd>node TLSClient.js
openssl config failed: error:02001005:system library:fopen:Input/output error
events.js:193
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 127.0.0.1:8081
at Object._errnoException (util.js:1031:13)
at _exceptionWithHostPort (util.js:1052:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)
What might be the reason for getting this issue:
openssl config failed: error:02001005:system library:fopen:Input/output error
httpserver.js
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('certificate.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.write("You are connected to https server");
res.end("\n hello world \n");
}).listen(8080)
https://localhost:8080
From browser I used to get the following output:
You are connected to https server
hello world
But not with TLS Client/Server. But what might be there to modify in OpenSSL config file?
Solved openssl config failed: error:02001005:system library:fopen:Input/output error by adding the path of openssl.cnf in Environment Variables -> System Variables
OPENSSL_CONF=C:\OpenSSL-Win64\bin\openssl.cnf
To validate it you can type in the shell:
echo %OPENSSL_CONF%
But still I'm getting error with TLSServer.js
cmd>node TLSServer.js
module.js:544
throw err;
^
Error: Cannot find module 'C:\Users\user\Desktop\TLSServer.js'
at Function.Module._resolveFilename (module.js:542:15)
at Function.Module._load (module.js:472:25)
at Function.Module.runMain (module.js:682:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:613:3
I am having issues getting https working with node http-proxy.
I've created a server using node http-server
forever /usr/local/lib/node_modules/http-server/bin/http-server /home/blah/public_html/ -p 5000 -S -C /myencrypt/blah.com/cert.pem -K /myencrypt/blah.com/privkey.pem
If I go to https://blah.com:5000 the Certs are working correctly.
If I go to blah.com I get the following error
Error: unable to verify the first certificate
at TLSSocket.<anonymous> (_tls_wrap.js:1088:38)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:188:7)
at TLSSocket._finishInit (_tls_wrap.js:610:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38)
What am I missing here?
var fs = require('fs');
var http = require('http');
var https = require('https');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxy();
var options = {
'blah.com':{
target:'https://blah.com:5000',
ssl:{
key:fs.readFileSync('/myencrypt/blah.com/privkey.pem', 'utf8'),
cert:fs.readFileSync('/myencrypt/blah.com/cert.pem', 'utf8')
}
}
}
http.createServer(function(req, res) {
proxy.web(req, res, {
target: options[req.headers.host].target,
ssl : options[req.headers.host].ssl
});
}).listen(80);
I decided to solve my problem using redbird
var redbird = require('redbird')({
port: 80,
secure:false,
ssl: {
port:443,
key: "/myencrypt/blah.com/privkey.pem",
cert: "/myencrypt/blah.com/cert.pem",
}
});
redbird.register('blah.com', 'https://blah.com:5000', {
ssl: {
key: "/myencrypt/blah.com/privkey.pem",
cert: "/myencrypt/blah.com/cert.pem",
}
});
I have a Node.js server, with an active SSL certificate on the domain.
I have read some replies on this website about this, yet even when I relate to such already-solved questions, I get an error.
var express = require('express');
var https = require('https');
var http = require('http');
var path = require('path');
var fs = require('fs');
var mysql = require('mysql');
var queue = {};
var qc = {};
var app = express();
var options = {
key: fs.readFileSync('sslcert/domain-name.key', 'utf8'),
cert: fs.readFileSync('sslcert/domain-name.csr', 'utf8')
};
var httpServer = http.createServer(app);
var httpsServer = https.createServer(options, app);
/* various stuff to display my website */
httpServer.listen(process.env.PORT);
httpsServer.listen(process.env.PORT);
I get the following error in my console.
_tls_common.js:67
c.context.setCert(options.cert);
^
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Error (native)
at Object.createSecureContext (_tls_common.js:67:17)
at Server (_tls_wrap.js:754:25)
at new Server (https.js:17:14)
at Object.exports.createServer (https.js:37:10)
at Object.<anonymous> (/srv/data/web/vhosts/default/server.js:35:25)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
Thank you in advance for your answer!
Noël.
cert should be your domain's PEM formatted certificate, followed by the PEM formatted intermediate certificates; not the CSR.
cert: fs.readFileSync('sslcert/domain-name.pem', 'utf8')
In addition to the answer by Anand Bhat note that you shouldn't bind both of those servers to the same port like you're trying to do:
httpServer.listen(process.env.PORT);
httpsServer.listen(process.env.PORT);
It might work but it might work not how you expect it. If you do:
httpServer.listen(process.env.PORT_HTTP);
httpsServer.listen(process.env.PORT_HTTPS);
where PORT_HTTP is e.g. 80 and PORT_HTTPS is e.g. 443, you will always know which server will process which request.
I am trying to use HTTP/2. My express version is 5.0.0-alpha.2, http2 version is 3.3.4.
I suppose http2 should work well with express 5.
const http2 = require('http2');
// const http2 = require('spdy'); // using spdy package here, everything works perfect
const options = {
key: fs.readFileSync(path.join(__dirname, 'private', 'server.key')),
cert: fs.readFileSync(path.join(__dirname, 'private', 'server.crt'))
};
const server = http2
.createServer(options, app)
.listen(3000, err => {
if (err) throw new Error(err);
// I can see "Listening..." message, which means the server starts running well.
console.log('Listening...');
});
The server starts running well, but when I open client website, it gives me this error in the terminal:
_stream_readable.js:512
dest.end();
^
TypeError: dest.end is not a function
at Stream.onend (_stream_readable.js:512:10)
at Stream.g (events.js:286:16)
at emitNone (events.js:91:20)
at Stream.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
It seems node-http2 has not been supported by Express yet.
Please track this issue Support for module http on github.
In the meanwhile, you can stay with node-spdy.
const spdy = require('spdy');
const options = {
key: fs.readFileSync(path.join(__dirname, 'private', 'server.key')),
cert: fs.readFileSync(path.join(__dirname, 'private', 'server.crt'))
};
const server = spdy
.createServer(options, app)
.listen(3000, err => {
if (err) throw new Error(err);
console.log('Listening...');
});
With Express 5.0 we have another solution :
express = require( 'express' ), //Web framework
// Solution
express.request.__proto__ = http2.IncomingMessage.prototype;
express.response.__proto__ = http2.ServerResponse.prototype;
// Create app for server http/2
var apph2 = express();
And this is the server code :
var
application_root = __dirname,
express = require( 'express' ), //Web framework
http2 = require('http2')
logger = require('morgan')
fs = require('fs')
constants = require('constants');
// Bunyan logger
var bunyan = require('bunyan');
var app = require('./apps/app_name');
var bunlog = bunyan.createLogger({name: "brqx_app"});
var credentials = {
// log : bunlog ,
key : fs.readFileSync('/etc/letsencrypt/live/domain/privkey.pem' ),
cert : fs.readFileSync('/etc/letsencrypt/live/domain/fullchain.pem' ),
ca : fs.readFileSync("/etc/letsencrypt/live/domain/chain.pem" ),
dhparam : fs.readFileSync("/etc/letsencrypt/archive/domain/dh1.pem" ),
secureOptions: constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_SSLv2
};
// Configure server
server = http2.createServer( credentials , app);
server.listen(PORT , function () {
console.log('Started Brqx http/2!');
} )
I hope these easy lines helps to people.
One thing is important when we search information on Internet is the date of test when code was tested : 2017 - October.
Regards.
Ricardo/Brqx.
I'm currently trying to setup an HTTP/HTTPS proxy server using NodeJS. Using the example of this gist, this is what I have.
var fs = require('fs'),
http = require('http'),
https = require('https'),
httpProxy = require('http-proxy');
var isHttps = true; // do you want a https proxy?
var options = {
https: {
key: fs.readFileSync('/home/ubuntu/key.key'),
cert: fs.readFileSync('/home/ubuntu/crt.crt')
}
};
// this is the target server
var proxy = new httpProxy.HttpProxy({
target: {
host: '127.0.0.1',
port: 11612
}
});
if (isHttps)
https.createServer(options.https, function(req, res) {
console.log('Proxying https request at %s', new Date());
proxy.proxyRequest(req, res);
}).listen(443, function(err) {
if (err)
console.log('Error serving https proxy request: %s', req);
console.log('Created https proxy. Forwarding requests from %s to %s:%s', '443', proxy.target.host, proxy.target.port);
});
else
http.createServer(options.https, function(req, res) {
console.log('Proxying http request at %s', new Date());
console.log(req);
proxy.proxyRequest(req, res);
}).listen(80, function(err) {
if (err)
console.log('Error serving http proxy request: %s', req);
console.log('Created http proxy. Forwarding requests from %s to %s:%s', '80', proxy.target.host, proxy.target.port);
});
Issue is, when I run it on my Ubuntu server, this is the error I'm getting. Kinda lost.
/home/ubuntu/prox.js:16
var proxy = new httpProxy.HttpProxy({
^
TypeError: undefined is not a function
at Object.<anonymous> (/home/ubuntu/prox.js:16:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
17 Jan 23:18:34 - [nodemon] app crashed - waiting for file changes before starting...
Have you tried the following, might help, this is from their git hub page.
var proxy = httpProxy.createProxyServer(options);