/nodejs-powered-ai-chatbot-proxy

NodeJS-powered AI chatbot with proxy for advanced scripting and network request/response modification... Created at https://coslynx.com

Primary LanguageJavaScript

nodejs-powered-ai-chatbot-proxy

nodejs-powered-ai-chatbot-proxy

AI Interaction Proxy & Scripting Toolkit (AI-IPST) - A comprehensive solution for analyzing, modifying, and scripting client-side interactions with web-based AI chatbots

Developed with the software and tools below.

Next.js JavaScript, HTML, CSS Node.js, Express.js Custom, Gemini, OpenAI
git-last-commit GitHub commit activity GitHub top language

๐Ÿ“‘ Table of Contents

  • ๐Ÿ“ Overview
  • ๐Ÿ“ฆ Features
  • ๐Ÿ“‚ Structure
  • ๐Ÿ’ป Installation
  • ๐Ÿ—๏ธ Usage
  • ๐ŸŒ Hosting
  • ๐Ÿ“„ License
  • ๐Ÿ‘ Authors

๐Ÿ“ Overview

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.

๐Ÿ“ฆ Features

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.

๐Ÿ“‚ Structure

โ””โ”€ 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

๐Ÿ’ป Installation

Warning

๐Ÿ”ง Prerequisites

  • Node.js v14+
  • npm 6+
  • MongoDB 4.4+

๐Ÿš€ Setup Instructions

  1. Clone the repository:
    git clone https://github.com/coslynx/nodejs-powered-ai-chatbot-proxy.git
    cd nodejs-powered-ai-chatbot-proxy
  2. Install dependencies:
    npm install
  3. Set up the MongoDB database:
    cp .env.example .env
    # Fill in the required environment variables, such as the MongoDB connection string
  4. 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

๐Ÿ—๏ธ Usage

๐Ÿƒโ€โ™‚๏ธ Running the MVP

  1. Start the development server:

    npm run dev
  2. 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
  3. Access the application:

Tip

โš™๏ธ Configuration

  • 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.

๐Ÿ“š Examples

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": {}}}'

๐ŸŒ Hosting

๐Ÿš€ Deployment Instructions

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:

Deploying to Heroku

  1. Install the Heroku CLI:
    npm install -g heroku
  2. Login to Heroku:
    heroku login
  3. Create a new Heroku app:
    heroku create nodejs-powered-ai-chatbot-proxy-production
  4. 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
  5. Deploy the code:
    git push heroku main
  6. Run database migrations (if applicable):
    heroku run npm run migrate

๐Ÿ”‘ Environment Variables

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

๐Ÿ“„ API Documentation

๐Ÿ” Endpoints

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 }

๐Ÿ”’ Authentication

The AI-IPST MVP uses JWT-based authentication for securing the API endpoints. The authentication flow is as follows:

  1. Register a new user or login to receive a JWT token.
  2. Include the token in the Authorization header for all protected routes:
    Authorization: Bearer YOUR_JWT_TOKEN
    
  3. The token will expire after a configured duration, and a refresh token can be used to obtain a new access token.

๐Ÿ“ Examples

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"
  }
}

Note

๐Ÿ“œ License & Attribution

๐Ÿ“„ License

This Minimum Viable Product (MVP) is licensed under the [GNU AGP