AnyProxy Documentation

repository·master·Indexed 26 days ago

https://github.com/alibaba/anyproxy

A fully configurable HTTP/HTTPS proxy server implemented in Node.js (version 4.1.3). AnyProxy features a plugin architecture for traffic interception, modification, and analysis. It provides a CLI for server management, a web GUI for request viewing, and an npm module for integration into Node.js applications. Key capabilities include HTTPS decryption via Root CA management, WebSocket interception, traffic throttling, and a rule-based processing flow for modifying requests and responses.

Tokens
11.6K
Snippets
33
Records
62
Agent score
91%

What's inside anyproxy

  1. Getting Started with AnyProxy

    master

    To start using AnyProxy, you can follow these primary workflows:

    1. Install: Install the package via npm.
    2. Launch: Run the AnyProxy CLI to start the proxy server.
    3. Configure Options: Use command-line options to customize the proxy behavior.
    4. Use as a Node Module: Import AnyProxy into your own Node.js application for programmatic control.
  2. Start AnyProxy via CLI

    master

    You can start the AnyProxy server from the terminal. By default, it runs on port 8001.

    1. Run the command:
      anyproxy
    2. Configure your terminal's HTTP proxy to 127.0.0.1:8001.
    3. Access the Web GUI at http://127.0.0.1:8002 to view request information.

    To start on a specific port (e.g., 1080):

    anyproxy --port 1080
    anyproxy
  3. Install and run AnyProxy as a global module

    master

    To use AnyProxy as a standalone CLI tool, install it globally via npm. For Debian or Ubuntu users, ensure nodejs-legacy is installed first.

    Installation:

    sudo apt-get install nodejs-legacy
    npm install -g anyproxy

    Running:

    • Start the proxy on the default port (8001):
      anyproxy
    • Start on a specific port:
      anyproxy --port 1080

    Accessing the Web Interface: Once running, visit http://127.0.0.1:8002 in your browser to view request information.

    # Installation
    sudo apt-get install nodejs-legacy
    npm install -g anyproxy
    
    # Start
    anyproxy
    
    # Other commands
    anyproxy --port 1080
  4. Configure Root CA for HTTPS interception

    master

    To intercept HTTPS traffic, you must trust the AnyProxy Root CA on your device.

    macOS

    1. Double-click rootCA.crt.
    2. Add the certificate to your Keychain (Login or System).
    3. Find the AnyProxy certificate and set it to Always Trust.

    Windows

    Install the certificate via the Windows Certificate Manager.

    iOS

    1. Click Root CA in the AnyProxy web UI to install.
    2. Crucial for iOS 10.3+: Go to Settings > General > About > Certificate Trust Settings and manually enable full trust for the AnyProxy certificate.

    Android

    1. Download the CA via the AnyProxy web UI (scan the QR code).
    2. Install via Settings > Security > Install from storage (or similar path depending on your Android version).
    3. Note: If .crt is not supported, you can choose a different certificate type in AnyProxy before installing.
  5. Handle HTTPS requests in Rule modules

    master

    If your Rule module cannot intercept HTTPS requests, you must enable HTTPS interception using one of the following methods:

    1. Via CLI: Use the --intercept flag when launching AnyProxy.
    2. Via npm module: Set forceProxyHttps: true in your configuration options.
    3. Via Rule module: Implement a beforeDealHttpsRequest function within your rule file to manually determine which requests to intercept.
  6. Trust the AnyProxy CA certificate on various platforms

    master

    To intercept HTTPS traffic, you must trust the AnyProxy Root CA certificate on your device.

    iOS

    1. Click Root CA in the AnyProxy Web UI and scan the QR code to install.
    2. For iOS >= 10.3: Go to Settings -> General -> About This Device -> Certificate Trust Settings and enable the switch for the AnyProxy certificate.

    Android

    1. Scan the QR code from the Web UI to download the certificate.
    2. Installation varies by device:
      • Direct install: Tap the downloaded file.
      • Manual: Settings -> Security -> Encryption & Credentials -> Install from storage (or similar path depending on Android version).
      • Note: Some devices (like OPPO) may require .cer files; AnyProxy provides multiple formats.

    macOS

    1. Double-click rootCA.crt.
    2. Add it to the login or system keychain.
    3. Find the certificate in Keychain Access and set it to Always Trust.

    Windows

    Follow the system prompts to install the certificate into the Trusted Root Certification Authorities store.

  7. Ignore HTTPS certificate errors (Global)

    master

    If you encounter 'The connection is not private' errors due to self-signed certificates, you can ignore SSL certificate authentication errors globally using one of these methods. Warning: This applies to all websites visited while AnyProxy is active.

    Via CLI: Use the --ignore-unauthorized-ssl flag.

    Via Node.js code: Pass dangerouslyIgnoreUnauthorized: true in the options object when initializing AnyProxy.ProxyCore.

    // Via Node.js code
    const options = {
      // ... other options
      dangerouslyIgnoreUnauthorized: true
    };
    
    const anyproxyIns = new AnyProxy.ProxyCore(options);
    anyproxyIns.start();
  8. Create a custom Rule module

    master

    You can extend AnyProxy by writing a JavaScript rule module to intercept and modify requests or responses.

    Rule Lifecycle and Flow

    1. beforeSendRequest(requestDetail): Called when a request is sent. You can modify headers, body, or the target URL. If you return a response object here, the request is short-circuited and the actual server is never contacted.
    2. beforeDealHttpsRequest(requestDetail): For HTTPS requests. If this returns true, the request is decrypted and processed like a standard HTTP request.
    3. beforeSendResponse(requestDetail, responseDetail): Called after receiving a response from the server. You can modify the status code, headers, or the response body.

    Example: Modify a response and add delay

    Create a file named sample.js:

    module.exports = {
      summary: 'a rule to hack response',
      *beforeSendResponse(requestDetail, responseDetail) {
        if (requestDetail.url === 'http://httpbin.org/user-agent') {
          const newResponse = responseDetail.response;
          newResponse.body += '- AnyProxy Hacked!';
    
          return new Promise((resolve, reject) => {
            setTimeout(() => { // delay
              resolve({ response: newResponse });
            }, 5000);
          });
        }
      },
    };

    How to run the rule

    Run AnyProxy pointing to your rule file:

    anyproxy --rule sample.js

    You can also reference rules via:

    • Local path: anyproxy --rule ./rule.js
    • Online URL: anyproxy --rule https://sample.com/rule.js
    • NPM package: anyproxy --rule myRulePkg (if installed globally) or anyproxy --rule ./path/to/pkg/
    // file: sample.js
    module.exports = {
      summary: 'a rule to hack response',
      *beforeSendResponse(requestDetail, responseDetail) {
        if (requestDetail.url === 'http://httpbin.org/user-agent') {
          const newResponse = responseDetail.response;
          newResponse.body += '- AnyProxy Hacked!';
    
          return new Promise((resolve, reject) => {
            setTimeout(() => { // delay
              resolve({ response: newResponse });
            }, 5000);
          });
        }
      },
    };
  9. Launch AnyProxy via CLI

    master

    You can start AnyProxy from the command line. By default, it runs on port 8001, and the web interface is available at port 8002.

    To specify a custom port, use the --port flag.

    To intercept all HTTPS traffic, use the --intercept flag (requires CA certificate setup).

    To intercept WebSocket traffic, use the --ws-intercept flag.

    anyproxy
    anyproxy --port 1080
    anyproxy --intercept
    anyproxy --ws-intercept