Skip to content

Instantly share code, notes, and snippets.

@namuyan
Forked from novi/logger.js
Last active February 24, 2020 13:09
Show Gist options
  • Save namuyan/a94d2363cc363a5d8393c8716d8f5143 to your computer and use it in GitHub Desktop.
Save namuyan/a94d2363cc363a5d8393c8716d8f5143 to your computer and use it in GitHub Desktop.
node-http-proxyを使ったWebsocket対応Proxy

bc4py-proxy

bc4pyのAPIをプロキシに通し、またSSLを導入する。参考

Install

  1. sudo apt-get install nodejs
  2. sudo ln -s `which nodejs` /usr/bin/node
  3. check nodejs -v
  4. sudo apt-get install npm
  5. mkdir proxy && cd proxy
  6. npm init
  7. 下記に入れ替え
{
   "name": "proxy",
   "version": "1.0.0",
   "description": "Proxy server for ethereum node",
   "main": "index.js",
   "scripts": {
      "start": "node index.js &",
      "stop": "./stop.sh"
   },
   "author": "Robert Lie",
   "license": "ISC",
   "dependencies": {
      "fs": "0.0.1-security",
      "http-proxy": "^1.16.2"
   }
} 
  1. index.js, logger.js を書き込む
  2. npm startで開始、npm stopで停止

Note

  • bc4pyはRESTとWSが同一ポート使用なので同じtargetです。
  • sslを使用しないなら"https"を"http"にしてsslの項目を消す。
  • 1~1024portはroot権限必要なので注意。
let https = require('https'),
httpProxy = require('http-proxy'),
fs = require('fs');
var ssl = {
key: fs.readFileSync('/etc/letsencrypt/live/pycontract.tk-0002/privkey.pem', 'utf8'),
cert: fs.readFileSync('/etc/letsencrypt/live/pycontract.tk-0002/fullchain.pem', 'utf8')
};
let HomepageProxy = new httpProxy.createProxyServer({
target: {host: 'localhost', port: 8000},
xfwd: true
});
let TestApiProxy = new httpProxy.createProxyServer({
target: {host: 'localhost', port: 3000},
xfwd: true
});
let wsProxy = new httpProxy.createProxyServer({
target: {host: 'localhost', port: 3000},
xfwd: true
});
let server = https.createServer(ssl, function ( req, res ) {
//console.log(req.headers.host);
//console.log(req.headers);
try{
let domain = req.headers.host;
let host = domain.split(":")[0];
if (host==='pycontract.tk') {
res.writeHead(302, {'Location': 'https://www.pycontract.tk'});
res.end();
} else if (host==='www.pycontract.tk'){
HomepageProxy.proxyRequest(req, res);
} else if (host==='testnet.pycontract.tk'){
TestApiProxy.proxyRequest(req, res);
} else if (host==='pool.pycontract.tk'){
PoolProxy.proxyRequest(req, res);
} else {
res.writeHead(404);
res.end();
}
} catch (e) {
console.log('error', e);
res.writeHead(404);
res.end();
}
});
server.on( 'upgrade', function( req, socket, head ) {
try{
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
//console.log(req.headers);
wsProxy.ws(req, socket, head);
} catch (e) {
res.writeHead(404);
res.end();
}
});
// Listen for the `error` event on `proxy`
server.on('error', function (err, req, res) {
if (res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
}
});
server.listen(443);
// c.f. http://blog.nodejitsu.com/http-proxy-middlewares
var fs = require('fs');
var logFile = fs.createWriteStream('./requests.log', {flags: 'a', encoding: 'utf8'});
module.exports = function (logging, next) {
return function (request, response) {
if (logging) {
logFile.write(JSON.stringify(request.headers, true));
}
next(request, response);
}
}
var httpProxy = require('http-proxy'),
fs = require('fs');
// 非常にシンプルなProxy
var ssl = {
key: fs.readFileSync('./ssl/priv.pem', 'utf8'),
cert: fs.readFileSync('./ssl/cert.pem', 'utf8')
}
var server = httpProxy.createProxyServer({
target: {host: 'localhost', port: 3000},
ssl: ssl,
ws: true,
xfwd: true
})
server.on('error', function(err, req, res) {
res.end();
});
server.listen(443);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment