Java Proxy Like Clash Guide | Generated by AI

Home 2026.02

Question: How to use Java to write a proxy application like Clash, the famous proxy program especially in mainland China? What libraries should we use, what’s the architecture, preferring the latest/advanced ones?

Answer:

Building a full-featured proxy client like Clash (or modern forks such as ClashMeta / mihomo) in pure Java is challenging and not very common. Clash itself is written in Go and most popular clients (FlClash, Clash Verge, etc.) are wrappers or use other languages. There are almost no mature, actively maintained, full Clash-compatible proxy clients written purely in Java as of 2026.

However, you can build a powerful local proxy client / transparent proxy tool in Java that supports multiple protocols (especially SOCKS5 + HTTP(S) inbound, and various outbounds like Shadowsocks, VMess, Trojan, Hysteria2, etc.). Below is a realistic modern approach using the latest/advanced libraries.

[Apps / Browser / curl]
       ↓
[System Proxy / PAC]  →  Local SOCKS5/HTTP Server (inbound)
                             │
                ┌────────────┼────────────┐
                │            │            │
         Routing Engine   DNS Hijack     Fake-IP
       (YAML / Domain / GeoIP / Process)   (optional)
                │
      ┌─────────┼─────────┬─────────┐
      │         │         │         │
  Direct   Shadowsocks   VMess    Hysteria2 / TUIC
   (outbound)  (outbound)  etc.
      │         │         │
      └─────────┼─────────┘
                ↓
          Remote Server(s)

Key components in code:

  1. Inbound server (SOCKS5 + HTTP)
    • Netty ServerBootstrap → two ports or one with protocol detection
    • SOCKS5 handshake (Netty has codec examples)
    • HTTP CONNECT for HTTPS
  2. Rule-based dispatcher
    • Parse Clash-like YAML config
    • Use domain trie / GeoIP database (MaxMind GeoLite2)
    • Process name matching (hard — needs JNA + native calls)
  3. Outbound connectors
    • Direct: just forward socket
    • Shadowsocks: Netty pipeline with SS codec
    • VMess/Trojan: implement protocol or use external core
  4. Connection management
    • Use Netty ChannelPool or OkHttp for HTTP outbounds
    • Virtual threads for each connection (very cheap)

Quick Start Skeleton (Netty + Java 21)

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class SimpleProxy {
    public static void main(String[] args) throws Exception {
        EventLoopGroup boss = new NioEventLoopGroup(1);
        EventLoopGroup worker = new NioEventLoopGroup(); // virtual threads possible in Netty 5

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(boss, worker)
             .channel(NioServerSocketChannel.class)
             .childHandler(new ChannelInitializer<Channel>() {
                 @Override
                 protected void initChannel(Channel ch) {
                     // Add SOCKS5 / HTTP codec here
                     ch.pipeline().addLast(new YourSocks5OrHttpHandler());
                 }
             });

            ChannelFuture f = b.bind(1080).sync();
            f.channel().closeFuture().sync();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}

Realistic Advice in 2026

Most advanced Java proxy projects today are either simple HTTP proxies or enterprise reverse proxies — not client-side censorship circumvention tools like Clash.

References:


Back Donate