初版功能完成
ci / Go checks (ubuntu-latest) (push) Has been cancelled
ci / Go checks (windows-latest) (push) Has been cancelled

This commit is contained in:
qsc
2026-08-29 13:12:17 +08:00
commit 142e5dc7d6
217 changed files with 21313 additions and 0 deletions
@@ -0,0 +1,80 @@
// Package route owns RemLink Remote routes and performs conflict/lookup checks.
package route
import (
"errors"
"net/netip"
"remlink/internal/platform/windows/netinfo"
)
type LookupResult string
const (
LookupDirect LookupResult = "DIRECT"
LookupRouted LookupResult = "ROUTED"
LookupDefaultOnly LookupResult = "DEFAULT_ONLY"
LookupNoRoute LookupResult = "NO_ROUTE"
LookupOverlayConflict LookupResult = "OVERLAY_CONFLICT"
)
type Entry struct {
InterfaceLUID uint64
Destination netip.Prefix
NextHop netip.Addr
}
// ConflictsFrom applies the Engineer rule: ignore default and RemLink-owned
// routes, reject any other prefix overlap.
func ConflictsFrom(entries []Entry, desired netip.Prefix, remLinkLUID uint64) []Entry {
var conflicts []Entry
for _, entry := range entries {
if entry.InterfaceLUID == remLinkLUID || !entry.Destination.Addr().Is4() || entry.Destination.Bits() == 0 {
continue
}
if netinfo.PrefixesOverlap(desired, entry.Destination) {
conflicts = append(conflicts, entry)
}
}
return conflicts
}
// LookupFrom returns the most-specific Windows route classification used by Site PREPARE.
func LookupFrom(entries []Entry, target netip.Addr, overlay netip.Prefix, remLinkLUID uint64) LookupResult {
if !target.Is4() {
return LookupNoRoute
}
if overlay.Contains(target) {
return LookupOverlayConflict
}
var best *Entry
for index := range entries {
entry := &entries[index]
if !entry.Destination.Contains(target) || entry.InterfaceLUID == remLinkLUID {
continue
}
if best == nil || entry.Destination.Bits() > best.Destination.Bits() {
best = entry
}
}
if best == nil {
return LookupNoRoute
}
if best.Destination.Bits() == 0 {
return LookupDefaultOnly
}
if !best.NextHop.IsValid() || best.NextHop.IsUnspecified() {
return LookupDirect
}
return LookupRouted
}
func validateRemote(prefix, overlay netip.Prefix) error {
if !prefix.Addr().Is4() || prefix != prefix.Masked() || prefix.Bits() == 0 {
return errors.New("Remote CIDR must be canonical IPv4 and must not be 0.0.0.0/0")
}
if netinfo.PrefixesOverlap(prefix, overlay) {
return errors.New("Remote CIDR overlaps Overlay CIDR")
}
return nil
}
@@ -0,0 +1,55 @@
package route
import (
"net/netip"
"testing"
)
func TestConflictsIgnoresDefaultAndRemLink(t *testing.T) {
entries := []Entry{
{InterfaceLUID: 1, Destination: netip.MustParsePrefix("0.0.0.0/0"), NextHop: netip.MustParseAddr("192.0.2.1")},
{InterfaceLUID: 2, Destination: netip.MustParsePrefix("192.168.0.0/16")},
{InterfaceLUID: 99, Destination: netip.MustParsePrefix("192.168.13.0/24")},
}
conflicts := ConflictsFrom(entries, netip.MustParsePrefix("192.168.13.0/24"), 99)
if len(conflicts) != 1 || conflicts[0].Destination.String() != "192.168.0.0/16" {
t.Fatalf("conflicts = %+v", conflicts)
}
}
func TestLookupClassifications(t *testing.T) {
overlay := netip.MustParsePrefix("10.88.0.0/16")
entries := []Entry{
{InterfaceLUID: 1, Destination: netip.MustParsePrefix("0.0.0.0/0"), NextHop: netip.MustParseAddr("192.0.2.1")},
{InterfaceLUID: 2, Destination: netip.MustParsePrefix("192.168.13.0/24"), NextHop: netip.IPv4Unspecified()},
{InterfaceLUID: 3, Destination: netip.MustParsePrefix("172.16.0.0/16"), NextHop: netip.MustParseAddr("192.0.2.254")},
}
for _, test := range []struct {
target string
want LookupResult
}{
{"10.88.0.5", LookupOverlayConflict},
{"192.168.13.10", LookupDirect},
{"172.16.4.2", LookupRouted},
{"8.8.8.8", LookupDefaultOnly},
} {
if got := LookupFrom(entries, netip.MustParseAddr(test.target), overlay, 99); got != test.want {
t.Errorf("LookupFrom(%s) = %s, want %s", test.target, got, test.want)
}
}
if got := LookupFrom(nil, netip.MustParseAddr("192.168.1.2"), overlay, 99); got != LookupNoRoute {
t.Fatalf("empty route lookup = %s", got)
}
}
func TestValidateRemote(t *testing.T) {
overlay := netip.MustParsePrefix("10.88.0.0/16")
for _, invalid := range []string{"0.0.0.0/0", "10.88.5.0/24"} {
if err := validateRemote(netip.MustParsePrefix(invalid), overlay); err == nil {
t.Errorf("validateRemote accepted %s", invalid)
}
}
if err := validateRemote(netip.MustParsePrefix("192.168.13.0/24"), overlay); err != nil {
t.Fatal(err)
}
}
@@ -0,0 +1,26 @@
//go:build !windows
package route
import (
"errors"
"net/netip"
)
var ErrWindowsRequired = errors.New("Windows RouteManager requires Windows")
type OwnershipStore interface {
LoadOwnedRoutes() ([]netip.Prefix, error)
SaveOwnedRoutes([]netip.Prefix) error
}
type Manager struct{}
func NewManager(uint64, netip.Prefix, OwnershipStore) (*Manager, error) {
return nil, ErrWindowsRequired
}
func (*Manager) AddRemote(netip.Prefix) error { return ErrWindowsRequired }
func (*Manager) RemoveRemote(netip.Prefix) error { return ErrWindowsRequired }
func (*Manager) Conflicts(netip.Prefix) ([]Entry, error) { return nil, ErrWindowsRequired }
func (*Manager) Lookup(netip.Addr) (LookupResult, error) { return LookupNoRoute, ErrWindowsRequired }
func (*Manager) Reconcile() error { return ErrWindowsRequired }
@@ -0,0 +1,151 @@
//go:build windows
package route
import (
"errors"
"fmt"
"net/netip"
"sync"
"golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
)
const remoteRouteMetric = 0
type OwnershipStore interface {
LoadOwnedRoutes() ([]netip.Prefix, error)
SaveOwnedRoutes([]netip.Prefix) error
}
// Manager is the sole writer for Engineer Remote CIDR routes.
type Manager struct {
mu sync.Mutex
luid winipcfg.LUID
overlay netip.Prefix
store OwnershipStore
owned map[netip.Prefix]struct{}
}
func NewManager(luid uint64, overlay netip.Prefix, store OwnershipStore) (*Manager, error) {
if luid == 0 || !overlay.Addr().Is4() || store == nil {
return nil, errors.New("RouteManager requires RemLink LUID, IPv4 Overlay, and ownership store")
}
ownedRoutes, err := store.LoadOwnedRoutes()
if err != nil {
return nil, err
}
owned := make(map[netip.Prefix]struct{}, len(ownedRoutes))
for _, prefix := range ownedRoutes {
owned[prefix.Masked()] = struct{}{}
}
return &Manager{luid: winipcfg.LUID(luid), overlay: overlay.Masked(), store: store, owned: owned}, nil
}
func (m *Manager) AddRemote(prefix netip.Prefix) error {
prefix = prefix.Masked()
if err := validateRemote(prefix, m.overlay); err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.owned[prefix]; exists {
return nil
}
conflicts, err := m.conflictsLocked(prefix)
if err != nil {
return err
}
if len(conflicts) != 0 {
return fmt.Errorf("Remote CIDR %s conflicts with existing route %s", prefix, conflicts[0].Destination)
}
if err := m.luid.AddRoute(prefix, netip.IPv4Unspecified(), remoteRouteMetric); err != nil {
return fmt.Errorf("add RemLink Remote route %s: %w", prefix, err)
}
m.owned[prefix] = struct{}{}
if err := m.persistLocked(); err != nil {
delete(m.owned, prefix)
_ = m.luid.DeleteRoute(prefix, netip.IPv4Unspecified())
return err
}
return nil
}
func (m *Manager) RemoveRemote(prefix netip.Prefix) error {
prefix = prefix.Masked()
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.owned[prefix]; !exists {
return nil
}
if err := m.luid.DeleteRoute(prefix, netip.IPv4Unspecified()); err != nil && !errors.Is(err, windows.ERROR_NOT_FOUND) {
return fmt.Errorf("remove RemLink Remote route %s: %w", prefix, err)
}
delete(m.owned, prefix)
return m.persistLocked()
}
func (m *Manager) Conflicts(prefix netip.Prefix) ([]Entry, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.conflictsLocked(prefix.Masked())
}
func (m *Manager) Lookup(target netip.Addr) (LookupResult, error) {
entries, err := windowsEntries()
if err != nil {
return LookupNoRoute, err
}
return LookupFrom(entries, target, m.overlay, uint64(m.luid)), nil
}
// Reconcile removes every route recorded by a previous non-Active Session.
func (m *Manager) Reconcile() error {
m.mu.Lock()
defer m.mu.Unlock()
for prefix := range m.owned {
if err := m.luid.DeleteRoute(prefix, netip.IPv4Unspecified()); err != nil && !errors.Is(err, windows.ERROR_NOT_FOUND) {
return fmt.Errorf("reconcile stale Remote route %s: %w", prefix, err)
}
delete(m.owned, prefix)
}
return m.persistLocked()
}
func (m *Manager) conflictsLocked(prefix netip.Prefix) ([]Entry, error) {
entries, err := windowsEntries()
if err != nil {
return nil, err
}
return ConflictsFrom(entries, prefix, uint64(m.luid)), nil
}
func (m *Manager) persistLocked() error {
prefixes := make([]netip.Prefix, 0, len(m.owned))
for prefix := range m.owned {
prefixes = append(prefixes, prefix)
}
if err := m.store.SaveOwnedRoutes(prefixes); err != nil {
return fmt.Errorf("persist RemLink route ownership: %w", err)
}
return nil
}
func windowsEntries() ([]Entry, error) {
rows, err := winipcfg.GetIPForwardTable2(winipcfg.AddressFamily(windows.AF_INET))
if err != nil {
return nil, fmt.Errorf("read Windows IPv4 route table: %w", err)
}
entries := make([]Entry, 0, len(rows))
for index := range rows {
prefix := rows[index].DestinationPrefix.Prefix()
if !prefix.Addr().Is4() {
continue
}
entries = append(entries, Entry{
InterfaceLUID: uint64(rows[index].InterfaceLUID), Destination: prefix.Masked(), NextHop: rows[index].NextHop.Addr(),
})
}
return entries, nil
}