@@ -8,9 +8,149 @@ permalink: /code/
88# Understand the Source Code
99{: .fs-9 }
1010
11- This article explains how to read the source code of OpenNHP.
11+ This article explains the architecture and structure of the OpenNHP codebase .
1212{: .fs-6 .fw-300 }
1313
1414[ 中文版] ( /zh-cn/code/ ) {: .label .fs-4 }
1515
1616---
17+
18+ ## Repository Structure
19+
20+ OpenNHP uses a multi-module Go architecture:
21+
22+ ```
23+ opennhp/
24+ ├── nhp/ # Core protocol library (github.com/OpenNHP/opennhp/nhp)
25+ ├── endpoints/ # Network daemons (github.com/OpenNHP/opennhp/endpoints)
26+ ├── examples/ # Example implementations
27+ ├── docs/ # Documentation (Jekyll)
28+ ├── docker/ # Container configurations
29+ └── release/ # Build outputs
30+ ```
31+
32+ ## Core Library (` nhp/ ` )
33+
34+ The ` nhp ` module contains the core NHP protocol implementation:
35+
36+ | Directory | Purpose |
37+ | -----------| ---------|
38+ | ` core/ ` | Protocol implementation: packets, encryption, device management |
39+ | ` common/ ` | Shared types, message structures, error definitions |
40+ | ` utils/ ` | Helper functions: IP utilities, iptables, crypto helpers |
41+ | ` plugins/ ` | Server plugin system interfaces |
42+ | ` log/ ` | Async logging framework |
43+ | ` etcd/ ` | etcd integration for distributed configuration |
44+ | ` ebpf/ ` | eBPF programs for XDP and traffic control |
45+ | ` test/ ` | Unit tests |
46+
47+ ### Key Files in ` core/ `
48+
49+ - ` device.go ` - NHP device lifecycle and connection management
50+ - ` packet.go ` - Packet structure and header definitions
51+ - ` crypto.go ` - Cryptographic primitives (ECDH, AEAD)
52+ - ` initiator.go ` - Client-side message encryption
53+ - ` responder.go ` - Server-side message decryption
54+
55+ ## Network Daemons (` endpoints/ ` )
56+
57+ The ` endpoints ` module contains executable daemons:
58+
59+ | Component | Binary | Purpose |
60+ | -----------| --------| ---------|
61+ | ` agent/ ` | ` nhp-agent ` | Client that sends knock requests |
62+ | ` server/ ` | ` nhp-server ` | Central server handling knock validation |
63+ | ` ac/ ` | ` nhp-ac ` | Access Controller managing firewall rules |
64+ | ` db/ ` | ` nhp-db ` | Data broker for DHP (Data Hiding Protocol) |
65+ | ` kgc/ ` | ` nhp-kgc ` | Key Generation Center for IBC keys |
66+
67+ Each daemon follows a similar structure:
68+
69+ ```
70+ agent/
71+ ├── main/ # Entry point and CLI
72+ │ ├── main.go # CLI commands
73+ │ ├── export.go # C FFI exports for SDK
74+ │ └── etc/ # Configuration files
75+ ├── udpagent.go # UDP transport implementation
76+ ├── config.go # Configuration handling
77+ └── msghandler.go # Message processing
78+ ```
79+
80+ ## Cryptographic Schemes
81+
82+ OpenNHP supports two cipher schemes:
83+
84+ | Scheme | Algorithms | Use Case |
85+ | --------| -----------| ----------|
86+ | ` CIPHER_SCHEME_CURVE ` | Curve25519 + ChaCha20-Poly1305 + BLAKE2s | International |
87+ | ` CIPHER_SCHEME_GMSM ` | SM2 + SM4-GCM + SM3 | Chinese standards |
88+
89+ See [ Cryptography] ( /cryptography/ ) for detailed protocol documentation.
90+
91+ ## Plugin System
92+
93+ Server plugins extend NHP server functionality:
94+
95+ ``` go
96+ type PluginHandler interface {
97+ Init (helper *NhpServerPluginHelper) error
98+ Close () error
99+ AuthWithNHP (req *AuthRequest) (*AuthResponse, error )
100+ AuthWithHttp (req *HttpAuthRequest) (*HttpAuthResponse, error )
101+ }
102+ ```
103+
104+ See [ Server Plugin Development] ( /server_plugin/ ) for implementation guide.
105+
106+ ## SDK Architecture
107+
108+ The agent provides SDKs for multiple platforms:
109+
110+ | Platform | Output | Build Target |
111+ | ----------| --------| --------------|
112+ | Linux | ` nhp-agent.so ` | ` make linuxagentsdk ` |
113+ | macOS | ` nhp-agent.dylib ` | ` make macosagentsdk ` |
114+ | iOS | ` nhpagent.xcframework ` | ` make iosagentsdk ` |
115+ | Android | ` libnhpagent.so ` | ` make androidagentsdk ` |
116+
117+ See [ Agent SDK] ( /agent_sdk/ ) for integration documentation.
118+
119+ ## Building from Source
120+
121+ ``` bash
122+ # Initialize dependencies
123+ make init
124+
125+ # Build all binaries
126+ make
127+
128+ # Build specific components
129+ make agentd # nhp-agent
130+ make serverd # nhp-server
131+ make acd # nhp-ac
132+
133+ # Development commands
134+ make test # Run tests
135+ make fmt # Format code
136+ make clean # Clean build artifacts
137+ ```
138+
139+ ## Testing
140+
141+ Tests are located in ` nhp/test/ ` and ` endpoints/test/ ` :
142+
143+ ``` bash
144+ # Run all tests
145+ make test
146+
147+ # Run with race detection
148+ make test-race
149+
150+ # Run with coverage
151+ make coverage
152+ ```
153+
154+ ## Contributing
155+
156+ See [ CONTRIBUTING.md] ( https://github.com/OpenNHP/opennhp/blob/main/CONTRIBUTING.md ) for development guidelines.
0 commit comments