Skip to content

Commit

Permalink
feat: whitelist support (#179)
Browse files Browse the repository at this point in the history
* add whitelistId config entry
* set the given whitelist
  • Loading branch information
garrappachc authored Mar 3, 2020
1 parent a942904 commit 0b1280c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
3 changes: 2 additions & 1 deletion configs/queue/6v6.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
],
"execConfigs": [
"etf2l_6v6_5cp"
]
],
"whitelistId": "10576"
}
19 changes: 17 additions & 2 deletions src/games/services/server-configurator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { PlayersService } from '@/players/services/players.service';
import { QueueConfigService } from '@/queue/services/queue-config.service';
import { RconFactoryService } from './rcon-factory.service';
import { logAddressAdd, kickAll, changelevel, execConfig, addGamePlayer, enablePlayerWhitelist, tvPort, tvPassword,
logAddressDel, delAllGamePlayers, disablePlayerWhitelist } from '../utils/rcon-commands';
logAddressDel, delAllGamePlayers, disablePlayerWhitelist, tftrueWhitelistId } from '../utils/rcon-commands';
import { QueueConfig } from '@/queue/queue-config';

class EnvironmentStub {
logRelayAddress = 'FAKE_RELAY_ADDRESS';
Expand All @@ -23,7 +24,7 @@ class PlayersServiceStub {
class QueueConfigServiceStub {
queueConfig = {
execConfigs: [ 'etf2l_6v6_5cp' ],
};
} as Partial<QueueConfig>;
}

class RconStub {
Expand Down Expand Up @@ -59,6 +60,7 @@ describe('ServerConfiguratorService', () => {
let service: ServerConfiguratorService;
let rconFactoryService: RconFactoryServiceStub;
let playersService: PlayersServiceStub;
let queueConfigService: QueueConfigServiceStub;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -74,6 +76,7 @@ describe('ServerConfiguratorService', () => {
service = module.get<ServerConfiguratorService>(ServerConfiguratorService);
rconFactoryService = module.get(RconFactoryService);
playersService = module.get(PlayersService);
queueConfigService = module.get(QueueConfigService);
});

it('should be defined', () => {
Expand Down Expand Up @@ -105,6 +108,18 @@ describe('ServerConfiguratorService', () => {
expect(spy).toHaveBeenCalledWith(tvPassword());
});

describe('when the whitelistId is set', () => {
beforeEach(() => {
queueConfigService.queueConfig.whitelistId = 'FAKE_WHITELIST_ID';
});

it('should set the whitelist', async () => {
const spy = jest.spyOn(rcon, 'send');
await service.configureServer(gameServer as any, game as any);
expect(spy).toHaveBeenCalledWith(tftrueWhitelistId('FAKE_WHITELIST_ID'));
});
});

it('should close the rcon connection', async () => {
const spy = jest.spyOn(rcon, 'end');
await service.configureServer(gameServer as any, game as any);
Expand Down
7 changes: 6 additions & 1 deletion src/games/services/server-configurator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PlayersService } from '@/players/services/players.service';
import { QueueConfigService } from '@/queue/services/queue-config.service';
import { RconFactoryService } from './rcon-factory.service';
import { logAddressAdd, changelevel, execConfig, setPassword, addGamePlayer, logAddressDel, delAllGamePlayers,
kickAll, enablePlayerWhitelist, disablePlayerWhitelist, tvPort, tvPassword } from '../utils/rcon-commands';
kickAll, enablePlayerWhitelist, disablePlayerWhitelist, tvPort, tvPassword, tftrueWhitelistId } from '../utils/rcon-commands';
import { deburr } from 'lodash';
import { extractConVarValue } from '../utils/extract-con-var-value';
import { Rcon } from 'rcon-client/lib';
Expand Down Expand Up @@ -46,6 +46,11 @@ export class ServerConfiguratorService {
await rcon.send(execConfig(configName));
}

if (this.queueConfigService.queueConfig.whitelistId) {
this.logger.debug(`[${server.name}] setting whitelist ${this.queueConfigService.queueConfig.whitelistId}...`);
await rcon.send(tftrueWhitelistId(this.queueConfigService.queueConfig.whitelistId));
}

const password = generate({ length: 10, numbers: true, uppercase: true });
this.logger.debug(`[${server.name}] settings password to ${password}...`);
await rcon.send(setPassword(password));
Expand Down
4 changes: 4 additions & 0 deletions src/games/utils/rcon-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function setPassword(password: string) {
return `sv_password ${password}`;
}

export function tftrueWhitelistId(whitelistId: string) {
return `tftrue_whitelist_id ${whitelistId}`;
}

export function tvPort(port?: string) {
return `tv_port ${port || ''}`;
}
Expand Down
3 changes: 3 additions & 0 deletions src/queue/queue-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export interface QueueConfig {
/* What configs to execute */
// fixme make this per-map instead of per-gamemode
execConfigs: string[];

/* Whitelist ID (http://whitelist.tf/) */
whitelistId: string;
}
3 changes: 3 additions & 0 deletions src/queue/services/queue-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export class QueueConfigService {
type: 'string',
},
},
whitelistId: {
type: 'string',
},
},
additionalItems: false,
};
Expand Down

0 comments on commit 0b1280c

Please sign in to comment.