# gfw-knocker/mahsang architecture

> Architecture diagram of the GitHub repository gfw-knocker/mahsang, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gfw-knocker/mahsang
- Repository: https://github.com/gfw-knocker/mahsang

## Overview

MahsaNG is an Android VPN client built around V2Ray, with a separate Psiphon VPN service and documented filtering-resistance features. The main workflow is: users add or import a proxy profile, store and select it, then start a VPN service that configures routing and runs the tunnel; users can also test server latency. The graph includes documented capabilities whose implementation is not visible in the excerpts. Source evidence is partial, so uncertain integration links are omitted or marked dashed.

Main entry points: [`V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt) (Tunnel coordinator), [`V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt`](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt) (Server profiles), [`V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt`](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt) (Latency testing), [`V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt`](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt) (Main screen)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_profiles["Profiles and settings"]
  node_server_ui["Server profiles<br/>[ServerActivity.kt]"]
  node_config_import["Config import/export"]
  node_server_model["Server config<br/>[ServerConfig.kt]"]
  node_config_types["Protocol types<br/>[EConfigType.kt]"]
  node_profile_store[("Profile storage<br/>[MmkvManager.kt]")]
  node_routing_settings["Routing settings"]
  node_routing_modes["Routing modes<br/>[ERoutingMode.kt]"]
end

subgraph group_tunnel["VPN and tunnel runtime"]
  node_config_builder["Runtime config builder<br/>[V2rayConfigUtil.kt]"]
  node_service_manager["Tunnel coordinator"]
  node_service_control["Service control<br/>[ServiceControl.kt]"]
  node_vpn_service["V2Ray VPN<br/>[V2RayVpnService.kt]"]
  node_proxy_service["Proxy-only service"]
  node_psiphon_service["Psiphon VPN"]
  node_psiphon_tunnel["Psiphon engine<br/>[PsiphonTunnel.java]"]
  node_latency_test["Latency testing"]
  node_speedtest["Ping measurement<br/>[SpeedtestUtil.kt]"]
end

subgraph group_resilience["Filtering resistance"]
  node_doh["DoH fragmentor"]
  node_tls_fragmentor["TLS fragmentor<br/>[TLS_Fragmentor.kt]"]
  node_https_fragmentor["HTTPS fragmentor"]
end

subgraph group_app["Android app experience"]
  node_main_activity["Main screen<br/>[MainActivity.kt]"]
  node_main_viewmodel["Main view model<br/>[MainViewModel.kt]"]
  node_app_config["App configuration<br/>[AppConfig.kt]"]
  node_message_bus["Service messages<br/>[MessageUtil.kt]"]
end

node_user(("VPN user"))

node_user -->|"manages profiles"| node_server_ui
node_server_ui -->|"imports and exports"| node_config_import
node_config_import -->|"creates configs"| node_server_model
node_server_model -->|"uses protocol"| node_config_types
node_server_ui -->|"persists profiles"| node_profile_store
node_user -->|"edits routing"| node_routing_settings
node_routing_settings -->|"reads preferences"| node_app_config
node_user -.->|"starts connection"| node_main_activity
node_main_activity -.->|"delegates actions"| node_main_viewmodel
node_service_manager -->|"reads selected config"| node_profile_store
node_service_manager -->|"builds runtime config"| node_config_builder
node_service_manager -->|"starts service"| node_service_control
node_service_manager -->|"routes VPN mode"| node_vpn_service
node_service_manager -->|"routes proxy mode"| node_proxy_service
node_vpn_service -->|"delegates lifecycle"| node_service_manager
node_user -.->|"starts Psiphon"| node_psiphon_service
node_psiphon_service -->|"starts tunnel"| node_psiphon_tunnel
node_user -.->|"tests servers"| node_latency_test
node_latency_test -->|"measures ping"| node_speedtest
node_latency_test -->|"reports result"| node_message_bus
node_service_manager -->|"reports status"| node_message_bus
node_service_manager -.->|"supports DoH"| node_doh
node_service_manager -.->|"supports fragmentation"| node_tls_fragmentor
node_service_manager -.->|"supports fragmentation"| node_https_fragmentor
node_user -->|"uses app"| node_main_activity

click node_server_ui "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt"
click node_config_import "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt"
click node_server_model "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt"
click node_config_types "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/EConfigType.kt"
click node_profile_store "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt"
click node_routing_settings "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt"
click node_routing_modes "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ERoutingMode.kt"
click node_config_builder "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt"
click node_service_manager "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt"
click node_service_control "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/ServiceControl.kt"
click node_vpn_service "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt"
click node_proxy_service "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayProxyOnlyService.kt"
click node_psiphon_service "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/PsiphonVpnService.kt"
click node_psiphon_tunnel "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/java/ca/psiphon/PsiphonTunnel.java"
click node_latency_test "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt"
click node_speedtest "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt"
click node_doh "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/DoH_over_Fragment.kt"
click node_tls_fragmentor "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/TLS_Fragmentor.kt"
click node_https_fragmentor "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/HTTPS_Fragmentor.kt"
click node_main_activity "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt"
click node_main_viewmodel "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt"
click node_app_config "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt"
click node_message_bus "https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MessageUtil.kt"

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_server_ui,node_config_import,node_server_model,node_config_types,node_profile_store,node_routing_settings,node_routing_modes,node_user toneBlue
class node_config_builder,node_service_manager,node_service_control,node_vpn_service,node_proxy_service,node_psiphon_service,node_psiphon_tunnel,node_latency_test,node_speedtest toneAmber
class node_doh,node_tls_fragmentor,node_https_fragmentor toneMint
class node_main_activity,node_main_viewmodel,node_app_config,node_message_bus toneRose
```

## Components

### Profiles and settings

- [Server profiles](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt`
- [Config import/export](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt`
- [Server config](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt`
- [Protocol types](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/EConfigType.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/EConfigType.kt`
- [Profile storage](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt`
- [Routing settings](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt`
- [Routing modes](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ERoutingMode.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ERoutingMode.kt`

### VPN and tunnel runtime

- [Runtime config builder](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt`
- [Tunnel coordinator](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`
- [Service control](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/ServiceControl.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/ServiceControl.kt`
- [V2Ray VPN](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt`
- [Proxy-only service](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayProxyOnlyService.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayProxyOnlyService.kt`
- [Psiphon VPN](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/PsiphonVpnService.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/PsiphonVpnService.kt`
- [Psiphon engine](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/java/ca/psiphon/PsiphonTunnel.java): `V2rayNG/app/src/main/java/ca/psiphon/PsiphonTunnel.java`
- [Latency testing](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt`
- [Ping measurement](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt`

### Filtering resistance

- [DoH fragmentor](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/DoH_over_Fragment.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/DoH_over_Fragment.kt`
- [TLS fragmentor](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/TLS_Fragmentor.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/TLS_Fragmentor.kt`
- [HTTPS fragmentor](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/HTTPS_Fragmentor.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/gfwknocker/HTTPS_Fragmentor.kt`

### Android app experience

- [Main screen](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt`
- [Main view model](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt`
- [App configuration](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt`
- [Service messages](https://github.com/gfw-knocker/mahsang/blob/master/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MessageUtil.kt): `V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MessageUtil.kt`

### Other components

- **VPN user**

## Connections

- VPN user → Server profiles: manages profiles (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt`)
- Server profiles → Config import/export: imports and exports (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt`)
- Config import/export → Server config: creates configs (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt`)
- Server config → Protocol types: uses protocol (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt`)
- Server profiles → Profile storage: persists profiles (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt`)
- VPN user → Routing settings: edits routing (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt`)
- Routing settings → App configuration: reads preferences (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsFragment.kt`)
- VPN user → Main screen: starts connection
- Main screen → Main view model: delegates actions (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt`)
- Tunnel coordinator → Profile storage: reads selected config (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`)
- Tunnel coordinator → Runtime config builder: builds runtime config (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`)
- Tunnel coordinator → Service control: starts service (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`)
- Tunnel coordinator → V2Ray VPN: routes VPN mode (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`)
- Tunnel coordinator → Proxy-only service: routes proxy mode (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`)
- V2Ray VPN → Tunnel coordinator: delegates lifecycle (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt`)
- VPN user → Psiphon VPN: starts Psiphon
- Psiphon VPN → Psiphon engine: starts tunnel (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/PsiphonVpnService.kt`)
- VPN user → Latency testing: tests servers
- Latency testing → Ping measurement: measures ping (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt`)
- Latency testing → Service messages: reports result (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt`)
- Tunnel coordinator → Service messages: reports status (evidence: `V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt`)
- Tunnel coordinator → DoH fragmentor: supports DoH (evidence: `README.md`)
- Tunnel coordinator → TLS fragmentor: supports fragmentation (evidence: `README.md`)
- Tunnel coordinator → HTTPS fragmentor: supports fragmentation (evidence: `README.md`)
- VPN user → Main screen: uses app (evidence: `README.md`)
