//OTHER IMPORTS
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const { RammerheadLogging, RammerheadProxy, RammerheadSessionFileCache } = require('./rammer/dist/index.js');
const RammerheadSession = require('./rammer/dist/classes/RammerheadSession.js');
const generateId = require('./rammer/dist/util/generateId.js');
const URLPath = require('./rammer/dist/util/URLPath.js');
const httpResponse = require('./rammer/dist/util/httpResponse.js');
const StrShuffler = require('./rammer/dist/util/StrShuffler.js');
const config = require('./rammer/dist/config.js');
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const rammerLogger = new RammerheadLogging({
logLevel: config.logLevel,
generatePrefix: (level) => config.generatePrefix(level),
});
const ipGet = config.getIPProxy;
const rammerProxy = new RammerheadProxy({
logger: rammerLogger,
loggerGetIP: ipGet,
bindingAddress: config.bindingAddress,
port: config.port,
crossDomainPort: config.crossDomainPort,
dontListen: false,
ssl: config.ssl,
getServerInfo: config.getServerInfo,
disableLocalStorageSync: config.disableLocalStorageSync,
jsCache: config.jsCache,
disableHttp2: config.disableHttp2
});
const rammerStore = new RammerheadSessionFileCache({
logger: rammerLogger,
...config.fileCacheSessionConfig
});
rammerStore.attachToProxy(rammerProxy);
// More code
const app = Fastify({
serverFactory: (handler) => {
const server = createServer();
server.on("request", (req, res) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else {
app.ready(() => handler(req, res));
}
});
server.on("upgrade", (req, socket, head) => {
//otherstuff
}
return server;
},
});
app.register(fastifyCookie);
app.register(fastifySession, {
secret: process.env.SESSION_SECRET || 'setasessionsecretinenvvarsplease',
cookie: { secure: true, httpOnly: true, maxAge: 86400000 },
});
app.register(fastifyMultipart);
//Other app config and routes
app.get('/rammer/:sessionId/*', async (request, reply) => {
const ip = config.getIP(request.raw);
const { sessionId } = request.params;
const remainingPath = request.params['*'];
let targetUrlPart = remainingPath;
let targetUrl;
try {
if (!targetUrlPart.startsWith('http://') && !targetUrlPart.startsWith('https://')) {
if (targetUrlPart.startsWith('//')) {
targetUrl = (request.protocol === 'https' ? 'https:' : 'http:') + targetUrlPart;
} else {
targetUrl = (request.protocol === 'https' ? 'https://' : 'http://') + targetUrlPart;
}
} else {
targetUrl = targetUrlPart;
}
new URL(targetUrl);
console.log(targetUrl);
} catch (e) {
httpResponse.badRequest(rammerLogger, request.raw, reply.raw, ip, `Invalid target URL format: ${e.message}`);
return reply.send();
}
const session = rammerStore.get(sessionId);
if (!session) {
httpResponse.badRequest(rammerLogger, request.raw, reply.raw, ip, 'Session not found for proxied URL');
return reply.send();
}
if (config.restrictSessionToIP) {
if (session.data.restrictIP && session.data.restrictIP !== ip) {
reply.code(403);
reply.send('Sessions must come from the same IP');
return;
}
}
for (const eachHeader of config.stripClientHeaders) {
delete request.raw.headers[eachHeader];
}
rammerLogger.traffic(`RAMMERHEAD_PROXY ${ip} Session: ${sessionId} Target: ${targetUrl}`);
request.raw.url = `http://localhost:8080/${sessionId}/${targetUrl}`
console.log(request.raw.url);
request.raw.rammerheadSession = session;
request.raw.proxiedUrl = targetUrl;
await rammerProxy._onRequest(request.raw, reply.raw, config.getServerInfo(request.raw));
});
app.get('/rammer/newsession', (req, reply) => {
const id = generateId();
const session = new RammerheadSession();
session.data.restrictIP = config.getIP(req.raw);
rammerStore.addSerializedSession(id, session.serializeSession());
reply.send(id);
});
app.get('/rammer/editsession', (req, reply) => {
const { id, httpProxy, enableShuffling } = new URLPath(req.raw.url).getParams();
if (!id || !rammerStore.has(id)) {
return httpResponse.badRequest(rammerLogger, req.raw, reply.raw, config.getIP(req.raw), 'not found');
}
const session = rammerStore.get(id);
session.setExternalProxySettings(httpProxy?.replace(/^http:\/\//, '') || null);
if (enableShuffling === '1') session.shuffleDict = StrShuffler.generateDictionary();
if (enableShuffling === '0') session.shuffleDict = null;
reply.send('Success');
});
app.get('/rammer/deletesession', (req, reply) => {
const { id } = new URLPath(req.raw.url).getParams();
if (!id || !rammerStore.has(id)) return reply.send('not found');
rammerStore.delete(id);
reply.send('Success');
});
app.get('/rammer/sessionexists', (req, reply) => {
const id = new URLPath(req.raw.url).get('id');
if (!id) return httpResponse.badRequest(rammerLogger, req.raw, reply.raw, config.getIP(req.raw), 'Must specify id');
reply.send(rammerStore.has(id) ? 'exists' : 'not found');
});
app.get('/rammer/mainport', (req, reply) => {
const info = config.getServerInfo(req.raw);
reply.send((info.port || '').toString());
});
if (process.env.DEVELOPMENT) app.get('/rammer/garbageCollect', (req, reply) => { global.gc(); reply.send('Ok'); });
//other stuff
const PORT = parseInt(process.env.PORT || "8080");
app.listen({ port: PORT, host: "0.0.0.0" }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log("Server listening on:");
console.log(`\thttp://localhost:${PORT}`);
console.log(`\thttp://${hostname()}:${PORT}`);
});
function shutdown() {
console.log("SIGINT/SIGTERM signal received: closing HTTP server");
app.close();
bare.close();
process.exit(0);
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
I have tried many things like editing the url and headers send to rammerhead, but it wont work. When using the actual proxy by itself, it works fine(although I have deleted files like the public folder and server folder, and I edited the config a bit)
Cannot read properties of null (reading 'nativeAutomation')
My server.js code(snippets)
I have tried many things like editing the url and headers send to rammerhead, but it wont work. When using the actual proxy by itself, it works fine(although I have deleted files like the public folder and server folder, and I edited the config a bit)