Documentation
¶
Overview ¶
Package mobile is the gomobile binding of the tunnel package for Android.
Design notes:
Config and status are passed as JSON strings. gomobile cannot bind maps, nested structs, time.Duration, uint64 or cross-package pointers; JSON lets fields evolve without changing the binding layer and the Kotlin side in lockstep.
Global single instance. Android never needs more than one tunnel at a time, and a singleton means Stop and Snapshot need no handle argument. A second Start returns an explicit error instead of silently replacing the running tunnel.
Every exported function recovers from panics. A Go panic does not become a Java exception; it kills the whole app process with a fatal crash that Kotlin's try/catch cannot catch, because the gomobile runtime never recovers.
Index ¶
- func RunSTUN(server, iface string, timeoutMillis int) (out string, err error)
- func RunTCPNAT(server, ports, iface, proxy string, localPort, rounds, timeoutMillis int) (out string, err error)
- func Snapshot() (out string)
- func StartClient(configJSON string, observer Observer) (err error)
- func StartServer(configJSON string, observer Observer) (err error)
- func Stop() (err error)
- func ValidateConfig(role string, configJSON string) (out string)
- func Version() string
- type Observer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunSTUN ¶
RunSTUN runs STUN NAT type detection and returns the result as JSON.
An empty server selects the default STUN server; an empty iface lets the kernel choose the route; timeoutMillis <= 0 uses the default timeout.
Even when an error is returned, the Detail field of the JSON may contain partial output, which is worth showing to the user for troubleshooting.
func RunTCPNAT ¶
func RunTCPNAT(server, ports, iface, proxy string, localPort, rounds, timeoutMillis int) (out string, err error)
RunTCPNAT probes TCP NAT mapping behavior and returns the result as JSON.
ports is a comma-separated list of at least two distinct ports; localPort 0 picks a fresh local port each round; timeoutMillis <= 0 uses the default timeout.
func Snapshot ¶
func Snapshot() (out string)
Snapshot returns the current status as JSON. When nothing is running it returns a snapshot whose State is "stopped".
func StartClient ¶
StartClient starts a client from a JSON config. It returns an error if a tunnel is already running.
configJSON has the same format as the client config file. cfg.Validate() applies exactly the same rules as the tunnel package's file loader (LoadClientConfig), so its messages can be shown to the user directly and a config that validates never fails to start for validation reasons.
func StartServer ¶
StartServer starts a server from a JSON config. It returns an error if a tunnel is already running.
It runs a NAT self-check before starting: with the birthday strategy, a symmetric NAT (NAT4) makes it refuse to start.
func Stop ¶
func Stop() (err error)
Stop stops the current instance. It is a no-op when nothing is running and is safe to call repeatedly.
If a Start is running build/Start outside the lock (current is still nil), Stop must not conclude there is nothing to stop: the user would think the tunnel stopped while it finishes starting and keeps running in the background. In that case Stop only sets stopRequested and returns at once (it does not wait, to avoid an ANR when called on the Android main thread); startRunner abandons the start and cleans up when it finishes.
func ValidateConfig ¶
ValidateConfig validates a config without starting anything; the config screen uses it as a pre-save check.
It returns an empty string if the config is valid, otherwise an error message that can be shown to the user. role is "client" or "server".
It reuses the tunnel package's validation, the same rules applied on a real start, so a config never passes here and then fails validation at start time.
Types ¶
type Observer ¶
type Observer interface {
// OnStateChange delivers a full status snapshot (JSON) on every state transition.
OnStateChange(statusJSON string)
// OnStats delivers a TCP sample (JSON) every 5 seconds.
OnStats(statsJSON string)
// OnLog delivers a single log line. level is "debug", "info", "warn" or "error".
OnLog(level string, line string)
}
Observer receives tunnel events. It is implemented on the Kotlin side.
All parameters are strings: gomobile cannot bind complex types, so status and stats are passed as JSON. OnLog takes level as a separate argument rather than inside JSON because the UI colors lines by level and should not have to parse every line.
These callbacks are synchronous, blocking JNI calls, not asynchronous deliveries. Heavy work in an implementation blocks the Go goroutine; UI updates should be posted to the main thread.