Browse Source

fix tun app lints

Shelikhoo 2 years ago
parent
commit
0ca849637d

+ 2 - 1
app/tun/device/device.go

@@ -1,8 +1,9 @@
 package device
 package device
 
 
 import (
 import (
-	"github.com/v2fly/v2ray-core/v5/common"
 	"gvisor.dev/gvisor/pkg/tcpip/stack"
 	"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
 //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen

+ 2 - 1
app/tun/device/gvisor/gvisor_linux.go

@@ -8,10 +8,11 @@ import (
 	"fmt"
 	"fmt"
 	"unsafe"
 	"unsafe"
 
 
-	"github.com/v2fly/v2ray-core/v5/app/tun/device"
 	"golang.org/x/sys/unix"
 	"golang.org/x/sys/unix"
 	"gvisor.dev/gvisor/pkg/tcpip/stack"
 	"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/fdbased"
 	"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
 	"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
 	"gvisor.dev/gvisor/pkg/tcpip/link/tun"
 	"gvisor.dev/gvisor/pkg/tcpip/link/tun"

+ 3 - 2
app/tun/device/linkWriterToWriter.go

@@ -1,10 +1,12 @@
 package device
 package device
 
 
 import (
 import (
+	"io"
+
 	"github.com/v2fly/v2ray-core/v5/common/errors"
 	"github.com/v2fly/v2ray-core/v5/common/errors"
+
 	"gvisor.dev/gvisor/pkg/buffer"
 	"gvisor.dev/gvisor/pkg/buffer"
 	"gvisor.dev/gvisor/pkg/tcpip/stack"
 	"gvisor.dev/gvisor/pkg/tcpip/stack"
-	"io"
 )
 )
 
 
 func NewLinkWriterToWriter(writer stack.LinkWriter) io.Writer {
 func NewLinkWriterToWriter(writer stack.LinkWriter) io.Writer {
@@ -16,7 +18,6 @@ type linkWriterToWriter struct {
 }
 }
 
 
 func (l linkWriterToWriter) Write(p []byte) (n int, err error) {
 func (l linkWriterToWriter) Write(p []byte) (n int, err error) {
-
 	buffer := buffer.MakeWithData(p)
 	buffer := buffer.MakeWithData(p)
 	packetBufferPtr := stack.NewPacketBuffer(stack.PacketBufferOptions{
 	packetBufferPtr := stack.NewPacketBuffer(stack.PacketBufferOptions{
 		Payload: buffer,
 		Payload: buffer,

+ 7 - 7
app/tun/handler_tcp.go

@@ -4,6 +4,13 @@ import (
 	"context"
 	"context"
 	"time"
 	"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"
 	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"
 	"github.com/v2fly/v2ray-core/v5/common/buf"
 	"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/policy"
 	"github.com/v2fly/v2ray-core/v5/features/routing"
 	"github.com/v2fly/v2ray-core/v5/features/routing"
 	internet "github.com/v2fly/v2ray-core/v5/transport/internet"
 	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 (
 const (
@@ -150,7 +151,6 @@ func applySocketOptions(s *stack.Stack, endpoint tcpip.Endpoint, config *interne
 
 
 	if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
 	if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
 		endpoint.SocketOptions().SetKeepAlive(true)
 		endpoint.SocketOptions().SetKeepAlive(true)
-
 	}
 	}
 	{
 	{
 		var sendBufferSizeRangeOption tcpip.TCPSendBufferSizeRangeOption
 		var sendBufferSizeRangeOption tcpip.TCPSendBufferSizeRangeOption

+ 5 - 4
app/tun/handler_udp.go

@@ -3,6 +3,11 @@ package tun
 import (
 import (
 	"context"
 	"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"
 	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/buf"
 	"github.com/v2fly/v2ray-core/v5/common/net"
 	"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/policy"
 	"github.com/v2fly/v2ray-core/v5/features/routing"
 	"github.com/v2fly/v2ray-core/v5/features/routing"
 	"github.com/v2fly/v2ray-core/v5/transport/internet/udp"
 	"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 {
 type UDPHandler struct {

+ 2 - 1
app/tun/option.go

@@ -1,12 +1,13 @@
 package tun
 package tun
 
 
 import (
 import (
-	"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
 	"gvisor.dev/gvisor/pkg/tcpip"
 	"gvisor.dev/gvisor/pkg/tcpip"
 	"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
 	"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
 	"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
 	"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
 	"gvisor.dev/gvisor/pkg/tcpip/stack"
 	"gvisor.dev/gvisor/pkg/tcpip/stack"
 	"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
 	"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 {
 func CreateNIC(id tcpip.NICID, linkEndpoint stack.LinkEndpoint) StackOption {

+ 4 - 3
app/tun/packetaddradaptar.go

@@ -1,10 +1,11 @@
 package tun
 package tun
 
 
 import (
 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"
 	"gvisor.dev/gvisor/pkg/tcpip/stack"
 	"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 {
 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) {
 func (p *packetAddrDevice) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) {
-	//TODO implement me
+	// TODO implement me
 	panic("implement me")
 	panic("implement me")
 }
 }
 
 

+ 6 - 3
app/tun/packetparse/udp.go

@@ -3,11 +3,14 @@ package packetparse
 import (
 import (
 	"github.com/google/gopacket"
 	"github.com/google/gopacket"
 	"github.com/google/gopacket/layers"
 	"github.com/google/gopacket/layers"
+
 	"github.com/v2fly/v2ray-core/v5/common/net"
 	"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"))
 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)
 	src = net.UDPDestination(srcIP, srcPort)
 	dst = net.UDPDestination(dstIP, dstPort)
 	dst = net.UDPDestination(dstIP, dstPort)
 	data = udp.Payload
 	data = udp.Payload
-	return
+	return // nolint: nakedret
 }
 }
 
 
 func TryConstructUDPPacket(src, dst net.Destination, data []byte) ([]byte, error) {
 func TryConstructUDPPacket(src, dst net.Destination, data []byte) ([]byte, error) {

+ 3 - 1
app/tun/tun.go

@@ -5,6 +5,9 @@ package tun
 
 
 import (
 import (
 	"context"
 	"context"
+
+	"gvisor.dev/gvisor/pkg/tcpip/stack"
+
 	core "github.com/v2fly/v2ray-core/v5"
 	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"
 	"github.com/v2fly/v2ray-core/v5/app/tun/device/gvisor"
 	"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/common/net/packetaddr"
 	"github.com/v2fly/v2ray-core/v5/features/policy"
 	"github.com/v2fly/v2ray-core/v5/features/policy"
 	"github.com/v2fly/v2ray-core/v5/features/routing"
 	"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
 //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen

+ 5 - 4
app/tun/tunsorter/tunsorter.go

@@ -2,6 +2,9 @@ package tunsorter
 
 
 import (
 import (
 	"context"
 	"context"
+	"io"
+	"sync"
+
 	"github.com/v2fly/v2ray-core/v5/app/tun/packetparse"
 	"github.com/v2fly/v2ray-core/v5/app/tun/packetparse"
 	"github.com/v2fly/v2ray-core/v5/common/buf"
 	"github.com/v2fly/v2ray-core/v5/common/buf"
 	"github.com/v2fly/v2ray-core/v5/common/net"
 	"github.com/v2fly/v2ray-core/v5/common/net"
@@ -9,8 +12,6 @@ import (
 	vudp "github.com/v2fly/v2ray-core/v5/common/protocol/udp"
 	vudp "github.com/v2fly/v2ray-core/v5/common/protocol/udp"
 	"github.com/v2fly/v2ray-core/v5/features/routing"
 	"github.com/v2fly/v2ray-core/v5/features/routing"
 	"github.com/v2fly/v2ray-core/v5/transport/internet/udp"
 	"github.com/v2fly/v2ray-core/v5/transport/internet/udp"
-	"io"
-	"sync"
 )
 )
 
 
 //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
 //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) {
 func (t *TunSorter) onNewConnection(connection *trackedUDPConnection) {
 	udpDispatcherConstructor := udp.NewSplitDispatcher
 	udpDispatcherConstructor := udp.NewSplitDispatcher
-	switch t.packetAddrType {
+	switch t.packetAddrType { // nolint: gocritic
 	case packetaddr.PacketAddrType_Packet:
 	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)
 		packetAddrDispatcherFactory := udp.NewPacketAddrDispatcherCreator(ctx)
 		udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher
 		udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher
 	}
 	}