mirror of
1
2
Fork 0

Remove typescript and add redis. Simple image serving and image verification

This commit is contained in:
Devshh 2020-03-17 19:21:25 +05:30
parent a9601eb68b
commit ef8a4d0e87
19 changed files with 308 additions and 148 deletions

29
dev/mat.json Normal file
View File

@ -0,0 +1,29 @@
{
"zKsAXcOCNd9U": {
"mat": [
0.0, 0.0, 0.7, 0.7,
0.0, 0.0, 0.0, 0.1,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
],
"nums": 2
},
"eFkYZBMofIVG": {
"mat": [
0.0, 0.0, 0.8, 0.4,
0.0, 0.0, 0.0, 0.4,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
],
"nums": 2
},
"JOAT6FV7VKKi": {
"mat": [
0.7, 0.6, 0.2, 0.7,
1.0, 0.9, 0.5, 0.1,
1.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
],
"nums": 2
}
}

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "ucaptcha",
"version": "0.0.0-pre1",
"version": "0.0.0-pre2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -3020,12 +3020,6 @@
"mime-types": "~2.1.24"
}
},
"typescript": {
"version": "3.8.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz",
"integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==",
"dev": true
},
"undefsafe": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.3.tgz",

View File

@ -33,7 +33,6 @@
"eslint-config-google": "^0.14.0",
"nodemon": "^2.0.2",
"rollup": "^2.0.6",
"rollup-plugin-terser": "^5.3.0",
"typescript": "^3.8.3"
"rollup-plugin-terser": "^5.3.0"
}
}

View File

@ -4,11 +4,11 @@ import commonjs from '@rollup/plugin-commonjs';
const isProd = process.env.NODE_ENV === 'production';
const typescriptOptions = {include: ['src/**/*', '../shared/**/*']};
const typescriptOptions = {include: ['src/**/*']};
if (!isProd) typescriptOptions.noEmitOnError = false;
module.exports = {
input: 'src/client.js',
input: 'src/client/client.js',
plugins: [
commonjs(),
resolve(),

View File

@ -1,8 +1,13 @@
import path from 'path';
import {fileURLToPath} from 'url';
const __dirname = fileURLToPath(import.meta.url);
export const PROJECT_ROOT = process.cwd();
/**
* A path string relative to the project folder.
* @typedef {String} IMAGES_FOLDER
*/
export const IMAGES_FOLDER = path.join(__dirname, '..', 'public', 'images');
export const IMAGES_FOLDER = path.join(PROJECT_ROOT, 'public', 'images');
export const MAX_SESSION_TIME = 60 * 30;

View File

@ -1,4 +1,4 @@
import UserSession from './shared/models/UserSession.js';
import UserSession from '../shared/models/UserSession.js';
/**
* Create a uCaptcha box

View File

@ -54,8 +54,15 @@ async function saveImage(filepath, uri) {
return new Promise((resolve, reject)=>{
Jimp.read(uri)
.then((image)=>{
let x;
if (image.getWidth() / 2 < 400) {
x = 0;
} else {
x = image.getWidth() / 2 - 200;
}
const y = image.getHeight() - 400;
image
.crop(0, 0, 384, 384)
.crop(x, y, 384, 384)
.write(filepath, (err)=>{
if (err) {
reject(err);
@ -75,8 +82,8 @@ async function saveImage(filepath, uri) {
/** @type {geoImageRequest} geoImageRequestDefaults */
const geoImageRequestDefaults = {
lat: 40.6971576,
lng: -83.608754,
lat: 40.624592,
lng: -81.785149,
radius: 5000,
};

22
src/helpers/idb.js Normal file
View File

@ -0,0 +1,22 @@
import redis from 'redis';
/** @type {import('redis').RedisClient | undefined} */
export let client;
/**
* Connect to the instant DB
* @param {Function} cb
*/
export function connect(cb) {
const _client = redis.createClient({
url: process.env.REDIS_URL,
password: process.env.REDIS_PASSWORD,
});
_client.on('ready', ()=>{
client = _client;
cb();
});
_client.on('error', (err)=>{
cb(err);
});
}

View File

@ -19,5 +19,24 @@ function randomBytes(length) {
return chars.join('');
}
/**
* Returns an array of indexes where element
* of `arr` is greater than `threshold`
* @param {Array<number>} arr
* @param {number} threshold
* @return {Array<number>}
*/
function argmaxThresh(arr, threshold) {
const indexes = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= threshold) {
indexes.push(i);
}
}
return indexes;
}
export {randomBytes};
export {
randomBytes,
argmaxThresh,
};

