-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
141 lines (121 loc) · 3.75 KB
/
main.go
File metadata and controls
141 lines (121 loc) · 3.75 KB
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
package main
import (
"fmt"
"html/template"
"image/png"
"log"
"net/http"
"strconv"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/tula3and/me-sign/blockchain"
"github.com/tula3and/me-sign/db"
"github.com/tula3and/me-sign/email"
"github.com/tula3and/me-sign/sign"
"github.com/tula3and/me-sign/utils"
)
const (
templateDir string = "templates/"
port string = ":4000"
)
var templates *template.Template
func home(rw http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(rw, "home", nil)
}
func make(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
templates.ExecuteTemplate(rw, "make", nil)
case "POST":
r.ParseForm()
address := r.Form.Get("address")
http.Redirect(rw, r, "/sent?email="+address, http.StatusPermanentRedirect)
}
}
func sent(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
address := r.URL.Query().Get("email")
verify := email.Verify(address)
var data string
if verify {
data = "Success: sent to " + address
} else {
data = "Failed: check your input again"
}
templates.ExecuteTemplate(rw, "sent", data)
}
}
func key(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
email := r.URL.Query().Get("email")
signed := r.URL.Query().Get("signed")
verify := sign.Verify(signed, email, sign.RestorePublicKey(sign.Key()))
if verify {
templates.ExecuteTemplate(rw, "realSign", nil)
} else {
http.Redirect(rw, r, "/", http.StatusPermanentRedirect)
}
case "POST":
email := r.URL.Query().Get("email")
r.ParseForm()
fileName := r.Form.Get("fileName")
http.Redirect(rw, r, "/yourSign?email="+email+"&fileName="+fileName, http.StatusPermanentRedirect)
}
}
//create qrcode
func yourSign(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
email := r.URL.Query().Get("email")
fileName := r.URL.Query().Get("fileName")
newKey := sign.CreatePrivKey()
encryptFileName := sign.Sign(fmt.Sprintf("%x", fileName), newKey)
encryptEmail := sign.Sign(email, newKey)
num := blockchain.Blockchain().Height + 1
blockchain.Blockchain().AddBlock(encryptFileName, encryptEmail)
dataString := fmt.Sprintf("http://localhost%s/check?num=%d&key=%s", port, num, sign.RestorePublicKey(newKey))
fmt.Println(dataString)
qrCode, _ := qr.Encode(dataString, qr.L, qr.Auto)
qrCode, _ = barcode.Scale(qrCode, 512, 512)
png.Encode(rw, qrCode)
}
}
func check(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
templates.ExecuteTemplate(rw, "check", nil)
case "POST":
num, err := strconv.Atoi(r.URL.Query().Get("num"))
utils.HandleErr(err)
len := blockchain.Blockchain().Height
target := blockchain.Blocks(blockchain.Blockchain())[len-num]
key := r.URL.Query().Get("key")
r.ParseForm()
fileName := r.Form.Get("fileName")
email := r.Form.Get("email")
verifyFileName := sign.Verify(target.FileName, fmt.Sprintf("%x", fileName), key)
verifyEmail := sign.Verify(target.Email, fmt.Sprintf("%x", email), key)
var data string
if verifyFileName && verifyEmail {
data = "Success: this < " + fileName + " > exists on the block"
} else {
data = "Failed: this < " + fileName + " > does not exist on the block"
}
templates.ExecuteTemplate(rw, "sent", data)
}
}
func main() {
defer db.Close()
templates = template.Must(template.ParseGlob(templateDir + "pages/*.gohtml"))
templates = template.Must(templates.ParseGlob(templateDir + "partials/*.gohtml"))
http.HandleFunc("/", home)
http.HandleFunc("/make", make)
http.HandleFunc("/sent", sent)
http.HandleFunc("/key", key)
http.HandleFunc("/yourSign", yourSign)
http.HandleFunc("/check", check)
fmt.Printf("Listening on http://localhost%s\n", port)
log.Fatal(http.ListenAndServe(port, nil))
}