# seaunit/hk-tunnel architecture

> Architecture diagram of the GitHub repository seaunit/hk-tunnel, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/seaunit/hk-tunnel
- Repository: https://github.com/seaunit/hk-tunnel

## Overview

HK-Tunnel is a two-platform SSH proxy client: Windows redirects system-proxy traffic through a local HTTP-to-SOCKS bridge, while Android captures device traffic with VpnService and translates it through the embedded native tunnel. The graph emphasizes the Android connection lifecycle and keeps the documented Windows path visible. Android excerpts and the README support the main links; native internals and most Windows behavior were not sampled, so their implementation links are omitted or marked unverified.

Main entry points: [`android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt`](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt) (VPN lifecycle), [`android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt`](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt) (Connection UI), [`android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-tunnel.c`](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-tunnel.c) (Packet tunnel core), [`android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session.c`](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session.c) (SOCKS sessions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_android_app["Android app"]
  node_main_activity["Connection UI<br/>[MainActivity.kt]"]
  node_proxy_config["Proxy config<br/>[ProxyConfig.kt]"]
  node_proxy_store[("Credential store<br/>[ProxyStore.kt]")]
  node_tunnel_bus["Status bus<br/>[TunnelBus.kt]"]
  node_vpn_service["VPN lifecycle"]
  node_ssh_tunnel["SSH forwarding<br/>[SshTunnel.kt]"]
  node_socks_server["Local SOCKS5<br/>[Socks5Server.kt]"]
end

subgraph group_native_tunnel["Native tunnel"]
  node_jni_binding["JNI service binding<br/>[TProxyService.java]"]
  node_native_config["Tunnel configuration<br/>[hev-config.c]"]
  node_native_tunnel_core["Packet tunnel core"]
  node_native_sessions["SOCKS sessions"]
  node_tcp_session["TCP session"]
  node_udp_session["UDP session"]
  node_mapped_dns["Mapped DNS<br/>[hev-mapped-dns.c]"]
  node_lwip["lwIP networking<br/>[ip4_frag.c]"]
end

subgraph group_windows_client["Windows client"]
  node_windows_bridge["HTTP SOCKS bridge<br/>[HK-Bridge.cs]"]
  node_windows_system_proxy["System proxy control<br/>[HK-Bridge.cs]"]
end

subgraph group_remote_network["Remote network"]
  node_ssh_server{{"SSH server"}}
end

node_android_user(("Android user"))
node_vpn_device(("Android traffic"))
node_windows_user(("Windows user"))
node_internet(("Internet destinations"))

node_android_user -->|"configures"| node_main_activity
node_main_activity -->|"builds config"| node_proxy_config
node_main_activity -->|"saves credentials"| node_proxy_store
node_main_activity -->|"starts service"| node_vpn_service
node_main_activity -->|"observes status"| node_tunnel_bus
node_vpn_service -->|"loads credentials"| node_proxy_store
node_vpn_service -->|"connects SSH"| node_ssh_tunnel
node_vpn_service -->|"starts SOCKS"| node_socks_server
node_vpn_service -->|"starts native service"| node_jni_binding
node_vpn_service -->|"updates status"| node_tunnel_bus
node_vpn_device -->|"routes IP traffic"| node_vpn_service
node_jni_binding -.->|"dispatches JNI"| node_native_tunnel_core
node_native_tunnel_core -.->|"uses config"| node_native_config
node_native_tunnel_core -.->|"dispatches sessions"| node_native_sessions
node_native_sessions -.->|"handles TCP"| node_tcp_session
node_native_sessions -.->|"handles UDP"| node_udp_session
node_native_tunnel_core -.->|"maps DNS"| node_mapped_dns
node_native_tunnel_core -.->|"uses stack"| node_lwip
node_socks_server -.->|"opens forwarded streams"| node_ssh_tunnel
node_ssh_tunnel -->|"forwards encrypted traffic"| node_ssh_server
node_ssh_server -->|"connects outbound"| node_internet
node_windows_user -->|"starts client"| node_windows_system_proxy
node_windows_system_proxy -->|"redirects proxy traffic"| node_windows_bridge
node_windows_bridge -->|"uses SSH forwarding"| node_ssh_server
node_ssh_server -->|"connects outbound"| node_internet

click node_main_activity "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt"
click node_proxy_config "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/ProxyConfig.kt"
click node_proxy_store "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/ProxyStore.kt"
click node_tunnel_bus "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/TunnelBus.kt"
click node_vpn_service "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt"
click node_ssh_tunnel "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/SshTunnel.kt"
click node_socks_server "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/Socks5Server.kt"
click node_jni_binding "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/android/hev/htproxy/TProxyService.java"
click node_native_config "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-config.c"
click node_native_tunnel_core "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-tunnel.c"
click node_native_sessions "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session.c"
click node_tcp_session "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session-tcp.c"
click node_udp_session "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session-udp.c"
click node_mapped_dns "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-mapped-dns.c"
click node_lwip "https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/third-part/lwip/src/core/ipv4/ip4_frag.c"
click node_windows_bridge "https://github.com/seaunit/hk-tunnel/blob/main/windows/HK-Bridge.cs"
click node_windows_system_proxy "https://github.com/seaunit/hk-tunnel/blob/main/windows/HK-Bridge.cs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main_activity,node_proxy_config,node_proxy_store,node_tunnel_bus,node_vpn_service,node_ssh_tunnel,node_socks_server,node_android_user,node_windows_user toneBlue
class node_jni_binding,node_native_config,node_native_tunnel_core,node_native_sessions,node_tcp_session,node_udp_session,node_mapped_dns,node_lwip toneAmber
class node_windows_bridge,node_windows_system_proxy toneMint
class node_ssh_server toneRose
class node_vpn_device,node_internet toneIndigo
```

## Components

### Android app

- [Connection UI](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt): `android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt`
- [Proxy config](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/ProxyConfig.kt): `android/app/src/main/java/com/hktunnel/tunnel/ProxyConfig.kt`
- [Credential store](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/ProxyStore.kt): `android/app/src/main/java/com/hktunnel/tunnel/ProxyStore.kt`
- [Status bus](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/TunnelBus.kt): `android/app/src/main/java/com/hktunnel/tunnel/TunnelBus.kt`
- [VPN lifecycle](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt): `android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt`
- [SSH forwarding](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/SshTunnel.kt): `android/app/src/main/java/com/hktunnel/tunnel/SshTunnel.kt`
- [Local SOCKS5](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/java/com/hktunnel/tunnel/Socks5Server.kt): `android/app/src/main/java/com/hktunnel/tunnel/Socks5Server.kt`

### Native tunnel

- [JNI service binding](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/android/hev/htproxy/TProxyService.java): `android/app/src/main/jni/hev-socks5-tunnel/android/hev/htproxy/TProxyService.java`
- [Tunnel configuration](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-config.c): `android/app/src/main/jni/hev-socks5-tunnel/src/hev-config.c`
- [Packet tunnel core](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-tunnel.c): `android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-tunnel.c`
- [SOCKS sessions](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session.c): `android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session.c`
- [TCP session](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session-tcp.c): `android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session-tcp.c`
- [UDP session](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session-udp.c): `android/app/src/main/jni/hev-socks5-tunnel/src/hev-socks5-session-udp.c`
- [Mapped DNS](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/src/hev-mapped-dns.c): `android/app/src/main/jni/hev-socks5-tunnel/src/hev-mapped-dns.c`
- [lwIP networking](https://github.com/seaunit/hk-tunnel/blob/main/android/app/src/main/jni/hev-socks5-tunnel/third-part/lwip/src/core/ipv4/ip4_frag.c): `android/app/src/main/jni/hev-socks5-tunnel/third-part/lwip/src/core/ipv4/ip4_frag.c`

### Windows client

- [HTTP SOCKS bridge](https://github.com/seaunit/hk-tunnel/blob/main/windows/HK-Bridge.cs): `windows/HK-Bridge.cs`
- [System proxy control](https://github.com/seaunit/hk-tunnel/blob/main/windows/HK-Bridge.cs): `windows/HK-Bridge.cs`

### Remote network

- **SSH server**

### Other components

- **Android user**
- **Android traffic**
- **Windows user**
- **Internet destinations**

## Connections

- Android user → Connection UI: configures (evidence: `README.md`)
- Connection UI → Proxy config: builds config (evidence: `android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt`)
- Connection UI → Credential store: saves credentials (evidence: `android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt`)
- Connection UI → VPN lifecycle: starts service (evidence: `android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt`)
- Connection UI → Status bus: observes status (evidence: `android/app/src/main/java/com/hktunnel/tunnel/MainActivity.kt`)
- VPN lifecycle → Credential store: loads credentials (evidence: `android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt`)
- VPN lifecycle → SSH forwarding: connects SSH (evidence: `android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt`)
- VPN lifecycle → Local SOCKS5: starts SOCKS (evidence: `android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt`)
- VPN lifecycle → JNI service binding: starts native service (evidence: `android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt`)
- VPN lifecycle → Status bus: updates status (evidence: `android/app/src/main/java/com/hktunnel/tunnel/TunnelVpnService.kt`)
- Android traffic → VPN lifecycle: routes IP traffic (evidence: `README.md`)
- JNI service binding → Packet tunnel core: dispatches JNI
- Packet tunnel core → Tunnel configuration: uses config
- Packet tunnel core → SOCKS sessions: dispatches sessions
- SOCKS sessions → TCP session: handles TCP
- SOCKS sessions → UDP session: handles UDP
- Packet tunnel core → Mapped DNS: maps DNS
- Packet tunnel core → lwIP networking: uses stack
- Local SOCKS5 → SSH forwarding: opens forwarded streams
- SSH forwarding → SSH server: forwards encrypted traffic (evidence: `README.md`)
- SSH server → Internet destinations: connects outbound (evidence: `README.md`)
- Windows user → System proxy control: starts client (evidence: `README.md`)
- System proxy control → HTTP SOCKS bridge: redirects proxy traffic (evidence: `README.md`)
- HTTP SOCKS bridge → SSH server: uses SSH forwarding (evidence: `README.md`)
- SSH server → Internet destinations: connects outbound (evidence: `README.md`)
