Skip to content

Commit

Permalink
skip address if IP is in private range
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sync committed Sep 17, 2024
1 parent 717a619 commit a55912c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 36 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ Check the list of [contributors](https://github.com/a-sync/game-server-watcher/c
# Acknowledgments
This project was inspired by (the sudden disappearance of) "_Game Status#5371_" bot and its creator [Ramzi Saheb](https://github.com/Ramzi-Sah) on discord.

IP regex stolen from the [ip-regex](https://github.com/sindresorhus/ip-regex) package source.

GSW icon stolen from _Marvel’s Voices: Indigenous Voices #1_ "_The Watcher_" by [Jeffrey Veregge](https://www.jeffreyveregge.com).

Backgrounds stolen from [purple nebulas](https://opengameart.org/content/seamless-space-backgrounds) by [Screaming Brain Studios](https://screamingbrainstudios.com) and [imgur](https://imgur.com).
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
"dotenv": "^16.3.2",
"gamedig": "^5.1.3",
"grammy": "^1.20.3",
"ip": "^2.0.1",
"lowdb": "^6.1.1",
"mustache": "^4.2.0"
},
"devDependencies": {
"@types/gamedig": "^5.0.0",
"@types/ip": "^1.1.3",
"@types/node": "^16.18.73",
"eslint": "^8.56.0",
"nodemon": "^3.0.3",
Expand Down
5 changes: 4 additions & 1 deletion src/discord-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JSONPreset } from 'lowdb/node';
import { GameServer } from './game-server.js';
import hhmmss from './lib/hhmmss.js';
import { DiscordConfig } from './watcher.js';
import ip from 'ip';

const DATA_PATH = process.env.DATA_PATH || './data/';
const DBG = Boolean(Number(process.env.DBG));
Expand Down Expand Up @@ -180,7 +181,9 @@ class ServerInfoMessage {
if (gs.info.game) fields.push({ name: 'Game', value: String(gs.info.game), inline: true });
if (gs.info.map) fields.push({ name: 'Map', value: String(gs.info.map), inline: true });
fields.push({ name: 'Players', value: String(gs.info.playersNum + '/' + gs.info.playersMax), inline: true });
fields.push({ name: 'Address', value: String(gs.info.connect) });

const connectIp = gs.info.connect.split(':')[0];
if (ip.isPublic(connectIp)) fields.push({ name: 'Address', value: String(gs.info.connect) });

if (gs.config.infoText) fields.push({ name: 'Info', value: String(gs.config.infoText).slice(0, 1024) });

Expand Down
4 changes: 2 additions & 2 deletions src/game-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { GameDig, Player, QueryOptions } from 'gamedig';
import { JSONPreset } from 'lowdb/node';
import ipRegex from './lib/ipregex.js';
import ip from 'ip';
import getIP from './lib/getip.js';
import { GameServerConfig } from './watcher.js';

Expand Down Expand Up @@ -205,7 +205,7 @@ export class GameServer {

async getIp() {
if (this.ip === '0.0.0.0') {
if (ipRegex.test(this.config.host)) {
if (ip.isV4Format(this.config.host) || ip.isV6Format(this.config.host)) {
this.ip = this.config.host;
} else {
this.ip = await getIP(this.config.host) || '0.0.0.0';
Expand Down
21 changes: 0 additions & 21 deletions src/lib/ipregex.ts

This file was deleted.

14 changes: 9 additions & 5 deletions src/slack-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { JSONPreset } from 'lowdb/node';
import { GameServer } from './game-server.js';
import hhmmss from './lib/hhmmss.js';
import { SlackConfig } from './watcher.js';
import ip from 'ip';

const DATA_PATH = process.env.DATA_PATH || './data/';
const DBG = Boolean(Number(process.env.DBG));
Expand Down Expand Up @@ -193,11 +194,14 @@ class ServerInfoMessage {
text: '*Players* \r\n' + String(gs.info.playersNum + '/' + gs.info.playersMax)
});

text += '\r\nAddress: ' + String(gs.info.connect);
fields2.push({
type: 'mrkdwn',
text: '*Address* \r\n' + String(gs.info.connect)
});
const connectIp = gs.info.connect.split(':')[0];
if (ip.isPublic(connectIp)) {
text += '\r\nAddress: ' + String(gs.info.connect);
fields2.push({
type: 'mrkdwn',
text: '*Address* \r\n' + String(gs.info.connect)
});
}

if (fields2.length > 0) {
blocks.push({
Expand Down
15 changes: 10 additions & 5 deletions src/telegram-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JSONPreset } from 'lowdb/node';
import { GameServer } from './game-server.js';
import hhmmss from './lib/hhmmss.js';
import { TelegramConfig } from './watcher.js';
import ip from 'ip';

const DATA_PATH = process.env.DATA_PATH || './data/';
const DBG = Boolean(Number(process.env.DBG));
Expand Down Expand Up @@ -137,12 +138,16 @@ class ServerInfoMessage {
let infoText = this.escapeMarkdown(gs.niceName) + ' offline...';

if (gs.info && gs.online) {
infoText = [
const infoBlocks = [
this.escapeMarkdown(gs.niceName),
this.escapeMarkdown(gs.info.game) + ' / ' + this.escapeMarkdown(gs.info.map),
this.escapeMarkdown(gs.info.connect),
'Players ' + gs.info.playersNum + '/' + gs.info.playersMax
].join('\n');
this.escapeMarkdown(gs.info.game) + ' / ' + this.escapeMarkdown(gs.info.map)
];

const connectIp = gs.info.connect.split(':')[0];
if (ip.isPublic(connectIp)) infoBlocks.push(this.escapeMarkdown(gs.info.connect));

infoBlocks.push('Players ' + gs.info.playersNum + '/' + gs.info.playersMax);
infoText = infoBlocks.join('\n');

if (gs.config.infoText) infoText += 'Info:\n' + String(gs.config.infoText).slice(0, 1024) + '\n';

Expand Down

0 comments on commit a55912c

Please sign in to comment.