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"
"fmt"
"github.com/sagernet/sing-tun/internal/gtcpip"
"github.com/sagernet/sing-tun/internal/gtcpip/checksum"
"github.com/sagernet/sing-tun/gtcpip"
"github.com/sagernet/sing-tun/gtcpip/checksum"
)
// PseudoHeaderChecksum calculates the pseudo-header checksum for the given

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -14,7 +14,7 @@
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
// only contain the body of an ICMPv6 packet.

View file

@ -14,7 +14,7 @@
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
// contain the body of an ICMPv6 packet.

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -10,8 +10,7 @@ import (
"time"
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing-tun/internal/gtcpip/header"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions"
@ -30,7 +29,6 @@ type Destination struct {
destination netip.Addr
routeContext tun.DirectRouteContext
timeout time.Duration
lastActive common.TypedValue[time.Time]
requestAccess sync.Mutex
requests map[pingRequest]time.Time
}
@ -75,7 +73,6 @@ func ConnectDestination(
timeout: timeout,
requests: make(map[pingRequest]time.Time),
}
d.lastActive.Store(time.Now())
go d.loopRead()
return d, nil
}
@ -83,21 +80,14 @@ func ConnectDestination(
func (d *Destination) loopRead() {
defer d.Close()
for {
deadline := d.lastActive.Load().Add(d.timeout)
if !time.Now().Before(deadline) {
return
}
err := d.conn.SetReadDeadline(deadline)
buffer := buf.NewSize(maxICMPPacketSize)
err := d.conn.SetReadDeadline(time.Now().Add(d.timeout))
if err != nil {
d.logger.ErrorContext(d.ctx, E.Cause(err, "set read deadline for ICMP conn"))
}
buffer := buf.NewSize(maxICMPPacketSize)
err = d.conn.ReadIP(buffer)
if err != nil {
buffer.Release()
if E.IsTimeout(err) {
continue
}
if !E.IsClosed(err) {
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.lastActive.Store(time.Now())
err = d.routeContext.WritePacket(buffer.Bytes())
if err != nil {
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 {
d.lastActive.Store(time.Now())
if !d.destination.Is6() {
ipHdr := header.IPv4(packet.Bytes())
if !ipHdr.IsValid(packet.Len()) {

View file

@ -4,7 +4,7 @@ import (
"net/netip"
"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"
)

View file

@ -9,7 +9,7 @@ import (
"sync/atomic"
"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/buf"
"github.com/sagernet/sing/common/control"

View file

@ -9,7 +9,7 @@ import (
"time"
"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/common/buf"

View file

@ -8,7 +8,7 @@ import (
"sync"
"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/buf"
"github.com/sagernet/sing/common/control"

View file

@ -6,7 +6,7 @@ import (
"sync"
"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"
)

View file

@ -11,7 +11,7 @@ import (
"github.com/sagernet/gvisor/pkg/tcpip/header"
"github.com/sagernet/gvisor/pkg/tcpip/stack"
"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"
M "github.com/sagernet/sing/common/metadata"
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/stack"
"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"
E "github.com/sagernet/sing/common/exceptions"
)

View file

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

View file

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

View file

@ -9,7 +9,7 @@ import (
"syscall"
"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"
"github.com/sagernet/sing-tun/internal/stopfd_darwin"
"github.com/sagernet/sing/common"

View file

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

View file

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

View file

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