/
rpcserver.go
364 lines (324 loc) · 11.1 KB
/
rpcserver.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
// Package rpcserver provides a JSON RPC to communicate with the client core.
package rpcserver
import (
"context"
"crypto/elliptic"
"crypto/sha256"
"crypto/subtle"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"sync"
"time"
"decred.org/dcrdex/client/asset"
"decred.org/dcrdex/client/core"
"decred.org/dcrdex/client/db"
"decred.org/dcrdex/client/websocket"
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/msgjson"
"github.com/decred/dcrd/certgen"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
const (
// rpcSemver is the RPC server's semantic API version. Move major up one for
// breaking changes. Move minor for backwards compatible features. Move
// patch for bug fixes. Dexcctl requiredRPCSemVer should be kept up to date
// with this version.
rpcSemverMajor uint32 = 0
rpcSemverMinor uint32 = 4
rpcSemverPatch uint32 = 0
// rpcTimeoutSeconds is the number of seconds a connection to the RPC server
// is allowed to stay open without authenticating before it is closed.
rpcTimeoutSeconds = 10
)
var (
// Check that core.Core satisfies clientCore.
_ clientCore = (*core.Core)(nil)
log dex.Logger
// errUnknownCmd is wrapped when the command is not know.
errUnknownCmd = errors.New("unknown command")
)
// clientCore is satisfied by core.Core.
type clientCore interface {
websocket.Core
AssetBalance(assetID uint32) (*core.WalletBalance, error)
Book(host string, base, quote uint32) (orderBook *core.OrderBook, err error)
Cancel(appPass []byte, orderID dex.Bytes) error
CloseWallet(assetID uint32) error
CreateWallet(appPass, walletPass []byte, form *core.WalletForm) error
DiscoverAccount(dexAddr string, pass []byte, certI interface{}) (*core.Exchange, bool, error)
Exchanges() (exchanges map[string]*core.Exchange)
InitializeClient(appPass, seed []byte) error
Login(appPass []byte) error
Logout() error
OpenWallet(assetID uint32, appPass []byte) error
ToggleWalletStatus(assetID uint32, disable bool) error
GetDEXConfig(dexAddr string, certI interface{}) (*core.Exchange, error)
Register(form *core.RegisterForm) (*core.RegisterResult, error) // V0PURGE
PostBond(form *core.PostBondForm) (*core.PostBondResult, error)
UpdateBondOptions(form *core.BondOptionsForm) error
Trade(appPass []byte, form *core.TradeForm) (order *core.Order, err error)
Wallets() (walletsStates []*core.WalletState)
WalletState(assetID uint32) *core.WalletState
RescanWallet(assetID uint32, force bool) error
Send(appPass []byte, assetID uint32, value uint64, addr string, subtract bool) (asset.Coin, error)
ExportSeed(pw []byte) ([]byte, error)
DeleteArchivedRecords(olderThan *time.Time, matchesFileStr, ordersFileStr string) (int, error)
WalletPeers(assetID uint32) ([]*asset.WalletPeer, error)
AddWalletPeer(assetID uint32, host string) error
RemoveWalletPeer(assetID uint32, host string) error
Notifications(int) ([]*db.Notification, error)
}
// RPCServer is a single-client http and websocket server enabling a JSON
// interface to the DEX client.
type RPCServer struct {
core clientCore
mux *chi.Mux
wsServer *websocket.Server
addr string
tlsConfig *tls.Config
srv *http.Server
authSHA [32]byte
wg sync.WaitGroup
dexcVersion *SemVersion
}
// genCertPair generates a key/cert pair to the paths provided.
func genCertPair(certFile, keyFile string, hosts []string) error {
log.Infof("Generating TLS certificates...")
org := "dcrdex autogenerated cert"
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
cert, key, err := certgen.NewTLSCertPair(elliptic.P521(), org,
validUntil, hosts)
if err != nil {
return err
}
// Write cert and key files.
if err = os.WriteFile(certFile, cert, 0644); err != nil {
return err
}
if err = os.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile)
return err
}
log.Infof("Done generating TLS certificates")
return nil
}
// writeJSON marshals the provided interface and writes the bytes to the
// ResponseWriter. The response code is assumed to be StatusOK.
func writeJSON(w http.ResponseWriter, thing interface{}) {
writeJSONWithStatus(w, thing, http.StatusOK)
}
// writeJSONWithStatus marshals the provided interface and writes the bytes to
// the ResponseWriter with the specified response code.
func writeJSONWithStatus(w http.ResponseWriter, thing interface{}, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
b, err := json.Marshal(thing)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Errorf("JSON encode error: %v", err)
return
}
w.WriteHeader(code)
_, err = w.Write(b)
if err != nil {
log.Errorf("Write error: %v", err)
}
}
// handleJSON handles all https json requests.
func (s *RPCServer) handleJSON(w http.ResponseWriter, r *http.Request) {
// All http routes are available over websocket too, so do not support
// persistent http connections. Inform the user and close the connection
// when response handling is completed.
w.Header().Set("Connection", "close")
w.Header().Set("Content-Type", "application/json")
r.Close = true
body, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
http.Error(w, "error reading request body", http.StatusBadRequest)
return
}
req := new(msgjson.Message)
err = json.Unmarshal(body, req)
if err != nil {
http.Error(w, "JSON decode error", http.StatusUnprocessableEntity)
return
}
if req.Type != msgjson.Request {
http.Error(w, "Responses not accepted", http.StatusMethodNotAllowed)
return
}
s.parseHTTPRequest(w, req)
}
// Config holds variables neede to create a new RPC Server.
type Config struct {
Core clientCore
Addr, User, Pass, Cert, Key string
DexcVersion *SemVersion
CertHosts []string
}
// SetLogger sets the logger for the RPCServer package.
func SetLogger(logger dex.Logger) {
log = logger
}
// New is the constructor for an RPCServer.
func New(cfg *Config) (*RPCServer, error) {
if cfg.Pass == "" {
return nil, fmt.Errorf("missing RPC password")
}
// Find or create the key pair.
keyExists := dex.FileExists(cfg.Key)
certExists := dex.FileExists(cfg.Cert)
if certExists == !keyExists {
return nil, fmt.Errorf("missing cert pair file")
}
if !keyExists && !certExists {
err := genCertPair(cfg.Cert, cfg.Key, cfg.CertHosts)
if err != nil {
return nil, err
}
}
keypair, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key)
if err != nil {
return nil, err
}
// Prepare the TLS configuration.
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{keypair},
MinVersion: tls.VersionTLS12,
}
// Create an HTTP router.
mux := chi.NewRouter()
httpServer := &http.Server{
Handler: mux,
ReadTimeout: rpcTimeoutSeconds * time.Second, // slow requests should not hold connections opened
WriteTimeout: rpcTimeoutSeconds * time.Second, // hung responses must die
}
// Make the server.
s := &RPCServer{
core: cfg.Core,
mux: mux,
srv: httpServer,
addr: cfg.Addr,
tlsConfig: tlsConfig,
dexcVersion: cfg.DexcVersion,
wsServer: websocket.New(cfg.Core, log.SubLogger("WS")),
}
// Create authSHA to verify requests against.
login := cfg.User + ":" + cfg.Pass
auth := "Basic " +
base64.StdEncoding.EncodeToString([]byte(login))
s.authSHA = sha256.Sum256([]byte(auth))
// Middleware
mux.Use(middleware.Recoverer)
mux.Use(middleware.RealIP)
mux.Use(s.authMiddleware)
// The WebSocket handler is mounted on /ws in Connect.
// HTTPS endpoint
mux.Post("/", s.handleJSON)
return s, nil
}
// Connect starts the RPC server. Satisfies the dex.Connector interface.
func (s *RPCServer) Connect(ctx context.Context) (*sync.WaitGroup, error) {
// Create listener.
listener, err := tls.Listen("tcp", s.addr, s.tlsConfig)
if err != nil {
return nil, fmt.Errorf("can't listen on %s. rpc server quitting: %w", s.addr, err)
}
// Update the listening address in case a :0 was provided.
s.addr = listener.Addr().String()
// Close the listener on context cancellation.
s.wg.Add(1)
go func() {
defer s.wg.Done()
<-ctx.Done()
if err := s.srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners:
log.Errorf("HTTP server Shutdown: %v", err)
}
}()
// Configure the websocket handler before starting the server.
s.mux.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
s.wsServer.HandleConnect(ctx, w, r)
})
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := s.srv.Serve(listener); !errors.Is(err, http.ErrServerClosed) {
log.Warnf("unexpected (http.Server).Serve error: %v", err)
}
// Disconnect the websocket clients since http.(*Server).Shutdown does
// not deal with hijacked websocket connections.
s.wsServer.Shutdown()
log.Infof("RPC server off")
}()
log.Infof("RPC server listening on %s", s.addr)
return &s.wg, nil
}
// handleRequest sends the request to the correct handler function if able.
func (s *RPCServer) handleRequest(req *msgjson.Message) *msgjson.ResponsePayload {
payload := new(msgjson.ResponsePayload)
if req.Route == "" {
log.Debugf("route not specified")
payload.Error = msgjson.NewError(msgjson.RPCUnknownRoute, "no route was supplied")
return payload
}
// Find the correct handler for this route.
h, exists := routes[req.Route]
if !exists {
log.Debugf("%v: %v", errUnknownCmd, req.Route)
payload.Error = msgjson.NewError(msgjson.RPCUnknownRoute, errUnknownCmd.Error())
return payload
}
params := new(RawParams)
err := req.Unmarshal(params) // NOT ¶ms to prevent setting it to nil for []byte("null") Payload
if err != nil {
log.Debugf("cannot unmarshal params for route %s", req.Route)
payload.Error = msgjson.NewError(msgjson.RPCParseError, "unable to unmarshal request")
return payload
}
return h(s, params)
}
// parseHTTPRequest parses the msgjson message in the request body, creates a
// response message, and writes it to the http.ResponseWriter.
func (s *RPCServer) parseHTTPRequest(w http.ResponseWriter, req *msgjson.Message) {
payload := s.handleRequest(req)
resp, err := msgjson.NewResponse(req.ID, payload.Result, payload.Error)
if err != nil {
msg := fmt.Sprintf("error encoding response: %v", err)
http.Error(w, msg, http.StatusInternalServerError)
log.Errorf("parseHTTPRequest: NewResponse failed: %s", msg)
return
}
writeJSON(w, resp)
}
// authMiddleware checks incoming requests for authentication.
func (s *RPCServer) authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fail := func() {
log.Warnf("authentication failure from ip: %s", r.RemoteAddr)
w.Header().Add("WWW-Authenticate", `Basic realm="dex RPC"`)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
auth := r.Header["Authorization"]
if len(auth) == 0 {
fail()
return
}
authSHA := sha256.Sum256([]byte(auth[0]))
if subtle.ConstantTimeCompare(s.authSHA[:], authSHA[:]) != 1 {
fail()
return
}
log.Debugf("authenticated user with ip: %s", r.RemoteAddr)
next.ServeHTTP(w, r)
})
}