-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·160 lines (122 loc) · 3.34 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
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/websocket"
)
type Server struct {
txnsPool *PoolTxns
blocksPool *PoolBlocks
server http.Server
isServerClosed bool
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func NewNet(ssl_on bool, port int) (*Server, error) {
var net Server
net.txnsPool = NewPoolTxns()
net.blocksPool = NewPoolBlocks()
go net.Loop(ssl_on, port)
return &net, nil
}
func (net *Server) Destroy() error {
net.isServerClosed = true
net.server.Close()
return nil
}
const MSG_TXN = 0
const MSG_BLOCK = 1
func (net *Server) Loop(ssl_on bool, port int) error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Client accepted %s\n", r.URL.Path)
if r.URL.Path == "/" {
http.ServeFile(w, r, "tin.html")
return
} else if r.URL.Path == "/data" {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Error: RunHub() failed: %v\n", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if mt == websocket.CloseMessage || websocket.IsCloseError(err) || websocket.IsUnexpectedCloseError(err) {
return
}
if err != nil {
log.Printf("Error: ReadMessage() failed: %v\n", err)
return
}
if mt != websocket.BinaryMessage {
log.Printf("Error: ReadMessage() is not binary: %v\n", err)
return
}
if len(message) < 2 {
log.Printf("Error: ReadMessage() is too small: %v\n", err)
return
}
//var ans []byte
if message[0] == MSG_TXN {
message = message[1:]
var txn TxnRaw
msg, pubKey, sign, err := txn.InitTxnFromBuffer(NewTBuffer(message), true, true)
if err != nil {
log.Printf("Error: InitTxnFromBuffer() failed: %v", err)
return
}
h, err := TBuffer_sha256(msg)
if err != nil {
log.Printf("Error: sha256() failed: %v", err)
return
}
if !sign.VerifyByte(pubKey, h) { //SLOW ...
log.Printf("Error: Signiture is invalid")
return
}
net.txnsPool.Add(message) //including pubKey
} else if message[0] == MSG_BLOCK {
net.blocksPool.Add(message[1:])
}
/*err = c.WriteMessage(mt, ans)
if err != nil {
log.Println("Error: WriteMessage() failed: %w", err)
return
}*/
}
}
})
net.server = http.Server{Addr: ":" + strconv.Itoa(port), Handler: mux}
var err error
if ssl_on {
err = net.server.ListenAndServeTLS("ssl/cert.pem", "ssl/key.pem")
} else {
err = net.server.ListenAndServe()
}
//show error only if loop is not closed naturaly
if err != nil && !net.isServerClosed {
log.Printf("Net.Loop() failed: %v\n", err)
return err
}
//for net.thread.Is() {
// time.Sleep(10 * time.Millisecond)
//}
return nil
}