"use strict"; // Import modules const fs = require('fs'); const express = require('express'); const serveIndex = require('serve-index'); const net = require('net'); const etag = require('etag'); // Initialize ExpressJs const app = express(); const port = 8080; const public_dir = '/opt/AptanaJaxer/public'; app.get('*.html', function(req, res) { // Check stats for file fs.stat(public_dir + req.url, function(err, stats) { if(err) { return res.status(404).end("File Not Found"); } // Read file body fs.readFile(public_dir + req.url, function(err, body) { if(err) { return res.status(500).end("Could not read file"); } console.log(req.headers); const INPUT = [ { "User-Agent" : req.headers['user-agent'], "Host" : req.headers['host'], "Accept" : "*/*" }, { "Last-Modified" : stats.mtime.toGMTString(), "ETag" : etag(body), "Accept-Ranges" : "bytes", "Content-Length" : stats.size.toString() }, { "CALLBACK_URI" : "/aptanaRPC", "HTTP_USER_AGENT" : req.headers['user-agent'], "HTTP_HOST" : req.headers['host'], "HTTP_ACCEPT" : "*/*", "PATH" : "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LD_LIBRARY_PATH" : "/opt/AptanaJaxer/Apache22/lib:/opt/AptanaJaxer/Apache22/lib", "SERVER_SIGNATURE" : "", "SERVER_SOFTWARE" : "(Apache/2.4.35 (Unix) ModJaxer/1.0.3.4549", "SERVER_NAME" : "192.168.1.7", "SERVER_ADDR" : "192.168.1.7", "SERVER_PORT" : "8080", "REMOTE_ADDR" : req.connection.remoteAddress, "DOCUMENT_ROOT" : public_dir, "REQUEST_SCHEME" : "http", "CONTEXT_PREFIX" : "", "CONTEXT_DOCUMENT_ROOT" : public_dir, "SERVER_ADMIN" : "you@example.com", "SCRIPT_FILENAME" : public_dir + req.url, "REMOTE_PORT" : "56956", "SERVER_PROTOCOL" : "HTTP/1.1", "REQUEST_METHOD" : "GET", "QUERY_STRING" : "", "REQUEST_URI" : req.url, "SCRIPT_NAME" : req.url, "REMOTE_HOST" : req.connection.remoteAddress, "STATUS_CODE" : "200", "HTTPS" : "off", "JAXER_REQ_TYPE" : "2", "DOC_CONTENT_TYPE" : "text/html" } ]; console.log("writing the emulated packet"); const packet = binary_json(INPUT, body); fs.writeFileSync("packets/emu.bin", packet); pass_to_jaxer(packet, function(err, jx) { if(err) { return res.status(500).end("Could jaxer proxy err"); } console.log("proxied to jaxer"); fs.writeFileSync("packets/jax.bin", jx); console.log(stats); console.log(stats.mtime.toGMTString()); res.end(jx.toString('ascii')); }); }); }); }); app.all('*/jaxer-include/*', function(req, res) { res.status(403); res.end('Server-only files'); }); app.post('/jaxer-callback', function(req, res) { // parse body and pass to jaxer }); app.use(express.static(public_dir)); app.listen(port, function() { console.log(`Apache Emulator listening on port ${port}!`) }); // Apache Emulator function binary_json(src, body) { let bytes = []; for(let i = 0; i < src.length; i++) { // First set the block number bytes.push(i + 1); // Then we get the total number of bytes let length = 0; let keys = 0; let block = src[i]; for(let key in block) { keys++; length += key.length; length += block[key].length; length += 4; } // Remove the last ending zero length -= 1; length += 3; bytes.push((length & 0xff00) >> 8); bytes.push(length & 0xff); bytes.push(0, keys, 0); // Then we encode all of the keys and values for(let key in block) { bytes.push(key.length); for(let k = 0; k < key.length; k++) { bytes.push(key.charCodeAt(k)); } bytes.push(0); bytes.push(block[key].length); for(let k = 0; k < block[key].length; k++) { bytes.push(block[key].charCodeAt(k)); } bytes.push(0); } bytes.pop(); } bytes.push(src.length + 1); bytes.push( (body.length & 0xff00) >> 8 ); bytes.push( body.length & 0xff ); let header = Buffer.from(bytes); let footer = Buffer.from([ 0x07, 0x00, 0x00]); return Buffer.concat([header, body, footer]); } function pass_to_jaxer(packet, callback) { console.log("a"); const JAXER_PORT = 4327; const ping = Buffer.from([0, 0, 3, 0, 4, 2]); const pong = Buffer.from([0, 0, 3, 0, 4, 1]); const client = new net.Socket(); client.connect(JAXER_PORT, '127.0.0.1', function() { console.log("ping"); client.write(ping); }); client.on('data', function(data) { console.log(data); if(pong.compare(data) === 0) { console.log("pong"); client.write(packet); } else { console.log("b"); client.destroy(); return callback(null, data); } }); }