-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
89 lines (80 loc) · 2.09 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
package main
import (
"database/sql"
"encoding/hex"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
_ "github.com/lib/pq"
"main.go/crypt"
"main.go/cryptstore"
)
// Data .. json payload
// type Data struct {
// Hashes Hashes
// }
// Hashes .. json payload
type Hashes struct {
File string
Hash string
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// This needs to have better URL parsing implemented...
keys := strings.Split(r.URL.Path, "/")[2]
bodyBytes, _ := ioutil.ReadAll(r.Body)
data := crypt.Encrypt(bodyBytes, []byte(strings.Split(keys, "=")[1]))
w.Write([]byte("200 OK\n"))
fileid := strings.Split(keys, "=")[0] + ":" + hex.EncodeToString(crypt.Signature(bodyBytes))
// Only upload if data key value (name:SHA256) does not exist.
if cryptstore.Get(fileid) == true {
log.Println("[*] " + r.RemoteAddr + "- Got Request but discarding.")
} else {
log.Println("[*] " + r.RemoteAddr + "- Got Request, Uploading File.")
cryptstore.Store(r.RemoteAddr, fileid, data)
}
}
func apicall(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
switch r.RequestURI {
case "/api/":
connStr := "postgres://docker:docker@gocryptdb-svc/filehashes?sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT id, fileid, ipfshash FROM filemaps")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id int
var fileid string
var ipfshash string
err = rows.Scan(&id, &fileid, &ipfshash)
if err != nil {
log.Fatal(err)
}
data := Hashes{fileid, ipfshash}
js, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
} else {
http.Error(w, "404 not found.", http.StatusNotFound)
return
}
}
func main() {
log.Println("[*] Starting GoCrypt -- Server listening on -- 8000")
http.HandleFunc("/api/", apicall)
http.HandleFunc("/upload/", uploadHandler)
log.Fatal(http.ListenAndServe("0.0.0.0:8000", nil))
}