Resolve golangci issues
This commit is contained in:
1 parent
f278c65e6f
commit
4712362729
1 file changed
+35
-18
+35
-18
@@ -48,7 +48,7 @@ func main() {
|
|||||||
// Generate a certificate and private key to secure the connection
|
// Generate a certificate and private key to secure the connection
|
||||||
certificate, genErr := selfsign.GenerateSelfSigned()
|
certificate, genErr := selfsign.GenerateSelfSigned()
|
||||||
if genErr != nil {
|
if genErr != nil {
|
||||||
panic(err)
|
panic(genErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare the configuration of the DTLS connection
|
// Prepare the configuration of the DTLS connection
|
||||||
@@ -98,19 +98,18 @@ func main() {
|
|||||||
|
|
||||||
// Perform the handshake with a 30-second timeout
|
// Perform the handshake with a 30-second timeout
|
||||||
ctx1, cancel1 := context.WithTimeout(ctx, 30*time.Second)
|
ctx1, cancel1 := context.WithTimeout(ctx, 30*time.Second)
|
||||||
|
defer cancel1()
|
||||||
|
|
||||||
dtlsConn, ok := conn.(*dtls.Conn)
|
dtlsConn, ok := conn.(*dtls.Conn)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Println("Type error")
|
log.Println("Type error: expected *dtls.Conn")
|
||||||
cancel1()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("Start handshake")
|
log.Println("Start handshake")
|
||||||
if err := dtlsConn.HandshakeContext(ctx1); err != nil {
|
if err := dtlsConn.HandshakeContext(ctx1); err != nil {
|
||||||
log.Println(err)
|
log.Printf("Handshake failed: %v", err)
|
||||||
cancel1()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cancel1()
|
|
||||||
log.Println("Handshake done")
|
log.Println("Handshake done")
|
||||||
|
|
||||||
if *vlessMode {
|
if *vlessMode {
|
||||||
@@ -222,7 +221,11 @@ func handleVLESSConnection(ctx context.Context, dtlsConn net.Conn, connectAddr s
|
|||||||
log.Printf("KCP session error: %s", err)
|
log.Printf("KCP session error: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer kcpSess.Close()
|
defer func() {
|
||||||
|
if err := kcpSess.Close(); err != nil {
|
||||||
|
log.Printf("failed to close KCP session: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
log.Printf("KCP session established (server)")
|
log.Printf("KCP session established (server)")
|
||||||
|
|
||||||
// 2. Create smux server session over KCP
|
// 2. Create smux server session over KCP
|
||||||
@@ -231,7 +234,11 @@ func handleVLESSConnection(ctx context.Context, dtlsConn net.Conn, connectAddr s
|
|||||||
log.Printf("smux server error: %s", err)
|
log.Printf("smux server error: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer smuxSess.Close()
|
defer func() {
|
||||||
|
if err := smuxSess.Close(); err != nil {
|
||||||
|
log.Printf("failed to close smux session: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
log.Printf("smux session established (server)")
|
log.Printf("smux session established (server)")
|
||||||
|
|
||||||
// 3. Accept smux streams and forward to backend via TCP
|
// 3. Accept smux streams and forward to backend via TCP
|
||||||
@@ -250,7 +257,12 @@ func handleVLESSConnection(ctx context.Context, dtlsConn net.Conn, connectAddr s
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(s *smux.Stream) {
|
go func(s *smux.Stream) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer s.Close()
|
|
||||||
|
defer func() {
|
||||||
|
if err := s.Close(); err != nil && err != smux.ErrGoAway {
|
||||||
|
log.Printf("failed to close smux stream: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Connect to backend (Xray/VLESS)
|
// Connect to backend (Xray/VLESS)
|
||||||
backendConn, err := net.DialTimeout("tcp", connectAddr, 10*time.Second)
|
backendConn, err := net.DialTimeout("tcp", connectAddr, 10*time.Second)
|
||||||
@@ -258,7 +270,11 @@ func handleVLESSConnection(ctx context.Context, dtlsConn net.Conn, connectAddr s
|
|||||||
log.Printf("backend dial error: %s", err)
|
log.Printf("backend dial error: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer backendConn.Close()
|
defer func() {
|
||||||
|
if err := backendConn.Close(); err != nil {
|
||||||
|
log.Printf("failed to close backend connection: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Bidirectional copy
|
// Bidirectional copy
|
||||||
pipeConn(ctx, s, backendConn)
|
pipeConn(ctx, s, backendConn)
|
||||||
@@ -270,6 +286,8 @@ func handleVLESSConnection(ctx context.Context, dtlsConn net.Conn, connectAddr s
|
|||||||
// pipeConn copies data bidirectionally between two connections.
|
// pipeConn copies data bidirectionally between two connections.
|
||||||
func pipeConn(ctx context.Context, c1, c2 net.Conn) {
|
func pipeConn(ctx context.Context, c1, c2 net.Conn) {
|
||||||
ctx2, cancel := context.WithCancel(ctx)
|
ctx2, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
context.AfterFunc(ctx2, func() {
|
context.AfterFunc(ctx2, func() {
|
||||||
if err := c1.SetDeadline(time.Now()); err != nil {
|
if err := c1.SetDeadline(time.Now()); err != nil {
|
||||||
log.Printf("pipeConn: failed to set deadline c1: %v", err)
|
log.Printf("pipeConn: failed to set deadline c1: %v", err)
|
||||||
@@ -281,25 +299,24 @@ func pipeConn(ctx context.Context, c1, c2 net.Conn) {
|
|||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer cancel()
|
|
||||||
if _, err := io.Copy(c1, c2); err != nil {
|
if _, err := io.Copy(c1, c2); err != nil {
|
||||||
log.Printf("pipeConn: c1<-c2 copy error: %v", err)
|
log.Printf("pipeConn: c1<-c2 copy error: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer cancel()
|
|
||||||
if _, err := io.Copy(c2, c1); err != nil {
|
if _, err := io.Copy(c2, c1); err != nil {
|
||||||
log.Printf("pipeConn: c2<-c1 copy error: %v", err)
|
log.Printf("pipeConn: c2<-c1 copy error: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
if err := c1.SetDeadline(time.Time{}); err != nil {
|
|
||||||
log.Printf("pipeConn: failed to reset deadline c1: %v", err)
|
// Reset deadlines
|
||||||
}
|
_ = c1.SetDeadline(time.Time{})
|
||||||
if err := c2.SetDeadline(time.Time{}); err != nil {
|
_ = c2.SetDeadline(time.Time{})
|
||||||
log.Printf("pipeConn: failed to reset deadline c2: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user