Make gtcpip public for external use

This commit is contained in:
世界 2026-04-03 02:07:40 +08:00
parent f8e7794cc2
commit a9027729be
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
42 changed files with 45 additions and 57 deletions

View file

@ -20,8 +20,8 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
) )
// PseudoHeaderChecksum calculates the pseudo-header checksum for the given // PseudoHeaderChecksum calculates the pseudo-header checksum for the given

View file

@ -17,7 +17,7 @@ package header
import ( import (
"encoding/binary" "encoding/binary"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
) )
const ( const (

View file

@ -17,8 +17,8 @@ package header
import ( import (
"encoding/binary" "encoding/binary"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
) )
// ICMPv4 represents an ICMPv4 header stored in a byte array. // ICMPv4 represents an ICMPv4 header stored in a byte array.

View file

@ -17,8 +17,8 @@ package header
import ( import (
"encoding/binary" "encoding/binary"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
) )
// ICMPv6 represents an ICMPv6 header stored in a byte array. // ICMPv6 represents an ICMPv6 header stored in a byte array.

View file

@ -17,7 +17,7 @@ package header
import ( import (
"net/netip" "net/netip"
tcpip "github.com/sagernet/sing-tun/internal/gtcpip" tcpip "github.com/sagernet/sing-tun/gtcpip"
) )
const ( const (

View file

@ -20,8 +20,8 @@ import (
"net/netip" "net/netip"
"time" "time"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
) )

View file

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"net/netip" "net/netip"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
) )
const ( const (

View file

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"math" "math"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
) )

View file

@ -17,7 +17,7 @@ package header
import ( import (
"encoding/binary" "encoding/binary"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
) )
const ( const (

View file

@ -14,7 +14,7 @@
package header package header
import "github.com/sagernet/sing-tun/internal/gtcpip" import "github.com/sagernet/sing-tun/gtcpip"
// NDPNeighborAdvert is an NDP Neighbor Advertisement message. It will // NDPNeighborAdvert is an NDP Neighbor Advertisement message. It will
// only contain the body of an ICMPv6 packet. // only contain the body of an ICMPv6 packet.

View file

@ -14,7 +14,7 @@
package header package header
import "github.com/sagernet/sing-tun/internal/gtcpip" import "github.com/sagernet/sing-tun/gtcpip"
// NDPNeighborSolicit is an NDP Neighbor Solicitation message. It will only // NDPNeighborSolicit is an NDP Neighbor Solicitation message. It will only
// contain the body of an ICMPv6 packet. // contain the body of an ICMPv6 packet.

View file

@ -23,7 +23,7 @@ import (
"math" "math"
"time" "time"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
) )

View file

@ -17,9 +17,9 @@ package header
import ( import (
"encoding/binary" "encoding/binary"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing-tun/internal/gtcpip/seqnum" "github.com/sagernet/sing-tun/gtcpip/seqnum"
"github.com/google/btree" "github.com/google/btree"
) )

View file

@ -18,8 +18,8 @@ import (
"encoding/binary" "encoding/binary"
"math" "math"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
) )
const ( const (

View file

@ -4,7 +4,7 @@ import (
"crypto/rand" "crypto/rand"
"testing" "testing"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing-tun/internal/tschecksum" "github.com/sagernet/sing-tun/internal/tschecksum"
) )

View file

@ -8,7 +8,7 @@ import (
"net/netip" "net/netip"
"sync/atomic" "sync/atomic"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"

View file

@ -10,8 +10,7 @@ import (
"time" "time"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/control" "github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
@ -30,7 +29,6 @@ type Destination struct {
destination netip.Addr destination netip.Addr
routeContext tun.DirectRouteContext routeContext tun.DirectRouteContext
timeout time.Duration timeout time.Duration
lastActive common.TypedValue[time.Time]
requestAccess sync.Mutex requestAccess sync.Mutex
requests map[pingRequest]time.Time requests map[pingRequest]time.Time
} }
@ -75,7 +73,6 @@ func ConnectDestination(
timeout: timeout, timeout: timeout,
requests: make(map[pingRequest]time.Time), requests: make(map[pingRequest]time.Time),
} }
d.lastActive.Store(time.Now())
go d.loopRead() go d.loopRead()
return d, nil return d, nil
} }
@ -83,21 +80,14 @@ func ConnectDestination(
func (d *Destination) loopRead() { func (d *Destination) loopRead() {
defer d.Close() defer d.Close()
for { for {
deadline := d.lastActive.Load().Add(d.timeout) buffer := buf.NewSize(maxICMPPacketSize)
if !time.Now().Before(deadline) { err := d.conn.SetReadDeadline(time.Now().Add(d.timeout))
return
}
err := d.conn.SetReadDeadline(deadline)
if err != nil { if err != nil {
d.logger.ErrorContext(d.ctx, E.Cause(err, "set read deadline for ICMP conn")) d.logger.ErrorContext(d.ctx, E.Cause(err, "set read deadline for ICMP conn"))
} }
buffer := buf.NewSize(maxICMPPacketSize)
err = d.conn.ReadIP(buffer) err = d.conn.ReadIP(buffer)
if err != nil { if err != nil {
buffer.Release() buffer.Release()
if E.IsTimeout(err) {
continue
}
if !E.IsClosed(err) { if !E.IsClosed(err) {
d.logger.ErrorContext(d.ctx, E.Cause(err, "receive ICMP echo reply")) d.logger.ErrorContext(d.ctx, E.Cause(err, "receive ICMP echo reply"))
} }
@ -168,7 +158,6 @@ func (d *Destination) loopRead() {
} }
d.logger.TraceContext(d.ctx, "read ICMPv6 echo reply from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr(), " id ", icmpHdr.Ident(), " seq ", icmpHdr.Sequence()) d.logger.TraceContext(d.ctx, "read ICMPv6 echo reply from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr(), " id ", icmpHdr.Ident(), " seq ", icmpHdr.Sequence())
} }
d.lastActive.Store(time.Now())
err = d.routeContext.WritePacket(buffer.Bytes()) err = d.routeContext.WritePacket(buffer.Bytes())
if err != nil { if err != nil {
d.logger.ErrorContext(d.ctx, E.Cause(err, "write ICMP echo reply")) d.logger.ErrorContext(d.ctx, E.Cause(err, "write ICMP echo reply"))
@ -178,7 +167,6 @@ func (d *Destination) loopRead() {
} }
func (d *Destination) WritePacket(packet *buf.Buffer) error { func (d *Destination) WritePacket(packet *buf.Buffer) error {
d.lastActive.Store(time.Now())
if !d.destination.Is6() { if !d.destination.Is6() {
ipHdr := header.IPv4(packet.Bytes()) ipHdr := header.IPv4(packet.Bytes())
if !ipHdr.IsValid(packet.Len()) { if !ipHdr.IsValid(packet.Len()) {

View file

@ -4,7 +4,7 @@ import (
"net/netip" "net/netip"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
) )

View file

@ -9,7 +9,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/control" "github.com/sagernet/sing/common/control"

View file

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/sagernet/gvisor/pkg/rand" "github.com/sagernet/gvisor/pkg/rand"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing-tun/ping" "github.com/sagernet/sing-tun/ping"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"

View file

@ -8,7 +8,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/control" "github.com/sagernet/sing/common/control"

View file

@ -6,7 +6,7 @@ import (
"sync" "sync"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/logger"
) )

View file

@ -11,7 +11,7 @@ import (
"github.com/sagernet/gvisor/pkg/tcpip/header" "github.com/sagernet/gvisor/pkg/tcpip/header"
"github.com/sagernet/gvisor/pkg/tcpip/stack" "github.com/sagernet/gvisor/pkg/tcpip/stack"
"github.com/sagernet/gvisor/pkg/tcpip/transport/tcp" "github.com/sagernet/gvisor/pkg/tcpip/transport/tcp"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"

View file

@ -12,7 +12,7 @@ import (
"github.com/sagernet/gvisor/pkg/tcpip/link/channel" "github.com/sagernet/gvisor/pkg/tcpip/link/channel"
"github.com/sagernet/gvisor/pkg/tcpip/stack" "github.com/sagernet/gvisor/pkg/tcpip/stack"
"github.com/sagernet/gvisor/pkg/tcpip/transport/udp" "github.com/sagernet/gvisor/pkg/tcpip/transport/udp"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
) )

View file

@ -8,8 +8,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/control" "github.com/sagernet/sing/common/control"

View file

@ -4,7 +4,7 @@ import (
"net/netip" "net/netip"
"syscall" "syscall"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
) )

View file

@ -9,7 +9,7 @@ import (
"syscall" "syscall"
"unsafe" "unsafe"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
rawfile "github.com/sagernet/sing-tun/internal/rawfile_darwin" rawfile "github.com/sagernet/sing-tun/internal/rawfile_darwin"
"github.com/sagernet/sing-tun/internal/stopfd_darwin" "github.com/sagernet/sing-tun/internal/stopfd_darwin"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"

View file

@ -14,8 +14,8 @@ import (
"unsafe" "unsafe"
"github.com/sagernet/netlink" "github.com/sagernet/netlink"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/control" "github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"

View file

@ -4,9 +4,9 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
) )
// GSOType represents the type of segmentation offload. // GSOType represents the type of segmentation offload.

View file

@ -14,9 +14,9 @@ import (
"slices" "slices"
"unsafe" "unsafe"
"github.com/sagernet/sing-tun/internal/gtcpip" "github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum" "github.com/sagernet/sing-tun/gtcpip/checksum"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/gtcpip/header"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )