From 0ca849637d66d21f48eab74b59dcc139dafaf9f8 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Mon, 23 Oct 2023 01:44:36 +0100 Subject: [PATCH] fix tun app lints --- app/tun/device/device.go | 3 ++- app/tun/device/gvisor/gvisor_linux.go | 3 ++- app/tun/device/linkWriterToWriter.go | 5 +++-- app/tun/handler_tcp.go | 14 +++++++------- app/tun/handler_udp.go | 9 +++++---- app/tun/option.go | 3 ++- app/tun/packetaddradaptar.go | 7 ++++--- app/tun/packetparse/udp.go | 9 ++++++--- app/tun/tun.go | 4 +++- app/tun/tunsorter/tunsorter.go | 9 +++++---- 10 files changed, 39 insertions(+), 27 deletions(-) diff --git a/app/tun/device/device.go b/app/tun/device/device.go index 029b63b48..598c070eb 100644 --- a/app/tun/device/device.go +++ b/app/tun/device/device.go @@ -1,8 +1,9 @@ package device import ( - "github.com/v2fly/v2ray-core/v5/common" "gvisor.dev/gvisor/pkg/tcpip/stack" + + "github.com/v2fly/v2ray-core/v5/common" ) //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen diff --git a/app/tun/device/gvisor/gvisor_linux.go b/app/tun/device/gvisor/gvisor_linux.go index a0a79fcbe..00ea930f7 100644 --- a/app/tun/device/gvisor/gvisor_linux.go +++ b/app/tun/device/gvisor/gvisor_linux.go @@ -8,10 +8,11 @@ import ( "fmt" "unsafe" - "github.com/v2fly/v2ray-core/v5/app/tun/device" "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/tcpip/stack" + "github.com/v2fly/v2ray-core/v5/app/tun/device" + "gvisor.dev/gvisor/pkg/tcpip/link/fdbased" "gvisor.dev/gvisor/pkg/tcpip/link/rawfile" "gvisor.dev/gvisor/pkg/tcpip/link/tun" diff --git a/app/tun/device/linkWriterToWriter.go b/app/tun/device/linkWriterToWriter.go index 47fc42259..2f47a0b1e 100644 --- a/app/tun/device/linkWriterToWriter.go +++ b/app/tun/device/linkWriterToWriter.go @@ -1,10 +1,12 @@ package device import ( + "io" + "github.com/v2fly/v2ray-core/v5/common/errors" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip/stack" - "io" ) func NewLinkWriterToWriter(writer stack.LinkWriter) io.Writer { @@ -16,7 +18,6 @@ type linkWriterToWriter struct { } func (l linkWriterToWriter) Write(p []byte) (n int, err error) { - buffer := buffer.MakeWithData(p) packetBufferPtr := stack.NewPacketBuffer(stack.PacketBufferOptions{ Payload: buffer, diff --git a/app/tun/handler_tcp.go b/app/tun/handler_tcp.go index e40f0789a..b00412374 100644 --- a/app/tun/handler_tcp.go +++ b/app/tun/handler_tcp.go @@ -4,6 +4,13 @@ import ( "context" "time" + "gvisor.dev/gvisor/pkg/tcpip" + "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" + "gvisor.dev/gvisor/pkg/tcpip/header" + "gvisor.dev/gvisor/pkg/tcpip/stack" + "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" + "gvisor.dev/gvisor/pkg/waiter" + tun_net "github.com/v2fly/v2ray-core/v5/app/tun/net" "github.com/v2fly/v2ray-core/v5/common" "github.com/v2fly/v2ray-core/v5/common/buf" @@ -15,12 +22,6 @@ import ( "github.com/v2fly/v2ray-core/v5/features/policy" "github.com/v2fly/v2ray-core/v5/features/routing" internet "github.com/v2fly/v2ray-core/v5/transport/internet" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" - "gvisor.dev/gvisor/pkg/tcpip/header" - "gvisor.dev/gvisor/pkg/tcpip/stack" - "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" - "gvisor.dev/gvisor/pkg/waiter" ) const ( @@ -150,7 +151,6 @@ func applySocketOptions(s *stack.Stack, endpoint tcpip.Endpoint, config *interne if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 { endpoint.SocketOptions().SetKeepAlive(true) - } { var sendBufferSizeRangeOption tcpip.TCPSendBufferSizeRangeOption diff --git a/app/tun/handler_udp.go b/app/tun/handler_udp.go index 4d3a862be..3a755743f 100644 --- a/app/tun/handler_udp.go +++ b/app/tun/handler_udp.go @@ -3,6 +3,11 @@ package tun import ( "context" + "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" + "gvisor.dev/gvisor/pkg/tcpip/stack" + gvisor_udp "gvisor.dev/gvisor/pkg/tcpip/transport/udp" + "gvisor.dev/gvisor/pkg/waiter" + tun_net "github.com/v2fly/v2ray-core/v5/app/tun/net" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" @@ -11,10 +16,6 @@ import ( "github.com/v2fly/v2ray-core/v5/features/policy" "github.com/v2fly/v2ray-core/v5/features/routing" "github.com/v2fly/v2ray-core/v5/transport/internet/udp" - "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" - "gvisor.dev/gvisor/pkg/tcpip/stack" - gvisor_udp "gvisor.dev/gvisor/pkg/tcpip/transport/udp" - "gvisor.dev/gvisor/pkg/waiter" ) type UDPHandler struct { diff --git a/app/tun/option.go b/app/tun/option.go index 39dffa412..0a6359290 100644 --- a/app/tun/option.go +++ b/app/tun/option.go @@ -1,12 +1,13 @@ package tun import ( - "github.com/v2fly/v2ray-core/v5/app/router/routercommon" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" + + "github.com/v2fly/v2ray-core/v5/app/router/routercommon" ) func CreateNIC(id tcpip.NICID, linkEndpoint stack.LinkEndpoint) StackOption { diff --git a/app/tun/packetaddradaptar.go b/app/tun/packetaddradaptar.go index 8fb925a60..d8554892f 100644 --- a/app/tun/packetaddradaptar.go +++ b/app/tun/packetaddradaptar.go @@ -1,10 +1,11 @@ package tun import ( - "github.com/v2fly/v2ray-core/v5/app/tun/device" - "github.com/v2fly/v2ray-core/v5/app/tun/tunsorter" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/stack" + + "github.com/v2fly/v2ray-core/v5/app/tun/device" + "github.com/v2fly/v2ray-core/v5/app/tun/tunsorter" ) func NewDeviceWithSorter(overlay device.Device, sorter *tunsorter.TunSorter) device.Device { @@ -30,7 +31,7 @@ func (p *packetAddrDevice) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNu } func (p *packetAddrDevice) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) { - //TODO implement me + // TODO implement me panic("implement me") } diff --git a/app/tun/packetparse/udp.go b/app/tun/packetparse/udp.go index 61cbfbd31..5f66e2ef7 100644 --- a/app/tun/packetparse/udp.go +++ b/app/tun/packetparse/udp.go @@ -3,11 +3,14 @@ package packetparse import ( "github.com/google/gopacket" "github.com/google/gopacket/layers" + "github.com/v2fly/v2ray-core/v5/common/net" ) -var errNotIPPacket = newError("not an IP packet") -var errNotUDPPacket = newError("not a UDP packet") +var ( + errNotIPPacket = newError("not an IP packet") + errNotUDPPacket = newError("not a UDP packet") +) var nullDestination = net.UnixDestination(net.DomainAddress("null")) @@ -56,7 +59,7 @@ func TryParseAsUDPPacket(packet []byte) (src, dst net.Destination, data []byte, src = net.UDPDestination(srcIP, srcPort) dst = net.UDPDestination(dstIP, dstPort) data = udp.Payload - return + return // nolint: nakedret } func TryConstructUDPPacket(src, dst net.Destination, data []byte) ([]byte, error) { diff --git a/app/tun/tun.go b/app/tun/tun.go index 05f045396..ed86f146e 100644 --- a/app/tun/tun.go +++ b/app/tun/tun.go @@ -5,6 +5,9 @@ package tun import ( "context" + + "gvisor.dev/gvisor/pkg/tcpip/stack" + core "github.com/v2fly/v2ray-core/v5" "github.com/v2fly/v2ray-core/v5/app/tun/device" "github.com/v2fly/v2ray-core/v5/app/tun/device/gvisor" @@ -13,7 +16,6 @@ import ( "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" "github.com/v2fly/v2ray-core/v5/features/policy" "github.com/v2fly/v2ray-core/v5/features/routing" - "gvisor.dev/gvisor/pkg/tcpip/stack" ) //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen diff --git a/app/tun/tunsorter/tunsorter.go b/app/tun/tunsorter/tunsorter.go index cf32e74f0..d8ed5d51d 100644 --- a/app/tun/tunsorter/tunsorter.go +++ b/app/tun/tunsorter/tunsorter.go @@ -2,6 +2,9 @@ package tunsorter import ( "context" + "io" + "sync" + "github.com/v2fly/v2ray-core/v5/app/tun/packetparse" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" @@ -9,8 +12,6 @@ import ( vudp "github.com/v2fly/v2ray-core/v5/common/protocol/udp" "github.com/v2fly/v2ray-core/v5/features/routing" "github.com/v2fly/v2ray-core/v5/transport/internet/udp" - "io" - "sync" ) //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen @@ -50,9 +51,9 @@ func (t *TunSorter) OnPacketReceived(b []byte) (n int, err error) { func (t *TunSorter) onNewConnection(connection *trackedUDPConnection) { udpDispatcherConstructor := udp.NewSplitDispatcher - switch t.packetAddrType { + switch t.packetAddrType { // nolint: gocritic case packetaddr.PacketAddrType_Packet: - ctx := context.WithValue(t.ctx, udp.DispatcherConnectionTerminationSignalReceiverMark, connection) + ctx := context.WithValue(t.ctx, udp.DispatcherConnectionTerminationSignalReceiverMark, connection) // nolint:staticcheck packetAddrDispatcherFactory := udp.NewPacketAddrDispatcherCreator(ctx) udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher }