This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is the OpenIoTHub server written in Go. It acts as a central server for IoT gateway connections, providing session management, multiple protocol support (TCP, KCP/UDP, TLS, gRPC), and HTTP/HTTPS proxy services. The server manages connections from gateways and handles proxying requests to IoT devices.
go build- Build the binary locallygo test ./...- Run all tests- GoReleaser is used for multi-platform releases (see
.goreleaser.yml)
- Tests are in
*_test.gofiles. The main test is inmain_test.go. - CI runs
go test ./...on pushes and PRs via GitHub Actions (.github/workflows/test.yml).
- Default command:
./server-go(requires config fileserver-go.yaml) - Generate config:
./server-go init - Generate tokens:
./server-go generate - Test command:
./server-go test - Configuration path can be set via
--configflag orServerConfigFilePathenv var.
- Make changes to Go files
- Run
go test ./...to ensure tests pass - Run
go buildto verify compilation - Use
go mod tidyto clean up dependencies (if needed)
- Main Entry Point (
main.go): CLI setup usingurfave/cli. Commands:generate,init,test, and default server start. - Configuration (
config/): YAML-based config (server-go.yaml). Supports Redis for persistent storage. Config constants inconst.go. - Session Management (
manager/,session/):SessionsManager(manager/SessionsManager.go): Central manager for all client sessions.Session(session/Session.go): Represents individual gateway connections.- Supports multiplexed (yamux) and non-multiplexed connections.
- Network Services (launched in
main.go:run()):- TCP server on port 34320
- KCP/UDP server on port 34320 (UDP)
- TLS server on port 34321
- gRPC server on port 34322
- HTTP proxy on port 80
- HTTPS proxy on port 443
- UDP API server on port 34321
- KCP API server on port 34322
- Storage Abstraction (
iface/,imp/):- Interface
RuntimeStorageIfce(iface/runtimeStorage/runtimeStorage.go) for runtime storage. - Implementations: in-memory (
memImp/) and Redis (redisImp/). - Used for HTTP proxy configurations and session data.
- Interface
- Protocol Support: Custom messaging via
github.com/OpenIoTHub/utils/msg, gRPC, KCP, HTTP/HTTPS.
Default config file server-go.yaml includes:
my_public_ip_or_domain: Public IP/domain for token generation.common: Bind address and port settings.security: Login key, TLS certificates.redisconfig: Redis persistence settings (enable for HTTP proxy config persistence).logconfig: Log output settings.
Important ports (defaults):
- 34320: TCP and KCP/UDP
- 34321: TLS and UDP API
- 34322: gRPC and KCP API
- 80: HTTP proxy
- 443: HTTPS proxy
- By default, HTTP proxy configs are stored in memory.
- Enable Redis in config (
redisconfig.enabled: true) for persistence across restarts. - Storage interface allows swapping implementations.
- Each gateway connection is a
Sessionwith unique RunId. - Sessions map in
SessionsManagerkeyed by RunId. - Supports multiplexing via yamux for multiple streams over a single connection.
- The server uses vendored dependencies (
vendor/). - The project uses Go modules (
go.modwith Go 1.21). - Dockerfile builds a minimal image with entrypoint script.
- Releases are automated via GoReleaser and GitHub Actions, producing binaries for multiple platforms, Docker images, and package manager releases (Homebrew, Snapcraft, Scoop, DEB/RPM).
- The codebase is primarily in Chinese (comments, logs, README), but the code and architecture follow standard Go practices.