How zhuhaow/nekit works
Project type and purpose
• NEKit is an open-source Swift library (deprecated) that implements a custom networking “stack” and proxy framework for iOS and macOS.
• It provides low-level components (raw sockets, DNS, TCP/UDP stacks, proxy servers, tunnel abstraction, rule engine, crypto utilities) intended for building VPN-like or proxy apps.
File structure
• Top-level targets:
• NEKit-iOS and NEKit-macOS frameworks (Objective-C headers & Info.plist)
• NEKitDemo app (macOS/iOS demo)
• Test folders for unit specs
• Core library under src/:
• Config (parsers, configuration model)
• Crypto (HMAC, MD5, Libsodium wrappers, stream crypto)
• GeoIP (country lookup)
• Event (observer pattern for logging/debug events)
• IPStack (DNS, packet parsing, router, TUN interface, TCP/UDP stacks)
• RawSocket (platform-specific socket factories for TCP/UDP, TUN)
• ProxyServer (HTTP, SOCKS5, generic GCD server)
• Socket adapters (AdapterSocket: factories for HTTP/SOCKS5/Shadowsocks, direct, speed test, reject)
• ProxySocket (client-side proxy protocols)
• Tunnel (abstract queue/tunnel for chained proxies)
• Rule (domain/IP/country rules, manager)
• Utils (scanners, buffers, IP types, atomic, checksums…)
• ResponseGenerator (to craft HTTP/SOCKS5 responses)
README for additional insights
• Author notes the library houses many patterns (factories, observer) but lacks an ideal architecture.
• Emphasis on developer understanding of I/O, packet flow, concurrency (GCD).
1) Demo App (entry point)
2) NEKit Framework (shared code)
– Configuration Layer
– Rule Engine
– Event Dispatcher
– GeoIP Service
– Crypto Module
– ProxyServer Module
– IPStack Module (DNS Resolver, Packet Parser, Router, TCP/UDP Stacks, TUN Interface)
– RawSocket Layer (GCD/NW sockets, TUN socket factory)
– Socket Adapter Layer (AdapterSocket factories & adapters)
– ProxySocket Layer (client-side proxies)
– Tunnel Abstraction
– ResponseGenerator Factory
– Utilities
– Demo App → Configuration (load rules, adapter settings)
– Configuration → Rule Engine (construct RuleManager)
– Rule Engine → Router (rule matching per packet/connection)
– Router → Tunnel (decide direct or chain through proxy adapters)
– Tunnel → Socket Adapter Layer → RawSocket Layer (establish socket or TUN interface)
– For inbound DNS: IPStack.DNS Resolver → RawSocket → Packet Parser → Router
– ProxyServer Module (listening servers) → RawSocket Layer → ProxySocket Layer → Rule Engine → ResponseGenerator → Event Dispatcher
– Crypto Module used by ShadowsocksAdapter & HMAC utilities in adapter layer
– Event Dispatcher observes significant events across modules (proxy connections, rule matches, errors)
– GeoIP consulted by CountryRule during rule matching
c. Architectural patterns & principles
– Factory pattern heavily in AdapterSocketFactory, RawSocketFactory, ResponseGeneratorFactory
– Observer pattern in Event/ObserverFactory
– Single Responsibility: each subfolder handles a cohesive concern
– Layered design: Configuration → Rule → Routing → Transport → Socket → OS
– Protocol-oriented Swift (protocols for Stack, Packet, Socket)
d. Key technologies & frameworks
– Swift (source code)
– Grand Central Dispatch (GCD) for concurrency
– Network framework (NWTCPSocket, NWUDPSocket)
– CommonCrypto or Libsodium for crypto
– Apple’s NetworkExtension API (when used by demo app or in host app)
Details to capture
• Platform targets (iOS vs macOS frameworks) can be side-by-side or combined under “NEKit Framework.”
• Demo App linking against NEKit-iOS/macOS.
• Call out test suite as a separate, nonproduction component.
• How the TUN interface slot sits between IPStack and RawSocket.