-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgsplit.go
63 lines (53 loc) · 2.1 KB
/
msgsplit.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
package main
import (
"fmt"
"os"
"net/http"
math_rand "math/rand"
)
func main() {
// this is a downscaled CRUD service
// Create a environment variable (max 256) for cipher
// Read AND Delete cipher
// no Update
http.HandleFunc("/writeread", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.Header().Set("Allow", "POST")
http.Error(w, "Method Not Allowed", 405)
return
}
// prefix environment variable, to prevent reading all system environment variables
storage_key := "msgsplit_"
r.ParseForm()
// stores _ciphertext_ on the server in env
if cipher, found := r.Form["cipher"]; found {
if len(cipher[0]) > 256 {
http.Error(w, "Payload Too Large", 413)
}
// create unique identifier name for environment variable
storage_rand := fmt.Sprintf("%d", math_rand.Int()) // example: 6334824724549167320
storage_key += storage_rand // example: msgsplit_6334824724549167320
os.Setenv(storage_key, cipher[0])
fmt.Fprintf(w, storage_rand) // example: 6334824724549167320
// server reads the _ciphertext_
} else if key, found := r.Form["key"]; found {
storage_key += key[0] // example: msgsplit_6334824724549167320
cipher, cipher_exists := os.LookupEnv(storage_key)
if !cipher_exists {
http.Error(w, "Gone", 410)
}
// delete cipher
os.Unsetenv(storage_key) // example: msgsplit_6334824724549167320
fmt.Fprintf(w, cipher)
} else {
http.Error(w, "Not Found", 404)
}
})
http.Handle("/", http.FileServer(http.Dir("./")))
httpPort, Port_exist := os.LookupEnv("PORT")
if !Port_exist {
httpPort = "8080"
}
fmt.Println("Start msgsplit server at port " + httpPort)
http.ListenAndServe(":" + httpPort, nil)
}