Skip to content

Instantly share code, notes, and snippets.

@RobinStamer
Created September 5, 2014 09:34
Show Gist options
  • Select an option

  • Save RobinStamer/b456b964fe3d87bf03a4 to your computer and use it in GitHub Desktop.

Select an option

Save RobinStamer/b456b964fe3d87bf03a4 to your computer and use it in GitHub Desktop.
var urlp = require("url");
var path = require("path");
var options = require("./options").options;
var ev = require('./ev') // PRIVATE MODULE
,harden = require('harden') // PRIVATE MODULE
,fs = require('fs')
,exp = require('express')
,proxy = require('http-proxy').createProxyServer()
,vhost = require('vhost')
,cookieParser = require('cookie-parser')
,app = exp()
,baldo = exp()
,hbb = require('./hybridbb') // INCLUDED
,PHP = hbb.PHP
function errHandle(e, args) {
console.log(e.stack)
}
function timestamp() {
var d = new Date
,month = d.getMonth()
,day = d.getDate()
,hour = d.getHours()
,minute = d.getMinutes()
if (10 > month) {
month = '0' + month
}
if (10 > day) {
day = '0' + day
}
if (10 > hour) {
hour = '0' + hour
}
if (10 > minute) {
minute = '0' + minute
}
return d.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute
}
app.use(cookieParser(), function (req, res, next) {
harden(req); harden(res)
ev.log(req, res, timestamp())
req.on('harden:exception', errHandle)
res.on('harden:exception', errHandle)
next()
})
app.all('/*.php', PHP)
server = app.listen(options.server.port, function() {
console.log('Listening on port %d', server.address().port);
})
getChildName = function(req) {
var s = (req.headers.host || '')
,i = s.indexOf(':')
return -1 == i ? [s, 80] : [s.substr(0, i), parseInt(s.substr(i + 1))]
}
baldo.use(exp.static(__dirname + '/baldo.REMOVED'))
app.use(vhost('baldo.REMOVED', baldo))
hbb(app, options)
app.all('/*', function(req, resp, next) {
var child = getChildName(req)
,host = child[0]
,stat
,dir = path.dirname(req.url)
,ext = path.extname(urlp.parse(req.url).pathname)
if ({gif: 1, png: 1, jpg: 1}[ext.toLowerCase().substr(1)]) {
} else {
console.log('[' + timestamp() + ']' + ext + '://' + child.join(':') + req.url + ' (' + req.connection.remoteAddress + '/' + req.headers.referer + ')') // CRAPPIEST ACCESS LOG EVER
}
next()
})
app.use(exp.static(__dirname))
app.all('/evlog', ev)
app.all('/pad/*', function(req, res) { req.url = '/pad' == req.url ? '/' : req.url.substr(4); proxy.web(req, res, { target: 'http://172.24.101.9:9001/' }) })
var agent
,php = require("./node-php");
function PHP(req, res) {
agent.request(req, res, function(err, response) {
if (err) {
console.log('!!!')
console.log(err)
console.log(err.stack)
res.end('Error!')
}
})
}
function hybridMain(app, options) {
agent = new php.Agent(4, options)
agent.on("error", function(err) {
console.log("client.error")
console.log(err)
})
app.get('/bb/', function(req, resp, next) {
req.url = '/bb/index.php'
if (req.query.$) {
req.url += '?$=' + req.query.$
}
PHP(req, resp)
})
app.get('/thread/:thread', function(req, resp, next) {
req.url = '/bb/viewtopic.php?t=' + req.params.thread
PHP(req, resp)
})
app.get('/forum/:forum', function(req, resp, next) {
req.url = '/bb/viewforum.php?f=' + req.params.forum
PHP(req, resp)
})
app.get('/mail/:folder', function(req, resp, next) {
req.url = '/bb/privmsg.php?folder=' + req.params.folder
PHP(req, resp)
})
}
hybridMain.PHP = PHP
module.exports = hybridMain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment