This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
335 lines (281 loc) · 10.4 KB
/
api.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
package api
import (
"crypto/tls"
"net"
"net/http"
"sync"
"time"
"github.com/kr/secureheader"
log "github.com/sirupsen/logrus"
cmn "github.com/tendermint/tmlibs/common"
"github.com/bytom/accesstoken"
"github.com/bytom/blockchain/txfeed"
cfg "github.com/bytom/config"
"github.com/bytom/dashboard"
"github.com/bytom/errors"
"github.com/bytom/mining/cpuminer"
"github.com/bytom/mining/miningpool"
"github.com/bytom/net/http/authn"
"github.com/bytom/net/http/gzip"
"github.com/bytom/net/http/httpjson"
"github.com/bytom/net/http/static"
"github.com/bytom/netsync"
"github.com/bytom/protocol"
"github.com/bytom/wallet"
)
var (
errNotAuthenticated = errors.New("not authenticated")
httpReadTimeout = 2 * time.Minute
httpWriteTimeout = time.Hour
)
const (
// SUCCESS indicates the rpc calling is successful.
SUCCESS = "success"
// FAIL indicated the rpc calling is failed.
FAIL = "fail"
crosscoreRPCPrefix = "/rpc/"
)
// Response describes the response standard.
type Response struct {
Status string `json:"status,omitempty"`
Msg string `json:"msg,omitempty"`
Data interface{} `json:"data,omitempty"`
}
//NewSuccessResponse success response
func NewSuccessResponse(data interface{}) Response {
return Response{Status: SUCCESS, Data: data}
}
//NewErrorResponse error response
func NewErrorResponse(err error) Response {
return Response{Status: FAIL, Msg: err.Error()}
}
type waitHandler struct {
h http.Handler
wg sync.WaitGroup
}
func (wh *waitHandler) Set(h http.Handler) {
wh.h = h
wh.wg.Done()
}
func (wh *waitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
wh.wg.Wait()
wh.h.ServeHTTP(w, req)
}
// API is the scheduling center for server
type API struct {
sync *netsync.SyncManager
wallet *wallet.Wallet
accessTokens *accesstoken.CredentialStore
chain *protocol.Chain
server *http.Server
handler http.Handler
txFeedTracker *txfeed.Tracker
cpuMiner *cpuminer.CPUMiner
miningPool *miningpool.MiningPool
}
func (a *API) initServer(config *cfg.Config) {
// The waitHandler accepts incoming requests, but blocks until its underlying
// handler is set, when the second phase is complete.
var coreHandler waitHandler
var handler http.Handler
coreHandler.wg.Add(1)
mux := http.NewServeMux()
mux.Handle("/", &coreHandler)
handler = mux
if config.Auth.Disable == false {
handler = AuthHandler(handler, a.accessTokens)
}
handler = RedirectHandler(handler)
secureheader.DefaultConfig.PermitClearLoopback = true
secureheader.DefaultConfig.HTTPSRedirect = false
secureheader.DefaultConfig.Next = handler
a.server = &http.Server{
// Note: we should not set TLSConfig here;
// we took care of TLS with the listener in maybeUseTLS.
Handler: secureheader.DefaultConfig,
ReadTimeout: httpReadTimeout,
WriteTimeout: httpWriteTimeout,
// Disable HTTP/2 for now until the Go implementation is more stable.
// https://github.com/golang/go/issues/16450
// https://github.com/golang/go/issues/17071
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
}
coreHandler.Set(a)
}
// StartServer start the server
func (a *API) StartServer(address string) {
log.WithField("api address:", address).Info("Rpc listen")
listener, err := net.Listen("tcp", address)
if err != nil {
cmn.Exit(cmn.Fmt("Failed to register tcp port: %v", err))
}
// The `Serve` call has to happen in its own goroutine because
// it's blocking and we need to proceed to the rest of the core setup after
// we call it.
go func() {
if err := a.server.Serve(listener); err != nil {
log.WithField("error", errors.Wrap(err, "Serve")).Error("Rpc server")
}
}()
}
// NewAPI create and initialize the API
func NewAPI(sync *netsync.SyncManager, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, cpuMiner *cpuminer.CPUMiner, miningPool *miningpool.MiningPool, chain *protocol.Chain, config *cfg.Config, token *accesstoken.CredentialStore) *API {
api := &API{
sync: sync,
wallet: wallet,
chain: chain,
accessTokens: token,
txFeedTracker: txfeeds,
cpuMiner: cpuMiner,
miningPool: miningPool,
}
api.buildHandler()
api.initServer(config)
return api
}
func (a *API) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
a.handler.ServeHTTP(rw, req)
}
// buildHandler is in charge of all the rpc handling.
func (a *API) buildHandler() {
walletEnable := false
m := http.NewServeMux()
if a.wallet != nil {
walletEnable = true
m.Handle("/create-account", jsonHandler(a.createAccount))
m.Handle("/list-accounts", jsonHandler(a.listAccounts))
m.Handle("/delete-account", jsonHandler(a.deleteAccount))
m.Handle("/create-account-receiver", jsonHandler(a.createAccountReceiver))
m.Handle("/list-addresses", jsonHandler(a.listAddresses))
m.Handle("/validate-address", jsonHandler(a.validateAddress))
m.Handle("/create-asset", jsonHandler(a.createAsset))
m.Handle("/update-asset-alias", jsonHandler(a.updateAssetAlias))
m.Handle("/get-asset", jsonHandler(a.getAsset))
m.Handle("/list-assets", jsonHandler(a.listAssets))
m.Handle("/create-key", jsonHandler(a.pseudohsmCreateKey))
m.Handle("/list-keys", jsonHandler(a.pseudohsmListKeys))
m.Handle("/delete-key", jsonHandler(a.pseudohsmDeleteKey))
m.Handle("/reset-key-password", jsonHandler(a.pseudohsmResetPassword))
m.Handle("/build-transaction", jsonHandler(a.build))
m.Handle("/sign-transaction", jsonHandler(a.pseudohsmSignTemplates))
m.Handle("/submit-transaction", jsonHandler(a.submit))
m.Handle("/estimate-transaction-gas", jsonHandler(a.estimateTxGas))
m.Handle("/get-transaction", jsonHandler(a.getTransaction))
m.Handle("/list-transactions", jsonHandler(a.listTransactions))
m.Handle("/list-balances", jsonHandler(a.listBalances))
m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
m.Handle("/backup-wallet", jsonHandler(a.backupWalletImage))
m.Handle("/restore-wallet", jsonHandler(a.restoreWalletImage))
} else {
log.Warn("Please enable wallet")
}
m.Handle("/", alwaysError(errors.New("not Found")))
m.Handle("/error", jsonHandler(a.walletError))
m.Handle("/create-access-token", jsonHandler(a.createAccessToken))
m.Handle("/list-access-tokens", jsonHandler(a.listAccessTokens))
m.Handle("/delete-access-token", jsonHandler(a.deleteAccessToken))
m.Handle("/check-access-token", jsonHandler(a.checkAccessToken))
m.Handle("/create-transaction-feed", jsonHandler(a.createTxFeed))
m.Handle("/get-transaction-feed", jsonHandler(a.getTxFeed))
m.Handle("/update-transaction-feed", jsonHandler(a.updateTxFeed))
m.Handle("/delete-transaction-feed", jsonHandler(a.deleteTxFeed))
m.Handle("/list-transaction-feeds", jsonHandler(a.listTxFeeds))
m.Handle("/get-unconfirmed-transaction", jsonHandler(a.getUnconfirmedTx))
m.Handle("/list-unconfirmed-transactions", jsonHandler(a.listUnconfirmedTxs))
m.Handle("/get-block-hash", jsonHandler(a.getBestBlockHash))
m.Handle("/get-block-header", jsonHandler(a.getBlockHeader))
m.Handle("/get-block", jsonHandler(a.getBlock))
m.Handle("/get-block-count", jsonHandler(a.getBlockCount))
m.Handle("/get-difficulty", jsonHandler(a.getDifficulty))
m.Handle("/get-hash-rate", jsonHandler(a.getHashRate))
m.Handle("/is-mining", jsonHandler(a.isMining))
m.Handle("/set-mining", jsonHandler(a.setMining))
m.Handle("/get-work", jsonHandler(a.getWork))
m.Handle("/submit-work", jsonHandler(a.submitWork))
m.Handle("/gas-rate", jsonHandler(a.gasRate))
m.Handle("/net-info", jsonHandler(a.getNetInfo))
handler := latencyHandler(m, walletEnable)
handler = maxBytesHandler(handler) // TODO(tessr): consider moving this to non-core specific mux
handler = webAssetsHandler(handler)
handler = gzip.Handler{Handler: handler}
a.handler = handler
}
func maxBytesHandler(h http.Handler) http.Handler {
const maxReqSize = 1e7 // 10MB
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// A block can easily be bigger than maxReqSize, but everything
// else should be pretty small.
if req.URL.Path != crosscoreRPCPrefix+"signer/sign-block" {
req.Body = http.MaxBytesReader(w, req.Body, maxReqSize)
}
h.ServeHTTP(w, req)
})
}
// json Handler
func jsonHandler(f interface{}) http.Handler {
h, err := httpjson.Handler(f, errorFormatter.Write)
if err != nil {
panic(err)
}
return h
}
// error Handler
func alwaysError(err error) http.Handler {
return jsonHandler(func() error { return err })
}
func webAssetsHandler(next http.Handler) http.Handler {
mux := http.NewServeMux()
mux.Handle("/dashboard/", http.StripPrefix("/dashboard/", static.Handler{
Assets: dashboard.Files,
Default: "index.html",
}))
mux.Handle("/", next)
return mux
}
// AuthHandler access token auth Handler
func AuthHandler(handler http.Handler, accessTokens *accesstoken.CredentialStore) http.Handler {
authenticator := authn.NewAPI(accessTokens)
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// TODO(tessr): check that this path exists; return early if this path isn't legit
req, err := authenticator.Authenticate(req)
if err != nil {
log.WithField("error", errors.Wrap(err, "Serve")).Error("Authenticate fail")
err = errors.Sub(errNotAuthenticated, err)
errorFormatter.Write(req.Context(), rw, err)
return
}
handler.ServeHTTP(rw, req)
})
}
// RedirectHandler redirect to dashboard handler
func RedirectHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
http.Redirect(w, req, "/dashboard/", http.StatusFound)
return
}
next.ServeHTTP(w, req)
})
}
// latencyHandler take latency for the request url path, and redirect url path to wait-disable when wallet is closed
func latencyHandler(m *http.ServeMux, walletEnable bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// latency for the request url path
if l := latency(m, req); l != nil {
defer l.RecordSince(time.Now())
}
// when the wallet is not been opened and the url path is not been found, modify url path to error,
// and redirect handler to error
if _, pattern := m.Handler(req); pattern != req.URL.Path && !walletEnable {
req.URL.Path = "/error"
walletRedirectHandler(w, req)
return
}
m.ServeHTTP(w, req)
})
}
// walletRedirectHandler redirect to error when the wallet is closed
func walletRedirectHandler(w http.ResponseWriter, req *http.Request) {
h := http.RedirectHandler(req.URL.String(), http.StatusMovedPermanently)
h.ServeHTTP(w, req)
}