Skip to content

hnb22/ahc-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AHC Proxy Server

A HTTP/HTTPS forward proxy server built with Java and Netty, featuring a CLI tool and distributed logging capabilities.

Installation

  1. Clone the repository:
git clone <repository-url>
cd ahc-proxy
  1. Build the project:
mvn clean install

Starting the Proxy Server

Regular Proxy Mode

package examples;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.proxy.config.ProxyConfig;
import com.example.proxy.core.server.ProxyServer;
import com.example.proxy.core.server.ServerInitializer;
import com.example.proxy.core.server.ServerInitializer.Notifier;
import com.example.proxy.exceptions.ProxyException;

public class TestHttp1 {

    private static final Logger logger = LoggerFactory.getLogger(TestHttp1.class);
    
    static final int LOCAL_PORT = Integer.parseInt(System.getProperty("localPort", "8000"));
    static final String LOCAL_HOST = "localhost";

    public static void main(String[] args) throws ProxyException {
        ProxyServer proxy = new ProxyServer(new ProxyConfig("HTTP/1.1"));

        try {
            proxy.initialize(new ServerInitializer(LOCAL_HOST, LOCAL_PORT, Notifier.NO));
            proxy.start();
            
            logger.info("Proxy server is running. Press Ctrl+C to stop.");
            
            proxy.sync();
            
        } catch (ProxyException e) {
            logger.error("Failed to start proxy server: {}", e.getMessage());
            proxy.stop();
        } finally {
            logger.info("Proxy server is shutting down");
            proxy.stop();
        }
    }
}

Notifier Mode

package examples;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.proxy.config.ProxyConfig;
import com.example.proxy.core.server.ProxyServer;
import com.example.proxy.core.server.ServerInitializer;
import com.example.proxy.core.server.ServerInitializer.Notifier;
import com.example.proxy.exceptions.ProxyException;

public class TestHttp1Notifier {

    private static final Logger logger = LoggerFactory.getLogger(TestHttp1Notifier.class);
    private static final int LOCAL_PORT = Integer.parseInt(System.getProperty("localPort", "8000"));
    private static final String LOCAL_HOST = "localhost";
    private static final List<String> loggingDestinations = List.of("localhost:8001", "localhost:8002", "localhost:8003");

    public static void main(String[] args) throws ProxyException {
        ProxyServer proxy = new ProxyServer(new ProxyConfig("HTTP/1.1"));

        try {
            proxy.initialize(new ServerInitializer(LOCAL_HOST, LOCAL_PORT, Notifier.YES, loggingDestinations));
            proxy.start();
            
            logger.info("Proxy server is running. Press Ctrl+C to stop.");
            
            proxy.sync();
            
        } catch (ProxyException e) {
            logger.error("Failed to start proxy server: {}", e.getMessage());
            proxy.stop();
        } finally {
            logger.info("Proxy server is shutting down");
            proxy.stop();
        }
    }
}

The cluster example routes requests to:

  • Original destination (from the request URL)
  • localhost:8001 (logs request to destination 1)
  • localhost:8002 (logs request to destination 2)
  • localhost:8003 (logs request to destination 3)

Making Requests

HTTP Requests

# Simple GET request
curl -x localhost:8000 "http://echo.free.beeceptor.com/sample-request?author=beeceptor"

# POST request with JSON body
curl -x localhost:8000 \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"name": "John", "age": 30}' \
  "http://echo.free.beeceptor.com/sample-request"

# Request with compression
curl -x localhost:8000 \
  -H "Accept-Encoding: gzip" \
  "http://echo.free.beeceptor.com/sample-request"

HTTPS Requests (Tunneling)

# HTTPS request via CONNECT tunneling
curl -x localhost:8000 "https://echo.free.beeceptor.com/sample-request?author=beeceptor"

Cluster Log Limitations

  • HTTPS requests: Logging requests via backend is not yet supported for HTTPS requests.

About

HTTP/HTTPS forward proxy tool with distributed logging. Built with Netty.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published