UNPKG

2.69 kBJavaScriptView Raw
1"use strict";
2
3const fs = require("fs");
4const net = require("net");
5const colors = require("colors/safe");
6const Helper = require("./helper");
7
8class Identification {
9 constructor(startedCallback) {
10 this.connectionId = 0;
11 this.connections = new Map();
12
13 if (typeof Helper.config.oidentd === "string") {
14 this.oidentdFile = Helper.expandHome(Helper.config.oidentd);
15 log.info(`Oidentd file: ${colors.green(this.oidentdFile)}`);
16
17 this.refresh();
18 }
19
20 if (Helper.config.identd.enable) {
21 if (this.oidentdFile) {
22 log.warn("Using both identd and oidentd at the same time, this is most likely not intended.");
23 }
24
25 var server = net.createServer(this.serverConnection.bind(this));
26 server.listen({
27 port: Helper.config.identd.port || 113,
28 host: Helper.config.bind || Helper.config.host,
29 }, () => {
30 var address = server.address();
31 log.info(`Identd server available on ${colors.green(address.address + ":" + address.port)}`);
32
33 startedCallback(this);
34 });
35 } else {
36 startedCallback(this);
37 }
38 }
39
40 serverConnection(socket) {
41 socket.on("data", (data) => {
42 this.respondToIdent(socket, data);
43 socket.end();
44 });
45 }
46
47 respondToIdent(socket, data) {
48 data = data.toString().split(",");
49
50 const lport = parseInt(data[0]);
51 const fport = parseInt(data[1]);
52
53 if (lport < 1 || fport < 1 || lport > 65535 || fport > 65535) {
54 return;
55 }
56
57 for (var connection of this.connections.values()) {
58 if (connection.socket.remoteAddress === socket.remoteAddress
59 && connection.socket.remotePort === fport
60 && connection.socket.localPort === lport
61 && connection.socket.localAddress === socket.localAddress) {
62 return socket.write(`${lport}, ${fport} : USERID : UNIX : ${connection.user}\r\n`);
63 }
64 }
65
66 socket.write(`${lport}, ${fport} : ERROR : NO-USER\r\n`);
67 }
68
69 addSocket(socket, user) {
70 const id = ++this.connectionId;
71
72 this.connections.set(id, {socket: socket, user: user});
73
74 if (this.oidentdFile) {
75 this.refresh();
76 }
77
78 return id;
79 }
80
81 removeSocket(id) {
82 this.connections.delete(id);
83
84 if (this.oidentdFile) {
85 this.refresh();
86 }
87 }
88
89 refresh() {
90 let file = "# Warning: file generated by The Lounge: changes will be overwritten!\n";
91
92 this.connections.forEach((connection) => {
93 file += `to ${connection.socket.remoteAddress}`
94 + ` lport ${connection.socket.localPort}`
95 + ` from ${connection.socket.localAddress}`
96 + ` fport ${connection.socket.remotePort}`
97 + ` { reply "${connection.user}" }\n`;
98 });
99
100 fs.writeFile(this.oidentdFile, file, {flag: "w+"}, function(err) {
101 if (err) {
102 log.error("Failed to update oidentd file!", err);
103 }
104 });
105 }
106}
107
108module.exports = Identification;