Skip to content

Commit

Permalink
[+] request: fix close connection error on InsecureSkipVerify (Nooost…
Browse files Browse the repository at this point in the history
…e#100)

* [+] request: fix close connection error on InsecureSkipVerify

* chore: Updated coverage badge.

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
Noooste and actions-user authored Jun 24, 2024
1 parent 4619e5c commit 067bebe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AzureTLS Client
[![GoDoc](https://godoc.org/github.com/Noooste/azuretls-client?status.svg)](https://godoc.org/github.com/Noooste/azuretls-client)
![Coverage](https://img.shields.io/badge/Coverage-79.5%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-78.4%25-brightgreen)
[![build](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml/badge.svg)](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml)
[![Go Report Card](https://goreportcard.com/badge/Noooste/azuretls-client)](https://goreportcard.com/report/Noooste/azuretls-client)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Noooste/azuretls-client/blob/master/LICENSE)
Expand Down
50 changes: 11 additions & 39 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"github.com/Noooste/fhttp/http2"
tls "github.com/Noooste/utls"
"io"
"net"
"net/url"
"strings"
Expand Down Expand Up @@ -169,14 +168,19 @@ func (cp *ConnPool) Remove(u *url.URL) {
}
}

func (c *Conn) makeTLS(isHTTPorS bool, addr string) error {
if c.checkTLS(isHTTPorS) {
func (c *Conn) makeTLS(addr string) error {
if c.checkTLS() {
return nil
}
return c.NewTLS(addr)

if c.TLS == nil {
return c.NewTLS(addr)
}

return nil
}

func (c *Conn) checkTLS(isHTTPorS bool) bool {
func (c *Conn) checkTLS() bool {
if c.TLS == nil {
return false
} else if c.TLS.ConnectionState().VerifiedChains != nil {
Expand All @@ -187,39 +191,8 @@ func (c *Conn) checkTLS(isHTTPorS bool) bool {
return false
}
}
} else if c.Conn == nil {
return false
} else if isHTTPorS && !c.isActive() {
return false
}
_, ok := c.Conn.(*net.TCPConn)
if !ok { // if the connection is dead
return false
}
return true
}

func (c *Conn) isActive() bool {
var buf [1]byte
err := c.Conn.SetReadDeadline(time.Now().Add(1 * time.Millisecond)) // Set immediate timeout
if err != nil {
return false
}
if _, err = c.Conn.Read(buf[:]); err != nil {
if err == io.EOF {
return false // Connection closed by the server
}

var nerr net.Error
if errors.As(err, &nerr) && !nerr.Timeout() { // If it's not a timeout error, the connection is not alive
return false // Network error or other non-timeout error
}
}

err = c.Conn.SetReadDeadline(time.Time{}) // Reset the deadline
if err != nil {
return false
}
return true
}

Expand Down Expand Up @@ -337,8 +310,7 @@ func (s *Session) initConn(req *Request) (conn *Conn, err error) {
conn.mu.Lock()
defer conn.mu.Unlock()

isHttpOrHttpsScheme := req.parsedUrl.Scheme == SchemeHttps || req.parsedUrl.Scheme == SchemeHttp
if conn.Conn == nil || !conn.checkTLS(isHttpOrHttpsScheme) { //no "use of closed network connection" ERROR after add this
if conn.Conn == nil {
if s.ProxyDialer != nil {
if err = s.getProxyConn(req, conn, host); err != nil {
return nil, err
Expand All @@ -357,7 +329,7 @@ func (s *Session) initConn(req *Request) (conn *Conn, err error) {

case SchemeHttps, SchemeWss:
// for secured http we need to make tls connection first
if err = conn.makeTLS(isHttpOrHttpsScheme, host); err != nil {
if err = conn.makeTLS(host); err != nil {
conn.Close()
return

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ go 1.20

require (
github.com/Noooste/fhttp v1.0.12
github.com/Noooste/utls v1.2.9
github.com/Noooste/utls v1.2.10
github.com/Noooste/websocket v1.0.3
github.com/fatih/color v1.17.0
golang.org/x/net v0.26.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/crypto v0.24.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ github.com/Noooste/fhttp v1.0.12 h1:2N15bIATKaC6q+LVyRGyxPyuqEPvwAS3Uk1peC3YVHU=
github.com/Noooste/fhttp v1.0.12/go.mod h1:CMVxKOhNheqJN5HYE4Rlvz2SRdV8Uv7YWmi6OwmB/Bk=
github.com/Noooste/utls v1.2.9 h1:VLNs0WmPFjswU4PxXTHl0AY4sarHi+638c3cSI9Hsng=
github.com/Noooste/utls v1.2.9/go.mod h1:dHM5MlRyB/ieog9axOxkEh4qZRgw5xA406y7sEdVmoQ=
github.com/Noooste/utls v1.2.10 h1:ZtnQYVGqhCqIkqT9tNdDCxZlMtDjT+HpgDrb730Q8Wk=
github.com/Noooste/utls v1.2.10/go.mod h1:/iJ0ORCNgIpyX/hrUk/8jK76ogcXvEJGldWFxqKtIUM=
github.com/Noooste/websocket v1.0.3 h1:drW7tvZ3YqzqI9wApnaH1Q0syFMXO7gbLlsBWjZvMNA=
github.com/Noooste/websocket v1.0.3/go.mod h1:Qhw0Rtuju/fPPbcb3R5XGq7poa51qPDL462jTltl9nQ=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cloudflare/circl v1.3.9 h1:QFrlgFYf2Qpi8bSpVPK1HBvWpx16v/1TZivyo7pGuBE=
github.com/cloudflare/circl v1.3.9/go.mod h1:PDRU+oXvdD7KCtgKxW95M5Z8BpSCJXQORiZFnBQS5QU=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
19 changes: 19 additions & 0 deletions test/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azuretls_test
import (
"bytes"
"context"
"fmt"
"github.com/Noooste/azuretls-client"
http "github.com/Noooste/fhttp"
"net/url"
Expand Down Expand Up @@ -193,3 +194,21 @@ func TestRequest_NoCookies2(t *testing.T) {
return
}
}

func TestRequest_InsecureSkipVerify(t *testing.T) {
session := azuretls.NewSession()

// commenting out this line will make the code work
session.InsecureSkipVerify = true

response, err := session.Get("https://www.google.com")
if err != nil {
panic(err)
}
fmt.Println(response.StatusCode)
response, err = session.Get("https://www.google.com")
if err != nil {
panic(err)
}
fmt.Println(response.StatusCode)
}

0 comments on commit 067bebe

Please sign in to comment.