Add PROXY protocol support.
This commit is contained in:
parent
a29b0ac898
commit
7590a3ba64
22
index.ts
22
index.ts
@ -1,3 +1,7 @@
|
||||
import * as net from "net";
|
||||
import * as sq from "sqlite3";
|
||||
import * as pp from "proxy-protocol-js";
|
||||
|
||||
if (process.argv[2] == null) {
|
||||
console.log("No IP specified");
|
||||
process.exit(1)
|
||||
@ -17,7 +21,6 @@ function msgToString(time: Date, address: string, msg: string) {
|
||||
}
|
||||
|
||||
|
||||
import * as sq from "sqlite3";
|
||||
const db = new sq.Database("data.db");
|
||||
db.run("CREATE TABLE IF NOT EXISTS messages (time INTEGER PRIMARY KEY, address TEXT NOT NULL, msg TEXT NOT NULL) STRICT");
|
||||
|
||||
@ -28,20 +31,20 @@ db.each("SELECT time, address, msg FROM messages", (err: Error, m: {time: number
|
||||
});
|
||||
|
||||
const textEncoder = new TextEncoder();
|
||||
import * as net from "net";
|
||||
let srv = net.createServer(function(socket: net.Socket) {
|
||||
let addr = socket.address() as net.AddressInfo;
|
||||
if (!addr.hasOwnProperty('address')) {
|
||||
let a = socket.address() as net.AddressInfo;
|
||||
if (!a.hasOwnProperty('address')) {
|
||||
console.log("?!?!?!");
|
||||
socket.end();
|
||||
return;
|
||||
}
|
||||
let addr = a.address;
|
||||
socket.on('data', function(data: Buffer | string) {
|
||||
switch (data[0]) {
|
||||
case 0x30:
|
||||
const store = db.prepare("INSERT INTO messages VALUES (?,?,?)");
|
||||
let t = new Date;
|
||||
let a = addr.address;
|
||||
let a = addr;
|
||||
let m = String(data.slice(1)).replace("/\n/g", "\\n").replace("/\x1b/g", "\\x1b");
|
||||
msize += textEncoder.encode(msgToString(t, a, m)).length
|
||||
store.run(Number(t),a,m);
|
||||
@ -60,8 +63,15 @@ let srv = net.createServer(function(socket: net.Socket) {
|
||||
socket.write(str);
|
||||
});
|
||||
return;
|
||||
case 'P'.charCodeAt(0):
|
||||
if (addr == "127.0.0.1") {
|
||||
addr = pp.V1ProxyProtocol.parse(String(data)).source.ipAddress;
|
||||
} else {
|
||||
console.log("Address " + addr + " tried to use proxy protocol.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
console.log(addr.address + "???" + data[0]);
|
||||
console.log(addr + "???" + data[0]);
|
||||
});
|
||||
socket.on('error', function(e: Error) {
|
||||
console.log(e.stack);
|
||||
|
6
package-lock.json
generated
6
package-lock.json
generated
@ -5,6 +5,7 @@
|
||||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"proxy-protocol-js": "^4.0.6",
|
||||
"sqlite3": "^5.1.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -958,6 +959,11 @@
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-protocol-js": {
|
||||
"version": "4.0.6",
|
||||
"resolved": "https://registry.npmjs.org/proxy-protocol-js/-/proxy-protocol-js-4.0.6.tgz",
|
||||
"integrity": "sha512-SjXgyBmr0dBbKUZ0jOzp0N9urTcDOI1cd1oEeE43W1vG4OMwYYLggCRcMJ0zv0gdTA8Imb4cAiYj8Ic/PWv1mw=="
|
||||
},
|
||||
"node_modules/pump": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.2.tgz",
|
||||
|
@ -4,6 +4,7 @@
|
||||
"typescript": "^5.7.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"proxy-protocol-js": "^4.0.6",
|
||||
"sqlite3": "^5.1.7"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user