forked from smallwat3r/shhh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
259 lines (231 loc) · 6.82 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Shhh CLI client
//
// Shhh application repository: https://github.com/smallwat3r/shhh
// Shhh CLI repository: https://github.com/smallwat3r/shhh-cli
//
// Author: Matthieu Petiteau <[email protected]>
//
// MIT License
//
// Copyright (c) 2020 Matthieu Petiteau
//
// 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.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path"
"reflect"
)
const (
helpSecret = "Secret message to encrypt."
helpEncryptPassphrase = "Passphrase to encrypt secret."
helpDays = "Optional, number of days to keep the secret alive."
helpLink = "URL link to access secret."
helpDecryptPassphrase = "Passphrase to decrypt secret."
)
func main() {
createCmd := flag.NewFlagSet("create", flag.ExitOnError)
secret := createCmd.String("m", "", helpSecret)
encryptPassphrase := createCmd.String("p", "", helpEncryptPassphrase)
days := createCmd.Int("d", 3, helpDays)
readCmd := flag.NewFlagSet("read", flag.ExitOnError)
link := readCmd.String("l", "", helpLink)
decryptPassphrase := readCmd.String("p", "", helpDecryptPassphrase)
if len(os.Args) == 1 {
usage()
return
}
switch os.Args[1] {
case "-h":
usage()
return
case "create":
createCmd.Parse(os.Args[2:])
case "read":
readCmd.Parse(os.Args[2:])
default:
fmt.Fprintf(os.Stderr, "%q is not valid command.\n", os.Args[1])
os.Exit(2)
}
if createCmd.Parsed() {
if *secret == "" {
fmt.Fprintf(
os.Stderr,
"Please supply a secret message using -m option.\n-m %s\n",
helpSecret,
)
return
}
if *encryptPassphrase == "" {
fmt.Fprintf(
os.Stderr,
"Please supply the passphrase using -p option.\n-p %s\n",
helpEncryptPassphrase,
)
return
}
createSecret(*secret, *encryptPassphrase, *days)
}
if readCmd.Parsed() {
if *link == "" {
fmt.Fprintf(
os.Stderr,
"Please supply the link using -l option.\n-l %s\n",
helpLink,
)
return
}
if *decryptPassphrase == "" {
fmt.Fprintf(
os.Stderr,
"Please supply the passphrase using -p option.\n-p %s\n",
helpDecryptPassphrase,
)
return
}
readSecret(*link, *decryptPassphrase)
}
}
func createSecret(secret string, passphrase string, days int) {
// Check env var for custom shhh server.
// If no custom server of shhh, defaults to standard.
domain := os.Getenv("SHHH_SERVER")
if domain == "" {
domain = "https://shhh-encrypt.com/api/c"
} else {
domain += "/api/c"
}
message := map[string]interface{}{
"secret": secret,
"passphrase": passphrase,
"days": days,
}
bytesRepr, err := json.Marshal(message)
if err != nil {
log.Fatalln(err)
}
resp, err := http.Post(domain, "application/json", bytes.NewBuffer(bytesRepr))
if err != nil {
log.Fatalln(err)
}
// Get response from server.
var response map[string]interface{}
json.NewDecoder(resp.Body).Decode(&response)
result := response["response"].(map[string]interface{})
if result["status"] == "error" {
errors := result["details"].(map[string]interface{})["json"].(map[string]interface{})
for _, v := range errors {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
s := reflect.ValueOf(v)
fmt.Println("Error:", s.Index(0))
}
}
return
}
if result["status"] == "created" {
fmt.Println("****************************************************************************")
fmt.Println("Secret link :", result["link"])
fmt.Println("One time passphrase :", passphrase)
fmt.Println("Expires on :", result["expires_on"])
fmt.Println("****************************************************************************")
return
}
}
func readSecret(link string, passphrase string) {
l, err := url.Parse(link)
if err != nil {
log.Fatalln(err)
}
host := l.Scheme + "://" + l.Host
// Get slug in URL path.
p := l.Path
slug := path.Base(p)
// Build API URL with args provided.
api_url := host + "/api/r"
u, err := url.Parse(api_url)
if err != nil {
log.Fatalln(err)
}
q, err := url.ParseQuery(u.RawQuery)
if err != nil {
log.Fatalln(err)
}
// Add args in querystring.
q.Add("slug", slug)
q.Add("passphrase", passphrase)
u.RawQuery = q.Encode()
read_url := u.String()
resp, err := http.Get(read_url)
if err != nil {
log.Fatalln(err)
}
// Get response from server.
var response map[string]interface{}
json.NewDecoder(resp.Body).Decode(&response)
result := response["response"].(map[string]interface{})
if result["status"] == "error" {
fmt.Println("Error:", result["msg"])
return
}
if result["status"] == "expired" {
fmt.Println("Error:", result["msg"])
return
}
if result["status"] == "invalid" {
fmt.Println("Error:", result["msg"])
return
}
if result["status"] == "success" {
fmt.Println("****************************************************************************")
fmt.Println(result["msg"])
fmt.Println("****************************************************************************")
return
}
}
func usage() {
h := "Create or read secrets from a Shhh server.\n\n"
h += "Usage:\n"
h += " shhh <command> [<args>]\n\n"
h += "Options:\n"
h += " -h Show help message.\n\n"
h += "Modes:\n"
h += " create Creates a secret message.\n"
h += " read Read a secret message.\n\n"
h += "Usage of create:\n"
h += " -h Show help message.\n"
h += " -m string " + helpSecret + "\n"
h += " -p string " + helpEncryptPassphrase + "\n"
h += " -d int " + helpDays + " (default 3).\n\n"
h += "Usage of read:\n"
h += " -h Show help message.\n"
h += " -l string " + helpLink + "\n"
h += " -p string " + helpDecryptPassphrase + "\n\n"
h += "Examples:\n"
h += " shhh create -m \"this is a secret msg.\" -p SuperPassphrase123 -d 2\n"
h += " shhh read -l https://shhh-encrypt.com/api/r/jKD8Uy0A9_51c8asqAYL -p SuperPassphrase123\n"
fmt.Println(h)
}