Initial commit

This commit is contained in:
undergroundwires
2019-12-31 16:09:39 +01:00
commit 4e7f244190
108 changed files with 17296 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import { CodeBuilder } from './CodeBuilder';
import { IUserSelection } from './../Selection/IUserSelection';
import { Signal } from '@/infrastructure/Events/Signal';
import { IApplicationCode } from './IApplicationCode';
import { IScript } from '@/domain/IScript';
export class ApplicationCode implements IApplicationCode {
public readonly changed = new Signal<string>();
public current: string;
private readonly codeBuilder: CodeBuilder;
constructor(userSelection: IUserSelection, private readonly version: string) {
if (!userSelection) { throw new Error('userSelection is null or undefined'); }
if (!version) { throw new Error('version is null or undefined'); }
this.codeBuilder = new CodeBuilder();
this.setCode(userSelection.selectedScripts);
userSelection.changed.on((scripts) => {
this.setCode(scripts);
});
}
private setCode(scripts: ReadonlyArray<IScript>) {
this.current = this.codeBuilder.buildCode(scripts, this.version);
this.changed.notify(this.current);
}
}

View File

@@ -0,0 +1,27 @@
import { AdminRightsFunctionRenderer } from './Renderer/AdminRightsFunctionRenderer';
import { AsciiArtRenderer } from './Renderer/AsciiArtRenderer';
import { FunctionRenderer } from './Renderer/FunctionRenderer';
import { Script } from '@/domain/Script';
export class CodeBuilder {
private readonly functionRenderer: FunctionRenderer;
private readonly adminRightsFunctionRenderer: AdminRightsFunctionRenderer;
private readonly asciiArtRenderer: AsciiArtRenderer;
public constructor() {
this.functionRenderer = new FunctionRenderer();
this.adminRightsFunctionRenderer = new AdminRightsFunctionRenderer();
this.asciiArtRenderer = new AsciiArtRenderer();
}
public buildCode(scripts: ReadonlyArray<Script>, version: string): string {
if (!scripts) { throw new Error('scripts is undefined'); }
if (!version) { throw new Error('version is undefined'); }
return `@echo off\n\n${this.asciiArtRenderer.renderAsciiArt(version)}\n\n`
+ `${this.adminRightsFunctionRenderer.renderAdminRightsFunction()}\n\n`
+ scripts.map((script) => this.functionRenderer.renderFunction(script.name, script.code)).join('\n\n')
+ '\n\n'
+ 'pause\n'
+ 'exit /b 0';
}
}

View File

@@ -0,0 +1,6 @@
import { ISignal } from '@/infrastructure/Events/ISignal';
export interface IApplicationCode {
readonly changed: ISignal<string>;
readonly current: string;
}

View File

@@ -0,0 +1,18 @@
import { FunctionRenderer } from './FunctionRenderer';
export class AdminRightsFunctionRenderer {
private readonly functionRenderer: FunctionRenderer;
constructor() {
this.functionRenderer = new FunctionRenderer();
}
public renderAdminRightsFunction() {
const name = 'Ensure admin priviliges';
const code = 'fltmc >nul 2>&1 || (\n' +
' echo This batch script requires administrator privileges. Right-click on\n' +
' echo the script and select "Run as administrator".\n' +
' pause\n' +
' exit 1\n' +
')';
return this.functionRenderer.renderFunction(name, code);
}
}

View File

@@ -0,0 +1,16 @@
import { CodeRenderer } from './CodeRenderer';
export class AsciiArtRenderer extends CodeRenderer {
public renderAsciiArt(version: string): string {
if (!version) { throw new Error('Version is not defined'); }
return (
'██████╗ ██████╗ ██╗██╗ ██╗ █████╗ ██████╗██╗ ██╗███████╗███████╗██╗ ██╗██╗ ██╗\n' +
'██╔══██╗██╔══██╗██║██║ ██║██╔══██╗██╔════╝╚██╗ ██╔╝██╔════╝██╔════╝╚██╗██╔╝╚██╗ ██╔╝\n' +
'██████╔╝██████╔╝██║██║ ██║███████║██║ ╚████╔╝ ███████╗█████╗ ╚███╔╝ ╚████╔╝ \n' +
'██╔═══╝ ██╔══██╗██║╚██╗ ██╔╝██╔══██║██║ ╚██╔╝ ╚════██║██╔══╝ ██╔██╗ ╚██╔╝ \n' +
'██║ ██║ ██║██║ ╚████╔╝ ██║ ██║╚██████╗ ██║██╗███████║███████╗██╔╝ ██╗ ██║ \n' +
'╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ')
.split('\n').map((line) => this.renderComment(line)).join('\n')
+ `\n${this.renderComment(`https://privacy.sexy — v${version}${new Date().toUTCString()}`)}`;
}
}

View File

@@ -0,0 +1,11 @@
export abstract class CodeRenderer {
protected readonly totalFunctionSeparatorChars = 58;
protected readonly trailingHyphens = '-'.repeat(this.totalFunctionSeparatorChars);
protected renderComment(line?: string): string {
return line ? `:: ${line}` : ':: ';
}
}

View File

@@ -0,0 +1,31 @@
import { CodeRenderer } from './CodeRenderer';
export class FunctionRenderer extends CodeRenderer {
public renderFunction(name: string, code: string) {
if (!name) { throw new Error('name cannot be empty or null'); }
if (!code) { throw new Error('code cannot be empty or null'); }
return this.renderFunctionStartComment(name) + '\n'
+ `echo --- ${name}` + '\n'
+ code + '\n'
+ this.renderFunctionEndComment();
}
private renderFunctionStartComment(functionName: string): string {
if (functionName.length >= this.totalFunctionSeparatorChars) {
return this.renderComment(functionName);
}
return this.renderComment(this.trailingHyphens) + '\n' +
this.renderFunctionName(functionName) + '\n' +
this.renderComment(this.trailingHyphens);
}
private renderFunctionName(functionName: string) {
const autoFirstHypens = '-'.repeat(Math.floor((this.totalFunctionSeparatorChars - functionName.length) / 2));
const secondHypens = '-'.repeat(Math.ceil((this.totalFunctionSeparatorChars - functionName.length) / 2));
return `${this.renderComment()}${autoFirstHypens}${functionName}${secondHypens}`;
}
private renderFunctionEndComment(): string {
return this.renderComment(this.trailingHyphens);
}
}