/
server.go
207 lines (182 loc) · 5.81 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"time"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
)
type AvailableSigner struct {
key *keystore.Key
}
type Server struct {
Host string
Port int
AvailableSigners map[string]AvailableSigner
LogLevel int
CORSWhitelist map[string]bool
}
type PingResponse struct {
Status string `json:"status"`
}
type SignDropperRequest struct {
ChainId int `json:"chain_id"`
Dropper string `json:"dropper"`
Signer string `json:"signer"`
Sensible bool `json:"sensible"`
Requests []*DropperClaimMessage `json:"requests"`
}
// CORS middleware
func (server *Server) corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
var allowedOrigin string
if server.CORSWhitelist["*"] {
allowedOrigin = "*"
} else {
for o := range server.CORSWhitelist {
if r.Header.Get("Origin") == o {
allowedOrigin = o
}
}
}
// If origin in list of CORS allowed origins, extend with required headers
if allowedOrigin != "" {
w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
w.Header().Set("Access-Control-Allow-Methods", "GET,POST")
// Credentials are cookies, authorization headers, or TLS client certificates
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}
// Log access requests in proper format
func (server *Server) logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, readErr := io.ReadAll(r.Body)
if readErr != nil {
http.Error(w, "Unable to read body", http.StatusBadRequest)
return
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
if len(body) > 0 {
defer r.Body.Close()
}
next.ServeHTTP(w, r)
var ip string
var splitErr error
realIp := r.Header["X-Real-Ip"]
if len(realIp) == 0 {
ip, _, splitErr = net.SplitHostPort(r.RemoteAddr)
if splitErr != nil {
http.Error(w, fmt.Sprintf("Unable to parse client IP: %s", r.RemoteAddr), http.StatusBadRequest)
return
}
} else {
ip = realIp[0]
}
logStr := fmt.Sprintf("%s %s %s", ip, r.Method, r.URL.Path)
if server.LogLevel >= 2 {
if r.URL.RawQuery != "" {
logStr += fmt.Sprintf(" %s", r.URL.RawQuery)
}
}
log.Printf("%s\n", logStr)
})
}
// Handle panic errors to prevent server shutdown
func (server *Server) panicMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if recoverErr := recover(); recoverErr != nil {
log.Println("recovered", recoverErr)
http.Error(w, "Internal server error", 500)
}
}()
// There will be a defer with panic handler in each next function
next.ServeHTTP(w, r)
})
}
// pingRoute response with status of load balancer server itself
func (server *Server) pingRoute(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response := PingResponse{Status: "ok"}
json.NewEncoder(w).Encode(response)
}
// signDropperRoute response with status of load balancer server itself
func (server *Server) signDropperRoute(w http.ResponseWriter, r *http.Request) {
body, readErr := io.ReadAll(r.Body)
if readErr != nil {
http.Error(w, "Unable to read body", http.StatusBadRequest)
return
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
if len(body) > 0 {
defer r.Body.Close()
}
var req *SignDropperRequest
parseErr := json.Unmarshal(body, &req)
if parseErr != nil {
http.Error(w, "Unable to parse body", http.StatusBadRequest)
return
}
// Check if server can sign with provided signer address
var chosenSigner string
for addr := range server.AvailableSigners {
if addr == common.HexToAddress(req.Signer).String() {
chosenSigner = addr
}
}
if chosenSigner == "" {
http.Error(w, "Unable to find signer", http.StatusBadRequest)
return
}
for _, message := range req.Requests {
messageHash, hashErr := DropperClaimMessageHash(int64(req.ChainId), req.Dropper, message.DropId, message.RequestID, message.Claimant, message.BlockDeadline, message.Amount)
if hashErr != nil {
http.Error(w, "Unable to generate message hash", http.StatusInternalServerError)
return
}
signedMessage, signatureErr := SignRawMessage(messageHash, server.AvailableSigners[chosenSigner].key, req.Sensible)
if signatureErr != nil {
http.Error(w, "Unable to sign message", http.StatusInternalServerError)
return
}
message.Signature = hex.EncodeToString(signedMessage)
message.Signer = server.AvailableSigners[chosenSigner].key.Address.Hex()
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(req)
}
// Serve handles server run
func (server *Server) Serve() error {
serveMux := http.NewServeMux()
serveMux.HandleFunc("/ping", server.pingRoute)
serveMux.HandleFunc("/sign/dropper", server.signDropperRoute)
// Set list of common middleware, from bottom to top
commonHandler := server.corsMiddleware(serveMux)
commonHandler = server.logMiddleware(commonHandler)
commonHandler = server.panicMiddleware(commonHandler)
s := http.Server{
Addr: fmt.Sprintf("%s:%d", server.Host, server.Port),
Handler: commonHandler,
ReadTimeout: 40 * time.Second,
WriteTimeout: 40 * time.Second,
}
log.Printf("Starting node load balancer HTTP server at %s:%d", server.Host, server.Port)
sErr := s.ListenAndServe()
if sErr != nil {
return fmt.Errorf("failed to start server listener, err: %v", sErr)
}
return nil
}