From 14ed9929a794b5adb9e885b718a2dbe21115d79d Mon Sep 17 00:00:00 2001 From: Spookerton Date: Sat, 27 Aug 2022 17:19:17 +0100 Subject: [PATCH] initial state --- .codebeatignore | 0 .codebeatsettings | 3 + .editorconfig | 9 ++ .gitattributes | 1 + .github/CODEOWNERS | 14 ++++ .github/dependabot.yml | 6 ++ .github/workflows/codeql-analysis.yml | 37 +++++++++ .gitignore | 104 ++++++++++++++++++++++++ .vscode/extensions.json | 6 ++ .vscode/settings.json | 9 ++ LICENSE.md | 7 ++ README.md | 5 ++ config.js | 4 + package.json | 16 ++++ rome.json | 3 + src/index.js | 113 ++++++++++++++++++++++++++ 16 files changed, 337 insertions(+) create mode 100644 .codebeatignore create mode 100644 .codebeatsettings create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/CODEOWNERS create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/codeql-analysis.yml create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 config.js create mode 100644 package.json create mode 100644 rome.json create mode 100644 src/index.js diff --git a/.codebeatignore b/.codebeatignore new file mode 100644 index 0000000..e69de29 diff --git a/.codebeatsettings b/.codebeatsettings new file mode 100644 index 0000000..282d2e8 --- /dev/null +++ b/.codebeatsettings @@ -0,0 +1,3 @@ +{ + "BLOCK_NESTING": [5, 6, 7, 8] +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..79621be --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..cbdcbbc --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.js text eol=lf diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..920831e --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,14 @@ +# Require reviews Core for changes to configuration, license, etc + +.github/ @Baystation12/Core +.vscode/ @Baystation12/Core + +.codebeatsettings @Baystation12/Core +.codebeatignore @Baystation12/Core +.editorconfig @Baystation12/Core +.gitattributes @Baystation12/Core +.gitignore @Baystation12/Core + +/LICENSE.md @Baystation12/Core +/README.md @Baystation12/Core +/package.json @Baystation12/Core diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..d1f0d08 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "npm" + directory: "/" + schedule: + interval: "daily" diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..1380159 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,37 @@ +name: CodeQL +on: + push: + branches: + - main + pull_request: + branches: + - main + schedule: + - cron: 22 2 * * 0 +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + security-events: write + strategy: + fail-fast: false + matrix: + language: + - javascript + steps: + - name: Checkout repository + uses: actions/checkout@v3 + - name: Setup Node.js environment + uses: actions/setup-node@v3.4.1 + with: + node-version: 18.x + architecture: x64 + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6704566 --- /dev/null +++ b/.gitignore @@ -0,0 +1,104 @@ +# 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/ + +# TypeScript v1 declaration files +typings/ + +# 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 + +# Next.js build output +.next + +# 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 diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..7f1640c --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "rome.rome", + "eamodio.gitlens" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7bd0b03 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "files.eol": "\n", + "files.encoding": "utf8", + "files.insertFinalNewline": true, + "files.trimFinalNewlines": true, + "files.trimTrailingWhitespace": true, + "editor.insertSpaces": true, + "editor.tabSize": 2 +} diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9763c9d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +ISC License (ISC) + +Copyright (c) 2022 Baystation + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1c1d4a7 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Warden [![License: ISC v3](https://img.shields.io/badge/License-ISC-informational.svg)](https://opensource.org/licenses/ISC) [![CodeQL](https://github.com/Baystation12/warden/workflows/CodeQL/badge.svg)](https://github.com/baystation12/warden/actions) [![codebeat badge](https://codebeat.co/badges/aff3ecf7-e0a9-42c8-864b-ed8c4106f9de)](https://codebeat.co/projects/github-com-baystation12-warden-main) + +[Website](https://bay.ss13.me) - [Discord](https://bay.ss13.me/discord) + +A simple front proxy that will permit payload inspection and connection bans. diff --git a/config.js b/config.js new file mode 100644 index 0000000..73c394a --- /dev/null +++ b/config.js @@ -0,0 +1,4 @@ +export const port = 0 +export const host = '0.0.0.0' +export const game_port = 0 +export const game_host = '127.0.0.1' diff --git a/package.json b/package.json new file mode 100644 index 0000000..4f0a5d9 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "warden", + "license": "ISC", + "version": "0.1.0", + "repository": "https://github.com/baystation12/warden", + "bugs": "https://github.com/baystation12/warden/issues", + "private": true, + "type": "module", + "main": "src/index.js", + "engines": { + "node": ">=16" + }, + "contributors": [ + "spookerton (https://github.com/spookerton)" + ] +} diff --git a/rome.json b/rome.json new file mode 100644 index 0000000..0db3279 --- /dev/null +++ b/rome.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..3925198 --- /dev/null +++ b/src/index.js @@ -0,0 +1,113 @@ +import * as net from 'node:net' +import * as readline from 'node:readline' +import * as config from '../config.js' + +process.on('uncaughtException', function (error) { + console.error('Uncaught Error', error) +}) + +let connections = {} + +let blockedAddresses = {} + +let listener = new net.Server({ + allowHalfOpen: false, + noDelay: true +}).on('error', function (error) { + console.error('listener error', error) + listener.close() +}).on('close', function () { + for (let address in connections) + connections[address].remote.close() + console.log('listener closed') +}).on('connection', function (remote) { + const address = remote.remoteAddress + if (blockedAddresses[address]) + return remote.destroy() + console.log('new connection from', address) + let local = new net.Socket({ + allowHalfOpen: false, + }).on('error', function (error) { + remote.end() + local.end() + }).on('close', function () { + remote.end() + }).on('data', function (data) { + remote.write(data) + }) + connections[address] = { remote, local } + remote.on('error', function (error) { + remote.end() + local.end() + }).on('close', function () { + delete connections[address] + local.end() + }).on('data', function (data) { + if (inspect(data, local)) + return remote.end() + local.write(data) + }) + local.connect({ + port: config.game_port, + host: config.game_host, + family: 4, + noDelay: true + }) +}).listen(config.port, config.host, function () { + console.log('Listener ready on', listener.address().port) + readline.createInterface({ + input: process.stdin, + output: process.stdout, + prompt: '%> ' + }).on('close', function () { + listener.close() + process.exit(0) + }).on('line', function (line) { + let [command, ...params] = line.trim().split(/\s+/) + command = commands[command.toLowerCase()] + if (!command) + console.log('No such command, try "help"') + else + command(params, this) + this.prompt() + }).prompt() +}).unref() + +let commands = { + 'help': function () { + let result = [] + for (let command in commands) + result.push(command) + console.log(result.join('\n')) + }, + 'exit': function (params, con) { + con.close() + }, + 'connections': function (params, con) { + let result = [] + for (let address in connections) + result.push(address) + console.log(result.join('\n')) + }, + 'block': function (params, con) { + const address = params.join('') + if (blockedAddresses[address]) + return console.log('address already blocked') + blockedAddresses[address] = true + if (!connections[address]) + return console.log('blocked') + connections[address].remote.end() + console.log('blocked & kicked') + }, + 'allow': function (params, con) { + const address = params.join('') + if (!blockedAddresses[address]) + return console.log('address already allowed') + delete blockedAddresses[address] + console.log('allowed') + } +} + +function inspect (data, local) { + return false +}