From 06a4e87b14b912ba8a6bec6da274807567fb9b3f Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Sat, 19 Oct 2024 13:05:20 -0500 Subject: [PATCH] fix CI errors --- cmd/dmsg-server/commands/config/gen.go | 2 +- cmd/dmsg-server/commands/start/root.go | 6 +- cmd/dmsg-socks5/commands/dmsg-socks5.go | 4 +- cmd/dmsgcurl/commands/dmsgcurl.go | 4 +- cmd/dmsghttp/commands/dmsghttp.go | 2 +- cmd/dmsgip/commands/dmsgip.go | 16 +- cmd/dmsgpty-cli/commands/root.go | 2 +- cmd/dmsgpty-cli/commands/whitelist.go | 2 +- cmd/dmsgpty-host/commands/confgen.go | 7 +- cmd/dmsgpty-host/commands/root.go | 3 +- cmd/dmsgpty-ui/commands/dmsgpty-ui.go | 2 +- cmd/dmsgweb/commands/dmsgweb.go | 10 +- cmd/dmsgweb/commands/dmsgwebsrv.go | 11 +- cmd/dmsgweb/commands/root.go | 13 +- .../dmsg-example-http-server.go | 3 +- examples/proxified/main.go | 5 +- go.mod | 3 +- go.sum | 6 +- .../api/entries_endpoint_test.go | 2 +- pkg/disc/client_mock_test.go | 4 +- pkg/dmsg/session_common.go | 12 +- pkg/dmsgcurl/dmsgcurl.go | 6 +- pkg/dmsghttp/examples_test.go | 2 +- pkg/dmsghttp/http_test.go | 2 +- pkg/dmsghttp/util.go | 2 +- pkg/dmsgpty/host.go | 6 +- pkg/dmsgpty/ui.go | 2 +- pkg/noise/read_writer.go | 13 +- .../github.com/chen3feng/safecast/.gitignore | 15 + .../github.com/chen3feng/safecast/LICENSE.md | 201 ++++ .../github.com/chen3feng/safecast/README.md | 303 ++++++ .../chen3feng/safecast/README_zh.md | 299 ++++++ vendor/github.com/chen3feng/safecast/casts.go | 859 ++++++++++++++++++ vendor/github.com/chen3feng/safecast/check.sh | 13 + .../github.com/chen3feng/safecast/generate.py | 167 ++++ .../chen3feng/safecast/generate_generic.py | 86 ++ .../chen3feng/safecast/generate_test.py | 150 +++ .../github.com/chen3feng/safecast/generics.go | 461 ++++++++++ .../github.com/chen3feng/safecast/safecast.go | 9 + .../github.com/chen3feng/safecast/safecast.py | 6 + .../chen3feng/safecast/updatedoc.sh | 4 + .../coder}/websocket/LICENSE.txt | 0 vendor/github.com/coder/websocket/README.md | 160 ++++ .../coder}/websocket/accept.go | 6 +- .../coder}/websocket/close.go | 13 +- .../coder}/websocket/compress.go | 2 - .../coder}/websocket/conn.go | 8 - .../coder}/websocket/dial.go | 6 +- .../coder}/websocket/doc.go | 6 +- .../coder}/websocket/frame.go | 2 +- .../coder}/websocket/internal/bpool/bpool.go | 0 .../coder}/websocket/internal/errd/wrap.go | 0 .../coder}/websocket/internal/util/util.go | 0 .../coder}/websocket/internal/wsjs/wsjs_js.go | 0 .../coder}/websocket/internal/xsync/go.go | 0 .../coder}/websocket/internal/xsync/int64.go | 0 .../coder}/websocket/make.sh | 0 .../coder}/websocket/mask.go | 0 .../coder}/websocket/mask_amd64.s | 0 .../coder}/websocket/mask_arm64.s | 0 .../coder}/websocket/mask_asm.go | 0 .../coder}/websocket/mask_go.go | 0 .../coder}/websocket/netconn.go | 2 - .../coder}/websocket/netconn_js.go | 0 .../coder}/websocket/netconn_notjs.go | 0 .../coder}/websocket/read.go | 14 +- .../coder}/websocket/stringer.go | 2 - .../coder}/websocket/write.go | 8 +- .../coder}/websocket/ws_js.go | 8 +- .../skywire-utilities/pkg/skyenv/values.go | 51 -- vendor/modules.txt | 20 +- vendor/nhooyr.io/websocket/README.md | 1 - 72 files changed, 2847 insertions(+), 187 deletions(-) create mode 100644 vendor/github.com/chen3feng/safecast/.gitignore create mode 100644 vendor/github.com/chen3feng/safecast/LICENSE.md create mode 100644 vendor/github.com/chen3feng/safecast/README.md create mode 100644 vendor/github.com/chen3feng/safecast/README_zh.md create mode 100644 vendor/github.com/chen3feng/safecast/casts.go create mode 100644 vendor/github.com/chen3feng/safecast/check.sh create mode 100644 vendor/github.com/chen3feng/safecast/generate.py create mode 100644 vendor/github.com/chen3feng/safecast/generate_generic.py create mode 100644 vendor/github.com/chen3feng/safecast/generate_test.py create mode 100644 vendor/github.com/chen3feng/safecast/generics.go create mode 100644 vendor/github.com/chen3feng/safecast/safecast.go create mode 100644 vendor/github.com/chen3feng/safecast/safecast.py create mode 100644 vendor/github.com/chen3feng/safecast/updatedoc.sh rename vendor/{nhooyr.io => github.com/coder}/websocket/LICENSE.txt (100%) create mode 100644 vendor/github.com/coder/websocket/README.md rename vendor/{nhooyr.io => github.com/coder}/websocket/accept.go (97%) rename vendor/{nhooyr.io => github.com/coder}/websocket/close.go (93%) rename vendor/{nhooyr.io => github.com/coder}/websocket/compress.go (98%) rename vendor/{nhooyr.io => github.com/coder}/websocket/conn.go (94%) rename vendor/{nhooyr.io => github.com/coder}/websocket/dial.go (97%) rename vendor/{nhooyr.io => github.com/coder}/websocket/doc.go (82%) rename vendor/{nhooyr.io => github.com/coder}/websocket/frame.go (98%) rename vendor/{nhooyr.io => github.com/coder}/websocket/internal/bpool/bpool.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/internal/errd/wrap.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/internal/util/util.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/internal/wsjs/wsjs_js.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/internal/xsync/go.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/internal/xsync/int64.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/make.sh (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/mask.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/mask_amd64.s (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/mask_arm64.s (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/mask_asm.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/mask_go.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/netconn.go (98%) rename vendor/{nhooyr.io => github.com/coder}/websocket/netconn_js.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/netconn_notjs.go (100%) rename vendor/{nhooyr.io => github.com/coder}/websocket/read.go (95%) rename vendor/{nhooyr.io => github.com/coder}/websocket/stringer.go (94%) rename vendor/{nhooyr.io => github.com/coder}/websocket/write.go (96%) rename vendor/{nhooyr.io => github.com/coder}/websocket/ws_js.go (98%) delete mode 100644 vendor/github.com/skycoin/skywire-utilities/pkg/skyenv/values.go delete mode 100644 vendor/nhooyr.io/websocket/README.md diff --git a/cmd/dmsg-server/commands/config/gen.go b/cmd/dmsg-server/commands/config/gen.go index fac91bbf8..22b278f1f 100644 --- a/cmd/dmsg-server/commands/config/gen.go +++ b/cmd/dmsg-server/commands/config/gen.go @@ -26,7 +26,7 @@ func init() { var genConfigCmd = &cobra.Command{ Use: "gen", Short: "Generate a config file", - Run: func(cmd *cobra.Command, args []string) { + Run: func(_ *cobra.Command, _ []string) { mLog := logging.NewMasterLogger() mLog.SetLevel(logrus.InfoLevel) logger := mLog.PackageLogger("dmsg-server config generator") diff --git a/cmd/dmsg-server/commands/start/root.go b/cmd/dmsg-server/commands/start/root.go index d691d623e..7217670a4 100644 --- a/cmd/dmsg-server/commands/start/root.go +++ b/cmd/dmsg-server/commands/start/root.go @@ -11,7 +11,7 @@ import ( "os" "strconv" - "github.com/go-chi/chi/v5" + chi "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/skycoin/skywire-utilities/pkg/buildinfo" "github.com/skycoin/skywire-utilities/pkg/cmdutil" @@ -40,8 +40,8 @@ func init() { var RootCmd = &cobra.Command{ Use: "start", Short: "Start Dmsg Server", - PreRunE: func(cmd *cobra.Command, args []string) error { return sf.Check() }, - Run: func(_ *cobra.Command, args []string) { + PreRunE: func(_ *cobra.Command, _ []string) error { return sf.Check() }, + Run: func(_ *cobra.Command, _ []string) { if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil { log.Printf("Failed to output build info: %v", err) } diff --git a/cmd/dmsg-socks5/commands/dmsg-socks5.go b/cmd/dmsg-socks5/commands/dmsg-socks5.go index 5b0841426..e7eac5c74 100644 --- a/cmd/dmsg-socks5/commands/dmsg-socks5.go +++ b/cmd/dmsg-socks5/commands/dmsg-socks5.go @@ -97,7 +97,7 @@ var serveCmd = &cobra.Command{ SilenceUsage: true, DisableSuggestions: true, DisableFlagsInUseLine: true, - Run: func(cmd *cobra.Command, args []string) { + Run: func(_ *cobra.Command, _ []string) { log := logging.MustGetLogger("ssh-proxy") interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) @@ -178,7 +178,7 @@ var serveCmd = &cobra.Command{ var proxyCmd = &cobra.Command{ Use: "client", Short: "socks5 proxy client for dmsg socks5 proxy server", - Run: func(cmd *cobra.Command, args []string) { + Run: func(_ *cobra.Command, _ []string) { log := logging.MustGetLogger("ssh-proxy-client") var pubKey cipher.PubKey err := pubKey.Set(pubk) diff --git a/cmd/dmsgcurl/commands/dmsgcurl.go b/cmd/dmsgcurl/commands/dmsgcurl.go index df85942e5..b5397090f 100644 --- a/cmd/dmsgcurl/commands/dmsgcurl.go +++ b/cmd/dmsgcurl/commands/dmsgcurl.go @@ -86,7 +86,7 @@ DMSG curl utility`, DisableFlagsInUseLine: true, Version: buildinfo.Version(), - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { if dmsgcurlLog == nil { dmsgcurlLog = logging.MustGetLogger("dmsgcurl") } @@ -254,7 +254,7 @@ func parseOutputFile(output string, replace bool) (*os.File, error) { return nil, statErr } if replace { - return os.OpenFile(filepath.Clean(output), os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm) + return os.OpenFile(filepath.Clean(output), os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm) //nolint } return nil, os.ErrExist } diff --git a/cmd/dmsghttp/commands/dmsghttp.go b/cmd/dmsghttp/commands/dmsghttp.go index 647fa294a..292957fc6 100644 --- a/cmd/dmsghttp/commands/dmsghttp.go +++ b/cmd/dmsghttp/commands/dmsghttp.go @@ -62,7 +62,7 @@ DMSG http file server`, DisableFlagsInUseLine: true, Version: buildinfo.Version(), - Run: func(cmd *cobra.Command, args []string) { + Run: func(_ *cobra.Command, _ []string) { log := logging.MustGetLogger("dmsghttp") if dmsgDisc == "" { log.Fatal("Dmsg Discovery URL not specified") diff --git a/cmd/dmsgip/commands/dmsgip.go b/cmd/dmsgip/commands/dmsgip.go index 2b6a14ab2..7a875d8e1 100644 --- a/cmd/dmsgip/commands/dmsgip.go +++ b/cmd/dmsgip/commands/dmsgip.go @@ -14,7 +14,6 @@ import ( "github.com/skycoin/skywire-utilities/pkg/cipher" "github.com/skycoin/skywire-utilities/pkg/cmdutil" "github.com/skycoin/skywire-utilities/pkg/logging" - "github.com/skycoin/skywire-utilities/pkg/skyenv" "github.com/spf13/cobra" "github.com/skycoin/dmsg/pkg/disc" @@ -22,20 +21,20 @@ import ( ) var ( - dmsgDisc string + dmsgDisc = dmsg.DiscAddr(false) sk cipher.SecKey logLvl string dmsgServers []string ) func init() { - RootCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "c", "", "dmsg discovery url default:\n"+skyenv.DmsgDiscAddr) + RootCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "c", dmsgDisc, "dmsg discovery url\033[0m") RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "fatal", "[ debug | warn | error | fatal | panic | trace | info ]\033[0m") if os.Getenv("DMSGIP_SK") != "" { sk.Set(os.Getenv("DMSGIP_SK")) //nolint } - RootCmd.Flags().StringSliceVarP(&dmsgServers, "srv", "d", []string{}, "dmsg server public keys\n\r") - RootCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\n\r") + RootCmd.Flags().StringSliceVarP(&dmsgServers, "srv", "d", []string{}, "dmsg server public keys\n\r\033[0m") + RootCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\n\r\033[0m") } // RootCmd containsa the root dmsgcurl command @@ -54,12 +53,7 @@ DMSG ip utility`, DisableSuggestions: true, DisableFlagsInUseLine: true, Version: buildinfo.Version(), - PreRun: func(cmd *cobra.Command, args []string) { - if dmsgDisc == "" { - dmsgDisc = skyenv.DmsgDiscAddr - } - }, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, _ []string) error { log := logging.MustGetLogger("dmsgip") if logLvl != "" { diff --git a/cmd/dmsgpty-cli/commands/root.go b/cmd/dmsgpty-cli/commands/root.go index ad57f3867..811b7b236 100644 --- a/cmd/dmsgpty-cli/commands/root.go +++ b/cmd/dmsgpty-cli/commands/root.go @@ -104,7 +104,7 @@ DMSG pseudoterminal command line interface`, remoteAddr.Port = dmsgpty.DefaultPort } }, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, _ []string) error { if _, err := buildinfo.Get().WriteTo(log.Writer()); err != nil { log.Printf("Failed to output build info: %v", err) } diff --git a/cmd/dmsgpty-cli/commands/whitelist.go b/cmd/dmsgpty-cli/commands/whitelist.go index 3f31bba19..4d0e7e96c 100644 --- a/cmd/dmsgpty-cli/commands/whitelist.go +++ b/cmd/dmsgpty-cli/commands/whitelist.go @@ -19,7 +19,7 @@ func init() { var whitelistCmd = &cobra.Command{ Use: "whitelist", Short: "lists all whitelisted public keys", - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, _ []string) error { wlC, err := cli.WhitelistClient() if err != nil { return err diff --git a/cmd/dmsgpty-host/commands/confgen.go b/cmd/dmsgpty-host/commands/confgen.go index c097c6c73..ba0c1e7d1 100644 --- a/cmd/dmsgpty-host/commands/confgen.go +++ b/cmd/dmsgpty-host/commands/confgen.go @@ -20,10 +20,9 @@ func init() { } var confgenCmd = &cobra.Command{ - Use: "confgen ", - Short: "generates config file", - Args: cobra.MaximumNArgs(1), - PreRun: func(cmd *cobra.Command, args []string) {}, + Use: "confgen ", + Short: "generates config file", + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { diff --git a/cmd/dmsgpty-host/commands/root.go b/cmd/dmsgpty-host/commands/root.go index 44dc90433..753ff3beb 100644 --- a/cmd/dmsgpty-host/commands/root.go +++ b/cmd/dmsgpty-host/commands/root.go @@ -84,8 +84,7 @@ DMSG host for pseudoterminal command line interface`, SilenceUsage: true, DisableSuggestions: true, DisableFlagsInUseLine: true, - PreRun: func(cmd *cobra.Command, args []string) {}, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { conf, err := getConfig(cmd, false) if err != nil { return fmt.Errorf("failed to get config: %w", err) diff --git a/cmd/dmsgpty-ui/commands/dmsgpty-ui.go b/cmd/dmsgpty-ui/commands/dmsgpty-ui.go index 69ddad929..140a2312e 100644 --- a/cmd/dmsgpty-ui/commands/dmsgpty-ui.go +++ b/cmd/dmsgpty-ui/commands/dmsgpty-ui.go @@ -43,7 +43,7 @@ var RootCmd = &cobra.Command{ │││││└─┐│ ┬├─┘ │ └┬┘───│ ││ ─┴┘┴ ┴└─┘└─┘┴ ┴ ┴ └─┘┴ ` + "DMSG pseudoterminal GUI", - Run: func(cmd *cobra.Command, args []string) { + Run: func(_ *cobra.Command, _ []string) { if _, err := buildinfo.Get().WriteTo(log.Writer()); err != nil { log.Printf("Failed to output build info: %v", err) } diff --git a/cmd/dmsgweb/commands/dmsgweb.go b/cmd/dmsgweb/commands/dmsgweb.go index 9d9d84977..d7f94cee2 100644 --- a/cmd/dmsgweb/commands/dmsgweb.go +++ b/cmd/dmsgweb/commands/dmsgweb.go @@ -18,6 +18,7 @@ import ( "syscall" "github.com/bitfield/script" + "github.com/chen3feng/safecast" "github.com/confiant-inc/go-socks5" "github.com/gin-gonic/gin" "github.com/skycoin/skywire-utilities/pkg/buildinfo" @@ -99,7 +100,7 @@ dmsgweb conf file detected: ` + dmsgwebconffile DisableSuggestions: true, DisableFlagsInUseLine: true, Version: buildinfo.Version(), - Run: func(cmd *cobra.Command, _ []string) { + Run: func(_ *cobra.Command, _ []string) { if isEnvs { envfile := envfileLinux if runtime.GOOS == "windows" { @@ -390,8 +391,11 @@ func proxyTCPConn(n int) { go func(conn net.Conn, n int) { defer wg.Done() defer conn.Close() //nolint - - dmsgConn, err := dmsgC.DialStream(context.Background(), dmsg.Addr{PK: dialPK[n], Port: uint16(dmsgPorts[n])}) + dp, ok := safecast.To[uint16](dmsgPorts[n]) + if !ok { + dmsgWebLog.Fatal("uint16 overflow when converting dmsg port") + } + dmsgConn, err := dmsgC.DialStream(context.Background(), dmsg.Addr{PK: dialPK[n], Port: dp}) //nolint if err != nil { log.Printf("Failed to dial dmsg address %v:%v %v", dialPK[n].String(), dmsgPorts[n], err) return diff --git a/cmd/dmsgweb/commands/dmsgwebsrv.go b/cmd/dmsgweb/commands/dmsgwebsrv.go index 04e9ca492..181d426ad 100644 --- a/cmd/dmsgweb/commands/dmsgwebsrv.go +++ b/cmd/dmsgweb/commands/dmsgwebsrv.go @@ -16,6 +16,7 @@ import ( "time" "github.com/bitfield/script" + "github.com/chen3feng/safecast" "github.com/gin-gonic/gin" "github.com/skycoin/skywire-utilities/pkg/cipher" "github.com/skycoin/skywire-utilities/pkg/cmdutil" @@ -152,15 +153,19 @@ func server() { var listN []net.Listener for _, dport := range dmsgPort { - lis, err := dmsgC.Listen(uint16(dport)) + dp, ok := safecast.To[uint16](dport) + if !ok { + log.Fatal("uint16 overflow when converting dmsg port") + } + lis, err := dmsgC.Listen(dp) if err != nil { log.Fatalf("Error listening on port %d: %v", dport, err) } listN = append(listN, lis) - dport := dport - go func(l net.Listener, port uint) { + dport := dp + go func(l net.Listener, port uint16) { <-ctx.Done() if err := l.Close(); err != nil { log.Printf("Error closing listener on port %d: %v", port, err) diff --git a/cmd/dmsgweb/commands/root.go b/cmd/dmsgweb/commands/root.go index 091a31233..38d1e7580 100644 --- a/cmd/dmsgweb/commands/root.go +++ b/cmd/dmsgweb/commands/root.go @@ -15,6 +15,7 @@ import ( "time" "github.com/bitfield/script" + "github.com/chen3feng/safecast" "github.com/gin-gonic/gin" "github.com/skycoin/skywire-utilities/pkg/cipher" "github.com/skycoin/skywire-utilities/pkg/logging" @@ -267,7 +268,11 @@ func scriptExecUint(s, envfile string) uint { } i, err := strconv.Atoi(strings.TrimSpace(strings.TrimRight(out, "\n"))) if err == nil { - return uint(i) + u, ok := safecast.To[uint](i) + if !ok { + log.Fatal("uint overflow") + } + return u } return 0 } @@ -280,7 +285,11 @@ func scriptExecUint(s, envfile string) uint { } i, err := strconv.Atoi(z) if err == nil { - return uint(i) + u, ok := safecast.To[uint](i) + if !ok { + log.Fatal("uint overflow") + } + return u } } return uint(0) diff --git a/examples/dmsgcurl/dmsg-example-http-server/dmsg-example-http-server.go b/examples/dmsgcurl/dmsg-example-http-server/dmsg-example-http-server.go index 42a025c66..44cff2f4f 100644 --- a/examples/dmsgcurl/dmsg-example-http-server/dmsg-example-http-server.go +++ b/examples/dmsgcurl/dmsg-example-http-server/dmsg-example-http-server.go @@ -12,7 +12,6 @@ import ( "github.com/skycoin/skywire-utilities/pkg/cipher" "github.com/skycoin/skywire-utilities/pkg/cmdutil" "github.com/skycoin/skywire-utilities/pkg/logging" - "github.com/skycoin/skywire-utilities/pkg/skyenv" "github.com/skycoin/dmsg/pkg/disc" dmsg "github.com/skycoin/dmsg/pkg/dmsg" @@ -20,7 +19,7 @@ import ( var ( dir = "." // local dir to serve via http - dmsgDisc = skyenv.DmsgDiscAddr + dmsgDisc = dmsg.DiscAddr(false) dmsgPort = uint(80) pk, sk = cipher.GenerateKeyPair() ) diff --git a/examples/proxified/main.go b/examples/proxified/main.go index 56f0975cb..880afb29d 100644 --- a/examples/proxified/main.go +++ b/examples/proxified/main.go @@ -7,7 +7,6 @@ import ( "github.com/skycoin/skywire-utilities/pkg/cipher" "github.com/skycoin/skywire-utilities/pkg/logging" - "github.com/skycoin/skywire-utilities/pkg/skyenv" "golang.org/x/net/proxy" "github.com/skycoin/dmsg/pkg/disc" @@ -42,10 +41,10 @@ func main() { } // instantiate clients with custom config - respC := dmsg.NewClient(respPK, respSK, disc.NewHTTP(skyenv.DmsgDiscAddr, httpClient, log), dmsg.DefaultConfig()) + respC := dmsg.NewClient(respPK, respSK, disc.NewHTTP(dmsg.DiscAddr(false), httpClient, log), dmsg.DefaultConfig()) go respC.Serve(context.Background()) - initC := dmsg.NewClient(initPK, initSK, disc.NewHTTP(skyenv.DmsgDiscAddr, &http.Client{}, log), dmsg.DefaultConfig()) + initC := dmsg.NewClient(initPK, initSK, disc.NewHTTP(dmsg.DiscAddr(false), &http.Client{}, log), dmsg.DefaultConfig()) go initC.Serve(context.Background()) time.Sleep(2 * time.Second) diff --git a/go.mod b/go.mod index 3b944eb3b..9567f1fd9 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,8 @@ require ( github.com/ActiveState/termtest/conpty v0.5.0 github.com/VictoriaMetrics/metrics v1.35.1 github.com/bitfield/script v0.23.0 + github.com/chen3feng/safecast v0.0.0-20220908170618-81b2ecd47937 + github.com/coder/websocket v1.8.12 github.com/confiant-inc/go-socks5 v0.0.0-20210816151940-c1124825b1d6 github.com/creack/pty v1.1.23 github.com/gin-gonic/gin v1.10.0 @@ -27,7 +29,6 @@ require ( golang.org/x/net v0.30.0 golang.org/x/sys v0.26.0 golang.org/x/term v0.25.0 - nhooyr.io/websocket v1.8.17 ) require ( diff --git a/go.sum b/go.sum index 9a9759752..bc8e95f4b 100644 --- a/go.sum +++ b/go.sum @@ -19,10 +19,14 @@ github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4 github.com/cenkalti/backoff v1.1.0/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/chen3feng/safecast v0.0.0-20220908170618-81b2ecd47937 h1:gJMTUTnqa+f2GzdU1p3UVa3E39hogZdiRHEgBBnqtVc= +github.com/chen3feng/safecast v0.0.0-20220908170618-81b2ecd47937/go.mod h1:HPBMB1GC+eBfIUWhh9IJKdL/mVhIBZbJzjvijHxG3F0= github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo= +github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs= github.com/confiant-inc/go-socks5 v0.0.0-20210816151940-c1124825b1d6 h1:sRQemCQ+r6Ht7uIT0D9Xcyjed4lKpDhNKarBEPFZp3c= github.com/confiant-inc/go-socks5 v0.0.0-20210816151940-c1124825b1d6/go.mod h1:S4w2wY39ZYaWQLXNMZ6uVfIyYrKmLP2N/S2/5YIFU6o= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -226,6 +230,4 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= mvdan.cc/sh/v3 v3.9.0 h1:it14fyjCdQUk4jf/aYxLO3FG8jFarR9GzMCtnlvvD7c= mvdan.cc/sh/v3 v3.9.0/go.mod h1:cdBk8bgoiBI7lSZqK5JhUuq7OB64VQ7fgm85xelw3Nk= -nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y= -nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/internal/dmsg-discovery/api/entries_endpoint_test.go b/internal/dmsg-discovery/api/entries_endpoint_test.go index 75689528c..84c402255 100644 --- a/internal/dmsg-discovery/api/entries_endpoint_test.go +++ b/internal/dmsg-discovery/api/entries_endpoint_test.go @@ -54,7 +54,7 @@ func TestEntriesEndpoint(t *testing.T) { contentType: "application/json", responseIsEntry: true, entry: baseEntry, - entryPreHook: func(t *testing.T, e *disc.Entry, body *string) { + entryPreHook: func(t *testing.T, e *disc.Entry, body *string) { //nolint err := e.Sign(sk) require.NoError(t, err) }, diff --git a/pkg/disc/client_mock_test.go b/pkg/disc/client_mock_test.go index b8976f8d1..5e75c4279 100644 --- a/pkg/disc/client_mock_test.go +++ b/pkg/disc/client_mock_test.go @@ -293,7 +293,7 @@ func TestNewMockUpdateEntriesEndpoint(t *testing.T) { name: "update entry iteration", responseShouldError: false, secretKey: sk, - storerPreHook: func(apiClient disc.APIClient, e *disc.Entry) { + storerPreHook: func(apiClient disc.APIClient, e *disc.Entry) { //nolint e.Server.Address = "different one" }, }, @@ -301,7 +301,7 @@ func TestNewMockUpdateEntriesEndpoint(t *testing.T) { name: "update entry unauthorized", responseShouldError: true, secretKey: ephemeralSk1, - storerPreHook: func(apiClient disc.APIClient, e *disc.Entry) { + storerPreHook: func(apiClient disc.APIClient, e *disc.Entry) { //nolint e.Server.Address = "different one" }, }, diff --git a/pkg/dmsg/session_common.go b/pkg/dmsg/session_common.go index 12e9c8186..d38afd1a7 100644 --- a/pkg/dmsg/session_common.go +++ b/pkg/dmsg/session_common.go @@ -3,11 +3,13 @@ package dmsg import ( "encoding/binary" + "fmt" "io" "net" "sync" "time" + "github.com/chen3feng/safecast" "github.com/hashicorp/yamux" "github.com/sirupsen/logrus" "github.com/skycoin/skywire-utilities/pkg/cipher" @@ -123,9 +125,13 @@ func (sc *SessionCommon) writeObject(w io.Writer, obj SignedObject) error { p := sc.ns.EncryptUnsafe(obj) sc.wMx.Unlock() p = append(make([]byte, 2), p...) - binary.BigEndian.PutUint16(p, uint16(len(p)-2)) - _, err := w.Write(p) - return err + lps2, ok := safecast.To[uint16](len(p) - 2) + if ok { + binary.BigEndian.PutUint16(p, lps2) + _, err := w.Write(p) + return err + } + return fmt.Errorf("writeObject failed cast to uint16") } func (sc *SessionCommon) readObject(r io.Reader) (SignedObject, error) { diff --git a/pkg/dmsgcurl/dmsgcurl.go b/pkg/dmsgcurl/dmsgcurl.go index d713115e7..5a32d63f9 100644 --- a/pkg/dmsgcurl/dmsgcurl.go +++ b/pkg/dmsgcurl/dmsgcurl.go @@ -43,10 +43,10 @@ func New(fs *flag.FlagSet) *DmsgCurl { w := fs.Output() flag.Usage = func() { - _, _ = fmt.Fprintf(w, "Skycoin %s %s, wget over dmsg.\n", ExecName, Version) - _, _ = fmt.Fprintf(w, "Usage: %s [OPTION]... [URL]\n\n", ExecName) + _, _ = fmt.Fprintf(w, "Skycoin %s %s, wget over dmsg.\n", ExecName, Version) //nolint + _, _ = fmt.Fprintf(w, "Usage: %s [OPTION]... [URL]\n\n", ExecName) //nolint flag.PrintDefaults() - _, _ = fmt.Fprintln(w, "") + _, _ = fmt.Fprintln(w, "") //nolint } return dg diff --git a/pkg/dmsghttp/examples_test.go b/pkg/dmsghttp/examples_test.go index df9493137..4243ae5dd 100644 --- a/pkg/dmsghttp/examples_test.go +++ b/pkg/dmsghttp/examples_test.go @@ -74,7 +74,7 @@ func ExampleMakeHTTPTransport() { }() r := chi.NewRouter() - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { //nolint _, _ = w.Write([]byte("

Hello World!

")) //nolint:errcheck }) go func() { _ = http.Serve(lis, r) }() //nolint diff --git a/pkg/dmsghttp/http_test.go b/pkg/dmsghttp/http_test.go index d098c7873..11a342a40 100644 --- a/pkg/dmsghttp/http_test.go +++ b/pkg/dmsghttp/http_test.go @@ -86,7 +86,7 @@ func (r httpClientResult) Assert(t *testing.T, i int) { func startHTTPServer(t *testing.T, results chan httpServerResult, lis net.Listener) { r := chi.NewRouter() - r.HandleFunc(endpointHTML, func(w http.ResponseWriter, r *http.Request) { + r.HandleFunc(endpointHTML, func(w http.ResponseWriter, r *http.Request) { //nolint result := httpServerResult{Path: endpointHTML} n, err := w.Write(endpointHTMLData) diff --git a/pkg/dmsghttp/util.go b/pkg/dmsghttp/util.go index af97bc7b9..a3ac740fd 100644 --- a/pkg/dmsghttp/util.go +++ b/pkg/dmsghttp/util.go @@ -52,7 +52,7 @@ func UpdateServers(ctx context.Context, dClient disc.APIClient, dmsgDisc string, for { select { case <-ctx.Done(): - return + return //nolint case <-ticker.C: servers, err := dmsgclient.AllServers(ctx) if err != nil { diff --git a/pkg/dmsgpty/host.go b/pkg/dmsgpty/host.go index 6784ae2f6..7ebaa9200 100644 --- a/pkg/dmsgpty/host.go +++ b/pkg/dmsgpty/host.go @@ -210,13 +210,15 @@ func dmsgEndpoints(h *Host) (mux hostMux) { } func handleWhitelist(h *Host) handleFunc { - return func(ctx context.Context, uri *url.URL, rpcS *rpc.Server) error { + // return func(ctx context.Context, uri *url.URL, rpcS *rpc.Server) error { + return func(_ context.Context, _ *url.URL, rpcS *rpc.Server) error { return rpcS.RegisterName(WhitelistRPCName, NewWhitelistGateway(h.wl)) } } func handlePty(h *Host) handleFunc { - return func(ctx context.Context, uri *url.URL, rpcS *rpc.Server) error { + // return func(ctx context.Context, uri *url.URL, rpcS *rpc.Server) error { + return func(ctx context.Context, _ *url.URL, rpcS *rpc.Server) error { pty := NewPty() go func() { <-ctx.Done() diff --git a/pkg/dmsgpty/ui.go b/pkg/dmsgpty/ui.go index 5aa52afe8..ba30e1ad2 100644 --- a/pkg/dmsgpty/ui.go +++ b/pkg/dmsgpty/ui.go @@ -12,10 +12,10 @@ import ( "sync/atomic" "time" + "github.com/coder/websocket" "github.com/sirupsen/logrus" "github.com/skycoin/skywire-utilities/pkg/httputil" "github.com/skycoin/skywire-utilities/pkg/logging" - "nhooyr.io/websocket" ) const ( diff --git a/pkg/noise/read_writer.go b/pkg/noise/read_writer.go index 8315753fc..a393266a1 100644 --- a/pkg/noise/read_writer.go +++ b/pkg/noise/read_writer.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/chen3feng/safecast" "github.com/skycoin/skywire-utilities/pkg/cipher" "github.com/skycoin/dmsg/pkg/ioutil" @@ -263,11 +264,15 @@ func ResponderHandshake(ns *Noise, r *bufio.Reader, w io.Writer) error { // It returns the bytes written. func WriteRawFrame(w io.Writer, p []byte) ([]byte, error) { buf := make([]byte, prefixSize+len(p)) - binary.BigEndian.PutUint16(buf, uint16(len(p))) - copy(buf[prefixSize:], p) + lenp, ok := safecast.To[uint16](len(p)) + if ok { + binary.BigEndian.PutUint16(buf, lenp) + copy(buf[prefixSize:], p) - n, err := w.Write(buf) - return buf[:n], err + n, err := w.Write(buf) + return buf[:n], err + } + return []byte{}, fmt.Errorf("failed to cast length of slice to uint16") } // ReadRawFrame attempts to read a raw frame from a buffered reader. diff --git a/vendor/github.com/chen3feng/safecast/.gitignore b/vendor/github.com/chen3feng/safecast/.gitignore new file mode 100644 index 000000000..66fd13c90 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/.gitignore @@ -0,0 +1,15 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ diff --git a/vendor/github.com/chen3feng/safecast/LICENSE.md b/vendor/github.com/chen3feng/safecast/LICENSE.md new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/LICENSE.md @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/chen3feng/safecast/README.md b/vendor/github.com/chen3feng/safecast/README.md new file mode 100644 index 000000000..a590b1738 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/README.md @@ -0,0 +1,303 @@ +# safecast + +Safe Numeric Type Cast Library for Go + +English | [简体中文](README_zh.md) + +[![License Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-red.svg)](COPYING) +[![Golang](https://img.shields.io/badge/Language-go1.18+-blue.svg)](https://go.dev/) +![Build Status](https://github.com/chen3feng/safecast/actions/workflows/go.yml/badge.svg) +[![Coverage Status](https://coveralls.io/repos/github/chen3feng/safecast/badge.svg?branch=master)](https://coveralls.io/github/chen3feng/safecast?branch=master) +[![GoReport](https://goreportcard.com/badge/github.com/securego/gosec)](https://goreportcard.com/report/github.com/chen3feng/safecast) +[![Go Reference](https://pkg.go.dev/badge/github.com/chen3feng/safecast.svg)](https://pkg.go.dev/github.com/chen3feng/safecast) + +Safe numeric type cast library. suppoer all integral and floating types, except uintptr. + +This library depends on go generics, which is introduced in 1.18+. + +Usage: + +```go +val, ok := To[type](value) +``` + +`ok == false` indicates overflow occured. but whatever,`val` is always equals to the result of the normal type cast (`type(value)`) expression。 + +There are also non-parametric forms of the target types: + +````go +val, ok := ToInt32(value) +```` + +The usage is the same as the according full generic version, and the performance will be better. + + + + + +# safecast + +```go +import "github.com/chen3feng/safecast" +``` + +Package safecast provide a safe way to cast a numeric value from type A to type B, with overflow and underflow check. + +## Index + +- [func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)](<#func-to>) +- [func ToFloat32[FromType numericType](value FromType) (float32, bool)](<#func-tofloat32>) +- [func ToFloat64[FromType numericType](value FromType) (float64, bool)](<#func-tofloat64>) +- [func ToInt[FromType numericType](value FromType) (int, bool)](<#func-toint>) +- [func ToInt16[FromType numericType](value FromType) (int16, bool)](<#func-toint16>) +- [func ToInt32[FromType numericType](value FromType) (int32, bool)](<#func-toint32>) +- [func ToInt64[FromType numericType](value FromType) (int64, bool)](<#func-toint64>) +- [func ToInt8[FromType numericType](value FromType) (int8, bool)](<#func-toint8>) +- [func ToUint[FromType numericType](value FromType) (uint, bool)](<#func-touint>) +- [func ToUint16[FromType numericType](value FromType) (uint16, bool)](<#func-touint16>) +- [func ToUint32[FromType numericType](value FromType) (uint32, bool)](<#func-touint32>) +- [func ToUint64[FromType numericType](value FromType) (uint64, bool)](<#func-touint64>) +- [func ToUint8[FromType numericType](value FromType) (uint8, bool)](<#func-touint8>) + + +## func [To]() + +```go +func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool) +``` + +To converts a numeric value from the FromType to the specified ToType type safely. result will always be same as the usual type cast \(type\(value\)\), but ok is false when overflow or underflow occured. + +
Example (Float Overflow) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" + "math" +) + +func main() { + n, ok := safecast.To[float32](math.MaxFloat32 * 2) + fmt.Print(n, ok) +} +``` + +#### Output + +``` ++Inf false +``` + +

+
+ +
Example (Int No Overflow) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + b, ok := safecast.To[byte](255) + fmt.Print(b, ok) +} +``` + +#### Output + +``` +255 true +``` + +

+
+ +
Example (Int Overflow) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + b, ok := safecast.To[byte](256) + fmt.Print(b, ok) +} +``` + +#### Output + +``` +0 false +``` + +

+
+ +
Example (Value In Range) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + n, ok := safecast.To[uint](1) + fmt.Print(n, ok) +} +``` + +#### Output + +``` +1 true +``` + +

+
+ +
Example (Value Out Of Range) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + n, ok := safecast.To[uint32](-1) + fmt.Print(n, ok) +} +``` + +#### Output + +``` +4294967295 false +``` + +

+
+ +## func [ToFloat32]() + +```go +func ToFloat32[FromType numericType](value FromType) (float32, bool) +``` + +ToFloat32 converts value to float32 type safely. result will always be same as the usual type cast\(float32\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToFloat64]() + +```go +func ToFloat64[FromType numericType](value FromType) (float64, bool) +``` + +ToFloat64 converts value to float64 type safely. result will always be same as the usual type cast\(float64\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt]() + +```go +func ToInt[FromType numericType](value FromType) (int, bool) +``` + +ToInt converts value to int type safely. result will always be same as the usual type cast\(int\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt16]() + +```go +func ToInt16[FromType numericType](value FromType) (int16, bool) +``` + +ToInt16 converts value to int16 type safely. result will always be same as the usual type cast\(int16\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt32]() + +```go +func ToInt32[FromType numericType](value FromType) (int32, bool) +``` + +ToInt32 converts value to int32 type safely. result will always be same as the usual type cast\(int32\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt64]() + +```go +func ToInt64[FromType numericType](value FromType) (int64, bool) +``` + +ToInt64 converts value to int64 type safely. result will always be same as the usual type cast\(int64\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt8]() + +```go +func ToInt8[FromType numericType](value FromType) (int8, bool) +``` + +ToInt8 converts value to int8 type safely. result will always be same as the usual type cast\(int8\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint]() + +```go +func ToUint[FromType numericType](value FromType) (uint, bool) +``` + +ToUint converts value to uint type safely. result will always be same as the usual type cast\(uint\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint16]() + +```go +func ToUint16[FromType numericType](value FromType) (uint16, bool) +``` + +ToUint16 converts value to uint16 type safely. result will always be same as the usual type cast\(uint16\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint32]() + +```go +func ToUint32[FromType numericType](value FromType) (uint32, bool) +``` + +ToUint32 converts value to uint32 type safely. result will always be same as the usual type cast\(uint32\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint64]() + +```go +func ToUint64[FromType numericType](value FromType) (uint64, bool) +``` + +ToUint64 converts value to uint64 type safely. result will always be same as the usual type cast\(uint64\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint8]() + +```go +func ToUint8[FromType numericType](value FromType) (uint8, bool) +``` + +ToUint8 converts value to uint8 type safely. result will always be same as the usual type cast\(uint8\(value\)\), but ok is false when overflow or underflow occured. + + + +Generated by [gomarkdoc]() + + + diff --git a/vendor/github.com/chen3feng/safecast/README_zh.md b/vendor/github.com/chen3feng/safecast/README_zh.md new file mode 100644 index 000000000..0972b5bfa --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/README_zh.md @@ -0,0 +1,299 @@ +# safecast -- Safe Numeric Type Cast Library For Go + +[English](README.md) | 简体中文 + +[![License Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-red.svg)](COPYING) +[![Python](https://img.shields.io/badge/Language-go1.18+-blue.svg)](https://www.python.org/) +![Build Status](https://github.com/chen3feng/safecast/actions/workflows/go.yml/badge.svg) +[![Coverage Status](https://coveralls.io/repos/github/chen3feng/safecast/badge.svg?branch=master)](https://coveralls.io/github/chen3feng/safecast?branch=master) +[![GoReport](https://goreportcard.com/badge/github.com/securego/gosec)](https://goreportcard.com/report/github.com/chen3feng/safecast) +[![Go Reference](https://pkg.go.dev/badge/github.com/chen3feng/safecast.svg)](https://pkg.go.dev/github.com/chen3feng/safecast) + +安全的数值类型转换库。支持除了 `uintptr` 外的所有整数和浮点数类型。 + +用法: + +```go +val, ok := To[type](value) +``` + +`ok == false` 表示值溢出。但是无论成功失败,`val` 都等效于直接用普通类型转换(`type(value)`)的结果。 + +另外也有目标类型非参数化的形式: + +```go +val, ok := ToInt32(value) +``` + +用法同全泛型版本,并且性能上会好一些。 + + + + + +# safecast + +```go +import "github.com/chen3feng/safecast" +``` + +Package safecast provide a safe way to cast a numeric value from type A to type B, with overflow and underflow check. + +## Index + +- [func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)](<#func-to>) +- [func ToFloat32[FromType numericType](value FromType) (float32, bool)](<#func-tofloat32>) +- [func ToFloat64[FromType numericType](value FromType) (float64, bool)](<#func-tofloat64>) +- [func ToInt[FromType numericType](value FromType) (int, bool)](<#func-toint>) +- [func ToInt16[FromType numericType](value FromType) (int16, bool)](<#func-toint16>) +- [func ToInt32[FromType numericType](value FromType) (int32, bool)](<#func-toint32>) +- [func ToInt64[FromType numericType](value FromType) (int64, bool)](<#func-toint64>) +- [func ToInt8[FromType numericType](value FromType) (int8, bool)](<#func-toint8>) +- [func ToUint[FromType numericType](value FromType) (uint, bool)](<#func-touint>) +- [func ToUint16[FromType numericType](value FromType) (uint16, bool)](<#func-touint16>) +- [func ToUint32[FromType numericType](value FromType) (uint32, bool)](<#func-touint32>) +- [func ToUint64[FromType numericType](value FromType) (uint64, bool)](<#func-touint64>) +- [func ToUint8[FromType numericType](value FromType) (uint8, bool)](<#func-touint8>) + + +## func [To]() + +```go +func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool) +``` + +To converts a numeric value from the FromType to the specified ToType type safely. result will always be same as the usual type cast \(type\(value\)\), but ok is false when overflow or underflow occured. + +
Example (Float Overflow) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" + "math" +) + +func main() { + n, ok := safecast.To[float32](math.MaxFloat32 * 2) + fmt.Print(n, ok) +} +``` + +#### Output + +``` ++Inf false +``` + +

+
+ +
Example (Int No Overflow) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + b, ok := safecast.To[byte](255) + fmt.Print(b, ok) +} +``` + +#### Output + +``` +255 true +``` + +

+
+ +
Example (Int Overflow) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + b, ok := safecast.To[byte](256) + fmt.Print(b, ok) +} +``` + +#### Output + +``` +0 false +``` + +

+
+ +
Example (Value In Range) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + n, ok := safecast.To[uint](1) + fmt.Print(n, ok) +} +``` + +#### Output + +``` +1 true +``` + +

+
+ +
Example (Value Out Of Range) +

+ +```go +package main + +import ( + "fmt" + "github.com/chen3feng/safecast" +) + +func main() { + n, ok := safecast.To[uint32](-1) + fmt.Print(n, ok) +} +``` + +#### Output + +``` +4294967295 false +``` + +

+
+ +## func [ToFloat32]() + +```go +func ToFloat32[FromType numericType](value FromType) (float32, bool) +``` + +ToFloat32 converts value to float32 type safely. result will always be same as the usual type cast\(float32\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToFloat64]() + +```go +func ToFloat64[FromType numericType](value FromType) (float64, bool) +``` + +ToFloat64 converts value to float64 type safely. result will always be same as the usual type cast\(float64\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt]() + +```go +func ToInt[FromType numericType](value FromType) (int, bool) +``` + +ToInt converts value to int type safely. result will always be same as the usual type cast\(int\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt16]() + +```go +func ToInt16[FromType numericType](value FromType) (int16, bool) +``` + +ToInt16 converts value to int16 type safely. result will always be same as the usual type cast\(int16\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt32]() + +```go +func ToInt32[FromType numericType](value FromType) (int32, bool) +``` + +ToInt32 converts value to int32 type safely. result will always be same as the usual type cast\(int32\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt64]() + +```go +func ToInt64[FromType numericType](value FromType) (int64, bool) +``` + +ToInt64 converts value to int64 type safely. result will always be same as the usual type cast\(int64\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToInt8]() + +```go +func ToInt8[FromType numericType](value FromType) (int8, bool) +``` + +ToInt8 converts value to int8 type safely. result will always be same as the usual type cast\(int8\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint]() + +```go +func ToUint[FromType numericType](value FromType) (uint, bool) +``` + +ToUint converts value to uint type safely. result will always be same as the usual type cast\(uint\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint16]() + +```go +func ToUint16[FromType numericType](value FromType) (uint16, bool) +``` + +ToUint16 converts value to uint16 type safely. result will always be same as the usual type cast\(uint16\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint32]() + +```go +func ToUint32[FromType numericType](value FromType) (uint32, bool) +``` + +ToUint32 converts value to uint32 type safely. result will always be same as the usual type cast\(uint32\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint64]() + +```go +func ToUint64[FromType numericType](value FromType) (uint64, bool) +``` + +ToUint64 converts value to uint64 type safely. result will always be same as the usual type cast\(uint64\(value\)\), but ok is false when overflow or underflow occured. + +## func [ToUint8]() + +```go +func ToUint8[FromType numericType](value FromType) (uint8, bool) +``` + +ToUint8 converts value to uint8 type safely. result will always be same as the usual type cast\(uint8\(value\)\), but ok is false when overflow or underflow occured. + + + +Generated by [gomarkdoc]() + + + diff --git a/vendor/github.com/chen3feng/safecast/casts.go b/vendor/github.com/chen3feng/safecast/casts.go new file mode 100644 index 000000000..dce81884a --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/casts.go @@ -0,0 +1,859 @@ +package safecast + +import "math" + +const intBits = 32 << (^uint(0) >> 63) + +// int8ToInt8 converts the int8 value to int8 safely. +func int8ToInt8(value int8) (result int8, ok bool) { + return int8(value), true +} + +// int8ToInt16 converts the int8 value to int16 safely. +func int8ToInt16(value int8) (result int16, ok bool) { + return int16(value), true +} + +// int8ToInt32 converts the int8 value to int32 safely. +func int8ToInt32(value int8) (result int32, ok bool) { + return int32(value), true +} + +// int8ToInt64 converts the int8 value to int64 safely. +func int8ToInt64(value int8) (result int64, ok bool) { + return int64(value), true +} + +// int8ToUint8 converts the int8 value to uint8 safely. +func int8ToUint8(value int8) (result uint8, ok bool) { + return uint8(value), value >= 0 +} + +// int8ToUint16 converts the int8 value to uint16 safely. +func int8ToUint16(value int8) (result uint16, ok bool) { + return uint16(value), value >= 0 +} + +// int8ToUint32 converts the int8 value to uint32 safely. +func int8ToUint32(value int8) (result uint32, ok bool) { + return uint32(value), value >= 0 +} + +// int8ToUint64 converts the int8 value to uint64 safely. +func int8ToUint64(value int8) (result uint64, ok bool) { + return uint64(value), value >= 0 +} + +// int16ToInt8 converts the int16 value to int8 safely. +func int16ToInt8(value int16) (result int8, ok bool) { + return int8(value), int16(int8(value)) == value +} + +// int16ToInt16 converts the int16 value to int16 safely. +func int16ToInt16(value int16) (result int16, ok bool) { + return int16(value), true +} + +// int16ToInt32 converts the int16 value to int32 safely. +func int16ToInt32(value int16) (result int32, ok bool) { + return int32(value), true +} + +// int16ToInt64 converts the int16 value to int64 safely. +func int16ToInt64(value int16) (result int64, ok bool) { + return int64(value), true +} + +// int16ToUint8 converts the int16 value to uint8 safely. +func int16ToUint8(value int16) (result uint8, ok bool) { + return uint8(value), value >= 0 && int16(uint8(value)) == value +} + +// int16ToUint16 converts the int16 value to uint16 safely. +func int16ToUint16(value int16) (result uint16, ok bool) { + return uint16(value), value >= 0 +} + +// int16ToUint32 converts the int16 value to uint32 safely. +func int16ToUint32(value int16) (result uint32, ok bool) { + return uint32(value), value >= 0 +} + +// int16ToUint64 converts the int16 value to uint64 safely. +func int16ToUint64(value int16) (result uint64, ok bool) { + return uint64(value), value >= 0 +} + +// int32ToInt8 converts the int32 value to int8 safely. +func int32ToInt8(value int32) (result int8, ok bool) { + return int8(value), int32(int8(value)) == value +} + +// int32ToInt16 converts the int32 value to int16 safely. +func int32ToInt16(value int32) (result int16, ok bool) { + return int16(value), int32(int16(value)) == value +} + +// int32ToInt32 converts the int32 value to int32 safely. +func int32ToInt32(value int32) (result int32, ok bool) { + return int32(value), true +} + +// int32ToInt64 converts the int32 value to int64 safely. +func int32ToInt64(value int32) (result int64, ok bool) { + return int64(value), true +} + +// int32ToUint8 converts the int32 value to uint8 safely. +func int32ToUint8(value int32) (result uint8, ok bool) { + return uint8(value), value >= 0 && int32(uint8(value)) == value +} + +// int32ToUint16 converts the int32 value to uint16 safely. +func int32ToUint16(value int32) (result uint16, ok bool) { + return uint16(value), value >= 0 && int32(uint16(value)) == value +} + +// int32ToUint32 converts the int32 value to uint32 safely. +func int32ToUint32(value int32) (result uint32, ok bool) { + return uint32(value), value >= 0 +} + +// int32ToUint64 converts the int32 value to uint64 safely. +func int32ToUint64(value int32) (result uint64, ok bool) { + return uint64(value), value >= 0 +} + +// int64ToInt8 converts the int64 value to int8 safely. +func int64ToInt8(value int64) (result int8, ok bool) { + return int8(value), int64(int8(value)) == value +} + +// int64ToInt16 converts the int64 value to int16 safely. +func int64ToInt16(value int64) (result int16, ok bool) { + return int16(value), int64(int16(value)) == value +} + +// int64ToInt32 converts the int64 value to int32 safely. +func int64ToInt32(value int64) (result int32, ok bool) { + return int32(value), int64(int32(value)) == value +} + +// int64ToInt64 converts the int64 value to int64 safely. +func int64ToInt64(value int64) (result int64, ok bool) { + return int64(value), true +} + +// int64ToUint8 converts the int64 value to uint8 safely. +func int64ToUint8(value int64) (result uint8, ok bool) { + return uint8(value), value >= 0 && int64(uint8(value)) == value +} + +// int64ToUint16 converts the int64 value to uint16 safely. +func int64ToUint16(value int64) (result uint16, ok bool) { + return uint16(value), value >= 0 && int64(uint16(value)) == value +} + +// int64ToUint32 converts the int64 value to uint32 safely. +func int64ToUint32(value int64) (result uint32, ok bool) { + return uint32(value), value >= 0 && int64(uint32(value)) == value +} + +// int64ToUint64 converts the int64 value to uint64 safely. +func int64ToUint64(value int64) (result uint64, ok bool) { + return uint64(value), value >= 0 +} + +// uint8ToInt8 converts the uint8 value to int8 safely. +func uint8ToInt8(value uint8) (result int8, ok bool) { + return int8(value), value <= math.MaxInt8 +} + +// uint8ToInt16 converts the uint8 value to int16 safely. +func uint8ToInt16(value uint8) (result int16, ok bool) { + return int16(value), true +} + +// uint8ToInt32 converts the uint8 value to int32 safely. +func uint8ToInt32(value uint8) (result int32, ok bool) { + return int32(value), true +} + +// uint8ToInt64 converts the uint8 value to int64 safely. +func uint8ToInt64(value uint8) (result int64, ok bool) { + return int64(value), true +} + +// uint8ToUint8 converts the uint8 value to uint8 safely. +func uint8ToUint8(value uint8) (result uint8, ok bool) { + return uint8(value), true +} + +// uint8ToUint16 converts the uint8 value to uint16 safely. +func uint8ToUint16(value uint8) (result uint16, ok bool) { + return uint16(value), true +} + +// uint8ToUint32 converts the uint8 value to uint32 safely. +func uint8ToUint32(value uint8) (result uint32, ok bool) { + return uint32(value), true +} + +// uint8ToUint64 converts the uint8 value to uint64 safely. +func uint8ToUint64(value uint8) (result uint64, ok bool) { + return uint64(value), true +} + +// uint16ToInt8 converts the uint16 value to int8 safely. +func uint16ToInt8(value uint16) (result int8, ok bool) { + return int8(value), uint16(int8(value)) == value +} + +// uint16ToInt16 converts the uint16 value to int16 safely. +func uint16ToInt16(value uint16) (result int16, ok bool) { + return int16(value), value <= math.MaxInt16 +} + +// uint16ToInt32 converts the uint16 value to int32 safely. +func uint16ToInt32(value uint16) (result int32, ok bool) { + return int32(value), true +} + +// uint16ToInt64 converts the uint16 value to int64 safely. +func uint16ToInt64(value uint16) (result int64, ok bool) { + return int64(value), true +} + +// uint16ToUint8 converts the uint16 value to uint8 safely. +func uint16ToUint8(value uint16) (result uint8, ok bool) { + return uint8(value), uint16(uint8(value)) == value +} + +// uint16ToUint16 converts the uint16 value to uint16 safely. +func uint16ToUint16(value uint16) (result uint16, ok bool) { + return uint16(value), true +} + +// uint16ToUint32 converts the uint16 value to uint32 safely. +func uint16ToUint32(value uint16) (result uint32, ok bool) { + return uint32(value), true +} + +// uint16ToUint64 converts the uint16 value to uint64 safely. +func uint16ToUint64(value uint16) (result uint64, ok bool) { + return uint64(value), true +} + +// uint32ToInt8 converts the uint32 value to int8 safely. +func uint32ToInt8(value uint32) (result int8, ok bool) { + return int8(value), uint32(int8(value)) == value +} + +// uint32ToInt16 converts the uint32 value to int16 safely. +func uint32ToInt16(value uint32) (result int16, ok bool) { + return int16(value), uint32(int16(value)) == value +} + +// uint32ToInt32 converts the uint32 value to int32 safely. +func uint32ToInt32(value uint32) (result int32, ok bool) { + return int32(value), value <= math.MaxInt32 +} + +// uint32ToInt64 converts the uint32 value to int64 safely. +func uint32ToInt64(value uint32) (result int64, ok bool) { + return int64(value), true +} + +// uint32ToUint8 converts the uint32 value to uint8 safely. +func uint32ToUint8(value uint32) (result uint8, ok bool) { + return uint8(value), uint32(uint8(value)) == value +} + +// uint32ToUint16 converts the uint32 value to uint16 safely. +func uint32ToUint16(value uint32) (result uint16, ok bool) { + return uint16(value), uint32(uint16(value)) == value +} + +// uint32ToUint32 converts the uint32 value to uint32 safely. +func uint32ToUint32(value uint32) (result uint32, ok bool) { + return uint32(value), true +} + +// uint32ToUint64 converts the uint32 value to uint64 safely. +func uint32ToUint64(value uint32) (result uint64, ok bool) { + return uint64(value), true +} + +// uint64ToInt8 converts the uint64 value to int8 safely. +func uint64ToInt8(value uint64) (result int8, ok bool) { + return int8(value), uint64(int8(value)) == value +} + +// uint64ToInt16 converts the uint64 value to int16 safely. +func uint64ToInt16(value uint64) (result int16, ok bool) { + return int16(value), uint64(int16(value)) == value +} + +// uint64ToInt32 converts the uint64 value to int32 safely. +func uint64ToInt32(value uint64) (result int32, ok bool) { + return int32(value), uint64(int32(value)) == value +} + +// uint64ToInt64 converts the uint64 value to int64 safely. +func uint64ToInt64(value uint64) (result int64, ok bool) { + return int64(value), value <= math.MaxInt64 +} + +// uint64ToUint8 converts the uint64 value to uint8 safely. +func uint64ToUint8(value uint64) (result uint8, ok bool) { + return uint8(value), uint64(uint8(value)) == value +} + +// uint64ToUint16 converts the uint64 value to uint16 safely. +func uint64ToUint16(value uint64) (result uint16, ok bool) { + return uint16(value), uint64(uint16(value)) == value +} + +// uint64ToUint32 converts the uint64 value to uint32 safely. +func uint64ToUint32(value uint64) (result uint32, ok bool) { + return uint32(value), uint64(uint32(value)) == value +} + +// uint64ToUint64 converts the uint64 value to uint64 safely. +func uint64ToUint64(value uint64) (result uint64, ok bool) { + return uint64(value), true +} + +// int8ToInt converts the int8 value to int safely. +func int8ToInt(value int8) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = int8ToInt32(value) + result = int(r) + } + var r int64 + r, ok = int8ToInt64(value) + result = int(r) + return +} + +// int8ToUint converts the int8 value to uint safely. +func int8ToUint(value int8) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = int8ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = int8ToUint64(value) + result = uint(r) + return +} + +// int16ToInt converts the int16 value to int safely. +func int16ToInt(value int16) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = int16ToInt32(value) + result = int(r) + } + var r int64 + r, ok = int16ToInt64(value) + result = int(r) + return +} + +// int16ToUint converts the int16 value to uint safely. +func int16ToUint(value int16) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = int16ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = int16ToUint64(value) + result = uint(r) + return +} + +// int32ToInt converts the int32 value to int safely. +func int32ToInt(value int32) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = int32ToInt32(value) + result = int(r) + } + var r int64 + r, ok = int32ToInt64(value) + result = int(r) + return +} + +// int32ToUint converts the int32 value to uint safely. +func int32ToUint(value int32) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = int32ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = int32ToUint64(value) + result = uint(r) + return +} + +// int64ToInt converts the int64 value to int safely. +func int64ToInt(value int64) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = int64ToInt32(value) + result = int(r) + } + var r int64 + r, ok = int64ToInt64(value) + result = int(r) + return +} + +// int64ToUint converts the int64 value to uint safely. +func int64ToUint(value int64) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = int64ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = int64ToUint64(value) + result = uint(r) + return +} + +// uint8ToInt converts the uint8 value to int safely. +func uint8ToInt(value uint8) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = uint8ToInt32(value) + result = int(r) + } + var r int64 + r, ok = uint8ToInt64(value) + result = int(r) + return +} + +// uint8ToUint converts the uint8 value to uint safely. +func uint8ToUint(value uint8) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = uint8ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = uint8ToUint64(value) + result = uint(r) + return +} + +// uint16ToInt converts the uint16 value to int safely. +func uint16ToInt(value uint16) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = uint16ToInt32(value) + result = int(r) + } + var r int64 + r, ok = uint16ToInt64(value) + result = int(r) + return +} + +// uint16ToUint converts the uint16 value to uint safely. +func uint16ToUint(value uint16) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = uint16ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = uint16ToUint64(value) + result = uint(r) + return +} + +// uint32ToInt converts the uint32 value to int safely. +func uint32ToInt(value uint32) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = uint32ToInt32(value) + result = int(r) + } + var r int64 + r, ok = uint32ToInt64(value) + result = int(r) + return +} + +// uint32ToUint converts the uint32 value to uint safely. +func uint32ToUint(value uint32) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = uint32ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = uint32ToUint64(value) + result = uint(r) + return +} + +// uint64ToInt converts the uint64 value to int safely. +func uint64ToInt(value uint64) (result int, ok bool) { + if intBits == 32 { + var r int32 + r, ok = uint64ToInt32(value) + result = int(r) + } + var r int64 + r, ok = uint64ToInt64(value) + result = int(r) + return +} + +// uint64ToUint converts the uint64 value to uint safely. +func uint64ToUint(value uint64) (result uint, ok bool) { + if intBits == 32 { + var r uint32 + r, ok = uint64ToUint32(value) + result = uint(r) + } + var r uint64 + r, ok = uint64ToUint64(value) + result = uint(r) + return +} + +// intToInt8 converts the int value to int8 safely. +func intToInt8(value int) (result int8, ok bool) { + if intBits == 32 { + return int32ToInt8(int32(value)) + } + return int64ToInt8(int64(value)) +} + +// intToInt16 converts the int value to int16 safely. +func intToInt16(value int) (result int16, ok bool) { + if intBits == 32 { + return int32ToInt16(int32(value)) + } + return int64ToInt16(int64(value)) +} + +// intToInt32 converts the int value to int32 safely. +func intToInt32(value int) (result int32, ok bool) { + if intBits == 32 { + return int32ToInt32(int32(value)) + } + return int64ToInt32(int64(value)) +} + +// intToInt64 converts the int value to int64 safely. +func intToInt64(value int) (result int64, ok bool) { + if intBits == 32 { + return int32ToInt64(int32(value)) + } + return int64ToInt64(int64(value)) +} + +// intToUint8 converts the int value to uint8 safely. +func intToUint8(value int) (result uint8, ok bool) { + if intBits == 32 { + return int32ToUint8(int32(value)) + } + return int64ToUint8(int64(value)) +} + +// intToUint16 converts the int value to uint16 safely. +func intToUint16(value int) (result uint16, ok bool) { + if intBits == 32 { + return int32ToUint16(int32(value)) + } + return int64ToUint16(int64(value)) +} + +// intToUint32 converts the int value to uint32 safely. +func intToUint32(value int) (result uint32, ok bool) { + if intBits == 32 { + return int32ToUint32(int32(value)) + } + return int64ToUint32(int64(value)) +} + +// intToUint64 converts the int value to uint64 safely. +func intToUint64(value int) (result uint64, ok bool) { + if intBits == 32 { + return int32ToUint64(int32(value)) + } + return int64ToUint64(int64(value)) +} + +// uintToInt8 converts the uint value to int8 safely. +func uintToInt8(value uint) (result int8, ok bool) { + if intBits == 32 { + return uint32ToInt8(uint32(value)) + } + return uint64ToInt8(uint64(value)) +} + +// uintToInt16 converts the uint value to int16 safely. +func uintToInt16(value uint) (result int16, ok bool) { + if intBits == 32 { + return uint32ToInt16(uint32(value)) + } + return uint64ToInt16(uint64(value)) +} + +// uintToInt32 converts the uint value to int32 safely. +func uintToInt32(value uint) (result int32, ok bool) { + if intBits == 32 { + return uint32ToInt32(uint32(value)) + } + return uint64ToInt32(uint64(value)) +} + +// uintToInt64 converts the uint value to int64 safely. +func uintToInt64(value uint) (result int64, ok bool) { + if intBits == 32 { + return uint32ToInt64(uint32(value)) + } + return uint64ToInt64(uint64(value)) +} + +// uintToUint8 converts the uint value to uint8 safely. +func uintToUint8(value uint) (result uint8, ok bool) { + if intBits == 32 { + return uint32ToUint8(uint32(value)) + } + return uint64ToUint8(uint64(value)) +} + +// uintToUint16 converts the uint value to uint16 safely. +func uintToUint16(value uint) (result uint16, ok bool) { + if intBits == 32 { + return uint32ToUint16(uint32(value)) + } + return uint64ToUint16(uint64(value)) +} + +// uintToUint32 converts the uint value to uint32 safely. +func uintToUint32(value uint) (result uint32, ok bool) { + if intBits == 32 { + return uint32ToUint32(uint32(value)) + } + return uint64ToUint32(uint64(value)) +} + +// uintToUint64 converts the uint value to uint64 safely. +func uintToUint64(value uint) (result uint64, ok bool) { + if intBits == 32 { + return uint32ToUint64(uint32(value)) + } + return uint64ToUint64(uint64(value)) +} + +// intToUint converts the int value to uint safely. +func intToUint(value int) (result uint, ok bool) { + return uint(value), value >= 0 +} + +// uintToInt converts the uint value to int safely. +func uintToInt(value uint) (result int, ok bool) { + return int(value), value <= math.MaxInt +} + +// float32ToInt8 converts the float32 value to int8 safely. +func float32ToInt8(value float32) (result int8, ok bool) { + return int8(value), value >= math.MinInt8 && value <= math.MaxInt8 +} + +func int8ToFloat32(value int8) (float32, bool) { + return float32(value), true +} + +// float32ToInt16 converts the float32 value to int16 safely. +func float32ToInt16(value float32) (result int16, ok bool) { + return int16(value), value >= math.MinInt16 && value <= math.MaxInt16 +} + +func int16ToFloat32(value int16) (float32, bool) { + return float32(value), true +} + +// float32ToInt32 converts the float32 value to int32 safely. +func float32ToInt32(value float32) (result int32, ok bool) { + return int32(value), value >= math.MinInt32 && value <= math.MaxInt32 +} + +func int32ToFloat32(value int32) (float32, bool) { + return float32(value), true +} + +// float32ToInt64 converts the float32 value to int64 safely. +func float32ToInt64(value float32) (result int64, ok bool) { + return int64(value), value >= math.MinInt64 && value <= math.MaxInt64 +} + +func int64ToFloat32(value int64) (float32, bool) { + return float32(value), true +} + +// float32ToInt converts the float32 value to int safely. +func float32ToInt(value float32) (result int, ok bool) { + return int(value), value >= math.MinInt && value <= math.MaxInt +} + +func intToFloat32(value int) (float32, bool) { + return float32(value), true +} + +// float32ToUint8 converts the float32 value to uint8 safely. +func float32ToUint8(value float32) (result uint8, ok bool) { + return uint8(value), value >= 0 && value <= math.MaxUint8 +} + +func uint8ToFloat32(value uint8) (float32, bool) { + return float32(value), true +} + +// float32ToUint16 converts the float32 value to uint16 safely. +func float32ToUint16(value float32) (result uint16, ok bool) { + return uint16(value), value >= 0 && value <= math.MaxUint16 +} + +func uint16ToFloat32(value uint16) (float32, bool) { + return float32(value), true +} + +// float32ToUint32 converts the float32 value to uint32 safely. +func float32ToUint32(value float32) (result uint32, ok bool) { + return uint32(value), value >= 0 && value <= math.MaxUint32 +} + +func uint32ToFloat32(value uint32) (float32, bool) { + return float32(value), true +} + +// float32ToUint64 converts the float32 value to uint64 safely. +func float32ToUint64(value float32) (result uint64, ok bool) { + return uint64(value), value >= 0 && value <= math.MaxUint64 +} + +func uint64ToFloat32(value uint64) (float32, bool) { + return float32(value), true +} + +// float32ToUint converts the float32 value to uint safely. +func float32ToUint(value float32) (result uint, ok bool) { + return uint(value), value >= 0 && value <= math.MaxUint +} + +func uintToFloat32(value uint) (float32, bool) { + return float32(value), true +} + +// float64ToInt8 converts the float64 value to int8 safely. +func float64ToInt8(value float64) (result int8, ok bool) { + return int8(value), value >= math.MinInt8 && value <= math.MaxInt8 +} + +func int8ToFloat64(value int8) (float64, bool) { + return float64(value), true +} + +// float64ToInt16 converts the float64 value to int16 safely. +func float64ToInt16(value float64) (result int16, ok bool) { + return int16(value), value >= math.MinInt16 && value <= math.MaxInt16 +} + +func int16ToFloat64(value int16) (float64, bool) { + return float64(value), true +} + +// float64ToInt32 converts the float64 value to int32 safely. +func float64ToInt32(value float64) (result int32, ok bool) { + return int32(value), value >= math.MinInt32 && value <= math.MaxInt32 +} + +func int32ToFloat64(value int32) (float64, bool) { + return float64(value), true +} + +// float64ToInt64 converts the float64 value to int64 safely. +func float64ToInt64(value float64) (result int64, ok bool) { + return int64(value), value >= math.MinInt64 && value <= math.MaxInt64 +} + +func int64ToFloat64(value int64) (float64, bool) { + return float64(value), true +} + +// float64ToInt converts the float64 value to int safely. +func float64ToInt(value float64) (result int, ok bool) { + return int(value), value >= math.MinInt && value <= math.MaxInt +} + +func intToFloat64(value int) (float64, bool) { + return float64(value), true +} + +// float64ToUint8 converts the float64 value to uint8 safely. +func float64ToUint8(value float64) (result uint8, ok bool) { + return uint8(value), value >= 0 && value <= math.MaxUint8 +} + +func uint8ToFloat64(value uint8) (float64, bool) { + return float64(value), true +} + +// float64ToUint16 converts the float64 value to uint16 safely. +func float64ToUint16(value float64) (result uint16, ok bool) { + return uint16(value), value >= 0 && value <= math.MaxUint16 +} + +func uint16ToFloat64(value uint16) (float64, bool) { + return float64(value), true +} + +// float64ToUint32 converts the float64 value to uint32 safely. +func float64ToUint32(value float64) (result uint32, ok bool) { + return uint32(value), value >= 0 && value <= math.MaxUint32 +} + +func uint32ToFloat64(value uint32) (float64, bool) { + return float64(value), true +} + +// float64ToUint64 converts the float64 value to uint64 safely. +func float64ToUint64(value float64) (result uint64, ok bool) { + return uint64(value), value >= 0 && value <= math.MaxUint64 +} + +func uint64ToFloat64(value uint64) (float64, bool) { + return float64(value), true +} + +// float64ToUint converts the float64 value to uint safely. +func float64ToUint(value float64) (result uint, ok bool) { + return uint(value), value >= 0 && value <= math.MaxUint +} + +func uintToFloat64(value uint) (float64, bool) { + return float64(value), true +} + +func float32ToFloat64(value float32) (float64, bool) { + return float64(value), true +} + +func float64ToFloat32(value float64) (float32, bool) { + return float32(value), value >= -math.MaxFloat32 && value <= math.MaxFloat32 +} diff --git a/vendor/github.com/chen3feng/safecast/check.sh b/vendor/github.com/chen3feng/safecast/check.sh new file mode 100644 index 000000000..211b409a3 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/check.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +go build + +go test ./... -coverprofile=coverage.out + +golint + +gosec . + +go tool cover -html=coverage.out diff --git a/vendor/github.com/chen3feng/safecast/generate.py b/vendor/github.com/chen3feng/safecast/generate.py new file mode 100644 index 000000000..f1728e4e3 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/generate.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 + +from safecast import ALL_INT_BITS, ALL_FIXED_INT_BITS, to_camel_case + + +def generate_header(): + print('package safecast\n') + print('import "math"\n') + print('const intBits = 32 << (^uint(0) >> 63)\n') + + +def generate_fixed_int(from_type, from_bits, to_type, to_bits): + full_from_type = f'{from_type}{from_bits}' + full_to_type = f'{to_type}{to_bits}' + + no_overflow = f'{full_from_type}({full_to_type}(value)) == value' + + def same_signedness(): + if int(from_bits) <= int(to_bits): + return + return no_overflow + + def uint_to_int(): + if int(from_bits) < int(to_bits): + return + if int(from_bits) > int(to_bits): + return no_overflow + else: + return f'value <= math.Max{to_camel_case(full_to_type)}' + + def int_to_uint(): + cond = 'value >= 0' + if from_bits and to_bits: # intnn to uintxx + if int(from_bits) <= int(to_bits): + return cond + return f'{cond} && {no_overflow}' + + def range_chack(): + if from_type == to_type: + return same_signedness() + elif from_type == 'uint': + return uint_to_int() + else: # int to uint + return int_to_uint() + + print_function_header(full_from_type, full_to_type) + ok = range_chack() + print_function_footer(full_to_type, ok) + + +def print_function_header(full_from_type, full_to_type): + func_name = f'{full_from_type}To{to_camel_case(full_to_type)}' + print(f'// {func_name} converts the {full_from_type} value to {full_to_type} safely.') + print(f'func {func_name}(value {full_from_type}) (result {full_to_type}, ok bool) {{') + + +def print_function_footer(full_to_type, ok): + if not ok: + ok = 'true' + print(f'\treturn {full_to_type}(value), {ok}') + print('}\n') + + +def generate_int_convs(): + """Generate int conversion functions.""" + for from_type in ('int', 'uint'): + for from_bits in ALL_FIXED_INT_BITS: + for to_type in ('int', 'uint'): + for to_bits in ALL_FIXED_INT_BITS: + generate_fixed_int(from_type, from_bits, to_type, to_bits) + for from_type in ('int', 'uint'): + for from_bits in ALL_FIXED_INT_BITS: + for to_type in ('int', 'uint'): + generate_fixed_int_to_int(from_type, from_bits, to_type) + for from_type in ('int', 'uint'): + for to_type in ('int', 'uint'): + for to_bit in ALL_FIXED_INT_BITS: + generate_int_to_fixed_int(from_type, to_type, to_bit) + + print_function_header('int', 'uint') + print(''' return uint(value), value >= 0''') + print('}') + + print_function_header('uint', 'int') + print(''' return int(value), value <= math.MaxInt''') + print('}') + + +def generate_fixed_int_to_int(from_type, from_bits, to_type): + full_from_type = f'{from_type}{from_bits}' + print_function_header(full_from_type, to_type) + print(f'''\ + if intBits == 32 {{ + var r {to_type}32 + r, ok = {full_from_type}To{to_camel_case(to_type)}32(value) + result = {to_type}(r) + }} + var r {to_type}64 + r, ok = {full_from_type}To{to_camel_case(to_type)}64(value) + result = {to_type}(r) + return''') + print('}') + + +def generate_int_to_fixed_int(from_type, to_type, to_bits): + full_to_type = f'{to_type}{to_bits}' + print_function_header(from_type, full_to_type) + print(f'''\ + if intBits == 32 {{ + return {from_type}32To{to_camel_case(full_to_type)}({from_type}32(value)) + }} + return {from_type}64To{to_camel_case(full_to_type)}({from_type}64(value))''') + print('}') + + +def generate_float_convs(): + """Generate int conversion functions.""" + for float_bit in (32, 64): + for int_type in ('int', 'uint'): + for int_bits in ALL_INT_BITS: + generate_float_to_conv(float_bit, int_type, int_bits) + generate_to_float_conv(int_type, int_bits, float_bit) + + print(""" + func float32ToFloat64(value float32) (float64, bool) { + return float64(value), true + } + + func float64ToFloat32(value float64) (float32, bool) { + return float32(value), value >= -math.MaxFloat32 && value <= math.MaxFloat32 + } + """) + + +def generate_float_to_conv(from_bits, to_type, to_bits): + full_from_type = f'float{from_bits}' + full_to_type = f'{to_type}{to_bits}' + + def range_chack(): + min = f'math.Min{to_camel_case(full_to_type)}' if to_type == 'int' else '0' + cond = f'value >= {min}' + cond += f' && value <= math.Max{to_camel_case(full_to_type)}' + return cond + + func_name = f'float{from_bits}To{to_camel_case(to_type)}{to_bits}' + print(f'// {func_name} converts the {full_from_type} value to {full_to_type} safely.') + print(f'func {func_name}(value {full_from_type}) (result {full_to_type}, ok bool) {{') + ok = range_chack() or 'true' + print(f'\treturn {full_to_type}(value), {ok}') + print('}\n') + + +def generate_to_float_conv(from_type, from_bits, to_bits): + full_from_type = f'{from_type}{from_bits}' + full_to_type = f'float{to_bits}' + print(f'func {full_from_type}To{to_camel_case(full_to_type)}(value {full_from_type}) ({full_to_type}, bool) {{') + print(f'\treturn {full_to_type}(value), true') + print('}') + + +def main(): + generate_header() + generate_int_convs() + generate_float_convs() + + +main() diff --git a/vendor/github.com/chen3feng/safecast/generate_generic.py b/vendor/github.com/chen3feng/safecast/generate_generic.py new file mode 100644 index 000000000..0bb437ccf --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/generate_generic.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +"""Generate the generic 'To' function.""" + +from safecast import ALL_INT_BITS, to_camel_case + + +def generate_header(): + """Generate file header.""" + print('package safecast\n') + print(""" +type numericType interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | + ~float32 | ~float64 +}""" + ) + + +def generate_to_function(): + """Generate int conversion functions.""" + + print(""" +// To converts a numeric value from the FromType to the specified ToType type safely. +// result will always be same as the usual type cast (type(value)), +// but ok is false when overflow or underflow occured. +func To[ToType numericType, FromType numericType](value FromType)(result ToType, ok bool) { + ok = true + switch t := any(result).(type) {""") + for to_type in ('int', 'uint'): + for to_bits in ALL_INT_BITS: + print(f'\tcase {to_type}{to_bits}:') + print(f'\t\tt, ok = To{to_camel_case(to_type)}{to_bits}(value)') + print(f'\t\tresult = ToType(t)') + for to_bits in (32, 64): + print(f'\tcase float{to_bits}:') + print(f'\t\tt, ok = ToFloat{to_bits}(value)') + print(f'\t\tresult = ToType(t)') + + print('\t}\n\treturn result, ok') + print('}\n') + + for to_type in ('int', 'uint'): + for to_bits in ALL_INT_BITS: + generate_to_type(to_type, to_bits) + for to_bits in (32, 64): + generate_to_type('float', to_bits) + + +def generate_to_type(to_type, to_bits): + full_to_type = f'{to_type}{to_bits}' + funcname = f'To{to_camel_case(to_type)}{to_bits}' + print(f'// {funcname} converts value to {full_to_type} type safely.\n' + f'// result will always be same as the usual type cast({full_to_type}(value)),\n' + f'// but ok is false when overflow or underflow occured.' + ) + print( + f'func {funcname}[FromType numericType](value FromType) ({full_to_type}, bool) {{') + print('\tvar zero FromType // Use zero to any for type switch to avoid malloc') + print(f'\tswitch any(zero).(type) {{') + for from_type in ('int', 'uint'): + for from_bits in ALL_INT_BITS: + full_from_type = f'{from_type}{from_bits}' + generate_call(full_from_type, full_to_type) + + for from_bits in (32, 64): + full_from_type = f'float{from_bits}' + generate_call(full_from_type, full_to_type) + print(f'\t}}\n\treturn {full_to_type}(value), false') + print('}\n') + + +def generate_call(full_from_type, full_to_type): + if full_from_type == full_to_type: + print(f'\tcase {full_from_type}: return {full_to_type}(value), true') + else: + print( + f'\tcase {full_from_type}: return {full_from_type}To{to_camel_case(full_to_type)}({full_from_type}(value))') + + +def main(): + generate_header() + generate_to_function() + + +main() diff --git a/vendor/github.com/chen3feng/safecast/generate_test.py b/vendor/github.com/chen3feng/safecast/generate_test.py new file mode 100644 index 000000000..9979db3a1 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/generate_test.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 + + +from safecast import ALL_INT_BITS, to_camel_case + + +print(''' +package safecast_test + +import ( + _ "fmt" + "math" + "testing" + + "github.com/chen3feng/safecast" +) + +func expectFalse(t * testing.T, value bool) { + if value { + t.Helper() + t.Errorf("Expect false, got true") + } +} + +func expectTrue(t * testing.T, value bool) { + if !value { + t.Helper() + t.Errorf("Expect true, got false") + } +} +''') + +for from_bit in ALL_INT_BITS: + for to_bit in ALL_INT_BITS: + to_type = f'uint{to_bit}' + from_type = f'int{from_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = -1 + _, ok := safecast.To[{to_type}](i) + expectFalse(t, ok) + i = 1 + _, ok = safecast.To[{to_type}](i) + expectTrue(t, ok) + }}''') + +for from_bit in ALL_INT_BITS: + for to_bit in ALL_INT_BITS: + from_type = f'int{from_bit}' + to_type = f'int{to_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = -1 + _, ok := safecast.To[{to_type}](i) + expectTrue(t, ok) + i = 1 + _, ok = safecast.To[{to_type}](i) + expectTrue(t, ok) + }}''') + +for from_bit in ALL_INT_BITS: + for to_bit in ALL_INT_BITS: + from_type = f'uint{from_bit}' + to_type = f'int{to_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = 1 + _, ok := safecast.To[{to_type}](i) + expectTrue(t, ok)''') + if from_bit and to_bit and int(from_bit) >= int(to_bit): + print(f''' + i = math.MaxInt{to_bit} + 1 + _, ok = safecast.To[{to_type}](i) + expectFalse(t, ok)''') + print('}') + +for from_bit in ALL_INT_BITS: + for to_bit in ALL_INT_BITS: + from_type = f'uint{from_bit}' + to_type = f'uint{to_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = 1 + _, ok := safecast.To[{to_type}](i) + expectTrue(t, ok)''') + if from_bit and to_bit and int(from_bit) > int(to_bit) and int(to_bit) < 64: + print(f''' + i = math.MaxUint{to_bit} + 1 + _, ok = safecast.To[{to_type}](i) + expectFalse(t, ok)''') + print('}') + +for from_bit in ALL_INT_BITS: + for to_bit in (32, 64): + to_type = f'float{to_bit}' + for int_type in ('int', 'uint'): + from_type = f'{int_type}{from_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = 1 + _, ok := safecast.To[{to_type}](i) + expectTrue(t, ok)''') + print('}') + +for from_bit in (32, 64): + for to_bit in ALL_INT_BITS: + from_type = f'float{from_bit}' + to_type = f'int{to_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = 1 + _, ok := safecast.To[{to_type}](i) + expectTrue(t, ok) + i = math.Max{to_camel_case(to_type)} + i *= 2 + _, ok = safecast.To[{to_type}](i) + expectFalse(t, ok)''') + print('}') + +for from_bit in (32, 64): + for to_bit in ALL_INT_BITS: + from_type = f'float{from_bit}' + to_type = f'uint{to_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = -1 + _, ok := safecast.To[{to_type}](i) + expectFalse(t, ok) + i = math.Max{to_camel_case(to_type)} + i *= 2 + _, ok = safecast.To[{to_type}](i) + expectFalse(t, ok) + i = 1 + _, ok = safecast.To[{to_type}](i) + expectTrue(t, ok)''') + print('}') + +for from_bit in (32, 64): + for to_bit in (32, 64): + from_type = f'float{from_bit}' + to_type = f'float{to_bit}' + print(f''' + func TestTo_{from_type}_to_{to_type}(t * testing.T) {{ + var i {from_type} = -1 + _, ok := safecast.To[{to_type}](i) + expectTrue(t, ok) + i = 1 + _, ok = safecast.To[{to_type}](i) + expectTrue(t, ok)''') + print('}') diff --git a/vendor/github.com/chen3feng/safecast/generics.go b/vendor/github.com/chen3feng/safecast/generics.go new file mode 100644 index 000000000..04bb53823 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/generics.go @@ -0,0 +1,461 @@ +package safecast + +type numericType interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | + ~float32 | ~float64 +} + +// To converts a numeric value from the FromType to the specified ToType type safely. +// result will always be same as the usual type cast (type(value)), +// but ok is false when overflow or underflow occured. +func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool) { + ok = true + switch t := any(result).(type) { + case int8: + t, ok = ToInt8(value) + result = ToType(t) + case int16: + t, ok = ToInt16(value) + result = ToType(t) + case int32: + t, ok = ToInt32(value) + result = ToType(t) + case int64: + t, ok = ToInt64(value) + result = ToType(t) + case int: + t, ok = ToInt(value) + result = ToType(t) + case uint8: + t, ok = ToUint8(value) + result = ToType(t) + case uint16: + t, ok = ToUint16(value) + result = ToType(t) + case uint32: + t, ok = ToUint32(value) + result = ToType(t) + case uint64: + t, ok = ToUint64(value) + result = ToType(t) + case uint: + t, ok = ToUint(value) + result = ToType(t) + case float32: + t, ok = ToFloat32(value) + result = ToType(t) + case float64: + t, ok = ToFloat64(value) + result = ToType(t) + } + return result, ok +} + +// ToInt8 converts value to int8 type safely. +// result will always be same as the usual type cast(int8(value)), +// but ok is false when overflow or underflow occured. +func ToInt8[FromType numericType](value FromType) (int8, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8(value), true + case int16: + return int16ToInt8(int16(value)) + case int32: + return int32ToInt8(int32(value)) + case int64: + return int64ToInt8(int64(value)) + case int: + return intToInt8(int(value)) + case uint8: + return uint8ToInt8(uint8(value)) + case uint16: + return uint16ToInt8(uint16(value)) + case uint32: + return uint32ToInt8(uint32(value)) + case uint64: + return uint64ToInt8(uint64(value)) + case uint: + return uintToInt8(uint(value)) + case float32: + return float32ToInt8(float32(value)) + case float64: + return float64ToInt8(float64(value)) + } + return int8(value), false +} + +// ToInt16 converts value to int16 type safely. +// result will always be same as the usual type cast(int16(value)), +// but ok is false when overflow or underflow occured. +func ToInt16[FromType numericType](value FromType) (int16, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToInt16(int8(value)) + case int16: + return int16(value), true + case int32: + return int32ToInt16(int32(value)) + case int64: + return int64ToInt16(int64(value)) + case int: + return intToInt16(int(value)) + case uint8: + return uint8ToInt16(uint8(value)) + case uint16: + return uint16ToInt16(uint16(value)) + case uint32: + return uint32ToInt16(uint32(value)) + case uint64: + return uint64ToInt16(uint64(value)) + case uint: + return uintToInt16(uint(value)) + case float32: + return float32ToInt16(float32(value)) + case float64: + return float64ToInt16(float64(value)) + } + return int16(value), false +} + +// ToInt32 converts value to int32 type safely. +// result will always be same as the usual type cast(int32(value)), +// but ok is false when overflow or underflow occured. +func ToInt32[FromType numericType](value FromType) (int32, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToInt32(int8(value)) + case int16: + return int16ToInt32(int16(value)) + case int32: + return int32(value), true + case int64: + return int64ToInt32(int64(value)) + case int: + return intToInt32(int(value)) + case uint8: + return uint8ToInt32(uint8(value)) + case uint16: + return uint16ToInt32(uint16(value)) + case uint32: + return uint32ToInt32(uint32(value)) + case uint64: + return uint64ToInt32(uint64(value)) + case uint: + return uintToInt32(uint(value)) + case float32: + return float32ToInt32(float32(value)) + case float64: + return float64ToInt32(float64(value)) + } + return int32(value), false +} + +// ToInt64 converts value to int64 type safely. +// result will always be same as the usual type cast(int64(value)), +// but ok is false when overflow or underflow occured. +func ToInt64[FromType numericType](value FromType) (int64, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToInt64(int8(value)) + case int16: + return int16ToInt64(int16(value)) + case int32: + return int32ToInt64(int32(value)) + case int64: + return int64(value), true + case int: + return intToInt64(int(value)) + case uint8: + return uint8ToInt64(uint8(value)) + case uint16: + return uint16ToInt64(uint16(value)) + case uint32: + return uint32ToInt64(uint32(value)) + case uint64: + return uint64ToInt64(uint64(value)) + case uint: + return uintToInt64(uint(value)) + case float32: + return float32ToInt64(float32(value)) + case float64: + return float64ToInt64(float64(value)) + } + return int64(value), false +} + +// ToInt converts value to int type safely. +// result will always be same as the usual type cast(int(value)), +// but ok is false when overflow or underflow occured. +func ToInt[FromType numericType](value FromType) (int, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToInt(int8(value)) + case int16: + return int16ToInt(int16(value)) + case int32: + return int32ToInt(int32(value)) + case int64: + return int64ToInt(int64(value)) + case int: + return int(value), true + case uint8: + return uint8ToInt(uint8(value)) + case uint16: + return uint16ToInt(uint16(value)) + case uint32: + return uint32ToInt(uint32(value)) + case uint64: + return uint64ToInt(uint64(value)) + case uint: + return uintToInt(uint(value)) + case float32: + return float32ToInt(float32(value)) + case float64: + return float64ToInt(float64(value)) + } + return int(value), false +} + +// ToUint8 converts value to uint8 type safely. +// result will always be same as the usual type cast(uint8(value)), +// but ok is false when overflow or underflow occured. +func ToUint8[FromType numericType](value FromType) (uint8, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToUint8(int8(value)) + case int16: + return int16ToUint8(int16(value)) + case int32: + return int32ToUint8(int32(value)) + case int64: + return int64ToUint8(int64(value)) + case int: + return intToUint8(int(value)) + case uint8: + return uint8(value), true + case uint16: + return uint16ToUint8(uint16(value)) + case uint32: + return uint32ToUint8(uint32(value)) + case uint64: + return uint64ToUint8(uint64(value)) + case uint: + return uintToUint8(uint(value)) + case float32: + return float32ToUint8(float32(value)) + case float64: + return float64ToUint8(float64(value)) + } + return uint8(value), false +} + +// ToUint16 converts value to uint16 type safely. +// result will always be same as the usual type cast(uint16(value)), +// but ok is false when overflow or underflow occured. +func ToUint16[FromType numericType](value FromType) (uint16, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToUint16(int8(value)) + case int16: + return int16ToUint16(int16(value)) + case int32: + return int32ToUint16(int32(value)) + case int64: + return int64ToUint16(int64(value)) + case int: + return intToUint16(int(value)) + case uint8: + return uint8ToUint16(uint8(value)) + case uint16: + return uint16(value), true + case uint32: + return uint32ToUint16(uint32(value)) + case uint64: + return uint64ToUint16(uint64(value)) + case uint: + return uintToUint16(uint(value)) + case float32: + return float32ToUint16(float32(value)) + case float64: + return float64ToUint16(float64(value)) + } + return uint16(value), false +} + +// ToUint32 converts value to uint32 type safely. +// result will always be same as the usual type cast(uint32(value)), +// but ok is false when overflow or underflow occured. +func ToUint32[FromType numericType](value FromType) (uint32, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToUint32(int8(value)) + case int16: + return int16ToUint32(int16(value)) + case int32: + return int32ToUint32(int32(value)) + case int64: + return int64ToUint32(int64(value)) + case int: + return intToUint32(int(value)) + case uint8: + return uint8ToUint32(uint8(value)) + case uint16: + return uint16ToUint32(uint16(value)) + case uint32: + return uint32(value), true + case uint64: + return uint64ToUint32(uint64(value)) + case uint: + return uintToUint32(uint(value)) + case float32: + return float32ToUint32(float32(value)) + case float64: + return float64ToUint32(float64(value)) + } + return uint32(value), false +} + +// ToUint64 converts value to uint64 type safely. +// result will always be same as the usual type cast(uint64(value)), +// but ok is false when overflow or underflow occured. +func ToUint64[FromType numericType](value FromType) (uint64, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToUint64(int8(value)) + case int16: + return int16ToUint64(int16(value)) + case int32: + return int32ToUint64(int32(value)) + case int64: + return int64ToUint64(int64(value)) + case int: + return intToUint64(int(value)) + case uint8: + return uint8ToUint64(uint8(value)) + case uint16: + return uint16ToUint64(uint16(value)) + case uint32: + return uint32ToUint64(uint32(value)) + case uint64: + return uint64(value), true + case uint: + return uintToUint64(uint(value)) + case float32: + return float32ToUint64(float32(value)) + case float64: + return float64ToUint64(float64(value)) + } + return uint64(value), false +} + +// ToUint converts value to uint type safely. +// result will always be same as the usual type cast(uint(value)), +// but ok is false when overflow or underflow occured. +func ToUint[FromType numericType](value FromType) (uint, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToUint(int8(value)) + case int16: + return int16ToUint(int16(value)) + case int32: + return int32ToUint(int32(value)) + case int64: + return int64ToUint(int64(value)) + case int: + return intToUint(int(value)) + case uint8: + return uint8ToUint(uint8(value)) + case uint16: + return uint16ToUint(uint16(value)) + case uint32: + return uint32ToUint(uint32(value)) + case uint64: + return uint64ToUint(uint64(value)) + case uint: + return uint(value), true + case float32: + return float32ToUint(float32(value)) + case float64: + return float64ToUint(float64(value)) + } + return uint(value), false +} + +// ToFloat32 converts value to float32 type safely. +// result will always be same as the usual type cast(float32(value)), +// but ok is false when overflow or underflow occured. +func ToFloat32[FromType numericType](value FromType) (float32, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToFloat32(int8(value)) + case int16: + return int16ToFloat32(int16(value)) + case int32: + return int32ToFloat32(int32(value)) + case int64: + return int64ToFloat32(int64(value)) + case int: + return intToFloat32(int(value)) + case uint8: + return uint8ToFloat32(uint8(value)) + case uint16: + return uint16ToFloat32(uint16(value)) + case uint32: + return uint32ToFloat32(uint32(value)) + case uint64: + return uint64ToFloat32(uint64(value)) + case uint: + return uintToFloat32(uint(value)) + case float32: + return float32(value), true + case float64: + return float64ToFloat32(float64(value)) + } + return float32(value), false +} + +// ToFloat64 converts value to float64 type safely. +// result will always be same as the usual type cast(float64(value)), +// but ok is false when overflow or underflow occured. +func ToFloat64[FromType numericType](value FromType) (float64, bool) { + var zero FromType // Use zero to any for type switch to avoid malloc + switch any(zero).(type) { + case int8: + return int8ToFloat64(int8(value)) + case int16: + return int16ToFloat64(int16(value)) + case int32: + return int32ToFloat64(int32(value)) + case int64: + return int64ToFloat64(int64(value)) + case int: + return intToFloat64(int(value)) + case uint8: + return uint8ToFloat64(uint8(value)) + case uint16: + return uint16ToFloat64(uint16(value)) + case uint32: + return uint32ToFloat64(uint32(value)) + case uint64: + return uint64ToFloat64(uint64(value)) + case uint: + return uintToFloat64(uint(value)) + case float32: + return float32ToFloat64(float32(value)) + case float64: + return float64(value), true + } + return float64(value), false +} diff --git a/vendor/github.com/chen3feng/safecast/safecast.go b/vendor/github.com/chen3feng/safecast/safecast.go new file mode 100644 index 000000000..c86180d28 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/safecast.go @@ -0,0 +1,9 @@ +// Package safecast provide a safe way to cast a numeric value from type A to type B, +// with overflow and underflow check. +package safecast + +// All the code are generated. + +//go:generate sh -c "python3 generate.py | gofmt > casts.go" +//go:generate sh -c "python3 generate_generic.py | gofmt > generics.go" +//go:generate sh -c "python3 generate_test.py | gofmt > safecast_test.go" diff --git a/vendor/github.com/chen3feng/safecast/safecast.py b/vendor/github.com/chen3feng/safecast/safecast.py new file mode 100644 index 000000000..ba9663de6 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/safecast.py @@ -0,0 +1,6 @@ +ALL_FIXED_INT_BITS = ('8', '16', '32', '64') +ALL_INT_BITS = ALL_FIXED_INT_BITS + ('',) + + +def to_camel_case(type): + return type[0].upper() + type[1:] diff --git a/vendor/github.com/chen3feng/safecast/updatedoc.sh b/vendor/github.com/chen3feng/safecast/updatedoc.sh new file mode 100644 index 000000000..a988f7899 --- /dev/null +++ b/vendor/github.com/chen3feng/safecast/updatedoc.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +gomarkdoc -o README.md -e . +gomarkdoc -o README_zh.md -e . diff --git a/vendor/nhooyr.io/websocket/LICENSE.txt b/vendor/github.com/coder/websocket/LICENSE.txt similarity index 100% rename from vendor/nhooyr.io/websocket/LICENSE.txt rename to vendor/github.com/coder/websocket/LICENSE.txt diff --git a/vendor/github.com/coder/websocket/README.md b/vendor/github.com/coder/websocket/README.md new file mode 100644 index 000000000..c74b79ddc --- /dev/null +++ b/vendor/github.com/coder/websocket/README.md @@ -0,0 +1,160 @@ +# websocket + +[![Go Reference](https://pkg.go.dev/badge/github.com/coder/websocket.svg)](https://pkg.go.dev/github.com/coder/websocket) +[![Go Coverage](https://img.shields.io/badge/coverage-91%25-success)](https://github.com/coder/websocket/coverage.html) + +websocket is a minimal and idiomatic WebSocket library for Go. + +## Install + +```sh +go get github.com/coder/websocket +``` + +> [!NOTE] +> Coder now maintains this project as explained in [this blog post](https://coder.com/blog/websocket). +> We're grateful to [nhooyr](https://github.com/nhooyr) for authoring and maintaining this project from +> 2019 to 2024. + +## Highlights + +- Minimal and idiomatic API +- First class [context.Context](https://blog.golang.org/context) support +- Fully passes the WebSocket [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite) +- [Zero dependencies](https://pkg.go.dev/github.com/coder/websocket?tab=imports) +- JSON helpers in the [wsjson](https://pkg.go.dev/github.com/coder/websocket/wsjson) subpackage +- Zero alloc reads and writes +- Concurrent writes +- [Close handshake](https://pkg.go.dev/github.com/coder/websocket#Conn.Close) +- [net.Conn](https://pkg.go.dev/github.com/coder/websocket#NetConn) wrapper +- [Ping pong](https://pkg.go.dev/github.com/coder/websocket#Conn.Ping) API +- [RFC 7692](https://tools.ietf.org/html/rfc7692) permessage-deflate compression +- [CloseRead](https://pkg.go.dev/github.com/coder/websocket#Conn.CloseRead) helper for write only connections +- Compile to [Wasm](https://pkg.go.dev/github.com/coder/websocket#hdr-Wasm) + +## Roadmap + +See GitHub issues for minor issues but the major future enhancements are: + +- [ ] Perfect examples [#217](https://github.com/nhooyr/websocket/issues/217) +- [ ] wstest.Pipe for in memory testing [#340](https://github.com/nhooyr/websocket/issues/340) +- [ ] Ping pong heartbeat helper [#267](https://github.com/nhooyr/websocket/issues/267) +- [ ] Ping pong instrumentation callbacks [#246](https://github.com/nhooyr/websocket/issues/246) +- [ ] Graceful shutdown helpers [#209](https://github.com/nhooyr/websocket/issues/209) +- [ ] Assembly for WebSocket masking [#16](https://github.com/nhooyr/websocket/issues/16) + - WIP at [#326](https://github.com/nhooyr/websocket/pull/326), about 3x faster +- [ ] HTTP/2 [#4](https://github.com/nhooyr/websocket/issues/4) +- [ ] The holy grail [#402](https://github.com/nhooyr/websocket/issues/402) + +## Examples + +For a production quality example that demonstrates the complete API, see the +[echo example](./internal/examples/echo). + +For a full stack example, see the [chat example](./internal/examples/chat). + +### Server + +```go +http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) { + c, err := websocket.Accept(w, r, nil) + if err != nil { + // ... + } + defer c.CloseNow() + + ctx, cancel := context.WithTimeout(r.Context(), time.Second*10) + defer cancel() + + var v interface{} + err = wsjson.Read(ctx, c, &v) + if err != nil { + // ... + } + + log.Printf("received: %v", v) + + c.Close(websocket.StatusNormalClosure, "") +}) +``` + +### Client + +```go +ctx, cancel := context.WithTimeout(context.Background(), time.Minute) +defer cancel() + +c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil) +if err != nil { + // ... +} +defer c.CloseNow() + +err = wsjson.Write(ctx, c, "hi") +if err != nil { + // ... +} + +c.Close(websocket.StatusNormalClosure, "") +``` + +## Comparison + +### gorilla/websocket + +Advantages of [gorilla/websocket](https://github.com/gorilla/websocket): + +- Mature and widely used +- [Prepared writes](https://pkg.go.dev/github.com/gorilla/websocket#PreparedMessage) +- Configurable [buffer sizes](https://pkg.go.dev/github.com/gorilla/websocket#hdr-Buffers) +- No extra goroutine per connection to support cancellation with context.Context. This costs github.com/coder/websocket 2 KB of memory per connection. + - Will be removed soon with [context.AfterFunc](https://github.com/golang/go/issues/57928). See [#411](https://github.com/nhooyr/websocket/issues/411) + +Advantages of github.com/coder/websocket: + +- Minimal and idiomatic API + - Compare godoc of [github.com/coder/websocket](https://pkg.go.dev/github.com/coder/websocket) with [gorilla/websocket](https://pkg.go.dev/github.com/gorilla/websocket) side by side. +- [net.Conn](https://pkg.go.dev/github.com/coder/websocket#NetConn) wrapper +- Zero alloc reads and writes ([gorilla/websocket#535](https://github.com/gorilla/websocket/issues/535)) +- Full [context.Context](https://blog.golang.org/context) support +- Dial uses [net/http.Client](https://golang.org/pkg/net/http/#Client) + - Will enable easy HTTP/2 support in the future + - Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client. +- Concurrent writes +- Close handshake ([gorilla/websocket#448](https://github.com/gorilla/websocket/issues/448)) +- Idiomatic [ping pong](https://pkg.go.dev/github.com/coder/websocket#Conn.Ping) API + - Gorilla requires registering a pong callback before sending a Ping +- Can target Wasm ([gorilla/websocket#432](https://github.com/gorilla/websocket/issues/432)) +- Transparent message buffer reuse with [wsjson](https://pkg.go.dev/github.com/coder/websocket/wsjson) subpackage +- [1.75x](https://github.com/nhooyr/websocket/releases/tag/v1.7.4) faster WebSocket masking implementation in pure Go + - Gorilla's implementation is slower and uses [unsafe](https://golang.org/pkg/unsafe/). + Soon we'll have assembly and be 3x faster [#326](https://github.com/nhooyr/websocket/pull/326) +- Full [permessage-deflate](https://tools.ietf.org/html/rfc7692) compression extension support + - Gorilla only supports no context takeover mode +- [CloseRead](https://pkg.go.dev/github.com/coder/websocket#Conn.CloseRead) helper for write only connections ([gorilla/websocket#492](https://github.com/gorilla/websocket/issues/492)) + +#### golang.org/x/net/websocket + +[golang.org/x/net/websocket](https://pkg.go.dev/golang.org/x/net/websocket) is deprecated. +See [golang/go/issues/18152](https://github.com/golang/go/issues/18152). + +The [net.Conn](https://pkg.go.dev/github.com/coder/websocket#NetConn) can help in transitioning +to github.com/coder/websocket. + +#### gobwas/ws + +[gobwas/ws](https://github.com/gobwas/ws) has an extremely flexible API that allows it to be used +in an event driven style for performance. See the author's [blog post](https://medium.freecodecamp.org/million-websockets-and-go-cc58418460bb). + +However it is quite bloated. See https://pkg.go.dev/github.com/gobwas/ws + +When writing idiomatic Go, github.com/coder/websocket will be faster and easier to use. + +#### lesismal/nbio + +[lesismal/nbio](https://github.com/lesismal/nbio) is similar to gobwas/ws in that the API is +event driven for performance reasons. + +However it is quite bloated. See https://pkg.go.dev/github.com/lesismal/nbio + +When writing idiomatic Go, github.com/coder/websocket will be faster and easier to use. diff --git a/vendor/nhooyr.io/websocket/accept.go b/vendor/github.com/coder/websocket/accept.go similarity index 97% rename from vendor/nhooyr.io/websocket/accept.go rename to vendor/github.com/coder/websocket/accept.go index e1fd1f4f1..f672a730a 100644 --- a/vendor/nhooyr.io/websocket/accept.go +++ b/vendor/github.com/coder/websocket/accept.go @@ -17,12 +17,10 @@ import ( "path/filepath" "strings" - "nhooyr.io/websocket/internal/errd" + "github.com/coder/websocket/internal/errd" ) // AcceptOptions represents Accept's options. -// -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. type AcceptOptions struct { // Subprotocols lists the WebSocket subprotocols that Accept will negotiate with the client. // The empty subprotocol will always be negotiated as per RFC 6455. If you would like to @@ -77,8 +75,6 @@ func (opts *AcceptOptions) cloneWithDefaults() *AcceptOptions { // Accept accepts a WebSocket handshake from a client and upgrades the // the connection to a WebSocket. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // Accept will not allow cross origin requests by default. // See the InsecureSkipVerify and OriginPatterns options to allow cross origin requests. // diff --git a/vendor/nhooyr.io/websocket/close.go b/vendor/github.com/coder/websocket/close.go similarity index 93% rename from vendor/nhooyr.io/websocket/close.go rename to vendor/github.com/coder/websocket/close.go index efbc20380..ff2e878a9 100644 --- a/vendor/nhooyr.io/websocket/close.go +++ b/vendor/github.com/coder/websocket/close.go @@ -11,13 +11,11 @@ import ( "net" "time" - "nhooyr.io/websocket/internal/errd" + "github.com/coder/websocket/internal/errd" ) // StatusCode represents a WebSocket status code. // https://tools.ietf.org/html/rfc6455#section-7.4 -// -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. type StatusCode int // https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number @@ -63,8 +61,6 @@ const ( // CloseError is returned when the connection is closed with a status and reason. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // Use Go 1.13's errors.As to check for this error. // Also see the CloseStatus helper. type CloseError struct { @@ -72,7 +68,6 @@ type CloseError struct { Reason string } -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. func (ce CloseError) Error() string { return fmt.Sprintf("status = %v and reason = %q", ce.Code, ce.Reason) } @@ -80,8 +75,6 @@ func (ce CloseError) Error() string { // CloseStatus is a convenience wrapper around Go 1.13's errors.As to grab // the status code from a CloseError. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // -1 will be returned if the passed error is nil or not a CloseError. func CloseStatus(err error) StatusCode { var ce CloseError @@ -93,8 +86,6 @@ func CloseStatus(err error) StatusCode { // Close performs the WebSocket close handshake with the given status code and reason. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // It will write a WebSocket close frame with a timeout of 5s and then wait 5s for // the peer to send a close frame. // All data messages received from the peer during the close handshake will be discarded. @@ -139,8 +130,6 @@ func (c *Conn) Close(code StatusCode, reason string) (err error) { // CloseNow closes the WebSocket connection without attempting a close handshake. // Use when you do not want the overhead of the close handshake. -// -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. func (c *Conn) CloseNow() (err error) { defer errd.Wrap(&err, "failed to immediately close WebSocket") diff --git a/vendor/nhooyr.io/websocket/compress.go b/vendor/github.com/coder/websocket/compress.go similarity index 98% rename from vendor/nhooyr.io/websocket/compress.go rename to vendor/github.com/coder/websocket/compress.go index e50ae0b39..1f3adcfb7 100644 --- a/vendor/nhooyr.io/websocket/compress.go +++ b/vendor/github.com/coder/websocket/compress.go @@ -12,8 +12,6 @@ import ( // CompressionMode represents the modes available to the permessage-deflate extension. // See https://tools.ietf.org/html/rfc7692 // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // Works in all modern browsers except Safari which does not implement the permessage-deflate extension. // // Compression is only used if the peer supports the mode selected. diff --git a/vendor/nhooyr.io/websocket/conn.go b/vendor/github.com/coder/websocket/conn.go similarity index 94% rename from vendor/nhooyr.io/websocket/conn.go rename to vendor/github.com/coder/websocket/conn.go index 2bf221caf..8690fb3b4 100644 --- a/vendor/nhooyr.io/websocket/conn.go +++ b/vendor/github.com/coder/websocket/conn.go @@ -17,8 +17,6 @@ import ( // MessageType represents the type of a WebSocket message. // See https://tools.ietf.org/html/rfc6455#section-5.6 -// -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. type MessageType int // MessageType constants. @@ -32,8 +30,6 @@ const ( // Conn represents a WebSocket connection. // All methods may be called concurrently except for Reader and Read. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // You must always read from the connection. Otherwise control // frames will not be handled. See Reader and CloseRead. // @@ -144,8 +140,6 @@ func newConn(cfg connConfig) *Conn { // Subprotocol returns the negotiated subprotocol. // An empty string means the default protocol. -// -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. func (c *Conn) Subprotocol() string { return c.subprotocol } @@ -204,8 +198,6 @@ func (c *Conn) flate() bool { // not read from the connection but instead waits for a Reader call // to read the pong. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // TCP Keepalives should suffice for most use cases. func (c *Conn) Ping(ctx context.Context) error { p := atomic.AddInt32(&c.pingCounter, 1) diff --git a/vendor/nhooyr.io/websocket/dial.go b/vendor/github.com/coder/websocket/dial.go similarity index 97% rename from vendor/nhooyr.io/websocket/dial.go rename to vendor/github.com/coder/websocket/dial.go index 6dd805026..ad61a35d5 100644 --- a/vendor/nhooyr.io/websocket/dial.go +++ b/vendor/github.com/coder/websocket/dial.go @@ -17,12 +17,10 @@ import ( "sync" "time" - "nhooyr.io/websocket/internal/errd" + "github.com/coder/websocket/internal/errd" ) // DialOptions represents Dial's options. -// -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. type DialOptions struct { // HTTPClient is used for the connection. // Its Transport must return writable bodies for WebSocket handshakes. @@ -93,8 +91,6 @@ func (opts *DialOptions) cloneWithDefaults(ctx context.Context) (context.Context // Dial performs a WebSocket handshake on url. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // The response is the WebSocket handshake response from the server. // You never need to close resp.Body yourself. // diff --git a/vendor/nhooyr.io/websocket/doc.go b/vendor/github.com/coder/websocket/doc.go similarity index 82% rename from vendor/nhooyr.io/websocket/doc.go rename to vendor/github.com/coder/websocket/doc.go index 0d8e66ae3..03edf1292 100644 --- a/vendor/nhooyr.io/websocket/doc.go +++ b/vendor/github.com/coder/websocket/doc.go @@ -3,8 +3,6 @@ // Package websocket implements the RFC 6455 WebSocket protocol. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // https://tools.ietf.org/html/rfc6455 // // Use Dial to dial a WebSocket server. @@ -17,7 +15,7 @@ // // The wsjson subpackage contain helpers for JSON and protobuf messages. // -// More documentation at https://nhooyr.io/websocket. +// More documentation at https://github.com/coder/websocket. // // # Wasm // @@ -33,4 +31,4 @@ // - Conn.CloseNow is Close(StatusGoingAway, "") // - HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op // - *http.Response from Dial is &http.Response{} with a 101 status code on success -package websocket // import "nhooyr.io/websocket" +package websocket // import "github.com/coder/websocket" diff --git a/vendor/nhooyr.io/websocket/frame.go b/vendor/github.com/coder/websocket/frame.go similarity index 98% rename from vendor/nhooyr.io/websocket/frame.go rename to vendor/github.com/coder/websocket/frame.go index d5631863c..e7ab76bed 100644 --- a/vendor/nhooyr.io/websocket/frame.go +++ b/vendor/github.com/coder/websocket/frame.go @@ -9,7 +9,7 @@ import ( "io" "math" - "nhooyr.io/websocket/internal/errd" + "github.com/coder/websocket/internal/errd" ) // opcode represents a WebSocket opcode. diff --git a/vendor/nhooyr.io/websocket/internal/bpool/bpool.go b/vendor/github.com/coder/websocket/internal/bpool/bpool.go similarity index 100% rename from vendor/nhooyr.io/websocket/internal/bpool/bpool.go rename to vendor/github.com/coder/websocket/internal/bpool/bpool.go diff --git a/vendor/nhooyr.io/websocket/internal/errd/wrap.go b/vendor/github.com/coder/websocket/internal/errd/wrap.go similarity index 100% rename from vendor/nhooyr.io/websocket/internal/errd/wrap.go rename to vendor/github.com/coder/websocket/internal/errd/wrap.go diff --git a/vendor/nhooyr.io/websocket/internal/util/util.go b/vendor/github.com/coder/websocket/internal/util/util.go similarity index 100% rename from vendor/nhooyr.io/websocket/internal/util/util.go rename to vendor/github.com/coder/websocket/internal/util/util.go diff --git a/vendor/nhooyr.io/websocket/internal/wsjs/wsjs_js.go b/vendor/github.com/coder/websocket/internal/wsjs/wsjs_js.go similarity index 100% rename from vendor/nhooyr.io/websocket/internal/wsjs/wsjs_js.go rename to vendor/github.com/coder/websocket/internal/wsjs/wsjs_js.go diff --git a/vendor/nhooyr.io/websocket/internal/xsync/go.go b/vendor/github.com/coder/websocket/internal/xsync/go.go similarity index 100% rename from vendor/nhooyr.io/websocket/internal/xsync/go.go rename to vendor/github.com/coder/websocket/internal/xsync/go.go diff --git a/vendor/nhooyr.io/websocket/internal/xsync/int64.go b/vendor/github.com/coder/websocket/internal/xsync/int64.go similarity index 100% rename from vendor/nhooyr.io/websocket/internal/xsync/int64.go rename to vendor/github.com/coder/websocket/internal/xsync/int64.go diff --git a/vendor/nhooyr.io/websocket/make.sh b/vendor/github.com/coder/websocket/make.sh similarity index 100% rename from vendor/nhooyr.io/websocket/make.sh rename to vendor/github.com/coder/websocket/make.sh diff --git a/vendor/nhooyr.io/websocket/mask.go b/vendor/github.com/coder/websocket/mask.go similarity index 100% rename from vendor/nhooyr.io/websocket/mask.go rename to vendor/github.com/coder/websocket/mask.go diff --git a/vendor/nhooyr.io/websocket/mask_amd64.s b/vendor/github.com/coder/websocket/mask_amd64.s similarity index 100% rename from vendor/nhooyr.io/websocket/mask_amd64.s rename to vendor/github.com/coder/websocket/mask_amd64.s diff --git a/vendor/nhooyr.io/websocket/mask_arm64.s b/vendor/github.com/coder/websocket/mask_arm64.s similarity index 100% rename from vendor/nhooyr.io/websocket/mask_arm64.s rename to vendor/github.com/coder/websocket/mask_arm64.s diff --git a/vendor/nhooyr.io/websocket/mask_asm.go b/vendor/github.com/coder/websocket/mask_asm.go similarity index 100% rename from vendor/nhooyr.io/websocket/mask_asm.go rename to vendor/github.com/coder/websocket/mask_asm.go diff --git a/vendor/nhooyr.io/websocket/mask_go.go b/vendor/github.com/coder/websocket/mask_go.go similarity index 100% rename from vendor/nhooyr.io/websocket/mask_go.go rename to vendor/github.com/coder/websocket/mask_go.go diff --git a/vendor/nhooyr.io/websocket/netconn.go b/vendor/github.com/coder/websocket/netconn.go similarity index 98% rename from vendor/nhooyr.io/websocket/netconn.go rename to vendor/github.com/coder/websocket/netconn.go index 9359bbced..86f7dadb5 100644 --- a/vendor/nhooyr.io/websocket/netconn.go +++ b/vendor/github.com/coder/websocket/netconn.go @@ -12,8 +12,6 @@ import ( // NetConn converts a *websocket.Conn into a net.Conn. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // It's for tunneling arbitrary protocols over WebSockets. // Few users of the library will need this but it's tricky to implement // correctly and so provided in the library. diff --git a/vendor/nhooyr.io/websocket/netconn_js.go b/vendor/github.com/coder/websocket/netconn_js.go similarity index 100% rename from vendor/nhooyr.io/websocket/netconn_js.go rename to vendor/github.com/coder/websocket/netconn_js.go diff --git a/vendor/nhooyr.io/websocket/netconn_notjs.go b/vendor/github.com/coder/websocket/netconn_notjs.go similarity index 100% rename from vendor/nhooyr.io/websocket/netconn_notjs.go rename to vendor/github.com/coder/websocket/netconn_notjs.go diff --git a/vendor/nhooyr.io/websocket/read.go b/vendor/github.com/coder/websocket/read.go similarity index 95% rename from vendor/nhooyr.io/websocket/read.go rename to vendor/github.com/coder/websocket/read.go index 6ea2d500c..1b9404b82 100644 --- a/vendor/nhooyr.io/websocket/read.go +++ b/vendor/github.com/coder/websocket/read.go @@ -13,16 +13,14 @@ import ( "strings" "time" - "nhooyr.io/websocket/internal/errd" - "nhooyr.io/websocket/internal/util" - "nhooyr.io/websocket/internal/xsync" + "github.com/coder/websocket/internal/errd" + "github.com/coder/websocket/internal/util" + "github.com/coder/websocket/internal/xsync" ) // Reader reads from the connection until there is a WebSocket // data message to be read. It will handle ping, pong and close frames as appropriate. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // It returns the type of the message and an io.Reader to read it. // The passed context will also bound the reader. // Ensure you read to EOF otherwise the connection will hang. @@ -41,8 +39,6 @@ func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error) { // Read is a convenience method around Reader to read a single message // from the connection. -// -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) { typ, r, err := c.Reader(ctx) if err != nil { @@ -56,8 +52,6 @@ func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) { // CloseRead starts a goroutine to read from the connection until it is closed // or a data message is received. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // Once CloseRead is called you cannot read any messages from the connection. // The returned context will be cancelled when the connection is closed. // @@ -95,8 +89,6 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { // SetReadLimit sets the max number of bytes to read for a single message. // It applies to the Reader and Read methods. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // By default, the connection has a message read limit of 32768 bytes. // // When the limit is hit, the connection will be closed with StatusMessageTooBig. diff --git a/vendor/nhooyr.io/websocket/stringer.go b/vendor/github.com/coder/websocket/stringer.go similarity index 94% rename from vendor/nhooyr.io/websocket/stringer.go rename to vendor/github.com/coder/websocket/stringer.go index f70b623dd..5a66ba290 100644 --- a/vendor/nhooyr.io/websocket/stringer.go +++ b/vendor/github.com/coder/websocket/stringer.go @@ -49,7 +49,6 @@ const _MessageType_name = "MessageTextMessageBinary" var _MessageType_index = [...]uint8{0, 11, 24} -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. func (i MessageType) String() string { i -= 1 if i < 0 || i >= MessageType(len(_MessageType_index)-1) { @@ -83,7 +82,6 @@ const _StatusCode_name = "StatusNormalClosureStatusGoingAwayStatusProtocolErrorS var _StatusCode_index = [...]uint16{0, 19, 34, 53, 74, 88, 106, 127, 156, 177, 196, 220, 239, 259, 278, 294, 312} -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. func (i StatusCode) String() string { i -= 1000 if i < 0 || i >= StatusCode(len(_StatusCode_index)-1) { diff --git a/vendor/nhooyr.io/websocket/write.go b/vendor/github.com/coder/websocket/write.go similarity index 96% rename from vendor/nhooyr.io/websocket/write.go rename to vendor/github.com/coder/websocket/write.go index 6eaecada6..e294a680e 100644 --- a/vendor/nhooyr.io/websocket/write.go +++ b/vendor/github.com/coder/websocket/write.go @@ -16,15 +16,13 @@ import ( "compress/flate" - "nhooyr.io/websocket/internal/errd" - "nhooyr.io/websocket/internal/util" + "github.com/coder/websocket/internal/errd" + "github.com/coder/websocket/internal/util" ) // Writer returns a writer bounded by the context that will write // a WebSocket message of type dataType to the connection. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // You must close the writer once you have written the entire message. // // Only one writer can be open at a time, multiple calls will block until the previous writer @@ -39,8 +37,6 @@ func (c *Conn) Writer(ctx context.Context, typ MessageType) (io.WriteCloser, err // Write writes a message to the connection. // -// Deprecated: coder now maintains this library at https://github.com/coder/websocket. -// // See the Writer method if you want to stream a message. // // If compression is disabled or the compression threshold is not met, then it diff --git a/vendor/nhooyr.io/websocket/ws_js.go b/vendor/github.com/coder/websocket/ws_js.go similarity index 98% rename from vendor/nhooyr.io/websocket/ws_js.go rename to vendor/github.com/coder/websocket/ws_js.go index 02d61f28c..a8de0c636 100644 --- a/vendor/nhooyr.io/websocket/ws_js.go +++ b/vendor/github.com/coder/websocket/ws_js.go @@ -1,4 +1,4 @@ -package websocket // import "nhooyr.io/websocket" +package websocket // import "github.com/coder/websocket" import ( "bytes" @@ -14,9 +14,9 @@ import ( "sync" "syscall/js" - "nhooyr.io/websocket/internal/bpool" - "nhooyr.io/websocket/internal/wsjs" - "nhooyr.io/websocket/internal/xsync" + "github.com/coder/websocket/internal/bpool" + "github.com/coder/websocket/internal/wsjs" + "github.com/coder/websocket/internal/xsync" ) // opcode represents a WebSocket opcode. diff --git a/vendor/github.com/skycoin/skywire-utilities/pkg/skyenv/values.go b/vendor/github.com/skycoin/skywire-utilities/pkg/skyenv/values.go deleted file mode 100644 index e46632cee..000000000 --- a/vendor/github.com/skycoin/skywire-utilities/pkg/skyenv/values.go +++ /dev/null @@ -1,51 +0,0 @@ -// Package skyenv pkg/skyenv/values.go -package skyenv - -// Constants for new default services. -const ( - ServiceConfAddr = "http://conf.skywire.skycoin.com" - TpDiscAddr = "http://tpd.skywire.skycoin.com" - DmsgDiscAddr = "http://dmsgd.skywire.skycoin.com" - ServiceDiscAddr = "http://sd.skycoin.com" - RouteFinderAddr = "http://rf.skywire.skycoin.com" - UptimeTrackerAddr = "http://ut.skywire.skycoin.com" - AddressResolverAddr = "http://ar.skywire.skycoin.com" - RouteSetupPKs = "0324579f003e6b4048bae2def4365e634d8e0e3054a20fc7af49daf2a179658557,024fbd3997d4260f731b01abcfce60b8967a6d4c6a11d1008812810ea1437ce438,03b87c282f6e9f70d97aeea90b07cf09864a235ef718725632d067873431dd1015" - TPSetupPKs = "03530b786c670fc7f5ab9021478c7ec9cd06a03f3ea1416c50c4a8889ef5bba80e,03271c0de223b80400d9bd4b7722b536a245eb6c9c3176781ee41e7bac8f9bad21,03a792e6d960c88c6fb2184ee4f16714c58b55f0746840617a19f7dd6e021699d9,0313efedc579f57f05d4f5bc3fbf0261f31e51cdcfde7e568169acf92c78868926,025c7bbf23e3441a36d7e8a1e9d717921e2a49a2ce035680fec4808a048d244c8a,030eb6967f6e23e81db0d214f925fc5ce3371e1b059fb8379ae3eb1edfc95e0b46,02e582c0a5e5563aad47f561b272e4c3a9f7ac716258b58e58eb50afd83c286a7f,02ddc6c749d6ed067bb68df19c9bcb1a58b7587464043b1707398ffa26a9746b26,03aa0b1c4e23616872058c11c6efba777c130a85eaf909945d697399a1eb08426d,03adb2c924987d8deef04d02bd95236c5ae172fe5dfe7273e0461d96bf4bc220be" - NetworkMonitorPKs = "0380ea88f0ad0aa4d93c330ba5f97aabca1d892190b94db69eee140b549d2817dd,0283bddb4357e2c4de0d470032cd809966aec65ce57e1188143ab32c7b589b38b6,02f4e33b75307267229b0c3d679d08dd23374333f558288cfcb114311a52199358,02090f03cb26c71779b8327067e2e37314d2db3e31dfe4f8f3cdd8e088a98eb7ec,03ff8dc39ed8d84be17a15b6a243edbcef1a5fd425209243fd7a9a28f0d23ddbea,02b9aa8276907db6f6ea8626d5d26aa6e119dd89d88bb222ce868376c5367d7b4c" - SurveyWhitelistPKs = "0327e2cf1d2e516ecbfdbd616a87489cc92a73af97335d5c8c29eafb5d8882264a,03abbb3eff140cf3dce468b3fa5a28c80fa02c6703d7b952be6faaf2050990ebf4,02b5ee5333aa6b7f5fc623b7d5f35f505cb7f974e98a70751cf41962f84c8c4637,03714c8bdaee0fb48f47babbc47c33e1880752b6620317c9d56b30f3b0ff58a9c3,020d35bbaf0a5abc8ec0ba33cde219fde734c63e7202098e1f9a6cf9daaeee55a9,027f7dec979482f418f01dfabddbd750ad036c579a16422125dd9a313eaa59c8e1,031d4cf1b7ab4c789b56c769f2888e4a61c778dfa5fe7e5cd0217fc41660b2eb65" - RewardSystemPKs = "036a70e6956061778e1883e928c1236189db14dfd446df23d83e45c321b330c91f" -) - -// Constants for testing deployment. -const ( - TestServiceConfAddr = "http://conf.skywire.dev" - TestTpDiscAddr = "http://tpd.skywire.dev" - TestDmsgDiscAddr = "http://dmsgd.skywire.dev" - TestServiceDiscAddr = "http://sd.skywire.dev" - TestRouteFinderAddr = "http://rf.skywire.dev" - TestUptimeTrackerAddr = "http://ut.skywire.dev" - TestAddressResolverAddr = "http://ar.skywire.dev" - TestRouteSetupPKs = "0324579f003e6b4048bae2def4365e634d8e0e3054a20fc7af49daf2a179658557,024fbd3997d4260f731b01abcfce60b8967a6d4c6a11d1008812810ea1437ce438,03b87c282f6e9f70d97aeea90b07cf09864a235ef718725632d067873431dd1015" - TestTPSetupPKs = "03530b786c670fc7f5ab9021478c7ec9cd06a03f3ea1416c50c4a8889ef5bba80e,03271c0de223b80400d9bd4b7722b536a245eb6c9c3176781ee41e7bac8f9bad21,03a792e6d960c88c6fb2184ee4f16714c58b55f0746840617a19f7dd6e021699d9,0313efedc579f57f05d4f5bc3fbf0261f31e51cdcfde7e568169acf92c78868926,025c7bbf23e3441a36d7e8a1e9d717921e2a49a2ce035680fec4808a048d244c8a,030eb6967f6e23e81db0d214f925fc5ce3371e1b059fb8379ae3eb1edfc95e0b46,02e582c0a5e5563aad47f561b272e4c3a9f7ac716258b58e58eb50afd83c286a7f,02ddc6c749d6ed067bb68df19c9bcb1a58b7587464043b1707398ffa26a9746b26,03aa0b1c4e23616872058c11c6efba777c130a85eaf909945d697399a1eb08426d,03adb2c924987d8deef04d02bd95236c5ae172fe5dfe7273e0461d96bf4bc220be" - TestNetworkMonitorPKs = "0380ea88f0ad0aa4d93c330ba5f97aabca1d892190b94db69eee140b549d2817dd,0283bddb4357e2c4de0d470032cd809966aec65ce57e1188143ab32c7b589b38b6,02f4e33b75307267229b0c3d679d08dd23374333f558288cfcb114311a52199358,02090f03cb26c71779b8327067e2e37314d2db3e31dfe4f8f3cdd8e088a98eb7ec,03ff8dc39ed8d84be17a15b6a243edbcef1a5fd425209243fd7a9a28f0d23ddbea,02b9aa8276907db6f6ea8626d5d26aa6e119dd89d88bb222ce868376c5367d7b4c" - TestSurveyWhitelistPKs = "0327e2cf1d2e516ecbfdbd616a87489cc92a73af97335d5c8c29eafb5d8882264a,03abbb3eff140cf3dce468b3fa5a28c80fa02c6703d7b952be6faaf2050990ebf4,02b5ee5333aa6b7f5fc623b7d5f35f505cb7f974e98a70751cf41962f84c8c4637,03714c8bdaee0fb48f47babbc47c33e1880752b6620317c9d56b30f3b0ff58a9c3,020d35bbaf0a5abc8ec0ba33cde219fde734c63e7202098e1f9a6cf9daaeee55a9,027f7dec979482f418f01dfabddbd750ad036c579a16422125dd9a313eaa59c8e1,031d4cf1b7ab4c789b56c769f2888e4a61c778dfa5fe7e5cd0217fc41660b2eb65" - TestRewardSystemPKs = "036a70e6956061778e1883e928c1236189db14dfd446df23d83e45c321b330c91f" -) - -// GetStunServers gives back default Stun Servers -func GetStunServers() []string { - return []string{ - "139.162.30.112:3478", - "192.53.118.31:3478", - "192.53.118.61:3478", - "170.187.228.44:3478", - "170.187.228.178:3478", - "139.162.30.129:3478", - "192.53.118.134:3478", - "192.53.118.209:3478", - } -} - -// DNSServer is value for DNS Server Address -const DNSServer = "1.1.1.1" diff --git a/vendor/modules.txt b/vendor/modules.txt index 9d6b77029..e2e1ba942 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -52,6 +52,9 @@ github.com/bytedance/sonic/loader/internal/rt # github.com/cespare/xxhash/v2 v2.3.0 ## explicit; go 1.11 github.com/cespare/xxhash/v2 +# github.com/chen3feng/safecast v0.0.0-20220908170618-81b2ecd47937 +## explicit; go 1.18 +github.com/chen3feng/safecast # github.com/cloudwego/base64x v0.1.4 ## explicit; go 1.16 github.com/cloudwego/base64x @@ -59,6 +62,14 @@ github.com/cloudwego/base64x ## explicit; go 1.16 github.com/cloudwego/iasm/expr github.com/cloudwego/iasm/x86_64 +# github.com/coder/websocket v1.8.12 +## explicit; go 1.19 +github.com/coder/websocket +github.com/coder/websocket/internal/bpool +github.com/coder/websocket/internal/errd +github.com/coder/websocket/internal/util +github.com/coder/websocket/internal/wsjs +github.com/coder/websocket/internal/xsync # github.com/confiant-inc/go-socks5 v0.0.0-20210816151940-c1124825b1d6 ## explicit github.com/confiant-inc/go-socks5 @@ -206,7 +217,6 @@ github.com/skycoin/skywire-utilities/pkg/logging github.com/skycoin/skywire-utilities/pkg/metricsutil github.com/skycoin/skywire-utilities/pkg/netutil github.com/skycoin/skywire-utilities/pkg/networkmonitor -github.com/skycoin/skywire-utilities/pkg/skyenv # github.com/spf13/cobra v1.8.1 ## explicit; go 1.15 github.com/spf13/cobra @@ -318,11 +328,3 @@ mvdan.cc/sh/v3/fileutil mvdan.cc/sh/v3/pattern mvdan.cc/sh/v3/shell mvdan.cc/sh/v3/syntax -# nhooyr.io/websocket v1.8.17 -## explicit; go 1.19 -nhooyr.io/websocket -nhooyr.io/websocket/internal/bpool -nhooyr.io/websocket/internal/errd -nhooyr.io/websocket/internal/util -nhooyr.io/websocket/internal/wsjs -nhooyr.io/websocket/internal/xsync diff --git a/vendor/nhooyr.io/websocket/README.md b/vendor/nhooyr.io/websocket/README.md deleted file mode 100644 index d663d5d0f..000000000 --- a/vendor/nhooyr.io/websocket/README.md +++ /dev/null @@ -1 +0,0 @@ -deprecated: Use https://github.com/coder/websocket instead