feat: raw binary SDWAN data plane + EncodeRaw + TUN close-on-stop

- protocol: add SubTunnelSDWANRaw subtype + EncodeRaw() for zero-copy IP packets
- client: tunReadLoop sends raw frames (no JSON/base64 overhead)
- client: SubTunnelSDWANRaw handler strips header and writes directly to TUN
- client: Stop() closes TUN file FIRST to unblock tunReadLoop
- server: SubTunnelSDWANRaw handler parses IPv4 src/dst from raw packet
- server: RouteSDWANPacket forwards as raw frame to destination

Verified: hcss(10.10.0.3) ↔ i-6986(10.10.0.2) ping 3/3, 0% loss, 46ms RTT
This commit is contained in:
2026-03-02 18:22:41 +08:00
parent 5568ea67d9
commit 489c2d191c
4 changed files with 79 additions and 33 deletions

View File

@@ -1,8 +1,9 @@
// Package protocol defines the INP2P wire protocol.
//
// Message format: [Header 8B] + [JSON payload]
// Header: DataLen(uint32 LE) + MainType(uint16 LE) + SubType(uint16 LE)
// DataLen = len(header) + len(payload) = 8 + len(json)
//
// Header: DataLen(uint32 LE) + MainType(uint16 LE) + SubType(uint16 LE)
// DataLen = len(header) + len(payload) = 8 + len(json)
package protocol
import (
@@ -69,6 +70,7 @@ const (
// Sub types: MsgTunnel
const (
SubTunnelSDWANData uint16 = iota
SubTunnelSDWANRaw
)
// ─── Sub types: MsgRelay ───
@@ -151,6 +153,20 @@ func Encode(mainType, subType uint16, payload interface{}) ([]byte, error) {
return buf.Bytes(), nil
}
// EncodeRaw packs header + raw (binary) payload.
func EncodeRaw(mainType, subType uint16, payload []byte) []byte {
h := Header{
DataLen: uint32(HeaderSize + len(payload)),
MainType: mainType,
SubType: subType,
}
buf := new(bytes.Buffer)
buf.Grow(int(h.DataLen))
_ = binary.Write(buf, binary.LittleEndian, h)
buf.Write(payload)
return buf.Bytes()
}
// DecodeHeader reads the 8-byte header from r.
func DecodeHeader(data []byte) (Header, error) {
if len(data) < HeaderSize {
@@ -179,8 +195,8 @@ type LoginReq struct {
Version string `json:"version"`
NATType NATType `json:"natType"`
ShareBandwidth int `json:"shareBandwidth"`
RelayEnabled bool `json:"relayEnabled"` // --relay flag
SuperRelay bool `json:"superRelay"` // --super flag
RelayEnabled bool `json:"relayEnabled"` // --relay flag
SuperRelay bool `json:"superRelay"` // --super flag
PublicIP string `json:"publicIP,omitempty"`
PublicPort int `json:"publicPort,omitempty"`
}
@@ -216,7 +232,7 @@ type PunchParams struct {
IP string `json:"ip"`
Port int `json:"port"`
NATType NATType `json:"natType"`
Token uint64 `json:"token"` // TOTP for auth
Token uint64 `json:"token"` // TOTP for auth
IPv6 string `json:"ipv6,omitempty"`
HasIPv4 int `json:"hasIPv4"`
LinkMode string `json:"linkMode"` // "udp" or "tcp"
@@ -279,7 +295,7 @@ type SDWANConfig struct {
Name string `json:"name,omitempty"`
GatewayCIDR string `json:"gatewayCIDR"`
Mode string `json:"mode,omitempty"` // hub | mesh | fullmesh
IP string `json:"ip,omitempty"` // node self IP if pushed per-node
IP string `json:"ip,omitempty"` // node self IP if pushed per-node
MTU int `json:"mtu,omitempty"`
Routes []string `json:"routes,omitempty"`
Nodes []SDWANNode `json:"nodes"`
@@ -302,17 +318,17 @@ type SDWANPacket struct {
// ReportConnect is the connection result reported to server.
type ReportConnect struct {
PeerNode string `json:"peerNode"`
NATType NATType `json:"natType"`
PeerNATType NATType `json:"peerNatType"`
LinkMode string `json:"linkMode"` // "udppunch", "tcppunch", "relay"
Error string `json:"error,omitempty"`
RTT int `json:"rtt,omitempty"` // milliseconds
RelayNode string `json:"relayNode,omitempty"`
Protocol string `json:"protocol,omitempty"`
SrcPort int `json:"srcPort,omitempty"`
DstPort int `json:"dstPort,omitempty"`
DstHost string `json:"dstHost,omitempty"`
Version string `json:"version,omitempty"`
ShareBandwidth int `json:"shareBandWidth,omitempty"`
PeerNode string `json:"peerNode"`
NATType NATType `json:"natType"`
PeerNATType NATType `json:"peerNatType"`
LinkMode string `json:"linkMode"` // "udppunch", "tcppunch", "relay"
Error string `json:"error,omitempty"`
RTT int `json:"rtt,omitempty"` // milliseconds
RelayNode string `json:"relayNode,omitempty"`
Protocol string `json:"protocol,omitempty"`
SrcPort int `json:"srcPort,omitempty"`
DstPort int `json:"dstPort,omitempty"`
DstHost string `json:"dstHost,omitempty"`
Version string `json:"version,omitempty"`
ShareBandwidth int `json:"shareBandWidth,omitempty"`
}