|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Architecture |
| 6 | + |
| 7 | +This document explains the architecture and components of Bethrou. |
| 8 | + |
| 9 | +## System Overview |
| 10 | + |
| 11 | +Bethrou consists of two main components that communicate over a peer-to-peer network: |
| 12 | + |
| 13 | +```mermaid |
| 14 | +flowchart LR |
| 15 | + A[Your Device\nApplication] -- SOCKS5 --> B[Bethrou Client] |
| 16 | + B -- libp2p --> C[Bethrou Node\nExit Node Server] |
| 17 | + C --> D[Internet\nexample.com:443] |
| 18 | +``` |
| 19 | + |
| 20 | +## Components |
| 21 | + |
| 22 | +### 1. Client |
| 23 | + |
| 24 | +The **Bethrou Client** runs on the user's device and provides a SOCKS5 proxy interface. |
| 25 | + |
| 26 | +**Responsibilities:** |
| 27 | +- Accept SOCKS5 connections from local applications |
| 28 | +- Maintain connections to one or more exit nodes |
| 29 | +- Route proxy requests to appropriate exit nodes based on strategy |
| 30 | +- Handle connection pooling and health checks |
| 31 | +- Discover new nodes (via Redis or static configuration) |
| 32 | + |
| 33 | +### 2. Node (Exit Node) |
| 34 | + |
| 35 | +The **Bethrou Node** runs on a server and acts as an exit point for proxied traffic. |
| 36 | + |
| 37 | +**Responsibilities:** |
| 38 | +- Listen for incoming libp2p connections from clients |
| 39 | +- Accept proxy requests via custom protocol over libp2p streams |
| 40 | +- Establish TCP connections to destination addresses |
| 41 | +- Forward traffic bidirectionally between client and destination |
| 42 | +- Optionally act as a relay node for NAT traversal |
| 43 | +- Publish presence to discovery service (Redis) |
| 44 | + |
| 45 | +### 3. Discovery Service (Optional) |
| 46 | + |
| 47 | +Uses **Redis pub/sub** for dynamic node discovery. |
| 48 | + |
| 49 | +**How it works:** |
| 50 | +1. Nodes publish their peer ID and addresses to a Redis topic |
| 51 | +2. Clients subscribe to the topic and discover available nodes |
| 52 | +3. Clients maintain a dynamic list of exit nodes |
| 53 | +4. Health checks remove unresponsive nodes |
| 54 | + |
| 55 | +## Network Architecture |
| 56 | + |
| 57 | +## Private Network Layer |
| 58 | + |
| 59 | +Bethrou uses **pre-shared keys (PSK)** to create isolated libp2p networks: |
| 60 | + |
| 61 | +```mermaid |
| 62 | +flowchart TB |
| 63 | + subgraph PSK[libp2p Private Network with PSK] |
| 64 | + CA[Client A] |
| 65 | + CB[Client B] |
| 66 | + N1[Node 1] |
| 67 | + N2[Node 2] |
| 68 | + R[Relay Node] |
| 69 | + CA --- N1 |
| 70 | + N1 --- N2 |
| 71 | + CB --- R |
| 72 | + R --- N1 |
| 73 | + end |
| 74 | + classDef small font-size:12px; |
| 75 | +``` |
| 76 | + |
| 77 | +**Security Properties:** |
| 78 | +- Only peers with the correct PSK can join the network |
| 79 | +- libp2p transport encryption (TLS/Noise) protects data in transit |
| 80 | +- Network is isolated from the public DHT |
| 81 | + |
| 82 | +## Communication Flow |
| 83 | + |
| 84 | +### 1. Connection Establishment |
| 85 | + |
| 86 | +```mermaid |
| 87 | +sequenceDiagram |
| 88 | + participant Client |
| 89 | + participant Node |
| 90 | + Client->>Node: libp2p connect (PSK auth) |
| 91 | + Node-->>Client: Connection established |
| 92 | + Client->>Node: keep-alive / ping |
| 93 | + Node-->>Client: pong |
| 94 | +``` |
| 95 | + |
| 96 | +### 2. Proxy Request |
| 97 | + |
| 98 | +```mermaid |
| 99 | +sequenceDiagram |
| 100 | + participant Client |
| 101 | + participant Node |
| 102 | + participant Internet |
| 103 | + Client->>Node: Proxy Request (target: example.com:443) |
| 104 | + Node->>Internet: TCP Connect to example.com:443 |
| 105 | + Internet-->>Node: Connection OK |
| 106 | + Node-->>Client: Proxy Response (OK) |
| 107 | + Client->>Node: Application Data |
| 108 | + Node->>Internet: Forward Data |
| 109 | + Internet-->>Node: Response Data |
| 110 | + Node-->>Client: Forward Response |
| 111 | +``` |
| 112 | + |
| 113 | +## Routing Strategies |
| 114 | + |
| 115 | +Clients can use different strategies to select exit nodes: |
| 116 | + |
| 117 | +### Random |
| 118 | +Selects a random healthy node for each connection. |
| 119 | + |
| 120 | +**Pros:** Simple, good load distribution |
| 121 | +**Cons:** No optimization for performance |
| 122 | + |
| 123 | +### Round-Robin |
| 124 | +Rotates through available nodes in order. |
| 125 | + |
| 126 | +**Pros:** Fair distribution, predictable |
| 127 | +**Cons:** May route to slower nodes |
| 128 | + |
| 129 | +### Fastest |
| 130 | +Selects the node with the lowest latency. |
| 131 | + |
| 132 | +**Pros:** Best performance |
| 133 | +**Cons:** All traffic may route through one node |
| 134 | + |
| 135 | +## NAT Traversal |
| 136 | + |
| 137 | +Bethrou supports NAT traversal through relay nodes: |
| 138 | + |
| 139 | +```mermaid |
| 140 | +flowchart LR |
| 141 | + Client[Client behind NAT] -->|Connect via relay| Relay[Relay public] |
| 142 | + Relay -->|Circuit Relay| Node[Node behind NAT] |
| 143 | + Relay -->|Traffic flows through relay| Client |
| 144 | +``` |
| 145 | + |
| 146 | +**How it works:** |
| 147 | + |
| 148 | +1. Relay nodes run with `--relay-mode` flag |
| 149 | + |
| 150 | +2. Clients and nodes behind NAT connect to relay |
| 151 | + |
| 152 | +3. Relay facilitates connection between NAT'd peers |
| 153 | + |
| 154 | +4. Once connected, data flows through relay (circuit relay) |
| 155 | + |
| 156 | +## Security Considerations |
| 157 | + |
| 158 | +### Trust Model |
| 159 | + |
| 160 | +- **Clients trust nodes**: Exit nodes can observe all proxied traffic |
| 161 | +- **Nodes trust clients**: Nodes forward traffic for authenticated clients |
| 162 | +- **PSK provides authentication**: Only holders of the network key can participate |
| 163 | + |
| 164 | +### Best Practices |
| 165 | + |
| 166 | +1. **Trust your exit nodes**: Only use nodes you control or trust |
| 167 | +2. **Use HTTPS**: Exit nodes cannot decrypt HTTPS traffic |
| 168 | +3. **Rotate PSK**: Change network keys periodically |
| 169 | +4. **Monitor nodes**: Track which nodes are active in your network |
| 170 | +5. **Limit access**: Don't share PSK with untrusted parties |
| 171 | + |
| 172 | +## Performance Characteristics |
| 173 | + |
| 174 | +### Latency |
| 175 | + |
| 176 | +```text |
| 177 | +Total Latency = Client → Node + Node → Destination |
| 178 | +
|
| 179 | +Typical overhead: 10-50ms depending on: |
| 180 | +- Physical distance to exit node |
| 181 | +- libp2p transport type (TCP, QUIC, WebRTC) |
| 182 | +- Network conditions |
| 183 | +``` |
| 184 | + |
| 185 | +### Throughput |
| 186 | + |
| 187 | +- **Limited by**: Slowest link in the chain |
| 188 | +- **Bottleneck**: Usually the exit node's internet connection |
| 189 | +- **Connection pooling**: Reuses libp2p streams for efficiency |
| 190 | + |
| 191 | +### Scalability |
| 192 | + |
| 193 | +- **Clients**: Can connect to multiple nodes for redundancy |
| 194 | +- **Nodes**: Can handle hundreds of concurrent clients |
| 195 | +- **Discovery**: Redis pub/sub scales to thousands of nodes |
| 196 | + |
| 197 | +## Next Steps |
| 198 | + |
| 199 | +- Learn about [libp2p concepts](./libp2p.md) |
| 200 | +- Understand [SOCKS5 protocol](./socks5.md) |
| 201 | +- Review [security model](./security.md) |
0 commit comments