From b8b37598267d19abdfbdd7bcaf1f02da25990edd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= <i@sekai.icu>
Date: Thu, 14 Nov 2024 22:04:31 +0800
Subject: [PATCH] Fix HandshakeFailure usages

---
 go.mod               |  2 +-
 go.sum               |  4 ++--
 stack_gvisor_lazy.go |  3 ++-
 stack_gvisor_udp.go  | 12 ++++++------
 4 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/go.mod b/go.mod
index 50b86dc..e7644c0 100644
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,7 @@ require (
 	github.com/sagernet/gvisor v0.0.0-20241021032506-a4324256e4a3
 	github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a
 	github.com/sagernet/nftables v0.3.0-beta.4
-	github.com/sagernet/sing v0.6.0-alpha.6
+	github.com/sagernet/sing v0.6.0-alpha.11
 	go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
 	golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
 	golang.org/x/net v0.26.0
diff --git a/go.sum b/go.sum
index b13c365..270fa12 100644
--- a/go.sum
+++ b/go.sum
@@ -22,8 +22,8 @@ github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a h1:ObwtHN2VpqE0ZN
 github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
 github.com/sagernet/nftables v0.3.0-beta.4 h1:kbULlAwAC3jvdGAC1P5Fa3GSxVwQJibNenDW2zaXr8I=
 github.com/sagernet/nftables v0.3.0-beta.4/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8=
-github.com/sagernet/sing v0.6.0-alpha.6 h1:R0abM8ZeazyAKo9d3DNxtrgW17g3tZAD8al7O5+ADOw=
-github.com/sagernet/sing v0.6.0-alpha.6/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
+github.com/sagernet/sing v0.6.0-alpha.11 h1:ZcZlA0/vdDeiipAbjK73x9VabGJ/RRcAJgWhOo/OoBk=
+github.com/sagernet/sing v0.6.0-alpha.11/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
 github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
 github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
 github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
diff --git a/stack_gvisor_lazy.go b/stack_gvisor_lazy.go
index 8195844..59c993b 100644
--- a/stack_gvisor_lazy.go
+++ b/stack_gvisor_lazy.go
@@ -5,6 +5,7 @@ package tun
 import (
 	"context"
 	"net"
+	"os"
 	"time"
 
 	"github.com/sagernet/gvisor/pkg/tcpip"
@@ -64,7 +65,7 @@ func (c *gLazyConn) HandshakeContext(ctx context.Context) error {
 
 func (c *gLazyConn) HandshakeFailure(err error) error {
 	if c.handshakeDone {
-		return nil
+		return os.ErrInvalid
 	}
 	c.request.Complete(err != ErrDrop)
 	c.handshakeDone = true
diff --git a/stack_gvisor_udp.go b/stack_gvisor_udp.go
index ab819a5..150fd1a 100644
--- a/stack_gvisor_udp.go
+++ b/stack_gvisor_udp.go
@@ -102,13 +102,13 @@ func (w *UDPBackWriter) HandshakeSuccess() error {
 func (w *UDPBackWriter) HandshakeFailure(err error) error {
 	w.access.Lock()
 	defer w.access.Unlock()
-	if w.packet != nil {
-		wErr := gWriteUnreachable(w.stack, w.packet)
-		w.packet.DecRef()
-		w.packet = nil
-		return wErr
+	if w.packet == nil {
+		return os.ErrInvalid
 	}
-	return nil
+	wErr := gWriteUnreachable(w.stack, w.packet)
+	w.packet.DecRef()
+	w.packet = nil
+	return wErr
 }
 
 func (w *UDPBackWriter) WritePacket(packetBuffer *buf.Buffer, destination M.Socksaddr) error {