7
src/models/IDBSession.js Normal file
View File

@ -0,0 +1,7 @@
/**
* Session storage in IDB
* @typedef {Object} IDBSession
* @property {string} sessionId User session ID
* @property {string} image Image URL for challenge
* @property {number} score How likely is the user to be a human?
*/

View File

@ -1,12 +1,47 @@
import express from 'express';
import {initializer} from './initializeSession.js';
import path from 'path';
import initializeSession from './initializeSession.js';
import serveImage from './serveImage.js';
import verifyImage from './verifyImage.js';
import sendJson from '../../helpers/sendJson.js';
import {IMAGES_FOLDER} from '../../R.js';
const router = new express.Router();
router.get('/init', async (req, res)=>{
const result = await initializer();
const websiteKey = req.query.k;
if (!websiteKey) {
res.end();
return;
}
const result = await initializeSession(websiteKey);
sendJson(res, result);
});
router.get('/image', async (req, res)=>{
const sessionId = req.query.s;
if (!sessionId) {
res.end();
return;
}
const [suspicious, filename] = await serveImage(sessionId);
if (suspicious) {
res.end();
return;
}
res.sendFile(path.join(IMAGES_FOLDER, filename));
});
router.post('/verify', async (req, res)=>{
const sessionId = req.body.s;
const mat = req.body.mat;
await verifyImage(sessionId, mat);
});
export default router;

View File

