-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptcha.js
More file actions
170 lines (146 loc) · 4.24 KB
/
captcha.js
File metadata and controls
170 lines (146 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const readline = require("readline");
const crypto = require("crypto");
const { FONT, FILL_CHARS, NOISE_CHARS } = require("./font");
const CHARS = Object.keys(FONT);
const CHAR_W = 5;
const CHAR_H = 7;
const SCALE_X = 2;
const PADDING = 3;
function makeLCG(seed) {
let s = seed >>> 0;
return () => {
s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
return s / 0xffffffff;
};
}
function renderChar(ch, fill) {
return FONT[ch].map((rowBits) => {
let line = "";
for (let bit = CHAR_W - 1; bit >= 0; bit--) {
line += (rowBits >> bit) & 1 ? fill.repeat(SCALE_X) : " ".repeat(SCALE_X);
}
return line;
});
}
function generateCaptcha(length = 5) {
const seed = crypto.randomBytes(4).readUInt32BE(0);
const rng = makeLCG(seed);
const code = Array.from(
{ length },
() => CHARS[Math.floor(rng() * CHARS.length)],
).join("");
const charPxW = CHAR_W * SCALE_X;
const totalW = length * charPxW + (length - 1) * PADDING + 8;
const totalH = CHAR_H + 4;
const canvas = Array.from({ length: totalH }, () => Array(totalW).fill(" "));
let xCursor = 4;
for (const ch of code) {
const fill = FILL_CHARS[Math.floor(rng() * FILL_CHARS.length)];
const vJit = Math.floor(rng() * 3) - 1;
const rows = renderChar(ch, fill);
rows.forEach((rowStr, rowIdx) => {
const y = rowIdx + 1 + vJit;
if (y < 0 || y >= totalH) return;
for (let col = 0; col < rowStr.length; col++) {
const x = xCursor + col;
if (x >= 0 && x < totalW && rowStr[col] !== " ") {
canvas[y][x] = rowStr[col];
}
}
});
xCursor += charPxW + PADDING;
}
const noiseDensity = 0.045;
const noiseCount = Math.floor(totalW * totalH * noiseDensity);
for (let i = 0; i < noiseCount; i++) {
const y = Math.floor(rng() * totalH);
const x = Math.floor(rng() * totalW);
if (canvas[y][x] === " ") {
canvas[y][x] = NOISE_CHARS[Math.floor(rng() * NOISE_CHARS.length)];
}
}
for (let l = 0; l < 2; l++) {
const y = 1 + Math.floor(rng() * (totalH - 2));
const start = Math.floor(rng() * (totalW / 3));
const end = Math.floor(totalW / 2 + rng() * (totalW / 2));
const lch = ["─", "~", "=", "-"][Math.floor(rng() * 4)];
for (let x = start; x < end; x++) {
if (canvas[y][x] === " ") canvas[y][x] = lch;
}
}
const artLines = canvas.map((row) => row.join(""));
return { code, artLines, width: totalW };
}
function printBanner() {
const inner = " .less X gitCaptcha ";
const bar = "─".repeat(inner.length);
console.log();
console.log(`┌${bar}┐`);
console.log(`│${inner}│`);
console.log(`└${bar}┘`);
console.log();
}
function printCaptcha({ artLines, width }) {
const bar = "─".repeat(width + 2);
console.log(`┌${bar}┐`);
for (const line of artLines) {
console.log(`│ ${line} │`);
}
console.log(`└${bar}┘`);
}
async function main() {
const CODE_LEN = 5;
printBanner();
console.log(`Type the characters shown.`);
console.log();
let inputStream = process.stdin;
let outputStream = process.stdout;
if (process.platform !== "win32") {
try {
const fs = require("fs");
const ttyFd = fs.openSync("/dev/tty", "r+");
inputStream = fs.createReadStream(null, { fd: ttyFd, autoClose: false });
outputStream = fs.createWriteStream(null, {
fd: ttyFd,
autoClose: false,
});
} catch {
}
}
const rl = readline.createInterface({
input: inputStream,
output: outputStream,
terminal: true,
});
const ask = (prompt) =>
new Promise((resolve, reject) => {
rl.question(prompt, resolve);
rl.once("close", () => reject(new Error("closed")));
});
const captcha = generateCaptcha(CODE_LEN);
printCaptcha(captcha);
let answer;
try {
answer = await ask(` » `);
} catch {
console.log(`\n\n Aborted. Commit blocked.\n`);
rl.close();
process.exit(1);
}
answer = answer.trim().toUpperCase();
if (answer === captcha.code) {
console.log();
console.log(`\n\n Correct! Proceeding with commit…`);
console.log();
rl.close();
process.exit(0);
}
console.log(`\n\n Commit blocked.`);
console.log();
rl.close();
process.exit(1);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});