-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessageHandler.go
139 lines (122 loc) · 3.77 KB
/
messageHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
)
// HandleMessage does stuff with a msg from js returns a response
func HandleMessage(w *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
switch m.Name {
case "getBaseDirectory":
// Send the current working directory to JS
dir, _ := os.Getwd()
payload = dir
return
case "ageRequest":
// Parse the request
var request AgeRequest
err = json.Unmarshal(m.Payload, &request)
// Return on error
if err != nil {
payload = "JSON Error"
return
}
// Handle the request and answer if needed
var publicKey, privateKey, outputPath string
publicKey, privateKey, outputPath, err = handleAgeRequest(request)
if err != nil {
Logger.Println("\n\nERROR OCCURED: " + err.Error() + "\n\n")
payload = err.Error()
return
} else if len(publicKey) > 0 {
payload = "generatedKeys%" + publicKey + "%" + privateKey + "%" + outputPath
} else if len(outputPath) > 0 {
payload = "outputPath%" + outputPath
}
return
default:
// Unknown command
payload = "Unknown command"
return
}
return
}
// Parses a given AgeRequest, does what it needs to do, and returns success / error
func handleAgeRequest(request AgeRequest) (publicKey string, privateKey string, outputPath string, err error) {
// Fix potential file path errors in output
request.OutputPath = ReplaceFilepathSeparator(request.OutputPath, string(filepath.Separator))
// Age Key Encryption / Decryption Setup
if !request.UsePassword && request.Encrypt {
publicKey, privateKey, err = PrepareRecipient(request.CryptKey)
if err != nil {
return "", "", "", err
}
} else if !request.UsePassword {
err = PrepareIdentity(request.CryptKey)
if err != nil {
return "", "", "", err
}
}
// Encrypt and Zip Files if needed
if request.Encrypt && request.ZipFiles {
// Prepare Zip
zipBytes, err := ZipFilesFromPaths(request.Files)
if err != nil {
return "", "", "", err
}
// Password
if request.UsePassword {
outputPath, err = EncryptWithPassword(zipBytes, GenerateRandomString(10)+".zip", request.OutputPath, request.UseArmor, request.CryptKey)
if err != nil {
return "", "", "", err
}
} else {
// Age Key
outputPath, err = Encrypt(zipBytes, GenerateRandomString(10)+".zip", request.OutputPath, request.UseArmor, Recipients)
if err != nil {
return "", "", "", err
}
}
} else {
// Iterate all File paths
for _, file := range request.Files {
// Fix potential file path errors
file = ReplaceFilepathSeparator(file, string(filepath.Separator))
// Password being used
if request.UsePassword {
if request.Encrypt {
outputPath, err = EncryptFileWithPassword(file, request.OutputPath, request.UseArmor, request.CryptKey)
} else {
outputPath, err = DecryptFileWithPassword(file, request.OutputPath, request.CryptKey)
}
} else {
// Age Key being used
if request.Encrypt {
outputPath, err = EncryptFile(file, request.OutputPath, request.UseArmor, Recipients)
} else {
outputPath, err = DecryptFile(file, request.OutputPath, Identities)
}
}
}
}
// JS Seite: Datei per Auswahl hinzufügen können, oder Drag & Drop
if publicKey == request.CryptKey {
publicKey = ""
privateKey = ""
}
fmt.Println("\n\nOUTPUT_PATH = " +outputPath+"\n\n")
return publicKey, privateKey, outputPath, err
}
// AgeRequest contains all info needed to proceed
type AgeRequest struct {
Encrypt bool `json:"encrypt"`
ZipFiles bool `json:"zip"`
UseArmor bool `json:"armor"`
CryptKey string `json:"key"`
UsePassword bool `json:"usePassword"`
OutputPath string `json:"output"`
Files []string `json:"paths"`
}