31466_limited_usage_chatbot+
Showing
1 changed file
with
34 additions
and
5 deletions
| ... | @@ -154,7 +154,7 @@ app.post('/api', async (req, res) => { | ... | @@ -154,7 +154,7 @@ app.post('/api', async (req, res) => { |
| 154 | console.log('Error encoding prompt text', e); | 154 | console.log('Error encoding prompt text', e); |
| 155 | } | 155 | } |
| 156 | let usage_params = { | 156 | let usage_params = { |
| 157 | "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 | 157 | "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) |
| 158 | }; | 158 | }; |
| 159 | if(IS_FREE_USER) { | 159 | if(IS_FREE_USER) { |
| 160 | await setUsage(usage_params); | 160 | await setUsage(usage_params); |
| ... | @@ -236,7 +236,7 @@ async function runGPTTurbo(req, res) { | ... | @@ -236,7 +236,7 @@ async function runGPTTurbo(req, res) { |
| 236 | console.log('Error encoding prompt text', e); | 236 | console.log('Error encoding prompt text', e); |
| 237 | } | 237 | } |
| 238 | let usage_params = { | 238 | let usage_params = { |
| 239 | "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 | 239 | "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) |
| 240 | }; | 240 | }; |
| 241 | if(IS_FREE_USER) { | 241 | if(IS_FREE_USER) { |
| 242 | await setUsage(usage_params); | 242 | await setUsage(usage_params); |
| ... | @@ -333,7 +333,7 @@ async function runOpensource(req, res) { | ... | @@ -333,7 +333,7 @@ async function runOpensource(req, res) { |
| 333 | console.log('Error encoding prompt text', e); | 333 | console.log('Error encoding prompt text', e); |
| 334 | } | 334 | } |
| 335 | let usage_params = { | 335 | let usage_params = { |
| 336 | "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 | 336 | "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) |
| 337 | }; | 337 | }; |
| 338 | if(IS_FREE_USER) { | 338 | if(IS_FREE_USER) { |
| 339 | await setUsage(usage_params); | 339 | await setUsage(usage_params); |
| ... | @@ -425,6 +425,7 @@ async function validation (aiwp_app_id, req, res) { | ... | @@ -425,6 +425,7 @@ async function validation (aiwp_app_id, req, res) { |
| 425 | const limit = req.cookies["WcvYPABR"] ? parseInt(req.cookies["WcvYPABR"].replace(/\D/g, '')) : 3; | 425 | const limit = req.cookies["WcvYPABR"] ? parseInt(req.cookies["WcvYPABR"].replace(/\D/g, '')) : 3; |
| 426 | let IS_FREE_USER = false; | 426 | let IS_FREE_USER = false; |
| 427 | let TRIED_USAGE = 0; | 427 | let TRIED_USAGE = 0; |
| 428 | const ip_address = getClientIP(req); | ||
| 428 | 429 | ||
| 429 | if (aiwp_logged_in) { | 430 | if (aiwp_logged_in) { |
| 430 | let auth = await authenticate({ "aiwp_logged_in": aiwp_logged_in, "user_event_data": {}, "user_event": "endpoint" }); | 431 | let auth = await authenticate({ "aiwp_logged_in": aiwp_logged_in, "user_event_data": {}, "user_event": "endpoint" }); |
| ... | @@ -452,12 +453,12 @@ async function validation (aiwp_app_id, req, res) { | ... | @@ -452,12 +453,12 @@ async function validation (aiwp_app_id, req, res) { |
| 452 | return false; | 453 | return false; |
| 453 | } | 454 | } |
| 454 | } | 455 | } |
| 455 | } else { | 456 | } else { |
| 456 | IS_FREE_USER = true; | 457 | IS_FREE_USER = true; |
| 457 | } | 458 | } |
| 458 | if(IS_FREE_USER) { | 459 | if(IS_FREE_USER) { |
| 459 | let data = await getLimitedUsage({ | 460 | let data = await getLimitedUsage({ |
| 460 | "aiwp_app_id": aiwp_app_id | 461 | "aiwp_app_id": aiwp_app_id, ipdflu: ip_address |
| 461 | }); | 462 | }); |
| 462 | 463 | ||
| 463 | if (data.usage !== null) { | 464 | if (data.usage !== null) { |
| ... | @@ -474,6 +475,34 @@ async function validation (aiwp_app_id, req, res) { | ... | @@ -474,6 +475,34 @@ async function validation (aiwp_app_id, req, res) { |
| 474 | return { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE }; | 475 | return { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE }; |
| 475 | }; | 476 | }; |
| 476 | 477 | ||
| 478 | const getClientIP = (req) => { | ||
| 479 | let ipAddress = ''; | ||
| 480 | |||
| 481 | // Order of checking headers for client IP | ||
| 482 | if (req.headers['x-forwarded-for']) { | ||
| 483 | ipAddress = Array.isArray(req.headers['x-forwarded-for']) | ||
| 484 | ? req.headers['x-forwarded-for'][0] | ||
| 485 | : req.headers['x-forwarded-for']; | ||
| 486 | } else if (req.headers['x-client-ip']) { | ||
| 487 | ipAddress = req.headers['x-client-ip']; | ||
| 488 | } else if (req.headers['x-cluster-client-ip']) { | ||
| 489 | ipAddress = req.headers['x-cluster-client-ip']; | ||
| 490 | } else if (req.headers['x-forwarded']) { | ||
| 491 | ipAddress = req.headers['x-forwarded']; | ||
| 492 | } else if (req.headers['forwarded-for']) { | ||
| 493 | ipAddress = req.headers['forwarded-for']; | ||
| 494 | } else if (req.headers['forwarded']) { | ||
| 495 | ipAddress = req.headers['forwarded']; | ||
| 496 | } else { | ||
| 497 | ipAddress = req.socket?.remoteAddress || '127.0.0.2'; | ||
| 498 | } | ||
| 499 | |||
| 500 | // Split the IP address in case of multiple entries in 'x-forwarded-for' | ||
| 501 | const arrIp = ipAddress.split(','); | ||
| 502 | return arrIp[0] || '127.0.0.2'; | ||
| 503 | }; | ||
| 504 | |||
| 505 | |||
| 477 | 506 | ||
| 478 | 507 | ||
| 479 | // Get Models Route | 508 | // Get Models Route | ... | ... |
-
Please register or sign in to post a comment