Skip to content

Commit

Permalink
Catch panics when loading transport on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Aug 2, 2023
1 parent 8959add commit c1e9e80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 37 deletions.
75 changes: 38 additions & 37 deletions pkg/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,8 @@ import (
"github.com/wakatime/wakatime-cli/pkg/log"
)

const serverName = "api.wakatime.com"

// NewTransport initializes a new http.Transport.
func NewTransport() *http.Transport {
return &http.Transport{
ForceAttemptHTTP2: true,
MaxConnsPerHost: 1,
MaxIdleConns: 1,
MaxIdleConnsPerHost: 1,
Proxy: nil,
TLSHandshakeTimeout: DefaultTimeoutSecs * time.Second,
}
}

// NewTransportWithHostVerificationDisabled initializes a new http.Transport with disabled host verification.
func NewTransportWithHostVerificationDisabled() *http.Transport {
t := NewTransport()

t.TLSClientConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: CACerts(),
ServerName: serverName,
}

return t
}

// LazyCreateNewTransport uses the client's Transport if exists, or creates a new one.
func LazyCreateNewTransport(c *Client) *http.Transport {
if c != nil && c.client != nil && c.client.Transport != nil {
return c.client.Transport.(*http.Transport).Clone()
}

return NewTransport()
}

const letsencryptCerts string = `
const (
letsencryptCerts = `
-----BEGIN CERTIFICATE-----
MIIEYDCCAkigAwIBAgIQB55JKIY3b9QISMI/xjHkYzANBgkqhkiG9w0BAQsFADBP
MQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJuZXQgU2VjdXJpdHkgUmVzZWFy
Expand Down Expand Up @@ -118,6 +83,42 @@ mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
-----END CERTIFICATE-----
`
serverName = "api.wakatime.com"
)

// NewTransport initializes a new http.Transport.
func NewTransport() *http.Transport {
return &http.Transport{
ForceAttemptHTTP2: true,
MaxConnsPerHost: 1,
MaxIdleConns: 1,
MaxIdleConnsPerHost: 1,
Proxy: nil,
TLSHandshakeTimeout: DefaultTimeoutSecs * time.Second,
}
}

// NewTransportWithHostVerificationDisabled initializes a new http.Transport with disabled host verification.
func NewTransportWithHostVerificationDisabled() *http.Transport {
t := NewTransport()

t.TLSClientConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: CACerts(),
ServerName: serverName,
}

return t
}

// LazyCreateNewTransport uses the client's Transport if exists, or creates a new one.
func LazyCreateNewTransport(c *Client) *http.Transport {
if c != nil && c.client != nil && c.client.Transport != nil {
return c.client.Transport.(*http.Transport).Clone()
}

return NewTransport()
}

// CACerts returns a root cert pool with the system's cacerts and LetsEncrypt's root certs.
func CACerts() *x509.CertPool {
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/transport_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ package api

import (
"crypto/x509"
"runtime/debug"
"syscall"
"unsafe"

"github.com/wakatime/wakatime-cli/pkg/log"
)

func loadSystemRoots() (*x509.CertPool, error) {
defer func() {
if err := recover(); err != nil {
log.Errorf("failed to load system roots on Windows. panicked: %v. Stack: %s", err, string(debug.Stack()))
}
}()

const cryptENotFound = 0x80092004

rootPtr, err := syscall.UTF16PtrFromString("ROOT")
Expand Down

0 comments on commit c1e9e80

Please sign in to comment.