48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package wg
|
||
|
||
import (
|
||
"encoding/binary"
|
||
"fmt"
|
||
)
|
||
|
||
// WGPlugin WireGuard 协议插件实现
|
||
type WGPlugin struct{}
|
||
|
||
// NewWGPlugin 创建 WireGuard 协议插件
|
||
func NewWGPlugin() *WGPlugin {
|
||
return &WGPlugin{}
|
||
}
|
||
|
||
// IsControlPacket 判断是否为控制包
|
||
// WG 控制包类型:1 (Initiation), 2 (Response), 3 (CookieReply)
|
||
func (p *WGPlugin) IsControlPacket(packet []byte) bool {
|
||
if len(packet) < 1 {
|
||
return false
|
||
}
|
||
|
||
packetType := packet[0]
|
||
return packetType == 1 || packetType == 2 || packetType == 3
|
||
}
|
||
|
||
// IsDataPacket 判断是否为数据包
|
||
// WG 数据包类型:4
|
||
func (p *WGPlugin) IsDataPacket(packet []byte) bool {
|
||
if len(packet) < 1 {
|
||
return false
|
||
}
|
||
|
||
return packet[0] == 4
|
||
}
|
||
|
||
// ExtractRouteID 从数据包中提取路由标识(WG receiver index)
|
||
// WG 数据包格式:[类型 (1 字节)][保留 (3 字节)][receiver index (4 字节)]...
|
||
func (p *WGPlugin) ExtractRouteID(packet []byte) (uint32, error) {
|
||
if len(packet) < 8 {
|
||
return 0, fmt.Errorf("数据包过短:%d", len(packet))
|
||
}
|
||
|
||
// 读取 packet[4:8],网络字节序解析为 uint32
|
||
routeID := binary.BigEndian.Uint32(packet[4:8])
|
||
return routeID, nil
|
||
}
|