Skip to content

Commit

Permalink
misc: more error cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed Mar 13, 2024
1 parent 1282e95 commit 11e3cb1
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion ipscanner/internal/iterator/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iterator

import (
"crypto/rand"
"errors"
"fmt"
"log"
"math/big"
Expand Down Expand Up @@ -229,7 +230,7 @@ func (g *IpGenerator) NextBatch() ([]netip.Addr, error) {
}
return g.NextBatch()
} else {
return nil, fmt.Errorf("no more IP addresses")
return nil, errors.New("no more IP addresses")
}
}
return results, nil
Expand Down
3 changes: 2 additions & 1 deletion ipscanner/internal/ping/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ping

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (h *HttpPing) PingContext(ctx context.Context) statute.IPingResult {
orighost := u.Host

if !h.IP.IsValid() {
return h.errorResult(fmt.Errorf("no IP specified"))
return h.errorResult(errors.New("no IP specified"))
}

req, err := http.NewRequestWithContext(ctx, h.Method, h.URL, nil)
Expand Down
3 changes: 2 additions & 1 deletion ipscanner/internal/ping/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ping

import (
"context"
"errors"
"fmt"
"net/netip"
"time"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (h *QuicPing) Ping() statute.IPingResult {

func (h *QuicPing) PingContext(ctx context.Context) statute.IPingResult {
if !h.IP.IsValid() {
return h.errorResult(fmt.Errorf("no IP specified"))
return h.errorResult(errors.New("no IP specified"))
}

addr := netip.AddrPortFrom(h.IP, h.Port)
Expand Down
3 changes: 2 additions & 1 deletion ipscanner/internal/ping/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ping

import (
"context"
"errors"
"fmt"
"net/netip"
"time"
Expand Down Expand Up @@ -46,7 +47,7 @@ func (t *TlsPing) Ping() statute.IPingResult {

func (t *TlsPing) PingContext(ctx context.Context) statute.IPingResult {
if !t.IP.IsValid() {
return t.errorResult(fmt.Errorf("no IP specified"))
return t.errorResult(errors.New("no IP specified"))
}
addr := netip.AddrPortFrom(t.IP, t.Port)
t0 := time.Now()
Expand Down
4 changes: 2 additions & 2 deletions psiphon/p.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ func RunPsiphon(wgBind, localSocksPort, country string, ctx context.Context) err
select {
case <-ctx.Done():
internalCtx.Done()
return fmt.Errorf("psiphon handshake operation canceled by user")
return errors.New("psiphon handshake operation canceled by user")
case <-timeoutTimer.C:
// Handle the internal timeout
internalCtx.Done()
return fmt.Errorf("psiphon handshake maximum time exceeded")
return errors.New("psiphon handshake maximum time exceeded")
default:
tunnel, err = StartTunnel(internalCtx, []byte(configJSON), "", p, nil, nil)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions warp/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ func (d *Dialer) makeTLSHelloPacketWithSNICurve(plainConn net.Conn, config *tls.
}
err := utlsConn.ApplyPreset(&spec)
if err != nil {
return nil, fmt.Errorf("uTlsConn.Handshake() error: %+v", err)
return nil, fmt.Errorf("uTlsConn.Handshake() error: %w", err)
}

err = utlsConn.Handshake()
if err != nil {
return nil, fmt.Errorf("uTlsConn.Handshake() error: %+v", err)
return nil, fmt.Errorf("uTlsConn.Handshake() error: %w", err)
}

return utlsConn, nil
Expand Down
7 changes: 4 additions & 3 deletions wiresocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"net/netip"
"strings"

Expand Down Expand Up @@ -41,7 +42,7 @@ var (
func parseString(section *ini.Section, keyName string) (string, error) {
key := section.Key(strings.ToLower(keyName))
if key == nil {
return "", errors.New(keyName + " should not be empty")
return "", fmt.Errorf("%s should not be empty", keyName)
}
return key.String(), nil
}
Expand All @@ -62,10 +63,10 @@ func parseBase64KeyToHex(section *ini.Section, keyName string) (string, error) {
func encodeBase64ToHex(key string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return "", errors.New("invalid base64 string: " + key)
return "", fmt.Errorf("invalid base64 string: %s", key)
}
if len(decoded) != 32 {
return "", errors.New("key should be 32 bytes: " + key)
return "", fmt.Errorf("key should be 32 bytes: %s", key)
}
return hex.EncodeToString(decoded), nil
}
Expand Down
3 changes: 2 additions & 1 deletion wiresocks/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wiresocks

import (
"context"
"errors"
"fmt"
"net"
"net/netip"
Expand Down Expand Up @@ -68,7 +69,7 @@ func RunScan(ctx context.Context, rtt time.Duration) (result []ipscanner.IPInfo,
select {
case <-ctx.Done():
// Context is done - canceled externally
return nil, fmt.Errorf("user canceled the operation")
return nil, errors.New("user canceled the operation")
case <-t.C:
// Prevent the loop from spinning too fast
continue
Expand Down
5 changes: 3 additions & 2 deletions wiresocks/udpfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wiresocks
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -152,7 +153,7 @@ func socks5Handshake(conn net.Conn) error {
}

if resp[0] != 0x05 || resp[1] != 0x00 {
return fmt.Errorf("invalid SOCKS5 authentication response")
return errors.New("invalid SOCKS5 authentication response")
}
return nil
}
Expand Down Expand Up @@ -219,7 +220,7 @@ func requestUDPAssociate(conn net.Conn) (*net.UDPAddr, error) {
}

if resp[1] != 0x00 {
return nil, fmt.Errorf("UDP ASSOCIATE request failed")
return nil, errors.New("UDP ASSOCIATE request failed")
}

// Parse the proxy UDP address
Expand Down

0 comments on commit 11e3cb1

Please sign in to comment.