Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(threshold): security command threshold per second #22

Merged
merged 1 commit into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
### Markdown ###
*.md
coverage/*
www/*
tests/*

### 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/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# 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
.env*.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Storybook build outputs
.out
.storybook-out
storybook-static

# rollup.js default build output
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

# Temporary folders
tmp/
temp/

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ The versioning scheme is [SemVer](http://semver.org/).
LARBIN_FILE=/tmp
# Debug mode
DEBUG=true
# Single command threshold per second
LARBIN_THRESHOLD=5

# Twitch Credentials
# Twitch Credentials (mandatory)
LARBIN_TWITCH_USERNAME: Larbin
LARBIN_TWITCH_PASSWORD: oic:password
LARBIN_TWITCH_CHANNEL: example
Expand Down
4 changes: 3 additions & 1 deletion src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class AppConfiguration {
public Debug: boolean;
public ConfigurationPath: string;
public ConfigurationFile: string;
public ThresholdInSeconds: number;
}

/**
Expand Down Expand Up @@ -36,7 +37,8 @@ export class Configuration implements IConfiguration {
this.App = {
Debug: process.env.DEBUG?.toLocaleLowerCase() == 'true' ?? false,
ConfigurationPath: process.env.LARBIN_FILE as string || __dirname,
ConfigurationFile: 'larbin.yml'
ConfigurationFile: 'larbin.yml',
ThresholdInSeconds: Number.parseInt(process.env.LARBIN_THRESHOLD as string || '5')
};

// Twitch
Expand Down
29 changes: 24 additions & 5 deletions src/services/TwitchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ITwitchService {

@singleton()
export class TwitchService implements ITwitchService {
private _thresholdKeyDate: Map<string, Date> = new Map<string, Date>();
private _commands: Array<ICommand> = new Array<ICommand>();
private _schedulers: Array<IScheduler> = new Array<IScheduler>();
private _schedulersIntervals: Array<NodeJS.Timeout> = new Array<NodeJS.Timeout>();
Expand All @@ -45,14 +46,32 @@ export class TwitchService implements ITwitchService {

public Listen(): void {
this.StartSchedulers();
this._client.on('message', (channels, userstate, message) => {
const command = this._commands.find((x) => message.startsWith(x.Trigger));
if (command != undefined) {
if (command.CanAction(userstate, this._configuration)) {
this._client.on('message', this._processTwitchMessage);
}

private _processTwitchMessage(channels: string, userstate: any, message: string) {
const command = this._commands.find((x) => message.startsWith(x.Trigger));
if (command != undefined) {
if (command.CanAction(userstate, this._configuration)) {
if (this._thresholdValidation(command.Trigger)) {
command.Action(this, message, userstate);
}
}
});
}
}

private _thresholdValidation(key: string): boolean {
const lastTrigger = this._thresholdKeyDate.get(key) ?? new Date(1970, 1);
const epochNow = new Date().getTime();
const epochLastTrigger = lastTrigger.getTime();
const thresholdInMiliseconds = this._configuration.App.ThresholdInSeconds * 1000;

if (!(epochLastTrigger + thresholdInMiliseconds < epochNow)) {
return false;
}

this._thresholdKeyDate.set(key, new Date());
return true;
}

public Write(message: string): void {
Expand Down
1 change: 1 addition & 0 deletions src/services/YamlService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class YamlService implements IYamlService {
Others: policies.others ?? defaultPolicies.Others
} as CommandPolicies;
}

public getCommands(): Array<ICommand> {
const yamlContent = this.getYamlContent();
const commands = new Array<ICommand>();
Expand Down
8 changes: 6 additions & 2 deletions tests/Configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('Configuration', function () {
// Arrange
process.env.DEBUG = 'true';
process.env.LARBIN_FILE = '/tmp/example';
process.env.LARBIN_THRESHOLD = '10';

// Act
const configuration = new Configuration();
Expand All @@ -15,14 +16,16 @@ describe('Configuration', function () {
expect(configuration.App).toStrictEqual({
Debug: true,
ConfigurationPath: '/tmp/example',
ConfigurationFile: 'larbin.yml'
ConfigurationFile: 'larbin.yml',
ThresholdInSeconds: 10
} as AppConfiguration);
});

it('App - Without environment', async function () {
// Arrange
delete process.env.DEBUG;
delete process.env.LARBIN_FILE;
delete process.env.LARBIN_THRESHOLD;

// Act
const configuration = new Configuration();
Expand All @@ -33,7 +36,8 @@ describe('Configuration', function () {
expect(configuration.App).toStrictEqual({
Debug: false,
ConfigurationPath: configuration.App.ConfigurationPath,
ConfigurationFile: 'larbin.yml'
ConfigurationFile: 'larbin.yml',
ThresholdInSeconds: 5
} as AppConfiguration);
});

Expand Down
45 changes: 42 additions & 3 deletions tests/services/TwitchService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,72 @@
import 'reflect-metadata'
import { IConfiguration, TwitchConfiguration } from '../../src/Configuration';
import { AppConfiguration, IConfiguration, TwitchConfiguration } from '../../src/Configuration';
import { ILoggerService, ITwitchService, TwitchService } from '../../src/services';
import { It, Mock, Times } from 'moq.ts';
import { ITmiFactory } from '../../src/factory/TmiFactory';
import { Client } from 'tmi.js';
import { ChatUserstate, Client } from 'tmi.js';
import { CommandPolicies, ICommand } from '../../src/lib/Commands';

describe('Service - Twitch', function () {
let twitchService : ITwitchService;
let twitchService : TwitchService;
let mockConfiguration : Mock<IConfiguration>;
const twitchConfiguration = {
Channel: 'TestChannel'
} as TwitchConfiguration;
const appConfiguration = {
ThresholdInSeconds: 2
} as AppConfiguration;
let mockLoggerService : Mock<ILoggerService>;
let mockTmiFactory : Mock<ITmiFactory>;
let mockClient : Mock<Client>;
let mockCommand: ICommand;

beforeEach(() => {
// Mock
mockConfiguration = new Mock<IConfiguration>();
mockConfiguration.setup(x => x.Twitch).returns(twitchConfiguration);
mockConfiguration.setup(x => x.App).returns(appConfiguration);
mockLoggerService = new Mock<ILoggerService>();
mockTmiFactory = new Mock<ITmiFactory>();
mockClient = new Mock<Client>();
mockTmiFactory.setup(x => x.Client).returns(mockClient.object());

// Service
twitchService = new TwitchService(mockLoggerService.object(), mockConfiguration.object(), mockTmiFactory.object());

// Commands
const policies = new CommandPolicies();
policies.Others = true;

mockCommand = {
Action: () => {},
CanAction: () => true,
Policies: policies,
Trigger: '!test'
};

twitchService.AddCommand(mockCommand);
});

it('Threshold', async function () {
// Arrange
const sleep = () => new Promise<void>(resolve => setTimeout(resolve, 2000));

// Act
const result1 = twitchService["_thresholdValidation"](mockCommand.Trigger);
const result2 = twitchService["_thresholdValidation"](mockCommand.Trigger);
const result3 = twitchService["_thresholdValidation"](mockCommand.Trigger);
await sleep();
const result4 = twitchService["_thresholdValidation"](mockCommand.Trigger);
const result5 = twitchService["_thresholdValidation"](mockCommand.Trigger);

// Assert
expect(result1).toBeTruthy();
expect(result2).toBeFalsy();
expect(result3).toBeFalsy();
expect(result4).toBeTruthy();
expect(result5).toBeFalsy();
}, 5 * 1000);

it('Write', async function () {
// Arrange
const message = 'Hello !';
Expand Down