添加了内容审核支持防止被ban掉交不了作业

This commit is contained in:
2023-12-17 02:59:53 +00:00
parent eceace3785
commit 9283940259
7 changed files with 197 additions and 15 deletions

View File

@ -2,6 +2,8 @@ const express = require('express');
const { createServer } = require('node:http');
const { join } = require('node:path');
const { Server } = require('socket.io');
const IsValid=require('./validator.js');
const Validing=process.env['VALIDING'];
const app = express();
const server = createServer(app);
@ -46,6 +48,12 @@ async function GetResult(session_token,operation_token) {
{
const ret=message_map.get(session_token).get(operation_token);
message_map.get(session_token).delete(operation_token);
if(Validing=='True'){
if(!(await IsValid(ret)))
{
ret="Invalid Content";
}
}
return ret;
}
}
@ -124,6 +132,13 @@ io.on('connection', async (socket) => {
});
socket.on('request', async (msg) => {
console.log('message: ' + msg);
if(Validing=='True'){
if(!(await IsValid(msg)))
{
socket.emit('response', "Invalid Input");
return;
}
}
const substrings = msg.trim().split('\n')[0].split(' ');
const head=substrings[0];
const session_token=substrings[1];

View File

@ -95,16 +95,13 @@
var username = document.getElementById("user_name").value;
var password = document.getElementById("password").value;
var ret=await Request("su "+username+" "+password);
if(ret=="Invalid")
{
alert("Invalid username or password");
}
else
if(ret=="[empty]")
{
await UpdateUserInfo();
document.querySelector('.content').innerHTML = '<div class="info-box"><h2>Login Success</h2><p>Redirecting to home page in 3 seconds...</p></div>';
setTimeout(function(){window.location.href="/";},3000);
}
else alert("Invalid username or password");
}
</script>
<script src="/sessioninit.js"></script>

View File

@ -9,6 +9,7 @@
"version": "0.0.0.1",
"license": "ISC",
"dependencies": {
"@alicloud/pop-core": "^1.7.13",
"async-lock": "^1.4.0",
"async-mutex": "^0.4.0",
"express": "^4.18.2",
@ -16,6 +17,34 @@
"socket.io": "^4.7.2"
}
},
"node_modules/@alicloud/pop-core": {
"version": "1.7.13",
"resolved": "https://registry.npmjs.org/@alicloud/pop-core/-/pop-core-1.7.13.tgz",
"integrity": "sha512-agzr4DU+aAGW7/2mp2hP1JcNJkn/zBS0jUGQt5etIASN0MVq1tMdudVqvWbExUG0mUouo/n2VgdnjOHjswvrlA==",
"dependencies": {
"debug": "^3.1.0",
"httpx": "^2.1.2",
"json-bigint": "^1.0.0",
"kitx": "^1.2.1",
"xml2js": "^0.5.0"
},
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/@alicloud/pop-core/node_modules/debug": {
"version": "3.2.7",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz",
"integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
"dependencies": {
"ms": "^2.1.1"
}
},
"node_modules/@alicloud/pop-core/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
},
"node_modules/@socket.io/component-emitter": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.1.0.tgz",
@ -80,6 +109,14 @@
"node": "^4.5.0 || >= 5.9"
}
},
"node_modules/bignumber.js": {
"version": "9.1.2",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.2.tgz",
"integrity": "sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==",
"engines": {
"node": "*"
}
},
"node_modules/body-parser": {
"version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
@ -455,6 +492,36 @@
"node": ">= 0.8"
}
},
"node_modules/httpx": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/httpx/-/httpx-2.3.1.tgz",
"integrity": "sha512-l5rcAoKP8A9XOIlcIA87Wt9A7AX2fgOslHOF4WB5Q24y/1+aeH8b7c7NKfm+Bcf+h0u4FHNtLCriC4mAFmCYgg==",
"dependencies": {
"@types/node": "^20",
"debug": "^4.1.1"
}
},
"node_modules/httpx/node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
"integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
"dependencies": {
"ms": "2.1.2"
},
"engines": {
"node": ">=6.0"
},
"peerDependenciesMeta": {
"supports-color": {
"optional": true
}
}
},
"node_modules/httpx/node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node_modules/iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
@ -479,6 +546,19 @@
"node": ">= 0.10"
}
},
"node_modules/json-bigint": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz",
"integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==",
"dependencies": {
"bignumber.js": "^9.0.0"
}
},
"node_modules/kitx": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/kitx/-/kitx-1.3.0.tgz",
"integrity": "sha512-fhBqFlXd0GkKTB+8ayLfpzPUw+LHxZlPAukPNBD1Om7JMeInT+/PxCAf1yLagvD+VKoyWhXtJR68xQkX/a0wOQ=="
},
"node_modules/media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
@ -660,6 +740,11 @@
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"node_modules/sax": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.3.0.tgz",
"integrity": "sha512-0s+oAmw9zLl1V1cS9BtZN7JAd0cW5e0QH4W3LWEK6a4LaLEA2OTpGYWDY+6XasBLtz6wkm3u1xRw95mRuJ59WA=="
},
"node_modules/send": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/send/-/send-0.18.0.tgz",
@ -894,6 +979,26 @@
"optional": true
}
}
},
"node_modules/xml2js": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.5.0.tgz",
"integrity": "sha512-drPFnkQJik/O+uPKpqSgr22mpuFHqKdbS835iAQrUC73L2F5WkboIRd63ai/2Yg6I1jzifPFKH2NTK+cfglkIA==",
"dependencies": {
"sax": ">=0.6.0",
"xmlbuilder": "~11.0.0"
},
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/xmlbuilder": {
"version": "11.0.1",
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz",
"integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==",
"engines": {
"node": ">=4.0"
}
}
}
}

