This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
/
server.go
178 lines (167 loc) · 4.11 KB
/
server.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
cr "github.com/number571/gopeer/crypto"
en "github.com/number571/gopeer/encoding"
lc "github.com/number571/gopeer/local"
)
var (
DATABASE = NewDB("server.db")
FLCONFIG = NewCFG("server.cfg")
)
func init() {
go delOldEmailsByTime(24*time.Hour, 6*time.Hour)
hesDefaultInit("localhost:8080")
fmt.Printf("Server is listening [%s] ...\n\n", OPENADDR)
}
func delOldEmailsByTime(deltime, period time.Duration) {
for {
DATABASE.DelEmailsByTime(deltime)
time.Sleep(period)
}
}
func main() {
http.HandleFunc("/", indexPage)
http.HandleFunc("/email/send", emailSendPage)
http.HandleFunc("/email/recv", emailRecvPage)
http.ListenAndServe(OPENADDR, nil)
}
func help() string {
return `
1. exit - close server;
2. help - commands info;
3. list - list connections;
4. append - append connect to list;
5. delete - delete connect from list;
`
}
func indexPage(w http.ResponseWriter, r *http.Request) {
var req struct {
Macp string `json:"macp"`
}
if r.Method != "POST" {
response(w, 0, "hidden email service")
return
}
if r.ContentLength > int64(MAXESIZE) {
response(w, 1, "error: max size")
return
}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
response(w, 2, "error: parse json")
return
}
pasw := cr.NewSHA256([]byte(FLCONFIG.Pasw)).Bytes()
cipher := cr.NewCipher(pasw)
dect := cipher.Decrypt(en.Base64Decode(req.Macp))
if !bytes.Equal([]byte(TMESSAGE), dect) {
response(w, 3, "error: message authentication code")
return
}
response(w, 0, "success: check connection")
}
func emailSendPage(w http.ResponseWriter, r *http.Request) {
var req struct {
Recv string `json:"recv"`
Data string `json:"data"`
Macp string `json:"macp"`
}
if r.Method != "POST" {
response(w, 1, "error: method != POST")
return
}
if r.ContentLength > int64(MAXESIZE) {
response(w, 2, "error: max size")
return
}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
response(w, 3, "error: parse json")
return
}
pack := lc.Package(req.Data).Deserialize()
if pack == nil {
response(w, 4, "error: deserialize package")
return
}
hash := pack.Body.Hash
puzzle := cr.NewPuzzle(uint8(POWSDIFF))
if !puzzle.Verify(hash, pack.Body.Npow) {
response(w, 5, "error: proof of work")
return
}
pasw := cr.NewSHA256([]byte(FLCONFIG.Pasw)).Bytes()
cipher := cr.NewCipher(pasw)
dech := cipher.Decrypt(en.Base64Decode(req.Macp))
if !bytes.Equal(hash, dech) {
response(w, 6, "error: message authentication code")
return
}
err = DATABASE.SetEmail(req.Recv, pack)
if err != nil {
response(w, 7, "error: save email")
return
}
for _, conn := range FLCONFIG.Conns {
go func() {
addr := conn[0]
pasw := cr.NewSHA256([]byte(conn[1])).Bytes()
cipher := cr.NewCipher(pasw)
req.Macp = en.Base64Encode(cipher.Encrypt(hash))
resp, err := HTCLIENT.Post(
addr+"/email/send",
"application/json",
bytes.NewReader(serialize(req)),
)
if err != nil {
return
}
resp.Body.Close()
}()
}
response(w, 0, "success: email saved")
}
func emailRecvPage(w http.ResponseWriter, r *http.Request) {
var req struct {
Recv string `json:"recv"`
Data int `json:"data"`
}
if r.Method != "POST" {
response(w, 1, "error: method != POST")
return
}
if r.ContentLength > int64(MAXESIZE) {
response(w, 2, "error: max size")
return
}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
response(w, 3, "error: parse json")
return
}
if req.Data == 0 {
response(w, 0, fmt.Sprintf("%d", DATABASE.Size(req.Recv)))
return
}
res := DATABASE.GetEmail(req.Data, req.Recv)
if res == "" {
response(w, 4, "error: nothing data")
return
}
response(w, 0, res)
}
func response(w http.ResponseWriter, ret int, res string) {
w.Header().Set("Content-Type", "application/json")
var resp struct {
Result string `json:"result"`
Return int `json:"return"`
}
resp.Result = res
resp.Return = ret
json.NewEncoder(w).Encode(resp)
}