Krysti.Engineer Official Chat
You are viewing an inferior version of the site because your browser does not support WebP. Do upgrade to something like Chrome or Firefox. Loading websites like this causes them to require a fallback set of images and they are almost always lower quality and larger in size.

NodeJS Based UDP Echo Server + IRC Relay for logging

Utility to pipe anything to an IRC staff channel via UDP.
2022-07-17T:00:00:00+00:00 July 17 2022 00:00:00

npm install irc-connect

Inspired by something old I ended up using back when until quite recently was the UDP to IRC bot from WikiMedia. It was an old PERL bot (WikiBot.pl) that does much of the same you're seeing here. Anyways.. create a project directory. This will be a minimal environment to get this going so we're going to have to include some very basic IRC functionality as we're only piping a UDP port to an IRC channel here. Why do it this way? UDP will allow us to send packets without the overhead of the TCP handshake to verify information was received, thus "connectionless". This way, even if the bot is down, the data being sent won't slow up your website when it makes a request to nowhere. It's gonna send the data and not care if it made it to the destination and that's perfect for this mostly non-essential data that is sending site data that is just a nice bird's eye view to what your projects are up to.

"use strict";
const fs = require('fs');
const dgram = require('dgram');
const irc = require("irc-connect");
const ircOptions = {
	server:'irc.yourserver.com',
    port: 6697,
    channel: '#backroom',
    secure: true,
    nick: 'echo-bot2',
    realname: 'Echo Bot v2',
    ident: 'echo'
}
const udp_port = 45454; // Open firewall port for UDP if applicable 
var kirc = irc.connect(ircOptions.server, ircOptions)
.use(irc.pong)
.on('welcome', function (msg) {
	this.send('JOIN '+ ircOptions.channel);
})
var server = dgram.createSocket('udp4')
.on('error', (err) => {
  console.log("Error:", err.stack);
  server.close();
}) 
.on('message', (msg, i) => {
	var bmsg = Buffer.from(msg).toString('utf8');
	console.log(i.address, bmsg);
	kirc.send('PRIVMSG '+ircOptions.channel+' :' + bmsg.replace(/(?:\r\n|\r|\n)/g, ' '));
}).bind(udp_port);



Here is a minimal node setup that'll get your bot on IRC and returning the text from the UDP port into an IRC channel. This is helpful when multiple staff could use some status, updates, or whatever you want sent in a convenient location for all to access.

/mode #backroom +i
/mode #backroom +I *!echo@*.bot.hostmask

If security is an issue you can use an invite exception on your IRC channel pending your IRCd supports it. You can add these exceptions for all your staff, use a vhost to pipe in a single exception or whatever configuration you like. Most cases it's probably fine just making a random +s (secret) channel.

cd ~/unrealircd
./unrealircd module install third/autovhost
nano conf/unrealircd.conf


loadmodule "third/autovhost";
autovhost {
        192.168.0.* pretty.guardian;
        127.0.0.1 land.of.oz;
};

The projects found here have been tested on UnrealIRCd 6. Third party modules like autovhost have made it easier to craft hostnames that make sense for specific IPs and ranges.
 

echo SERVER1: `uptime` | socat - UDP-DATAGRAM:yourserver.com:45454

This can be used to report uptime to your bot and then have it displayed in the configured IRC channel above. Save it to a script and make it executable. Be sure you have the socat command installed.

crontab -e
0 */1 * * * /home/youruser/uptime.sh >/dev/null 2>&1

You can schedule this task for an hourly report. Also you can expand on this idea similarly tying in commands to the 'socat' command like in the above example to be reported on by your bot.

<?php
class K_UDP {
	private $udp_info = array(
			'udp_host' 	=> "ip.bot.ip.bot", // change to your bot IP
			'udp_port'	=> "45454" // change if you changed from project's default
	);
	private $udp_host,$udp_send,$udp_msg;
	private $udp_nick,$udp_type,$udp_id,$udp_ip;
	function __construct() {
		$this->udp_ip = $_SERVER['REMOTE_ADDR'];
		$this->udp_host = gethostbyaddr($this->udp_ip);
	}
	function parse() {
		if(empty($this->udp_nick)) { $this->udp_nick = "Anonymous"; }
		switch($this->udp_type) {
			case "message": {
				$this->udp_send = $this->udp_id." - ".$this->udp_nick. " ". $this->udp_msg;
				break;
			}
		}
	}
	function send() {
		$sock = fsockopen("udp://".$this->udp_info['udp_host'], $this->udp_info['udp_port'], $errno, $errstr); 
		$res = fwrite($sock, $this->udp_send); 
	}
}
?>

Be sure you change the IP and port if you changed the default port I suggested above. Also make sure you have opened up those ports on your server firewall as a UDP connection.

<?php
$udp = new K_UDP();
$udp->udp_id = "https://krysti.engineer";
$udp->udp_msg = "hello, test message";
$udp->udp_nick = "krysti@WOPR";
$udp->udp_type = "message";
$udp->parse();
$udp->send();
?>

Now we expand the use by applying it's function to something like a message. You'll want to import the K_UDP class and it's functionality in this example to any site with PHP that you need output from into the echobot channel. You can use this to monitor what's going on with your websites by monitoring them on an IRC debugging channel now. Good examples include login attempts, comments, banned IPs and flagged DNSBL IPs making requests, moderator actions, new articles and edits.

Contact Krysti

@itskrystibitch Twitter Photo
Twitter:
@KrystiOnline
Facebook:
@KrystiOnline
IRC:
Official Chat
Direct.Me:
@Krysti
Ko-fi:
@Krysti
Photos:
Virtual Krysti
E-Mail / Notify of errors:
coder [@] krysti.engineer
Please be patient contacting me, I don't really check much of social media or anything. If you use the IRC be sure to stick around because that's how IRC works, silly. :P