{"size": 3471, "ext": "js", "max_stars_repo_path": "teapot.js", "max_stars_repo_name": "linuxgemini/secure-teapot", "max_stars_repo_stars_event_min_datetime": "2021-09-10T00:05:40.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-10T00:05:40.000Z", "max_issues_repo_path": "teapot.js", "max_issues_repo_name": "linuxgemini/secure-teapot", "max_issues_repo_issues_event_min_datetime": "2017-10-14T07:59:12.000Z", "max_issues_repo_issues_event_max_datetime": "2017-10-14T09:35:10.000Z", "max_forks_repo_path": "teapot.js", "max_forks_repo_name": "linuxgemini/secure-teapot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 38.1428571429, "max_line_length": 220, "alphanum_fraction": 0.6528377989}
'use strict';

const fs = require('fs');

try {
    require('./config.json'); // try gathering config
} catch (e) { // if error
    console.error("config.json is not set. Use the config-base file to make one");
    return setTimeout(() => {
        process.exit(1);
    }, 838);
}

const config = require('./config.json');

var hostName = config.hostname;
const HTTPSopts = {
    cert: fs.readFileSync(config.pathToCertificate),
    key: fs.readFileSync(config.pathToPrivateKey)
};

var securePort = 443;
var generalPort = 80;

const url = require('url');
const http = require('http');
const https = require('https');
const connect = require('connect'); // import connect package
var mainApp = connect(); // initiate connect
var secondApp = connect();

function filterIPAddresses(req) {
    if (req.connection.remoteAddress && /[\w\d]*?:[\w\d]*?:ffff:[\d]*\.[\d]*\.[\d]*\.[\d]*/.test(req.connection.remoteAddress)) { // if the remoteAddress is ipv4 under ipv6 format (this does NOT check for 6to4 tunneling)
        var filteredFirstBatch = req.connection.remoteAddress.replace(/[\w\d]*?:[\w\d]*?:ffff:/g, ''); // set remoteAddress IP as IPv4, remove ::ffff:
    } else { // else
        var filteredFirstBatch = req.connection.remoteAddress; // set remoteAddress as it is before
    }

    if (req.headers['x-forwarded-for']) { // if the request has the header X-Forwarded-For
        var finalisedIPs = `${req.headers['x-forwarded-for']} or ${filteredFirstBatch}`; // make a variable with those remoteAddress and X-Forwarded-For address
    } else if (req.connection.remoteAddress) { // else
        var finalisedIPs = filteredFirstBatch; // make a variable with only remoteAddress
    } else {
        var finalisedIPs = `Unable to get IP.`;
    }
    return finalisedIPs;
}

function reportConn(type, request, customMessage) {
    if (!type || !request || !request.connection) throw new TypeError('Excuse me wtf');
    if (customMessage) return console.log(`${customMessage} from:\n${filterIPAddresses(request)}\n`);
    console.log(`Got an ${type} request from:\n${filterIPAddresses(request)}\n`);
}

mainApp.use((request, response, next) => {
    var responseData = "<html><head><title>root</title></head><body><h1>Teapot.</h1></body></html>";
    response.writeHead(418, 'still brewing', {
        'Content-Length': Buffer.byteLength(responseData),
        'Content-Type': 'text/html',
        'X-is-secure': 'true',
        'X-is-making-tea': 'true',
        'Server': 'Apache Helicopter',
    });
    response.end(responseData, 'utf-8', () => {
        reportConn('https', request);
    });
});

secondApp.use((request, response, next) => {
    var whatHasBeenGathered = url.parse(request.url, true).path;
    response.writeHead(301, 'We have moved', {
        'X-is-a-redirector': 'true',
        'Server': 'Apache Helicopter',
        'Location': `https://${hostName}${whatHasBeenGathered}`
    });
    response.end(() => {
        reportConn('http', request, 'Redirected http request');
    });
});

var mainServer = https.createServer(HTTPSopts, mainApp);
var secondaryServer = http.createServer(secondApp);

mainServer.listen(securePort, hostName, () => { // listen on target port
    console.log(`HTTPS Server running on port ${securePort}...\n`); // report back to console
});

secondaryServer.listen(generalPort, hostName, () => { // listen on target port
    console.log(`HTTP Server running on port ${generalPort}...\n`); // report back to console
});
