commit 0b35370b31f4131b34f63fc1f56aaaaa3305338b Author: Semisol Date: Thu Sep 2 01:00:50 2021 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e5bece --- /dev/null +++ b/.gitignore @@ -0,0 +1,120 @@ +# ---> Node +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +wallopsserv.json +log.txt \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5596625 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright (c) 2021 chmod/Semisol. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7170d95 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# WallopsServ + +Wallops service for Pissnet, broadcasts any message +to wallops. \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..441410c --- /dev/null +++ b/index.js @@ -0,0 +1,265 @@ +let IRC = require("irc-framework") +const c = (s) => String.fromCodePoint(s); +const bold = c(0x02) +const color = c(0x03) +const reset = c(0x0f) +let fs = require("fs") +const nodemailer = require("nodemailer"); +let conf = JSON.parse(fs.readFileSync("wallopsserv.json")) +let transporter = nodemailer.createTransport({ + host: conf.mail.host, + port: conf.mail.port, + secure: conf.mail.secure, + auth: { + user: conf.mail.user, + pass: conf.mail.pass + } +}); +const bot = new IRC.Client({ + nick: conf.user.nick, + username: conf.user.ident, + gecos: conf.user.gecos, + version: conf.user.nick, + host: conf.server.host, + tls: conf.server.tls, + port: conf.server.port +}); +let map = conf.relays +let exemptUsers = conf.cooldownExempt +let admin = conf.admin +let map2 = conf.shortMap +bot.connect(); +let cd = {} +let globCd = 0 +let wallops = [] +let joins = [] +if (!fs.existsSync("log.txt")) fs.writeFileSync("log.txt", "") +let log = fs.readFileSync("log.txt").toString().split("\n").filter(x=>x) +let lockActive = false +function logS(str){ + let d = new Date() + log.push(`[${d.getUTCHours().toString().padStart(2, "0")}:${d.getUTCMinutes().toString().padStart(2, "0")}:${d.getUTCSeconds().toString().padStart(2, "0")}] ${str}`) + if (log.length > conf.logLength) log.shift() + fs.writeFileSync("log.txt", log.join("\n")) +} +logS("Bot started") +bot.on("registered", () => { + bot.raw("oper " + conf.oper.name + " " + conf.oper.pass); + bot.join(conf.channel); + bot.join("#*Serv") + bot.mode(conf.user.nick, conf.user.mode) +}); +function genIdentity(e) { + if (map2[genIdentity2(e)]) { + return `${bold}${color}14[${color}13${map2[genIdentity2(e)]}${color}14]` + } + let identity = `${bold}${color}14[` + let tn = e.nick + let bridge = "" + if (tn.includes("/") && tn.split("/").length === 2) { + let els = tn.split("/") + if (map[els[1]]) { + tn = els[0] + bridge = map[els[1]] + } + } + if (tn || e.ident) { + identity += `${color}07${tn}${color}03!${color}12${e.ident}${color}03@${color}10${e.hostname.replace(/~/g, "")}` + } else { + identity += `${color}03~${color}10${e.hostname.replace(/~/g, "")}` + } + if (bridge) { + identity += `${color}03~${color}12${bridge}` + } + identity += `${color}14]` + return identity +} +function genIdentity2(e) { + let identity = `` + let tn = e.nick + let bridge = "" + if (tn.includes("/") && tn.split("/").length === 2) { + let els = tn.split("/") + if (map[els[1]]) { + tn = els[0] + bridge = map[els[1]] + } + } + if (tn || e.ident) { + identity += `${tn}!${e.ident}@${e.hostname.replace(/~/g, "")}` + } else { + identity += `~${e.hostname.replace(/~/g, "")}` + } + if (bridge) { + identity += `~${bridge}` + } + return identity +} +bot.on("wallops", (wallop) => { + if (wallop.nick.toLowerCase() !== conf.user.nick.toLowerCase()) { + bot.notice(conf.channel, `${genIdentity(wallop)} ${reset}${wallop.message}`) + } +}) +bot.on("privmsg", (e) => { + if (e.target === conf.user.nick){ + bot.notice(e.nick, bold + "+--------------- WallopsServ ---------------+") + bot.notice(e.nick, "" + "| WallopsServ provides a channel to send |") + bot.notice(e.nick, "" + "| and receive wallops. The prefix "+conf.prefix+" can be |") + bot.notice(e.nick, "" + "| used in the channel to send one. |") + bot.notice(e.nick, "" + "| Channel: " + conf.channel.padEnd(31, " ") +" |") + bot.notice(e.nick, bold + "+--------------- WallopsServ ---------------+") + return + } + if (e.target === conf.channel && e.message === conf.cmdPrefix + "sendlog" && admin.includes(genIdentity2(e))){ + logS(genIdentity2(e) + " requested the log") + transporter.sendMail({ + from: conf.mail.sender, + to: conf.mail.receiver, + subject: "Log request", + text: "The log is attached to this message.", + attachments: [ + { + filename: "log.txt", + contentDisposition: "inline", + content: log.join("\n"), + contentType: "text/plain" + } + ] + }) + bot.notice(conf.channel, "Log sent.") + return + } + if (e.target === conf.channel && e.message === conf.cmdPrefix + "rehash" && admin.includes(genIdentity2(e))){ + logS(genIdentity2(e) + " rehashed the config") + conf = JSON.parse(fs.readFileSync("wallopsserv.json")) + map = conf.relays + exemptUsers = conf.cooldownExempt + admin = conf.admin + map2 = conf.shortMap + bot.notice(conf.channel, "Rehashed.") + return + } + if (e.target === conf.channel && e.message === conf.cmdPrefix + "hostmask"){ + bot.say(conf.channel, "Your hostmask is: " + genIdentity2(e)) + return + } + if (!e.message.startsWith(conf.prefix) || e.message.length < 2) return + if (e.target === conf.channel) { + if (e.message.length > 1000) { + bot.say(conf.channel, `${e.nick}: Message too long`) + return + } + if (!exemptUsers.includes(genIdentity2(e))){ + if (cd[e.nick.toLowerCase()] > Date.now()){ + bot.say(conf.channel, `${e.nick}: Whow, slow down there! (${bold}${((cd[e.nick.toLowerCase()] - Date.now()) / 1000).toFixed(1)}s${reset})`) + return + } + if (globCd > Date.now()){ + bot.say(conf.channel, `${e.nick}: Whow, slow down there! (${bold}${((globCd - Date.now()) / 1000).toFixed(1)}s${reset} channel)`) + return + } + globCd = Date.now() + 5000 + cd[e.nick.toLowerCase()] = Date.now() + 20000 + } + bot.raw(`wallops :${genIdentity(e)} ${reset}${e.message.slice(1)}`) + logS(genIdentity2(e) + ` (${e.tags["unrealircd.org/userip"] || "???"})` + ": " + e.message.slice(1)) + if (!exemptUsers.includes(genIdentity2(e))){ + wallops.push(Date.now() + 60000) + wallops = wallops.filter(l => l > Date.now()) + if (wallops.length > 9 && !lockActive){ + logS("Channel locked: spam") + lockActive = true + bot.say(conf.channel, bold + "Locking channel for 30 seconds") + bot.mode(conf.channel, "+m") + bot.raw("locops", "#wallops has been locked temporarily due to spam") + transporter.sendMail({ + from: conf.mail.sender, + to: conf.mail.receiver, + subject: "#wallops has been locked (probable spam attack)", + text: "#wallops has been locked due to a probable spam attack.\nA log has been attached.", + attachments: [ + { + filename: "log.txt", + contentDisposition: "inline", + content: log.join("\n"), + contentType: "text/plain" + } + ] + }) + setTimeout(()=>{ + lockActive = false + bot.say(conf.channel, bold + "Channel unlocked") + bot.mode(conf.channel, "-m") + logS("Channel unlocked") + }, 30000) + } + } + } +}); +bot.on("join", (evt)=>{ + if (evt.channel !== conf.channel || evt.nick === conf.user.nick) return + logS(`${evt.nick}!${evt.ident}@${evt.hostname}${evt.gecos?`#${evt.gecos}`:""} (${evt.tags["unrealircd.org/userip"] || "???"}) joins`) + if (evt.nick.includes("/")) return + joins = joins.filter(l => l[0] !== evt.nick.toLowerCase()) + joins.push([evt.nick.toLowerCase(), Date.now() + 60000]) + joins = joins.filter(l => l[1] > Date.now()) + if (joins.length > 5 && !lockActive){ + logS("Channel locked: too many joins") + lockActive = true + bot.say(conf.channel, bold + "Locking channel for 60 seconds") + bot.mode(conf.channel, "+i") + bot.raw("locops", "#wallops has been locked temporarily due to a probable botnet attack") + transporter.sendMail({ + from: conf.mail.sender, + to: conf.mail.receiver, + subject: "#wallops has been locked (probable botnet)", + text: "#wallops has been locked due to a probable botnet.\nA log has been attached.", + attachments: [ + { + filename: "log.txt", + contentDisposition: "inline", + content: log.join("\n"), + contentType: "text/plain" + } + ] + }) + setTimeout(()=>{ + lockActive = false + bot.say(conf.channel, bold + "Channel unlocked") + bot.mode(conf.channel, "-i") + logS("Channel unlocked") + }, 60000) + } +}) +bot.on("part", (evt)=>{ + if (evt.nick === conf.user.nick) return + logS(`${evt.nick}!${evt.ident}@${evt.hostname}${evt.gecos?`#${evt.gecos}`:""} (${evt.tags["unrealircd.org/userip"] || "???"}) parts`) +}) +bot.on("kick", (e) => { + if (e.kicked === conf.user.nick && e.channel === conf.channel) { + bot.join(conf.channel); + bot.raw(`kill ${e.nick} :Go away and don't disturb me (kicked WallopsServ from ${conf.channel})`); + } +}); +bot.on("raw", (s)=>{ + if (!s.from_server) return + if (!s.line.endsWith("\r\n")) return + let r = s.line.slice(0, -2).split(" ") + if (r[0].startsWith(":") && r[1] === "KILL" && r[2].toLowerCase() === conf.user.nick){ + logS("Killed by " + r[0].slice(1)) + transporter.sendMail({ + from: conf.mail.sender, + to: conf.mail.receiver, + subject: "Bot killed", + text: "The bot has been killed by "+r[0].slice(1)+"\nA log has been attached.", + attachments: [ + { + filename: "log.txt", + contentDisposition: "inline", + content: log.join("\n"), + contentType: "text/plain" + } + ] + }) + } +}) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..8246738 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "wallopsserv", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@git.semisol.dev:Semisol/WallopsServ.git" + }, + "author": "chmod/semisol", + "license": "BSD-3-Clause" +} diff --git a/wallopsserv.example.json b/wallopsserv.example.json new file mode 100644 index 0000000..5d655c4 --- /dev/null +++ b/wallopsserv.example.json @@ -0,0 +1,53 @@ +{ + "mail": { + "host": "mail.example.com", + "port": 465, + "secure": true, + "user": "notify@example.com", + "pass": "", + "sender": "WallopsServ ", + "receiver": "Example " + }, + "user": { + "nick": "WallopsServ", + "ident": "bot", + "gecos": "WallopsServ", + "mode": "+B" + }, + "server": { + "host": "localhost", + "port": 5555, + "tls": false + }, + "oper": { + "name": "wallopsserv", + "pass": "" + }, + "relays": { + "lc": "libera-lc", + "libera": "libera", + "semirc": "semirc", + "hackint": "hackint", + "ergochat": "ergochat", + "wcknet": "wcknet" + }, + "cooldownExempt": [ + "user1!ident@cloak", + "user2!ident@ip", + "user3!ident@cloak~bridge" + ], + "admin": [ + "user1!ident@cloak", + "user2!ident@ip", + "user3!ident@cloak~bridge" + ], + "shortMap": { + "user1!ident@cloak": "user1", + "user2!ident@ip": "user2", + "user3!ident@cloak~bridge": "user2" + }, + "channel": "#wallops", + "logLength": 5000, + "prefix": "!", + "cmdPrefix": ";" +} \ No newline at end of file