๐Ÿ“ฆ easychen / any2api

A framework( or a tool? ) that turns any website into an API

โ˜… 86 stars โ‘‚ 8 forks ๐Ÿ‘ 86 watching โš–๏ธ GNU General Public License v3.0
๐Ÿ“ฅ Clone https://github.com/easychen/any2api.git
HTTPS git clone https://github.com/easychen/any2api.git
SSH git clone git@github.com:easychen/any2api.git
CLI gh repo clone easychen/any2api
Easy Easy Added domain whitelist to enhance security 68bdd7f 2 years ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ api
๐Ÿ“ extension
๐Ÿ“ images
๐Ÿ“„ .gitignore
๐Ÿ“„ docker-compose.yml
๐Ÿ“„ Dockerfile
๐Ÿ“„ LICENSE
๐Ÿ“„ README_CN.md
๐Ÿ“„ README.md
๐Ÿ“„ README.md

any2api

A framework that turns any website into an API

็ฎ€ไฝ“ไธญๆ–‡ | English

This project is mainly aimed at developers and only provides a brief explanation. If you have any questions, you can directly read the code, as the core code is only about 500 lines.

Recent Updates

  • Added domain whitelist to enhance security.

Loading extension

Directory: extension

To load an unpacked extension in Chrome, follow these steps:

  • Open the Chrome browser and click on the menu button (three vertical dots) in the top right corner.
  • Select "More tools" > "Extensions".
  • In the top right corner of the Extensions page, enable "Developer mode".
  • Click on the "Load unpacked" button.
  • In the dialog box that appears, select the folder where the extension is located and click the "Select folder" button.
  • The extension will be loaded into Chrome.
To load an unpacked extension in Edge, follow these steps:

  • Open the Edge browser and click on the menu button (three horizontal dots) in the top right corner.
  • Select "Extensions".
  • In the bottom left corner of the Extensions page, enable "Developer mode".
  • Click on the "Load unpacked" button.
  • In the dialog box that appears, select the folder where the extension is located and click the "Select folder" button.
  • The extension will be loaded into Edge.

Starting the Server

Local Start

Environment Variables

  • API_PASSWORD - The password used when making API requests, passed through the any2api-key header. Leave empty to disable authentication.
  • API_TIMEOUT - The timeout duration in seconds. Default is 30 seconds.
After installing the node environment, run cd api && node app.js.

Docker-compose

Run docker-compose up -d in the root directory.

Docker

docker build -t any2api . && docker run -p 9000:9000 -v $(pwd)/api/filter:/app/filter -v $(pwd)/api/ssl:/app/ssl any2api

Connection Service

To start:

  • Select Enable
  • Click Save Connection
To reconnect:

  • Click Disconnect
  • Select Enable
  • Click Save Connection

API Requests

  • /send - Send a standard HTTP request.
  • url: Passed through query, needs to be URL-encoded.
  • any2api-key: Passed through header, specified during startup through the environment variable API_PASSWORD.
  • Other method, headers, and body will be forwarded as is.
  • The response headers returned are from the browser plugin.
  • /stream - Send SSE (Server-Sent Events) request.
  • url: Passed through query, needs to be URL-encoded.
  • any2api-key: Passed through header, specified during startup through the environment variable API_PASSWORD.
  • Other method, headers, and body will be forwarded as is.
  • The response headers returned are forcibly replaced with:
res.writeHead(200, {
        'Content-Type': 'text/event-stream;charset=utf-8',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
    });

  • /:domain/v1/chat/completions - Alias for /stream, used for compatibility with certain OpenAI clients.

Custom Services

SSL Certificate

Place under api/ssl:

  • api/ssl/ssl.cert - Certificate file.
  • api/ssl/ssl.key - Private key file.

Data Filter

  • Create a directory under api/filter based on the request domain.
  • The directory supports three types of files:
  • in.js - Filter for the data received on the server before being sent to the browser plugin.
  • out.js - Filter for the data returned by the browser plugin to the server before being sent back.
  • chunk.js - Filter for the data returned by the browser plugin to the server in Server-Sent Events mode, for each returned chunk.
The format for writing the filters is as follows:

module.exports = function filter(data) {
    return {...data, headers:{...data.headers, "ai-api":"very-good" }};
}

The data in the in filter includes:

let payload = {
        url: url,
        headers: headers,
        body: body,
        method: req.method
    };

The data in the out filter includes:

let payload = {
    headers: ret.headers,
    body: ret.any2api
}

The data in the chunk filter includes:

let chunkInfo = JSON.parse(chunk);

Thanks to