explode-self-service/server.ts

152 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-06-17 16:20:51 +02:00
import express, { Request, Response } from "express"
import {
json as bodyParserJson,
urlencoded as bodyParserUrlEncoded,
} from "body-parser"
2023-06-17 16:20:51 +02:00
import cookieParser from "cookie-parser"
import { Axios } from "axios"
2023-06-17 18:29:44 +02:00
import * as fs from "fs"
const sw = require("js-sensitivewords")
2023-12-10 07:48:54 +01:00
/**
* The server address without graphql path.
*/
const serverAddr = "http://47.97.110.29:10443"
/*
2023 (c) Explode Team
This script is licensed under MIT.
This script is used to create a user-friendly website for registering the accounts of Dynamite Explode,
with ability to block users with invalid or illegal names.
*/
2023-06-17 18:29:44 +02:00
initSensitiveWordList()
2023-06-17 16:20:51 +02:00
const app = express()
app.use(bodyParserJson())
app.use(bodyParserUrlEncoded({ extended: true }))
app.use(cookieParser())
app.use(express.static(__dirname + "/public"))
app.get("/", (req: Request, res: Response) => {
res.render("layout")
2023-06-17 16:20:51 +02:00
})
app.post("/register", async (req: Request, res: Response) => {
try {
2023-06-17 16:20:51 +02:00
let isRegistered = req.cookies.isRegistered
if (isRegistered) {
return res.send("服务器正在探索:利维坦,涅索斯轨道(请勿重复注册!)")
2023-06-17 16:20:51 +02:00
}
let username: string = req.body.RegisterUsername
let password: string = req.body.RegisterPassword
if (!username || !password) {
return res.status(400).send("注册失败:无效的用户名或密码!")
2023-06-17 16:20:51 +02:00
}
if (sw.containsDfa(username)) {
return res.status(500).send("服务器正在探索:过往灾祸,欧洲无人区(名字或密码带有敏感内容!)")
2023-06-17 18:29:44 +02:00
}
if (username.length < 4) {
return res.status(400).send("账号太短啦~")
2023-06-17 16:20:51 +02:00
}
if (password.length < 4) {
return res.status(400).send("密码太短啦~")
2023-06-17 16:20:51 +02:00
}
if (await explode_does_user_exists(username)) {
return res.status(400).send("名字撞车啦!")
2023-06-17 16:20:51 +02:00
}
let hasId = await explode_register(username, password)
if (hasId != null) {
// 使用 cookie 避免重复注册
res.cookie("isRegistered", "1", {
expires: new Date(Date.now() + 9000000),
httpOnly: true,
})
return res.send(`注册成功用户ID${hasId._id}。要记住哦!`)
2023-06-17 16:20:51 +02:00
} else {
return res.status(500).send("出错啦,注册失败了。")
2023-06-17 16:20:51 +02:00
}
} catch (e) {
2023-12-10 07:48:54 +01:00
console.error(e)
return res.status(500).send("服务器错误,请联系管理员!")
}
2023-06-17 16:20:51 +02:00
})
2023-06-17 19:05:43 +02:00
const server = app.listen(31234, () => console.log("Server is running..."))
2023-06-17 16:20:51 +02:00
2023-06-17 18:29:44 +02:00
////////// Interact with Explode Server
2023-06-17 16:20:51 +02:00
const _axios = new Axios({})
type HasId = {
_id: string
2023-06-17 16:20:51 +02:00
}
async function explode_graphql<T>(query: string) {
return _axios.post<T>(
2023-12-10 07:48:54 +01:00
`${serverAddr}/graphql`,
// "http://192.168.1.199:10443/graphql",
JSON.stringify({
operationName: "",
query: query,
variables: {},
}),
{
// idk why is this happening, but it just happened that
// the response is string which should be transformed to JSON auto.
transformResponse: (resp) => JSON.parse(resp),
}
)
2023-06-17 16:20:51 +02:00
}
async function explode_register(username: string, password: string) {
2023-12-10 07:48:54 +01:00
console.log(`Registering ${username}`)
let response = await explode_graphql<{
data: {
registerUser: HasId
}
}>(
`mutation { registerUser(username: "${username}", password: "${password}") { _id } }`
)
2023-06-17 16:20:51 +02:00
return response.data.data.registerUser
2023-06-17 16:20:51 +02:00
}
async function explode_does_user_exists(username: string) {
let response = await explode_graphql<{
data: {
userByUsername: HasId | undefined
}
}>(`query { userByUsername(username: "${username}") { _id } }`)
2023-06-17 16:20:51 +02:00
return !!response.data.data.userByUsername
2023-06-17 18:29:44 +02:00
}
////////// The Sensitive Words Blocking
async function initSensitiveWordList() {
console.log("Loading sensitive words")
let words = fs
.readFileSync(
__dirname + "/tencent-sensitive-words/sensitive_words_lines.txt",
{ encoding: "utf-8" }
)
.split("\n")
sw.addWords(words)
}