View File

@ -11,6 +11,7 @@
"license": "ISC",
"type": "commonjs",
"dependencies": {
"@alicloud/pop-core": "^1.7.13",
"async-lock": "^1.4.0",
"async-mutex": "^0.4.0",
"express": "^4.18.2",

View File

@ -104,17 +104,14 @@
var old_password = document.getElementById("old_password").value;
var new_password = document.getElementById("new_password").value;
var ret=await Request("passwd "+username+" "+old_password+" "+new_password);
if(ret=="Invalid")
{
alert("Invalid username or old password");
}
else
if(ret=="[empty]")
{
// 删除注册框,在中间显示“注册成功”,三秒后自动跳转到首页
document.querySelector('.passwd-box').style.display = 'none';
document.querySelector('.content').innerHTML = '<div class="info-box"><h2>Successfully changed password</h2><p>Redirecting to home page in 3 seconds...</p></div>';
setTimeout(function(){window.location.href="/";},3000);
}
else alert("Invalid username or old password");
}
</script>
<script src="/sessioninit.js"></script>

View File

@ -98,17 +98,14 @@
var password = document.getElementById("password").value;
var nick_name = document.getElementById("nickname").value;
var ret=await Request("register "+username+" "+password+" "+nick_name);
if(ret=="Invalid")
{
alert("Invalid username or password");
}
else
if(ret=="[empty]")
{
// 删除注册框,在中间显示“注册成功”,三秒后自动跳转到首页
document.querySelector('.register-box').style.display = 'none';
document.querySelector('.content').innerHTML = '<div class="info-box"><h2>Register Success</h2><p>Redirecting to home page in 3 seconds...</p></div>';
setTimeout(function(){window.location.href="/";},3000);
}
else alert("Invalid username or password");
}
</script>
<script src="/sessioninit.js"></script>

70
frontend/Web/validator.js Normal file
View File

@ -0,0 +1,70 @@
const RPCClient = require("@alicloud/pop-core");
console.log(process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'])
console.log(process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
// 注意此处实例化的client请尽可能重复使用避免重复建立连接提升检测性能。
let client = new RPCClient({
/**
* 阿里云账号AccessKey拥有所有API的访问权限建议您使用RAM用户进行API访问或日常运维。
* 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里否则可能导致AccessKey泄露威胁您账号下所有资源的安全。
* 常见获取环境变量方式:
* 获取RAM用户AccessKey IDprocess.env['ALIBABA_CLOUD_ACCESS_KEY_ID']
* 获取RAM用户AccessKey Secretprocess.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
*/
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
// 接入区域和地址请根据实际情况修改
endpoint: "https://green-cip.cn-beijing.aliyuncs.com",
apiVersion: '2022-03-02',
// 设置http代理
// httpProxy: "http://xx.xx.xx.xx:xxxx",
// 设置https代理
// httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
});
async function IsValid(text) {
console.log("IsValid called");
console.log("cheching text: "+text);
// 去除text中的空格、tab、无意义特殊符号!@#$%^&*()_+_=~`{}|[]\;':",./<>?)等。
//text = text.replace(/\s+/g, "");
text = text.replace(/[!@#$%^&*()_+_=~`{}|[\]\\;':",./<>?]+/g, "");
// 再去除换行和回车
//text = text.replace(/[\r\n]/g, "");
console.log("after replace: "+text);
// 通过以下代码创建API请求并设置参数。
const params = {
// 文本检测service内容安全控制台文本增强版规则配置的serviceCode示例chat_detection
"Service": "chat_detection",
"ServiceParameters": JSON.stringify({
//待检测文本内容。
"content": text
}),
};
const requestOption = {
method: 'POST',
formatParams: false,
};
let response;
try {
// 调用接口获取检测结果。
response = await client.request('TextModeration', params, requestOption);
// 自动路由。
if (response.Code === 500) {
// 区域切换到cn-beijing。
client.endpoint = "https://green-cip.cn-shanghai.aliyuncs.com";
response = await client.request('TextModeration', params, requestOption);
}
} catch (err) {
console.log(err);
}
if(response['Message']=='OK')
{
console.log(response['Data']['reason']);
return response['Data']['reason']=="";
}
return false;
}
module.exports=IsValid;