@ -4,18 +4,35 @@
import pickRandomFile from './pickRandomFile.js';
import UserSession from '../../shared/models/UserSession.js';
import {randomBytes} from '../../helpers/utils.js';
import {client} from '../../helpers/idb.js';
import {MAX_SESSION_TIME} from '../../R.js';
/**
* @typedef {import('../../models/IDBSession.js').IDBSession} IDBSession
*/
/**
* @param {string} websiteKey
* @return {any}
*/
export async function initializer() {
export default async function initializeSession(websiteKey) {
const randomSessionId = randomBytes(8);
const imageFilepath = await pickRandomFile();
const image = await pickRandomFile();
const initSession = new UserSession();
initSession.setSessionId(randomSessionId);
initSession.setImageUrl(imageFilepath);
const payload = initSession.serialize();
const session = new UserSession();
session.sessionId = randomSessionId;
const userPayload = session.serialize();
return payload;
/** @type {IDBSession} */
const idbPayload = {
sessionId: session.sessionId,
image,
score: 0.5,
};
client.setex(
session.sessionId, MAX_SESSION_TIME, JSON.stringify(idbPayload));
return userPayload;
};

View File

@ -1,24 +1,17 @@
import fs from 'fs';
import path from 'path';
import Jimp from 'jimp';
import {promisify} from 'util';
import {IMAGES_FOLDER} from '../../R.js';
const readdirAsync = promisify(fs.readdir);
/**
* @return {string} Image file path
* @return {Promise<string>} Image file without extension
*/
export default async () => {
const files = await readdirAsync(IMAGES_FOLDER);
return files[Math.floor(Math.random() * files.length)];
const randomFilePath = path.join(
IMAGES_FOLDER,
files[Math.floor(
Math.random() * files.length,
)],
);
return randomFilePath;
export default () => {
return new Promise((reject, resolve)=>{
fs.readdir(IMAGES_FOLDER, (err, files)=>{
if (err) return reject(err);
return resolve(
files[
Math.floor(Math.random() * files.length)
].split('.')[0]);
});
});
};

View File

@ -0,0 +1,44 @@
import {client} from '../../helpers/idb.js';
/**
* @typedef {import('../../models/IDBSession.js').IDBSession} IDBSession
*/
/**
* @param {string} sessionId
* @return {Promise}
*/
export default function(sessionId) {
return new Promise((resolve, reject)=>{
client.get(sessionId, (err, result)=>{
if (err) return reject(err);
/** @type {IDBSession} */
const sessionData = JSON.parse(result);
console.log(sessionData);
const {
sessionId,
image,
score,
} = sessionData;
if (score < 0.1) {
client.del(sessionId, (err)=>{
if (err) return reject(err);
return resolve([
true,
null,
]);
});
}
return resolve([
null,
image + '.jpg',
]);
});
});
}

View File

@ -0,0 +1,60 @@
import fs from 'fs';
import path from 'path';
import {client} from '../../helpers/idb.js';
import {PROJECT_ROOT, MAX_SESSION_TIME} from '../../R.js';
import {argmaxThresh} from '../../helpers/utils.js';
import pickRandomFile from './pickRandomFile.js';
/**
* @typedef {Object} FileMat
* @property {Array<number>} mat Matrix of floats
* @property {number} nums Number of users that have seen this challenge
*/
/**
* Get file matrix
* @param {string} filename
* @return {FileMat}
*/
function getMat(filename) {
const mats = JSON.parse(
fs.readFileSync(
path.join(PROJECT_ROOT, 'dev', 'mat.json'), {encoding: 'utf8'}));
return mats[filename];
}
/**
* @param {string} sessionId
* @param {Array<number>} mat
* @return {Promise}
*/
export default function(sessionId, mat) {
return new Promise((resolve, reject)=>{
client.get(sessionId, (err, result)=>{
if (err) return reject(err);
/** @type {import('../../models/IDBSession.js').IDBSession} */
result = JSON.parse(result);
const imageMat = getMat(result.image);
const trueArgmax = argmaxThresh(imageMat.mat, 0.2).join(',');
const userArgmax = argmaxThresh(mat, 1).join(',');
if (userArgmax !== trueArgmax) {
// TODO: Decrease the score based on mat dispersion
// High variance = small reduction
// Low variance = high reduction
result.score -= 0.2;
}
pickRandomFile().then((image)=>{
client.setex(sessionId, MAX_SESSION_TIME,
Object.assign(result, {image: image}), (err)=>{
if (err) return reject(err);
resolve();
});
});
});
});
}

View File

@ -13,14 +13,30 @@ app.use(express.json({
limit: '128kb',
}));
if (process.env.NODE_ENV === 'development') {
app.use((req, _res, next)=>{
console.log(`${req.method} ${req.url}`);
next();
});
}
import apis from './routes/api/index.js';
app.use('/api', apis);
import {fetchImagesJob, fetchImages} from './helpers/fetchImagesJob.js';
// import {fetchImagesJob} from './helpers/fetchImagesJob.js';
// import {fetchImages} from './helpers/fetchImagesJob.js';
app.listen(8080, ()=>{
fetchImagesJob.start();
// fetchImages({radius: 1000});
console.log('Server started');
console.log('=========================================================');
import {connect as connectToIdb} from './helpers/idb.js';
connectToIdb((err)=>{
if (err) {
console.error(err);
process.exit(1);
}
app.listen(8080, ()=>{
// fetchImagesJob.start();
// fetchImages({radius: 500});
console.log('Server started');
console.log('=========================================================');
});
});

View File

@ -1,15 +1,11 @@
/**
* User session model
* @type {Object} Session Represents a Session Object.
* @property {string | undefined} _sessionIds Internal The Id of the Session
* @property {string | undefined} _imageUrl Internal The Image Url
* @property {string | undefined} imageUrl Public The Image Url
* @property {string | undefined} sessionId Public The Image Url
* @type {Object} Session Object.
* @property {string} _sessionId The session identifier
*/
export class Session {
export class UserSession {
/**
* Set the Session._sessionId
* Set session ID
* @param {string} id
*/
set sessionId(id) {
@ -25,42 +21,26 @@ export class Session {
return this._sessionId;
}
/**
* Set the image URL for the initial challenge
* @param {string} url
*/
set imageUrl(url) {
this._imageUrl = url;
}
/**
* Get the image URL for the initial challenge
* @type {string}
*/
get imageUrl() {
return this._imageUrl;
}
/**
* Serialize the session into a JSON object
* @return {Array<any>}
*/
serialize() {
const {sessionId, imageUrl} = this;
if (!sessionId || !imageUrl) {
const {sessionId} = this;
if (!sessionId) {
throw Error('Unable to serialize because of missing field(s)');
}
return [sessionId, imageUrl];
return [sessionId];
}
/**
* Deserialize session data from JSON object
* @param {Array<any>} payload
*/
deserialize([sessionId, imageUrl]) {
Object.assign(this, {sessionId, imageUrl});
deserialize([sessionId]) {
Object.assign(this, {sessionId});
}
};
export default Session
export default UserSession
;

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<title>uCaptcha Test</title>
<script src="../dist/bundle.js"></script>
<script src="../dist/client/bundle.js"></script>
</head>
<body>

View File

@ -1,67 +0,0 @@
{
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ESNEXT", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
"checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["src/**/*.js"]
}