From 2649f5f3f37cafaae47e4db82c814de119929339 Mon Sep 17 00:00:00 2001 From: Thomas Jost Date: Sat, 17 Feb 2024 00:12:06 +0100 Subject: [PATCH 1/3] Use the XDG Data Home for the data directory This allows the desktop app to be installed in /usr/bin and have certificates created in the user's home directory, for instance. --- go.mod | 1 + go.sum | 3 +++ libproxy/crypto.go | 13 ++++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index df62424..97808ea 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/hoppscotch/proxyscotch go 1.18 require ( + github.com/adrg/xdg v0.4.0 github.com/atotto/clipboard v0.1.4 github.com/deckarep/gosx-notifier v0.0.0-20180201035817-e127226297fb github.com/gen2brain/dlgs v0.0.0-20211108104213-bade24837f0b diff --git a/go.sum b/go.sum index 5355b95..f83def7 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls= +github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E= github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= @@ -150,6 +152,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/libproxy/crypto.go b/libproxy/crypto.go index 080f620..da6f8ec 100644 --- a/libproxy/crypto.go +++ b/libproxy/crypto.go @@ -18,6 +18,7 @@ import ( "runtime" "time" + "github.com/adrg/xdg" "github.com/gen2brain/dlgs" ) @@ -83,7 +84,7 @@ func EnsurePrivateKeyInstalled() error { if os.IsNotExist(err) { encodedPEM := CreateKeyPair() err = os.WriteFile(GetOrCreateDataPath()+"/cert.pem", encodedPEM[0].Bytes(), 0600) - + // There's no point writing the key if we failed to write the certificate, so only do that // if there is no error. if err == nil { @@ -123,9 +124,15 @@ func EnsurePrivateKeyInstalled() error { } func GetOrCreateDataPath() string { - dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) + // Backward compatibility: use the legacy data dir if it contains + // a cert.pem file + binDir, _ := filepath.Abs(filepath.Dir(os.Args[0])) + dataDir := filepath.Join(binDir, "data") + if _, err := os.Stat(filepath.Join(dataDir, "cert.pem")); err == nil { + return dataDir + } - dataDir := dir + string(os.PathSeparator) + "data" + dataDir = filepath.Join(xdg.DataHome, "proxyscotch") // If the data directory stat fails because the direcotry does not exist, // create the data directory. From 82d5a1af43650bf7e557c0399755b902b29cf722 Mon Sep 17 00:00:00 2001 From: Thomas Jost Date: Sat, 17 Feb 2024 00:28:11 +0100 Subject: [PATCH 2/3] Use XDG data directories for icons too So that the icon can be globally installed too. --- notifier/notifier.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/notifier/notifier.go b/notifier/notifier.go index 0bed68d..6a303fd 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -3,13 +3,21 @@ package notifier import ( "os" "path/filepath" + + "github.com/adrg/xdg" ) func GetIcon() string { - return GetIconPath() + "/icon.png" -} - -func GetIconPath() string { + // Backward compatibility: use the legacy icons dir if it contains + // icon.png dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) - return dir + "/icons" + legacyFile := filepath.Join(dir, "icons", "icon.png") + if _, err := os.Stat(legacyFile); err == nil { + return legacyFile + } + + // Otherwise, use the file from a standard location, or return an empty + // string if not found. + path, _ := xdg.SearchDataFile("proxyscotch/icon.png") + return path } From 63971ace2a2f0fa25832d8082dc57da0532cdd55 Mon Sep 17 00:00:00 2001 From: Thomas Jost Date: Sat, 17 Feb 2024 00:40:31 +0100 Subject: [PATCH 3/3] Add very basic Linux support for notifier.Notify --- notifier/notifier_linux.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/notifier/notifier_linux.go b/notifier/notifier_linux.go index ff88b3d..83fb062 100644 --- a/notifier/notifier_linux.go +++ b/notifier/notifier_linux.go @@ -1,5 +1,16 @@ package notifier +import ( + "os/exec" +) + func Notify(appName, title, message, icon string) error { - return nil + cmd := exec.Command( + "notify-send", + "--app-name", appName, + "--icon", icon, + title, + message, + ) + return cmd.Run() }