AI Interaction Proxy & Scripting Toolkit (AI-IPST) - A comprehensive solution for analyzing, modifying, and scripting client-side interactions with web-based AI chatbots
- ๐ Overview
- ๐ฆ Features
- ๐ Structure
- ๐ป Installation
- ๐๏ธ Usage
- ๐ Hosting
- ๐ License
- ๐ Authors
The AI Interaction Proxy & Scripting Toolkit (AI-IPST) is a Minimum Viable Product (MVP) that provides a comprehensive solution for analyzing, modifying, and scripting client-side interactions with web-based AI chatbots. The project leverages a Node.js/Express.js backend, a Next.js 14 frontend with the App Router, and integrates with custom, Gemini, and OpenAI language models to enable advanced functionality.
Feature | Description | |
---|---|---|
โ๏ธ | Architecture | The codebase follows a modular architectural pattern with separate directories for different functionalities, such as the proxy server, userscript management, and network traffic logging. This ensures easier maintenance and scalability. |
๐ | Documentation | The repository includes a comprehensive README file that provides an overview of the MVP, its dependencies, and detailed usage instructions. |
๐ | Dependencies | The codebase relies on various external libraries and packages, including Express.js, Mongoose, Jsonwebtoken, and Tailwind CSS, which are essential for building the proxy server, managing user authentication, and styling the user interface. |
๐งฉ | Modularity | The modular structure allows for easier maintenance and reusability of the code, with separate directories and files for the proxy controller, script controller, logging controller, and various supporting services and models. |
๐งช | Testing | The project includes unit tests using Jest and Supertest to ensure the reliability and robustness of the codebase, with a target of at least 90% code coverage. |
โก๏ธ | Performance | The performance of the system is optimized through techniques such as caching strategies, asynchronous processing, and event loop optimizations to ensure high-performance request handling. |
๐ | Security | The application implements comprehensive security measures, including input validation, data sanitization, rate limiting, and JWT-based authentication and authorization, to protect against common vulnerabilities. |
๐ | Version Control | The project utilizes Git for version control, with a GitHub repository and GitHub Actions workflow files for automated build and release processes. |
๐ | Integrations | The AI-IPST toolkit integrates with various browser APIs, external services through HTTP requests, and includes integrations with Greasemonkey, Tampermonkey, and Violentmonkey userscript managers. |
๐ถ | Scalability | The system is designed to handle increased user load and data volume, utilizing caching strategies and cloud-based solutions for better scalability. |
โโ src
โโ controllers
โโ proxyController.js
โโ scriptController.js
โโ loggingController.js
โโ services
โโ proxyService.js
โโ scriptService.js
โโ loggingService.js
โโ models
โโ ProxyConfig.js
โโ Userscript.js
โโ LogEntry.js
โโ routes
โโ proxyRoutes.js
โโ scriptRoutes.js
โโ loggingRoutes.js
โโ middleware
โโ proxyMiddleware.js
โโ authMiddleware.js
โโ errorHandler.js
โโ config
โโ database.js
โโ config.js
โโ utils
โโ logger.js
โโ helpers.js
โโ app.js
โโ tests
โโ unit
โโ proxyService.test.js
โโ scriptService.test.js
โโ loggingService.test.js
โโ integration
โโ proxyRoutes.test.js
โโ scriptRoutes.test.js
โโ loggingRoutes.test.js
โโ .env
โโ package.json
โโ README.md
โโ startup.sh
โโ commands.json
- Clone the repository:
git clone https://github.com/coslynx/nodejs-powered-ai-chatbot-proxy.git cd nodejs-powered-ai-chatbot-proxy
- Install dependencies:
npm install
- Set up the MongoDB database:
cp .env.example .env # Fill in the required environment variables, such as the MongoDB connection string
- Configure environment variables:
cp .env.example .env # Fill in the necessary environment variables, such as the JWT secret, proxy target hostname and port, logging settings, and external API keys
-
Start the development server:
npm run dev
-
Start the MongoDB database and API server:
# Start MongoDB database docker-compose up -d mongodb # Run database migrations npm run migrate # Start the API server npm run api
-
Access the application:
- Web interface: http://localhost:3000
- API endpoint: http://localhost:3000/api
Tip
- The
.env
file contains all the necessary environment variables for the application, such as the MongoDB connection string, JWT secret, proxy target hostname and port, logging settings, and external API keys. - Modify the values in the
.env
file to match your specific deployment requirements.
Explore the following examples to understand the key features of the AI-IPST MVP:
-
๐ Intercepting and Modifying a Proxy Request:
curl -X POST http://localhost:3000/api/proxy/modify/request \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -d '{"method": "POST", "url": "https://example.com/api", "headers": {"Content-Type": "application/json"}, "body": {"data": "foo"}}'
-
๐ Injecting a Custom Proxy Response:
curl -X POST http://localhost:3000/api/proxy/inject \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -d '{"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": {"data": "bar"}}'
-
๐ Executing a Userscript:
curl -X POST http://localhost:3000/api/scripts/123/execute \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -d '{"context": {"window": {}}}'
The AI-IPST MVP can be deployed to various cloud platforms, such as Heroku, AWS, or Google Cloud. The following example demonstrates the deployment process for Heroku:
- Install the Heroku CLI:
npm install -g heroku
- Login to Heroku:
heroku login
- Create a new Heroku app:
heroku create nodejs-powered-ai-chatbot-proxy-production
- Set up environment variables:
heroku config:set NODE_ENV=production heroku config:set MONGODB_URI=your_mongodb_uri_here heroku config:set JWT_SECRET=your_jwt_secret_here heroku config:set PROXY_TARGET_HOSTNAME=example.com heroku config:set PROXY_TARGET_PORT=80 heroku config:set LOG_LEVEL=info heroku config:set LOG_DIR=./logs
- Deploy the code:
git push heroku main
- Run database migrations (if applicable):
heroku run npm run migrate
The following environment variables are required for the AI-IPST MVP:
MONGODB_URI
: Connection string for the MongoDB database Example:mongodb://user:password@host:port/database
JWT_SECRET
: Secret key for JWT token generation Example:your-256-bit-secret
PROXY_TARGET_HOSTNAME
: Hostname of the target server for the proxy Example:example.com
PROXY_TARGET_PORT
: Port of the target server for the proxy Example:80
LOG_LEVEL
: Log level for the application (e.g., 'info', 'debug', 'error') Example:info
LOG_DIR
: Directory for storing log files Example:./logs
OPENAI_API_KEY
: API key for the OpenAI language model (if applicable) Example:your_openai_api_key
ANTHROPIC_API_KEY
: API key for the Anthropic language model (if applicable) Example:your_anthropic_api_key
The AI-IPST MVP provides the following API endpoints:
-
GET /api/proxy/config
- Description: Fetch the current proxy configuration
- Authentication: Required
- Response:
{ targetHostname: string, targetPort: number, requestModifications: { [key: string]: any }, responseModifications: { [key: string]: any } }
-
PUT /api/proxy/config
- Description: Update the proxy configuration
- Authentication: Required
- Body:
{ targetHostname: string, targetPort: number, requestModifications: { [key: string]: any }, responseModifications: { [key: string]: any } }
- Response:
{ message: string }
-
GET /api/proxy/traffic
- Description: Retrieve the logged proxy traffic
- Authentication: Required
- Query Params:
{ startDate: string, endDate: string, targetUrl: string, method: string, page: number, limit: number }
- Response:
[ { method: string, url: string, headers: { [key: string]: string }, body: any, statusCode: number, responseHeaders: { [key: string]: string }, responseBody: any } ]
-
POST /api/proxy/modify/request
- Description: Modify an intercepted proxy request
- Authentication: Required
- Body:
{ method: string, url: string, headers: { [key: string]: string }, body: any }
- Response:
{ method: string, url: string, headers: { [key: string]: string }, body: any }
-
POST /api/proxy/modify/response
- Description: Modify an intercepted proxy response
- Authentication: Required
- Body:
{ statusCode: number, headers: { [key: string]: string }, body: any }
- Response:
{ statusCode: number, headers: { [key: string]: string }, body: any }
-
POST /api/proxy/inject
- Description: Inject a custom response without forwarding the original request
- Authentication: Required
- Body:
{ statusCode: number, headers: { [key: string]: string }, body: any }
- Response:
{ statusCode: number, headers: { [key: string]: string }, body: any }
The AI-IPST MVP uses JWT-based authentication for securing the API endpoints. The authentication flow is as follows:
- Register a new user or login to receive a JWT token.
- Include the token in the
Authorization
header for all protected routes:Authorization: Bearer YOUR_JWT_TOKEN
- The token will expire after a configured duration, and a refresh token can be used to obtain a new access token.
Explore the following examples to understand the API usage:
# Register a new user
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username": "aiuser", "email": "user@example.com", "password": "securepassword123"}'
# Response
{
"id": "user123",
"username": "aiuser",
"email": "user@example.com",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
# Modify an intercepted proxy request
curl -X POST http://localhost:3000/api/proxy/modify/request \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{"method": "POST", "url": "https://example.com/api", "headers": {"Content-Type": "application/json"}, "body": {"data": "foo"}}'
# Response
{
"method": "POST",
"url": "https://example.com/api",
"headers": {
"Content-Type": "application/json"
},
"body": {
"data": "foo"
}
}