Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Simple transparent proxy library.

For Linux, Windows, macOS and iOS.
For Linux, FreeBSD, Windows, macOS and iOS.

## License

Expand Down
167 changes: 167 additions & 0 deletions monitor_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package tun

import (
"net"
"net/netip"
"os"
"sync"

"github.com/metacubex/sing/common/buf"
"github.com/metacubex/sing/common/control"
E "github.com/metacubex/sing/common/exceptions"
"github.com/metacubex/sing/common/logger"
"github.com/metacubex/sing/common/x/list"

"golang.org/x/net/route"
"golang.org/x/sys/unix"
)

type networkUpdateMonitor struct {
access sync.Mutex
callbacks list.List[NetworkUpdateCallback]
routeSocketFile *os.File
closeOnce sync.Once
done chan struct{}
logger logger.Logger
}

func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
return &networkUpdateMonitor{
logger: logger,
done: make(chan struct{}),
}, nil
}

func (m *networkUpdateMonitor) Start() error {
go m.loopUpdate()
return nil
}

func (m *networkUpdateMonitor) loopUpdate() {
for {
select {
case <-m.done:
return
default:
}
err := m.loopUpdate0()
if err != nil {
m.logger.Error("listen network update: ", err)
return
}
}
}

func (m *networkUpdateMonitor) loopUpdate0() error {
routeSocket, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, 0)
if err != nil {
return err
}
err = unix.SetNonblock(routeSocket, true)
if err != nil {
unix.Close(routeSocket)
return err
}
routeSocketFile := os.NewFile(uintptr(routeSocket), "route")
defer routeSocketFile.Close()
m.routeSocketFile = routeSocketFile
m.loopUpdate1(routeSocketFile)
return nil
}

func (m *networkUpdateMonitor) loopUpdate1(routeSocketFile *os.File) {
buffer := buf.NewPacket()
defer buffer.Release()
done := make(chan struct{})
go func() {
select {
case <-m.done:
routeSocketFile.Close()
case <-done:
}
}()
n, err := routeSocketFile.Read(buffer.FreeBytes())
close(done)
if err != nil {
return
}
buffer.Truncate(n)
messages, err := route.ParseRIB(route.RIBTypeRoute, buffer.Bytes())
if err != nil {
return
}
for _, message := range messages {
if _, isRouteMessage := message.(*route.RouteMessage); isRouteMessage {
m.emit()
return
}
}
}

func (m *networkUpdateMonitor) Close() error {
m.closeOnce.Do(func() {
close(m.done)
})
return nil
}

func (m *defaultInterfaceMonitor) checkUpdate() error {
ribMessage, err := route.FetchRIB(unix.AF_UNSPEC, route.RIBTypeRoute, 0)
if err != nil {
return err
}
routeMessages, err := route.ParseRIB(route.RIBTypeRoute, ribMessage)
if err != nil {
return err
}
var defaultInterface *control.Interface
for _, rawRouteMessage := range routeMessages {
routeMessage := rawRouteMessage.(*route.RouteMessage)
if len(routeMessage.Addrs) <= unix.RTAX_NETMASK {
continue
}
destination, isIPv4Destination := routeMessage.Addrs[unix.RTAX_DST].(*route.Inet4Addr)
if !isIPv4Destination {
continue
}
if destination.IP != netip.IPv4Unspecified().As4() {
continue
}
mask, isIPv4Mask := routeMessage.Addrs[unix.RTAX_NETMASK].(*route.Inet4Addr)
if !isIPv4Mask {
continue
}
ones, _ := net.IPMask(mask.IP[:]).Size()
if ones != 0 {
continue
}
routeInterface, err := m.interfaceFinder.ByIndex(routeMessage.Index)
if err != nil {
return err
}
if routeMessage.Flags&unix.RTF_UP == 0 {
continue
}
if routeMessage.Flags&unix.RTF_GATEWAY == 0 {
continue
}
if routeInterface.Flags&net.FlagLoopback != 0 {
continue
}
defaultInterface = routeInterface
break
}
if defaultInterface == nil {
return ErrNoRoute
}
newInterface, err := m.interfaceFinder.ByIndex(defaultInterface.Index)
if err != nil {
return E.Cause(err, "find updated interface: ", defaultInterface.Name)
}
oldInterface := m.defaultInterface.Swap(newInterface)
if oldInterface != nil && oldInterface.Equals(*newInterface) {
return nil
}
m.emit(newInterface, 0)
return nil
}
2 changes: 1 addition & 1 deletion monitor_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !(linux || windows || darwin)
//go:build !(linux || windows || darwin || freebsd)

package tun

Expand Down
2 changes: 1 addition & 1 deletion monitor_shared.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || windows || darwin
//go:build linux || windows || darwin || freebsd

package tun

Expand Down
8 changes: 8 additions & 0 deletions tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ type Options struct {
FileDescriptor int
Logger logger.Logger

// FreeBSDInterfaceDescription, when non-empty, is written to the tun(4)
// interface's description field (ifconfig description) on FreeBSD. FreeBSD
// preserves this description after the owning process exits, so the caller
// can use it as a marker to identify and clean up tun devices it created.
// sing-tun itself attaches no meaning to the value; the caller (e.g. mihomo)
// owns both the marker string and any cleanup logic. Ignored on other platforms.
FreeBSDInterfaceDescription string

// No work for TCP, do not use.
_TXChecksumOffload bool

Expand Down
Loading