From 7b8bb4bd85069be75200fa505cb6d85a74e3b3ff Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Tue, 7 Apr 2020 18:03:13 +0300 Subject: [PATCH 1/3] Change json.NewDecoder to json.Unmarshal in binaries --- cmd/apps/skychat/chat.go | 14 +++++++++++--- cmd/setup-node/commands/root.go | 13 +++++++++++-- cmd/skywire-visor/commands/root.go | 11 +++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index 4dfe3d76bc..5ae1f22954 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -9,6 +9,7 @@ import ( "encoding/json" "flag" "fmt" + "io/ioutil" "net" "net/http" "sync" @@ -132,8 +133,14 @@ func handleConn(conn net.Conn) { } func messageHandler(w http.ResponseWriter, req *http.Request) { + raw, err := ioutil.ReadAll(req.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + data := map[string]string{} - if err := json.NewDecoder(req.Body).Decode(&data); err != nil { + if err := json.Unmarshal(raw, &data); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -171,12 +178,13 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { go handleConn(conn) } - _, err := conn.Write([]byte(data["message"])) - if err != nil { + if _, err := conn.Write([]byte(data["message"])); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + connsMu.Lock() delete(chatConns, pk) connsMu.Unlock() + return } diff --git a/cmd/setup-node/commands/root.go b/cmd/setup-node/commands/root.go index a0ab28e8cf..f4c67358a5 100644 --- a/cmd/setup-node/commands/root.go +++ b/cmd/setup-node/commands/root.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/json" "io" + "io/ioutil" "log" "log/syslog" "net/http" @@ -62,10 +63,18 @@ var rootCmd = &cobra.Command{ } conf := &setup.Config{} - if err := json.NewDecoder(rdr).Decode(&conf); err != nil { - log.Fatalf("Failed to decode %s: %s", rdr, err) + + raw, err := ioutil.ReadAll(rdr) + if err != nil { + logger.Fatalf("Failed to read config: %v", err) } + if err := json.Unmarshal(raw, &conf); err != nil { + logger.WithField("raw", string(raw)).Fatalf("Failed to decode config: %s", err) + } + + logger.Infof("Config: %+v", conf) + sn, err := setup.NewNode(conf, metrics.NewPrometheus("setupnode")) if err != nil { logger.Fatal("Failed to create a setup node: ", err) diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index 7d6e37c74e..240b74fd61 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -171,10 +171,17 @@ func (cfg *runCfg) readConfig() *runCfg { rdr = bufio.NewReader(os.Stdin) } - if err := json.NewDecoder(rdr).Decode(&cfg.conf); err != nil { - cfg.logger.Fatalf("Failed to decode %s: %s", rdr, err) + raw, err := ioutil.ReadAll(rdr) + if err != nil { + cfg.logger.Fatalf("Failed to read config: %v", err) + } + + if err := json.Unmarshal(raw, &cfg.conf); err != nil { + cfg.logger.WithField("raw", string(raw)).Fatalf("Failed to decode config: %s", err) } + cfg.logger.Infof("Config: %+v", &cfg.conf) + cfg.conf.Path = configPath return cfg From 0012ac1406d9d46f030cc7026bd3a9d2c8da2047 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Tue, 7 Apr 2020 18:21:22 +0300 Subject: [PATCH 2/3] Fix format of printing config --- cmd/setup-node/commands/root.go | 2 +- cmd/skywire-visor/commands/root.go | 2 +- pkg/visor/config.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/setup-node/commands/root.go b/cmd/setup-node/commands/root.go index f4c67358a5..2fe966d289 100644 --- a/cmd/setup-node/commands/root.go +++ b/cmd/setup-node/commands/root.go @@ -73,7 +73,7 @@ var rootCmd = &cobra.Command{ logger.WithField("raw", string(raw)).Fatalf("Failed to decode config: %s", err) } - logger.Infof("Config: %+v", conf) + logger.Infof("Config: %#v", conf) sn, err := setup.NewNode(conf, metrics.NewPrometheus("setupnode")) if err != nil { diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index 240b74fd61..1b689f8567 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -180,7 +180,7 @@ func (cfg *runCfg) readConfig() *runCfg { cfg.logger.WithField("raw", string(raw)).Fatalf("Failed to decode config: %s", err) } - cfg.logger.Infof("Config: %+v", &cfg.conf) + cfg.logger.Infof("Config: %#v", &cfg.conf) cfg.conf.Path = configPath diff --git a/pkg/visor/config.go b/pkg/visor/config.go index 5898717327..07d2e7f6e2 100644 --- a/pkg/visor/config.go +++ b/pkg/visor/config.go @@ -85,7 +85,7 @@ func (c *Config) flush() error { return ErrNoConfigPath } - c.log.Infof("Updating visor config to %+v", c) + c.log.Infof("Updating visor config to %#v", c) bytes, err := json.MarshalIndent(c, "", "\t") if err != nil { From 2da630ea85f22c596bfb80777b86a6c36073ae9e Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Tue, 7 Apr 2020 18:31:42 +0300 Subject: [PATCH 3/3] Revert changes to skychat --- cmd/apps/skychat/chat.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index 5ae1f22954..31a4d25513 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -9,7 +9,6 @@ import ( "encoding/json" "flag" "fmt" - "io/ioutil" "net" "net/http" "sync" @@ -133,14 +132,8 @@ func handleConn(conn net.Conn) { } func messageHandler(w http.ResponseWriter, req *http.Request) { - raw, err := ioutil.ReadAll(req.Body) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - data := map[string]string{} - if err := json.Unmarshal(raw, &data); err != nil { + if err := json.NewDecoder(req.Body).Decode(&data); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -178,7 +171,8 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { go handleConn(conn) } - if _, err := conn.Write([]byte(data["message"])); err != nil { + _, err := conn.Write([]byte(data["message"])) + if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) connsMu.Lock()