init
This commit is contained in:
14
src/config.js
Normal file
14
src/config.js
Normal file
@ -0,0 +1,14 @@
|
||||
export const config = {
|
||||
database: {
|
||||
url: 'mongodb://localhost:27017',
|
||||
db: 'yggdrasil'
|
||||
},
|
||||
server: {
|
||||
port: 3000,
|
||||
url: '',
|
||||
},
|
||||
signing: {
|
||||
public: '/path/to/public.pem',
|
||||
private: '/path/to/private.key'
|
||||
}
|
||||
}
|
11
src/generator.js
Normal file
11
src/generator.js
Normal file
@ -0,0 +1,11 @@
|
||||
import * as Crypto from 'crypto'
|
||||
import { server } from './index.js'
|
||||
|
||||
export function generateToken(username) {
|
||||
const key = `${Date.now()}-${ Crypto.createHash('sha256').update(Crypto.randomBytes(32)).digest('hex')}-${username}-lsp-${Math.random() * 1.048596}`
|
||||
const token = Crypto.createHash('sha256').update(key).digest('hex')
|
||||
|
||||
server.log.info(`随机生成器 > 为玩家 ${username} 生成令牌: ${token} | 随机 key = ${key}`)
|
||||
|
||||
return token
|
||||
}
|
21
src/hooks.js
Normal file
21
src/hooks.js
Normal file
@ -0,0 +1,21 @@
|
||||
import { server } from './index.js'
|
||||
|
||||
export async function headerValidation(req, rep) {
|
||||
if(!(/(authserver)|(sessionserver)|(api)/g).test(req.url)) {
|
||||
return
|
||||
}
|
||||
|
||||
if(Object.keys(req.headers).some(key => {
|
||||
return key.toLowerCase() === "content-type" && req.headers[key].toLowerCase() !== "application/json"
|
||||
})) {
|
||||
return rep.code(400).send({
|
||||
error: "IllegalArgumentException",
|
||||
errorMessage: "请求内容不正确",
|
||||
cause: "请求头中 Content-Type 不是 application/json"
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export async function deserilize(req, payload) {
|
||||
return JSON.parse(payload)
|
||||
}
|
32
src/index.js
Normal file
32
src/index.js
Normal file
@ -0,0 +1,32 @@
|
||||
import { fastify } from 'fastify'
|
||||
import { mongoose } from 'mongoose'
|
||||
import { generateToken } from './generator.js'
|
||||
import { registerModels } from './models/index.js';
|
||||
import * as Hooks from './hooks.js'
|
||||
|
||||
export const server = fastify({
|
||||
logger: {
|
||||
prettyPrint: true
|
||||
}
|
||||
})
|
||||
|
||||
//export const logger = server.log
|
||||
|
||||
;(async () => {
|
||||
const { config } = await import('./config.js')
|
||||
|
||||
server.decorate('config', config)
|
||||
|
||||
const mongooseClient = await mongoose.connect(config.database.url)
|
||||
const models = registerModels(mongooseClient)
|
||||
|
||||
server.decorate('mongoose', mongooseClient)
|
||||
server.decorate('models', models)
|
||||
|
||||
server.addHook('preHandler', Hooks.headerValidation)
|
||||
|
||||
|
||||
await server.listen(config.server.port, config.server.url)
|
||||
|
||||
server.log.info("老色批世界树 > 基于 fastify 的高性能 HTTP 服务器已启动")
|
||||
})()
|
9
src/models/index.js
Normal file
9
src/models/index.js
Normal file
@ -0,0 +1,9 @@
|
||||
import { TokenSchema } from "./token.js";
|
||||
import { PlayerSchema } from "./player.js";
|
||||
|
||||
export function registerModels(mongoose) {
|
||||
return {
|
||||
Token: mongoose.model("Token", TokenSchema),
|
||||
Player: mongoose.model("Player", PlayerSchema),
|
||||
}
|
||||
}
|
15
src/models/player.js
Normal file
15
src/models/player.js
Normal file
@ -0,0 +1,15 @@
|
||||
import mongoose from 'mongoose'
|
||||
const { Schema } = mongoose
|
||||
|
||||
export const PlayerSchema = new Schema({
|
||||
username: String,
|
||||
password: String,
|
||||
email: String,
|
||||
uuid: String,
|
||||
textures: {
|
||||
skin: String,
|
||||
cape: String,
|
||||
},
|
||||
registerDate: Number,
|
||||
permissions: [{ node: String, allowed: Boolean, duration: Number, eternal: Boolean, startDate: Number, highPriority: Boolean }], // ban -> true
|
||||
})
|
10
src/models/token.js
Normal file
10
src/models/token.js
Normal file
@ -0,0 +1,10 @@
|
||||
import mongoose from 'mongoose'
|
||||
const { Schema } = mongoose
|
||||
|
||||
export const TokenSchema = new Schema({
|
||||
uuid: String,
|
||||
token: String,
|
||||
expireDate: Number,
|
||||
deadDate: Number,
|
||||
state: String, // alive, linbo, dead
|
||||
})
|
0
src/routes/authenticate.js
Normal file
0
src/routes/authenticate.js
Normal file
Reference in New Issue
Block a user