2022-06-19 00:25:21 +00:00
|
|
|
import fs from "fs";
|
|
|
|
import net, {Socket} from "net";
|
|
|
|
import colors from "chalk";
|
|
|
|
import Helper from "./helper";
|
|
|
|
import Config from "./config";
|
2024-05-09 15:24:53 +00:00
|
|
|
import log from "./log";
|
2022-06-19 00:25:21 +00:00
|
|
|
|
|
|
|
type Connection = {
|
|
|
|
socket: Socket;
|
|
|
|
user: string;
|
|
|
|
};
|
2016-12-17 09:51:33 +00:00
|
|
|
class Identification {
|
2022-06-19 00:25:21 +00:00
|
|
|
private connectionId: number;
|
|
|
|
private connections: Map<number, Connection>;
|
|
|
|
private oidentdFile?: string;
|
|
|
|
|
|
|
|
constructor(startedCallback: (identHandler: Identification, err?: Error) => void) {
|
2016-12-17 09:51:33 +00:00
|
|
|
this.connectionId = 0;
|
|
|
|
this.connections = new Map();
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (typeof Config.values.oidentd === "string") {
|
|
|
|
this.oidentdFile = Helper.expandHome(Config.values.oidentd);
|
2016-12-17 09:51:33 +00:00
|
|
|
log.info(`Oidentd file: ${colors.green(this.oidentdFile)}`);
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.identd.enable) {
|
2016-12-17 09:51:33 +00:00
|
|
|
if (this.oidentdFile) {
|
2019-07-17 09:33:59 +00:00
|
|
|
log.warn(
|
|
|
|
"Using both identd and oidentd at the same time, this is most likely not intended."
|
|
|
|
);
|
2016-12-17 09:51:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const server = net.createServer(this.serverConnection.bind(this));
|
2019-06-10 09:48:43 +00:00
|
|
|
|
2022-04-12 00:45:36 +00:00
|
|
|
server.on("error", (err) => {
|
|
|
|
startedCallback(this, err);
|
|
|
|
});
|
2019-06-10 09:48:43 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
server.listen(
|
|
|
|
{
|
2022-05-01 19:12:39 +00:00
|
|
|
port: Config.values.identd.port || 113,
|
|
|
|
host: Config.values.bind,
|
2019-07-17 09:33:59 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
const address = server.address();
|
2022-06-19 00:25:21 +00:00
|
|
|
|
|
|
|
if (typeof address === "string") {
|
|
|
|
log.info(`Identd server available on ${colors.green(address)}`);
|
|
|
|
} else if (address?.address) {
|
|
|
|
log.info(
|
|
|
|
`Identd server available on ${colors.green(
|
|
|
|
address.address + ":" + address.port.toString()
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
2019-07-17 09:33:59 +00:00
|
|
|
|
|
|
|
startedCallback(this);
|
|
|
|
}
|
|
|
|
);
|
2016-12-17 09:51:33 +00:00
|
|
|
} else {
|
2017-03-17 20:19:08 +00:00
|
|
|
startedCallback(this);
|
2016-12-17 09:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
serverConnection(socket: Socket) {
|
|
|
|
socket.on("error", (err: string) => log.error(`Identd socket error: ${err}`));
|
2017-04-08 12:34:31 +00:00
|
|
|
socket.on("data", (data) => {
|
2016-12-17 09:51:33 +00:00
|
|
|
this.respondToIdent(socket, data);
|
|
|
|
socket.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
respondToIdent(socket: Socket, buffer: Buffer) {
|
|
|
|
const data = buffer.toString().split(",");
|
2016-12-17 09:51:33 +00:00
|
|
|
|
2018-05-15 13:04:05 +00:00
|
|
|
const lport = parseInt(data[0], 10) || 0;
|
|
|
|
const fport = parseInt(data[1], 10) || 0;
|
2016-12-17 09:51:33 +00:00
|
|
|
|
|
|
|
if (lport < 1 || fport < 1 || lport > 65535 || fport > 65535) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
for (const connection of this.connections.values()) {
|
2019-01-06 19:44:43 +00:00
|
|
|
if (connection.socket.remotePort === fport && connection.socket.localPort === lport) {
|
2019-07-17 09:33:59 +00:00
|
|
|
return socket.write(
|
|
|
|
`${lport}, ${fport} : USERID : TheLounge : ${connection.user}\r\n`
|
|
|
|
);
|
2016-12-17 09:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.write(`${lport}, ${fport} : ERROR : NO-USER\r\n`);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
addSocket(socket: Socket, user: string) {
|
2016-12-17 09:51:33 +00:00
|
|
|
const id = ++this.connectionId;
|
|
|
|
|
2018-03-05 00:59:16 +00:00
|
|
|
this.connections.set(id, {socket, user});
|
2016-12-17 09:51:33 +00:00
|
|
|
|
|
|
|
if (this.oidentdFile) {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
removeSocket(id: number) {
|
2016-12-17 09:51:33 +00:00
|
|
|
this.connections.delete(id);
|
|
|
|
|
|
|
|
if (this.oidentdFile) {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
let file = "# Warning: file generated by The Lounge: changes will be overwritten!\n";
|
|
|
|
|
2023-02-18 10:34:28 +00:00
|
|
|
this.connections.forEach((connection, id) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!connection.socket.remotePort || !connection.socket.localPort) {
|
2023-02-09 03:05:22 +00:00
|
|
|
// Race condition: this can happen when more than one socket gets disconnected at
|
|
|
|
// once, since we `refresh()` for each one being added/removed. This results
|
2023-02-14 04:50:16 +00:00
|
|
|
// in there possibly being one or more disconnected sockets remaining when we get here.
|
2023-02-18 10:46:31 +00:00
|
|
|
log.warn(
|
|
|
|
`oidentd: socket has no remote or local port (id=${id}). See https://github.com/thelounge/thelounge/pull/4695.`
|
|
|
|
);
|
2023-02-09 03:05:22 +00:00
|
|
|
return;
|
2022-06-19 00:25:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
file +=
|
|
|
|
`fport ${connection.socket.remotePort}` +
|
|
|
|
` lport ${connection.socket.localPort}` +
|
|
|
|
` { reply "${connection.user}" }\n`;
|
2016-12-17 09:51:33 +00:00
|
|
|
});
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (this.oidentdFile) {
|
|
|
|
fs.writeFile(this.oidentdFile, file, {flag: "w+"}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
log.error("Failed to update oidentd file!", err.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-12-17 09:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default Identification;
|