From 2d2d9497cc687e0c2622b5b9c16b5c3edb3cad47 Mon Sep 17 00:00:00 2001 From: Alexandre Bourget Date: Tue, 9 May 2017 09:57:50 -0400 Subject: [PATCH] Use compression for the bridge config, to shrink it under 4096 characters.. This is to work around the Docker "exec" and "run -ti" terminal which breaks when you paste more than 4096 characters. --- cmd/serve.go | 13 ++++++++++++- pkg/bridge/auth.go | 28 +++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 6582594..045f678 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -1,6 +1,8 @@ package cmd import ( + "bytes" + "compress/gzip" "crypto/tls" "encoding/base64" "encoding/json" @@ -70,9 +72,18 @@ func serve(cmd *cobra.Command, args []string) { jsonConfig, _ := json.Marshal(b) - bridgeConfText := base64.StdEncoding.EncodeToString(jsonConfig) + buf := &bytes.Buffer{} + + gz, _ := gzip.NewWriterLevel(buf, gzip.BestCompression) + gz.Write([]byte(jsonConfig)) + gz.Close() + + configBytes := buf.Bytes() + + bridgeConfText := base64.RawURLEncoding.EncodeToString(configBytes) if writeConf { log.Printf("Writing bridge conf to %q\n", confFile) + err = ioutil.WriteFile(confFile, []byte(bridgeConfText), 0600) if err != nil { log.Fatalf("Error writing %q: %s\n", confFile, err) diff --git a/pkg/bridge/auth.go b/pkg/bridge/auth.go index a5d9eea..f2c9d95 100644 --- a/pkg/bridge/auth.go +++ b/pkg/bridge/auth.go @@ -1,6 +1,8 @@ package bridge import ( + "bytes" + "compress/gzip" "crypto/tls" "crypto/x509" "encoding/base64" @@ -41,20 +43,24 @@ func NewFromDefaultConfig() (bridge *Bridge, err error) { } func NewFromString(conf string) (bridge *Bridge, err error) { - var content []byte + content := []byte(strings.TrimSpace(conf)) - conf = strings.TrimSpace(conf) + if !strings.HasPrefix(string(content), "{") { + content, err = base64.RawURLEncoding.DecodeString(conf) + if err != nil { + return nil, fmt.Errorf("decoding base64: %s", err) + } + } - if strings.HasPrefix(conf, "{") { - content = []byte(conf) - } else { - content, err = base64.StdEncoding.DecodeString(conf) + if !strings.HasPrefix(string(content), "{") { + // GZip became necessary when we hit a maximum of 4096 BYTES + // limitation on `docker exec` initiated terminals. You could + // never paste more than 4096 bytes in a swift.. By gzipping + // the JSON, we can shrink it under 4096 bytes. + gz, _ := gzip.NewReader(bytes.NewReader(content)) + content, err = ioutil.ReadAll(gz) if err != nil { - var err2 error - content, err2 = base64.RawStdEncoding.DecodeString(conf) - if err2 != nil { - return nil, fmt.Errorf("decoding base64 input: %s or %s", err2, err) - } + return nil, fmt.Errorf("gunzip: %s", err) } }