我终于学会了黑客帝国里的矩阵雨,瞬间高大上了

56 阅读3分钟

class MatrixRain {

constructor(opts) {

this.transpose = opts.direction === h;

this.color = opts.color;

this.charRange = opts.charRange;

this.maxSpeed = 20;

this.colDroplets = [];

this.numCols = 0;

this.numRows = 0;

// handle reading from file

if (opts.filePath) {

if (!fs.existsSync(opts.filePath)) {

throw new Error(${opts.filePath} doesn't exist);

}

this.fileChars = fs.readFileSync(opts.filePath, utf-8).trim().split(``);

this.filePos = 0;

this.charRange = file;

}

}

generateChars(len, charRange) {

// by default charRange == ascii

let chars = new Array(len);

if (charRange === ascii) {

for (let i = 0; i < len; i++) {

chars[i] = String.fromCharCode(rand(0x21, 0x7E));

}

} else if (charRange === braille) {

for (let i = 0; i < len; i++) {

chars[i] = String.fromCharCode(rand(0x2840, 0x28ff));

}

} else if (charRange === katakana) {

for (let i = 0; i < len; i++) {

chars[i] = String.fromCharCode(rand(0x30a0, 0x30ff));

}

} else if (charRange === emoji) {

// emojis are two character widths, so use a prefix

const emojiPrefix = String.fromCharCode(0xd83d);

for (let i = 0; i < len; i++) {

chars[i] = emojiPrefix + String.fromCharCode(rand(0xde01, 0xde4a));

}

} else if (charRange === file) {

for (let i = 0; i < len; i++, this.filePos++) {

this.filePos = this.filePos < this.fileChars.length ? this.filePos : 0;

chars[i] = this.fileChars[this.filePos];

}

}

return chars;

}

makeDroplet(col) {

return {

col,

alive: 0,

curRow: rand(0, this.numRows),

height: rand(this.numRows / 2, this.numRows),

speed: rand(1, this.maxSpeed),

chars: this.generateChars(this.numRows, this.charRange),

};

}

resizeDroplets() {

[this.numCols, this.numRows] = process.stdout.getWindowSize();

// transpose for direction

if (this.transpose) {

[this.numCols, this.numRows] = [this.numRows, this.numCols];

}

// Create droplets per column

// add/remove droplets to match column size

if (this.numCols > this.colDroplets.length) {

for (let col = this.colDroplets.length; col < this.numCols; ++col) {

// make two droplets per row that start in random positions

this.colDroplets.push([this.makeDroplet(col), this.makeDroplet(col)]);

}

} else {

this.colDroplets.splice(this.numCols, this.colDroplets.length - this.numCols);

}

}

writeAt(row, col, str, color) {

// Only output if in viewport

if (row >=0 && row < this.numRows && col >=0 && col < this.numCols) {

const pos = this.transpose ? ansi.cursorPos(col, row) : ansi.cursorPos(row, col);

write(${pos}${color || ``}${str || ``});

}

}

renderFrame() {

const ansiColor = ansi.colorsfg${this.color.charAt(0).toUpperCase()}${this.color.substr(1)};

for (const droplets of this.colDroplets) {

for (const droplet of droplets) {

const {curRow, col: curCol, height} = droplet;

droplet.alive++;

if (droplet.alive % droplet.speed === 0) {

this.writeAt(curRow - 1, curCol, droplet.chars[curRow - 1], ansiColor);

this.writeAt(curRow, curCol, droplet.chars[curRow], ansi.colors.fgWhite());

this.writeAt(curRow - height, curCol, );

droplet.curRow++;

}

if (curRow - height > this.numRows) {

// reset droplet

Object.assign(droplet, this.makeDroplet(droplet.col), {curRow: 0});

}

}

}

flush();

}

}

还有几个工具方法:

// Simple string stream buffer + stdout flush at once

let outBuffer = [];

function write(chars) {

return outBuffer.push(chars);

}

function flush() {

process.stdout.write(outBuffer.join(``));

return outBuffer = [];

}

function rand(start, end) {

return start + Math.floor(Math.random() * (end - start));

}

matrix-rain 的启动代码如下:

const args = argParser.parseArgs();

const matrixRain = new MatrixRain(args);

function start() {

if (!process.stdout.isTTY) {

console.error(Error: Output is not a text terminal);

process.exit(1);

}

// clear terminal and use alt buffer

process.stdin.setRawMode(true);

write(ansi.useAltBuffer());

write(ansi.cursorInvisible());

write(ansi.colors.bgBlack());

write(ansi.colors.fgBlack());

write(ansi.clearScreen());

flush();

matrixRain.resizeDroplets();

}

function stop() {

write(ansi.cursorVisible());

write(ansi.clearScreen());

write(ansi.cursorHome());

write(ansi.useNormalBuffer());

flush();

process.exit();

}

process.on(SIGINT, () => stop());

process.stdin.on(data, () => stop());

process.stdout.on(resize, () => matrixRain.resizeDroplets());

本人从事网路安全工作12年,曾在2个大厂工作过,安全服务、售后服务、售前、攻防比赛、安全讲师、销售经理等职位都做过,对这个行业了解比较全面。

最近遍览了各种网络安全类的文章,内容参差不齐,其中不伐有大佬倾力教学,也有各种不良机构浑水摸鱼,在收到几条私信,发现大家对一套完整的系统的网络安全从学习路线到学习资料,甚至是工具有着不小的需求。

最后,我将这部分内容融会贯通成了一套282G的网络安全资料包,所有类目条理清晰,知识点层层递进,需要的小伙伴可以点击下方小卡片领取哦!下面就开始进入正题,如何从一个萌新一步一步进入网络安全行业。

学习路线图

其中最为瞩目也是最为基础的就是网络安全学习路线图,这里我给大家分享一份打磨了3个月,已经更新到4.0版本的网络安全学习路线图。

相比起繁琐的文字,还是生动的视频教程更加适合零基础的同学们学习,这里也是整理了一份与上述学习路线一一对应的网络安全视频教程。

网络安全工具箱

当然,当你入门之后,仅仅是视频教程已经不能满足你的需求了,你肯定需要学习各种工具的使用以及大量的实战项目,这里也分享一份我自己整理的网络安全入门工具以及使用教程和实战。

项目实战

最后就是项目实战,这里带来的是SRC资料&HW资料,毕竟实战是检验真理的唯一标准嘛~

面试题

归根结底,我们的最终目的都是为了就业,所以这份结合了多位朋友的亲身经验打磨的面试题合集你绝对不能错过! 详情docs.qq.com/doc/DSlhRRFFyU2pVZGhS