7e2544ea by Administrator

Merge branch '31466_limited_usage_chatbot+' into 'master'

31466_limited_usage_chatbot+

See merge request !119
2 parents 74c76309 cd44b1b5
Pipeline #29831 for 7e2544ea passed in 16 seconds
Showing 1 changed file with 33 additions and 4 deletions
......@@ -154,7 +154,7 @@ app.post('/api', async (req, res) => {
console.log('Error encoding prompt text', e);
}
let usage_params = {
"aiwp_logged_in": aiwp_logged_in, "app": "chatbot+", "prompt_token": usage.prompt_tokens, "total_token": usage.total_tokens, "aiwp_app_id":aiwp_app_id , "usage_tries": TRIED_USAGE
"aiwp_logged_in": aiwp_logged_in, "app": "chatbot+", "prompt_token": usage.prompt_tokens, "total_token": usage.total_tokens, "aiwp_app_id":aiwp_app_id , "usage_tries": TRIED_USAGE, ipdflu: getClientIP(req)
};
if(IS_FREE_USER) {
await setUsage(usage_params);
......@@ -236,7 +236,7 @@ async function runGPTTurbo(req, res) {
console.log('Error encoding prompt text', e);
}
let usage_params = {
"aiwp_logged_in": aiwp_logged_in, "app": "chatbot+", "prompt_token": usage.prompt_tokens, "total_token": usage.total_tokens, "aiwp_app_id":aiwp_app_id , "usage_tries": TRIED_USAGE
"aiwp_logged_in": aiwp_logged_in, "app": "chatbot+", "prompt_token": usage.prompt_tokens, "total_token": usage.total_tokens, "aiwp_app_id":aiwp_app_id , "usage_tries": TRIED_USAGE, ipdflu: getClientIP(req)
};
if(IS_FREE_USER) {
await setUsage(usage_params);
......@@ -333,7 +333,7 @@ async function runOpensource(req, res) {
console.log('Error encoding prompt text', e);
}
let usage_params = {
"aiwp_logged_in": aiwp_logged_in, "app": "chatbot+", "prompt_token": usage.prompt_tokens, "total_token": usage.total_tokens, "aiwp_app_id":aiwp_app_id , "usage_tries": TRIED_USAGE
"aiwp_logged_in": aiwp_logged_in, "app": "chatbot+", "prompt_token": usage.prompt_tokens, "total_token": usage.total_tokens, "aiwp_app_id":aiwp_app_id , "usage_tries": TRIED_USAGE, ipdflu: getClientIP(req)
};
if(IS_FREE_USER) {
await setUsage(usage_params);
......@@ -425,6 +425,7 @@ async function validation (aiwp_app_id, req, res) {
const limit = req.cookies["WcvYPABR"] ? parseInt(req.cookies["WcvYPABR"].replace(/\D/g, '')) : 3;
let IS_FREE_USER = false;
let TRIED_USAGE = 0;
const ip_address = getClientIP(req);
if (aiwp_logged_in) {
let auth = await authenticate({ "aiwp_logged_in": aiwp_logged_in, "user_event_data": {}, "user_event": "endpoint" });
......@@ -457,7 +458,7 @@ async function validation (aiwp_app_id, req, res) {
}
if(IS_FREE_USER) {
let data = await getLimitedUsage({
"aiwp_app_id": aiwp_app_id
"aiwp_app_id": aiwp_app_id, ipdflu: ip_address
});
if (data.usage !== null) {
......@@ -474,6 +475,34 @@ async function validation (aiwp_app_id, req, res) {
return { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE };
};
const getClientIP = (req) => {
let ipAddress = '';
// Order of checking headers for client IP
if (req.headers['x-forwarded-for']) {
ipAddress = Array.isArray(req.headers['x-forwarded-for'])
? req.headers['x-forwarded-for'][0]
: req.headers['x-forwarded-for'];
} else if (req.headers['x-client-ip']) {
ipAddress = req.headers['x-client-ip'];
} else if (req.headers['x-cluster-client-ip']) {
ipAddress = req.headers['x-cluster-client-ip'];
} else if (req.headers['x-forwarded']) {
ipAddress = req.headers['x-forwarded'];
} else if (req.headers['forwarded-for']) {
ipAddress = req.headers['forwarded-for'];
} else if (req.headers['forwarded']) {
ipAddress = req.headers['forwarded'];
} else {
ipAddress = req.socket?.remoteAddress || '127.0.0.2';
}
// Split the IP address in case of multiple entries in 'x-forwarded-for'
const arrIp = ipAddress.split(',');
return arrIp[0] || '127.0.0.2';
};
// Get Models Route
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!