From 6b2bde5938849c89a24c328233612cdd6d3945b4 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Wed, 8 May 2024 15:07:15 +0800 Subject: [PATCH] add code for fake data --- configs/fake_address.json | 3 + package.json | 1 + src/common/Utils.ts | 42 +++++++++ src/fixdata.ts | 68 ++++++++++++++ src/models/ctrl/FixInfo.ts | 32 +++++++ src/models/ctrl/FixRecord.ts | 27 ++++++ src/services/fix.svr.ts | 177 +++++++++++++++++++++++++++++++++++ 7 files changed, 350 insertions(+) create mode 100644 configs/fake_address.json create mode 100644 src/fixdata.ts create mode 100644 src/models/ctrl/FixInfo.ts create mode 100644 src/models/ctrl/FixRecord.ts create mode 100644 src/services/fix.svr.ts diff --git a/configs/fake_address.json b/configs/fake_address.json new file mode 100644 index 0000000..3d445e6 --- /dev/null +++ b/configs/fake_address.json @@ -0,0 +1,3 @@ +[ + "" +] \ No newline at end of file diff --git a/package.json b/package.json index 11ed879..6d15656 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "repairredis": "ts-node -r tsconfig-paths/register src/repairredis.ts", "checkredis": "ts-node -r tsconfig-paths/register src/checkredis.ts", "additem": "ts-node -r tsconfig-paths/register src/addboxdata.ts", + "fixdata": "ts-node -r tsconfig-paths/register src/fixdata.ts", "test:watch": "jest --watch", "test": "jest" }, diff --git a/src/common/Utils.ts b/src/common/Utils.ts index bcfbfae..c1bfcb6 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -29,3 +29,45 @@ export const formatNumShow = (num: number) => { return num.toFixed(1) } } + +export const randomUserAgent = () => { + // 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.97 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', + // 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', + let n1 = Math.floor(Math.random() * 15) + 70 + let n2 = Math.floor(Math.random() * 100) + let osName = Math.random() > 0.5 ? 'Windows NT 10.0' : 'Macintosh; Intel Mac OS X 10_15_7' + let s0 = `Mozilla/5.0 (${osName}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/` + let s2 = `.0.3029.` + let s3 = 'Safari/537.46' + return `${s0}${n1}${s2}${n2}${s3}` +} + +export const randomIp = () => { + let n1 = Math.floor(Math.random() * 254) + let n2 = Math.floor(Math.random() * 254) + let n3 = Math.floor(Math.random() * 254) + let n4 = Math.floor(Math.random() * 254) + let ip1 = `${n1}.${n2}.${n3}.${n4}` + let ip2 = Math.random() > 0.5 ? '10.0.2.69' : '10.0.0.80' + return `${ip1}, ${ip2}` +} + +// 0xd1c7bad0033678f3e347076e5db7290f990d2fcd69369a95918db6d8c6d87c3021fa2334da8cdacb99bdf311ff2087e1b7f5c8f4d8fdb0a653bfeae0a9ebbda41c +// get 132 random hex string with 0x prefix +export const randomSign = () => { + let hex = '0x' + for (let i = 0; i < 64; i++) { + hex += Math.floor(Math.random() * 16).toString(16) + hex += Math.floor(Math.random() * 16).toString(16) + } + hex += Math.random() > 0.5 ? '1b' : '1c' + return hex +} diff --git a/src/fixdata.ts b/src/fixdata.ts new file mode 100644 index 0000000..51eb298 --- /dev/null +++ b/src/fixdata.ts @@ -0,0 +1,68 @@ +import * as dotenv from 'dotenv' + +const envFile = process.env.NODE_ENV && process.env.NODE_ENV === 'production' ? `.env.production` : '.env.development' +dotenv.config({ path: envFile }) +console.log(process.env.DB_MAIN) +import mongoose from 'mongoose' +import { CheckIn } from 'models/chain/CheckIn' +import { toEIP55 } from 'zutils/utils/chain.util' +import { inertUser, insertWalletLoginLog } from 'services/fix.svr' + +const dbMain = mongoose.createConnection(process.env.DB_MAIN) +const dbChain = mongoose.createConnection(process.env.DB_CHAIN) +const dbCtrl = mongoose.createConnection(process.env.DB_CTRL) + +const ACTIVITY = 'uaw_activity' + +const insertLoginRecord = async () => { + console.log('insertLoginRecord') +} + +const insertCheckinRecord = async () => { + console.log('insertCheckinRecord') +} + +const insertExploreRecord = async () => { + console.log('insertExploreRecord') +} + +const insertOpenBoxRecord = async () => { + console.log('insertOpenBoxRecord') +} + +const insertTaskRecord = async () => { + console.log('insertTaskRecord') +} + +const parseOneRecord = async (record: any) => { + let addressEip55 = toEIP55(record.from) + console.log('parseOneRecord', addressEip55) + const user = await dbCtrl.collection('users_test').findOne({ address: addressEip55 }) + if (!user) { + // 如果没有用户记录,插入用户记录, 并插入一条钱包登录记录(在插入用户记录之前) + const { uid, timestamp } = await inertUser(dbCtrl, addressEip55, record.blockTime) + await insertWalletLoginLog(dbCtrl, addressEip55, uid, timestamp - Math.floor(Math.random() * 2)) + } + // 插入签到记录 +} + +;(async () => { + try { + let beginBlocktime = 1713583747 + // for await (const doc of User.find().cursor()) { + // console.log(doc.name); + // } + let idx = 0 + await CheckIn.find({ blockTime: { $gt: beginBlocktime } }) + .sort({ _id: 1 }) + .cursor() + .eachAsync(parseOneRecord) + console.log('done', idx) + // await dbMain.collection('test1').insertOne({ name: 'test' }) + // await dbChain.collection('test2').insertOne({ name: 'test' }) + } catch (e) { + console.log(e) + } + console.log('end') + process.exit(0) +})() diff --git a/src/models/ctrl/FixInfo.ts b/src/models/ctrl/FixInfo.ts new file mode 100644 index 0000000..5d0f6d5 --- /dev/null +++ b/src/models/ctrl/FixInfo.ts @@ -0,0 +1,32 @@ +import { getModelForClass, index, modelOptions, prop, Severity } from '@typegoose/typegoose' +import { dbconn } from 'decorators/dbconn' +import { BaseModule } from '../Base' + +@dbconn('ctrl') +@index({ address: 1 }, { unique: true }) +@index({ uid: 1 }, { unique: true }) +@modelOptions({ + schemaOptions: { collection: 'fix_info', timestamps: true }, + options: { allowMixed: Severity.ALLOW }, +}) +export class FixInfoClass extends BaseModule { + @prop({ required: true }) + public address: string + + @prop({ required: true }) + public uid: string + + @prop() + public wallet: string + + @prop() + public ip: string + + @prop() + public user_agent: string + + @prop({ default: 0 }) + public version: number +} + +export const FixInfo = getModelForClass(FixInfoClass, { existingConnection: FixInfoClass.db }) diff --git a/src/models/ctrl/FixRecord.ts b/src/models/ctrl/FixRecord.ts new file mode 100644 index 0000000..d6519cd --- /dev/null +++ b/src/models/ctrl/FixRecord.ts @@ -0,0 +1,27 @@ +import { getModelForClass, index, modelOptions, mongoose, prop, ReturnModelType, Severity } from '@typegoose/typegoose' +import { dbconn } from 'decorators/dbconn' +import { BaseModule } from '../Base' + +@dbconn('ctrl') +@index({ address: 1 }, { unique: false }) +@index({ address: 1, action: 1 }, { unique: false }) +@index({ address: 1, action: 1, dataId: 1 }, { unique: false }) +@modelOptions({ + schemaOptions: { collection: 'fix_record', timestamps: true }, + options: { allowMixed: Severity.ALLOW }, +}) +export class FixRecordClass extends BaseModule { + @prop({ required: true }) + public address: string + + @prop() + public action: string + + @prop() + public dataId: string + + @prop({ type: mongoose.Schema.Types.Mixed }) + public outData: any +} + +export const FixRecord = getModelForClass(FixRecordClass, { existingConnection: FixRecordClass.db }) diff --git a/src/services/fix.svr.ts b/src/services/fix.svr.ts new file mode 100644 index 0000000..de24a33 --- /dev/null +++ b/src/services/fix.svr.ts @@ -0,0 +1,177 @@ +import { BASE52_ALPHABET } from 'common/Constants' +import { randomIp, randomSign, randomUserAgent } from 'common/Utils' +import { FixInfo } from 'models/ctrl/FixInfo' +import { convert } from 'zutils/utils/number.util' + +const ACTIVITY = 'uaw_activity' +const { ObjectId } = require('mongodb') + +export const generateShareCode = id => { + // 取ObjectId的time和inc段, + // 将time段倒序(倒序后, 如果以0开始, 则移除0, 随机拼接一个hex字符), 然后拼接inc段, 再转换成52进制 + let timeStr = id.slice(0, 8).split('').reverse().join('') + if (timeStr.indexOf('0') === 0) { + let randomStr = convert({ numStr: ((Math.random() * 51) | (0 + 1)) + '', base: 10, to: 52 }) + timeStr = randomStr + timeStr.slice(1) + } + let shortId = timeStr + id.slice(-6) + return convert({ numStr: shortId, base: 16, to: 52, alphabet: BASE52_ALPHABET }) +} + +export const generateObjectId = (timestamp: number) => { + // get last 3 bytes of tmp objectid + const tmp = new ObjectId().toString().slice(-6) + return new ObjectId(timestamp.toString(16) + 'ea158e7e49' + tmp) +} + +export const inertUser = async (db: any, address: string, timestamp: number) => { + // 根据blockTime, 随机往前推1小时内的时间, 作为用户记录创建时间 + const createTimestamp = timestamp - Math.floor(Math.random() * 3600) + const id = generateObjectId(createTimestamp) + const shareCode = generateShareCode(id.toString()) + const createdAt = new Date(createTimestamp * 1000 + Math.floor(Math.random() * 1000)) + await db.collection('users_test').insertOne({ + _id: id, + activity: ACTIVITY, + address, + __v: 0, + boost: 1, + createdAt, + inWhiteList: false, + locked: false, + taskProgress: [ + { + id: 'e2yhq2lj30vwcpedv7p', + task: 'TwitterConnect', + status: 0, + }, + { + id: 'e2fclylj30vwcpe0szl', + task: 'TwitterFollow', + status: 0, + }, + { + id: 'e2far3lj30vwcpe0mh7', + task: 'DiscordConnect', + status: 0, + }, + { + id: 'e2far3lj30vwcpe0mf8', + task: 'DiscordJoin', + status: 0, + }, + { + id: 'e2fuah0j30vwcpe0my7', + task: 'TwitterRetweet', + status: 0, + }, + { + id: 'e2fuah0j30vwcpe0my9', + task: 'TwitterLike', + status: 0, + }, + ], + updatedAt: createdAt, + inviteCode: shareCode, + lastLogin: createdAt, + }) + return { uid: id, timestamp: createTimestamp } +} + +export const insertWalletLoginLog = async (db: any, address: string, uid: string, timestamp: number) => { + let record = await FixInfo.findOne({ + address: address.toLowerCase(), + }) + if (!record) { + record = new FixInfo({ + address: address.toLowerCase(), + uid, + ip: randomIp(), + user_agent: randomUserAgent(), + wallet: Math.random() > 0.5 ? 'okx' : 'metamask', + version: 0, + }) + await record.save() + } + let nonce = generateObjectId(timestamp - Math.floor(Math.random() * 30)).toString() + const params = { + message: { + domain: 'treasure.counterfire.games', + address, + chainId: 214, + uri: 'https://treasure.counterfire.games', + version: '1', + statement: 'CF UAW', + nonce, + issuedAt: new Date(timestamp * 1000).toISOString(), + }, + signature: randomSign(), + activity: ACTIVITY, + } + const timestamp2 = timestamp - Math.floor(Math.random() * 20) + const createdAt = new Date(timestamp2 * 1000) + await db.collection('user_login_record').insertOne({ + _id: generateObjectId(timestamp + Math.floor(Math.random() * 10)), + user: uid, + wallet: record.wallet, + activity: ACTIVITY, + user_agent: record.user_agent, + ip: record.ip, + createdAt: createdAt, + updatedAt: createdAt, + _v: 0, + }) + return insertUserLog({ + db, + user: '', + timestamp, + name: 'wallet_login', + method: 'POST', + path: '/api/wallet/login', + referer: '', + userAgent: record.user_agent, + ip: record.ip, + params, + }) +} + +export const insertUserLog = async ({ + db, + user, + timestamp, + name, + method, + path, + referer, + userAgent, + ip, + params, +}: { + db: any + user: string + timestamp: number + name: string + method: string + path: string + referer: string + userAgent: string + ip: string + params: any +}) => { + const id = generateObjectId(timestamp) + const createdAt = new Date(timestamp * 1000 + Math.floor(Math.random() * 1000)) + await db.collection('user_logs').insertOne({ + _id: id, + user, + name, + method, + path, + referer, + user_agent: userAgent, + ip, + params, + createdAt, + updatedAt: createdAt, + __v: 0, + }) +}