Skip to content

Commit 612508b

Browse files
committed
[2.1.0] Changed connection count fetching to redis
1 parent 4966b74 commit 612508b

File tree

6 files changed

+72
-47
lines changed

6 files changed

+72
-47
lines changed

config.json

+4
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
"internal": {
1010
"host": "localhost",
1111
"port": 23500
12+
},
13+
"redis": {
14+
"host": "127.0.0.1",
15+
"port": 6380
1216
}
1317
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kocity-proxy",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "A simple auth proxy server for kocity",
55
"main": "dist/index.js",
66
"scripts": {
@@ -22,7 +22,8 @@
2222
"chalk": "^3.0.0",
2323
"express": "^4.18.2",
2424
"http-proxy": "^1.18.1",
25-
"http-proxy-middleware": "^2.0.6"
25+
"http-proxy-middleware": "^2.0.6",
26+
"redis": "^4.6.7"
2627
},
2728
"devDependencies": {
2829
"@types/express": "^4.17.17",

src/config.ts

+38-33
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
import fs from "fs";
2-
3-
import { config } from "./interfaces"
4-
5-
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
6-
7-
const maxPlayers = process.env.MAX_PLAYERS || config.maxPlayers
8-
9-
/**
10-
* This exports a default object that contains the configuration for the server.
11-
* The object contains the following properties:
12-
* - name: the name of the server, taken from the environment variable SERVER_NAME or the config file.
13-
* - authServer: the URL of the authentication server, taken from the environment variable AUTH_SERVER or the config file.
14-
* - publicAddr: the public address of the server, taken from the environment variable PUBLIC_ADDRESS or the config file.
15-
* - maxPlayers: the maximum number of players allowed on the server, taken from the environment variable MAX_PLAYERS or the config file.
16-
* - external: an object containing the following properties:
17-
* - port: the port number for external connections, taken from the environment variable EXTERNAL_PORT or the config file.
18-
* - internal: an object containing the following properties:
19-
* - host: the host name for internal connections, taken from the environment variable INTERNAL_HOST or the config file.
20-
* - port: the port number for internal connections, taken from the environment variable INTERNAL_PORT or the config file.
21-
*/
22-
export default {
23-
name: process.env.SERVER_NAME || config.name,
24-
authServer: process.env.AUTH_SERVER || config.authServer,
25-
publicAddr: process.env.PUBLIC_ADDRESS || config.publicAddr,
26-
maxPlayers: typeof maxPlayers != "number" ? parseInt(maxPlayers) : maxPlayers,
27-
external: {
28-
port: process.env.EXTERNAL_PORT || config.external.port,
29-
},
30-
internal: {
31-
host: process.env.INTERNAL_HOST || config.internal.host,
32-
port: process.env.INTERNAL_PORT || config.internal.port,
33-
},
1+
import fs from "fs";
2+
3+
import { config } from "./interfaces"
4+
5+
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
6+
7+
const maxPlayers = process.env.MAX_PLAYERS || config.maxPlayers
8+
9+
/**
10+
* This exports a default object that contains the configuration for the server.
11+
* The object contains the following properties:
12+
* - name: the name of the server, taken from the environment variable SERVER_NAME or the config file.
13+
* - authServer: the URL of the authentication server, taken from the environment variable AUTH_SERVER or the config file.
14+
* - publicAddr: the public address of the server, taken from the environment variable PUBLIC_ADDRESS or the config file.
15+
* - maxPlayers: the maximum number of players allowed on the server, taken from the environment variable MAX_PLAYERS or the config file.
16+
* - external: an object containing the following properties:
17+
* - port: the port number for external connections, taken from the environment variable EXTERNAL_PORT or the config file.
18+
* - internal: an object containing the following properties:
19+
* - host: the host name for internal connections, taken from the environment variable INTERNAL_HOST or the config file.
20+
* - port: the port number for internal connections, taken from the environment variable INTERNAL_PORT or the config file.
21+
*/
22+
export default {
23+
name: process.env.SERVER_NAME || config.name,
24+
authServer: process.env.AUTH_SERVER || config.authServer,
25+
publicAddr: process.env.PUBLIC_ADDRESS || config.publicAddr,
26+
maxPlayers: typeof maxPlayers != "number" ? parseInt(maxPlayers) : maxPlayers,
27+
external: {
28+
port: process.env.EXTERNAL_PORT || config.external.port,
29+
},
30+
internal: {
31+
host: process.env.INTERNAL_HOST || config.internal.host,
32+
port: process.env.INTERNAL_PORT || config.internal.port,
33+
},
34+
redis: {
35+
host: process.env.REDIS_HOST || config.redis.host,
36+
port: process.env.REDIS_PORT || config.redis.port,
37+
password: process.env.REDIS_PASSWORD || config.redis.password || undefined,
38+
},
3439
} as config

src/index.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import axios from 'axios';
22
import express from 'express';
33
import { createProxyMiddleware } from 'http-proxy-middleware';
44
import config from './config';
5-
import chalk from 'chalk';
6-
import fs from 'fs';
7-
import os from 'os';
5+
import { createClient } from 'redis';
86

97
import Logger from './logger.js'
108

@@ -14,24 +12,33 @@ const log = new Logger();
1412

1513
if(config.name == "ServerName") log.warn("Please change the name in the config.json or via the environment (SERVER_NAME)");
1614

15+
const redis = createClient({
16+
socket: {
17+
host: config.redis.host,
18+
port: config.redis.port,
19+
},
20+
password: config.redis.password,
21+
});
22+
23+
redis.connect().then(() => {
24+
log.info("Connected to Redis");
25+
}).catch((err) => {
26+
log.fatal("Failed to connect to Redis");
27+
log.fatal(err.message);
28+
log.fatal(err.stack ? err.stack.toString() : '');
29+
process.exit(1);
30+
});
1731

1832
const app = express();
1933

2034
app.use(express.json());
2135

2236
app.get('/stats/status', async (req, res) => {
23-
log.info(`Status requested by ${req.ip}`);
24-
2537
res.send({
2638
status: "OK",
2739
version: require('../package.json').version,
2840
uptime: process.uptime(),
29-
connections: await new Promise((resolve, reject) => {
30-
server.getConnections((err, connections) => {
31-
if(err) return reject(err);
32-
resolve(connections-1);
33-
});
34-
}),
41+
connections: (await redis.KEYS('user:session:*')).length,
3542
maxConnections: config.maxPlayers,
3643
});
3744
});

src/interfaces.ts

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ export interface config {
2323
/** The port number for internal connections. */
2424
port: number,
2525
},
26+
redis: {
27+
/** The host name for the Redis server. */
28+
host: string,
29+
/** The port number for the Redis server. */
30+
port: number,
31+
/** The password for the Redis server. */
32+
password?: string,
33+
}
2634
}
2735

2836

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "ESNext",
44
"module": "NodeNext",
55
"outDir": "dist",
66
"strict": true,

0 commit comments

Comments
 (0)