Add comprehensive console logging at key data path points: - [TUN-RX]: per-packet TUN read (size, proto, src/dst IP, slow write detection) - [TUN-TX]: per-packet TUN write (size, success/error) - [WS-RX]: per-packet WebSocket read from client (size, proto, src/dst IP) - [WS-TX]: per-packet WebSocket write to client (size, lockWaitMs, writeMs) - [ROUTE]: routing decisions (TUN->client, client->tun, client->relay, anti-spoof) - [CONN]: connection lifecycle (init, ready, stats every 10s, disconnect with final stats) - [STATS]: global traffic stats every 10s (clients, rx/tx bytes and rates) - [PING]: WebSocket ping failures - [WS]: WebSocket upgrade with client IP and auth method - [TUN]: MTU set confirmation, VPN start/stop environment info
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
//go:build darwin
|
|
|
|
package vpn
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
)
|
|
|
|
func inetFamily(ip net.IP) string {
|
|
if ip.To4() == nil {
|
|
return "inet6"
|
|
}
|
|
return "inet"
|
|
}
|
|
|
|
func (t *TUNInterface) Configure(localIP net.IP, prefix int, peerIP net.IP) error {
|
|
if localIP == nil {
|
|
return execCmd("ifconfig", t.Name(), "up")
|
|
}
|
|
localCidr := fmt.Sprintf("%s/%d", localIP.String(), prefix)
|
|
inetType := inetFamily(localIP)
|
|
if t.Iface.IsTUN() && inetType == "inet" && peerIP != nil {
|
|
return execCmd("ifconfig", t.Name(), inetType, localCidr, peerIP.String(), "up")
|
|
}
|
|
return execCmd("ifconfig", t.Name(), inetType, localCidr, "up")
|
|
}
|
|
|
|
func (t *TUNInterface) AddSubnetRoute(subnet *net.IPNet) error {
|
|
inetType := inetFamily(subnet.IP)
|
|
return execCmd("route", "add", fmt.Sprintf("-%s", inetType), "-net", subnet.String(), "-interface", t.Name())
|
|
}
|
|
|
|
func (t *TUNInterface) SetMTU(mtu int) error {
|
|
err := execCmd("ifconfig", t.Name(), "mtu", fmt.Sprintf("%d", mtu))
|
|
if err != nil {
|
|
log.Printf("[TUN] set MTU dev=%s mtu=%d ok=false err=%v", t.Name(), mtu, err)
|
|
} else {
|
|
log.Printf("[TUN] set MTU dev=%s mtu=%d ok=true", t.Name(), mtu)
|
|
}
|
|
return err
|
|
}
|