This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
/
routes.go
205 lines (182 loc) · 9.36 KB
/
routes.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
package api
import (
"net/http"
"strings"
"time"
"github.com/NebulousLabs/Sia/build"
"github.com/julienschmidt/httprouter"
)
// buildHttpRoutes sets up and returns an * httprouter.Router.
// it connected the Router to the given api using the required
// parameters: requiredUserAgent and requiredPassword
func (api *API) buildHTTPRoutes(requiredUserAgent string, requiredPassword string) {
router := httprouter.New()
router.NotFound = http.HandlerFunc(UnrecognizedCallHandler)
router.RedirectTrailingSlash = false
// Consensus API Calls
if api.cs != nil {
router.GET("/consensus", api.consensusHandler)
router.GET("/consensus/blocks", api.consensusBlocksHandler)
router.POST("/consensus/validate/transactionset", api.consensusValidateTransactionsetHandler)
}
// Explorer API Calls
if api.explorer != nil {
router.GET("/explorer", api.explorerHandler)
router.GET("/explorer/blocks/:height", api.explorerBlocksHandler)
router.GET("/explorer/hashes/:hash", api.explorerHashHandler)
}
// Gateway API Calls
if api.gateway != nil {
router.GET("/gateway", api.gatewayHandler)
router.POST("/gateway/connect/:netaddress", RequirePassword(api.gatewayConnectHandler, requiredPassword))
router.POST("/gateway/disconnect/:netaddress", RequirePassword(api.gatewayDisconnectHandler, requiredPassword))
}
// Host API Calls
if api.host != nil {
// Calls directly pertaining to the host.
router.GET("/host", api.hostHandlerGET) // Get the host status.
router.POST("/host", RequirePassword(api.hostHandlerPOST, requiredPassword)) // Change the settings of the host.
router.POST("/host/announce", RequirePassword(api.hostAnnounceHandler, requiredPassword)) // Announce the host to the network.
router.GET("/host/contracts", api.hostContractInfoHandler) // Get info about contracts.
router.GET("/host/estimatescore", api.hostEstimateScoreGET)
// Calls pertaining to the storage manager that the host uses.
router.GET("/host/storage", api.storageHandler)
router.POST("/host/storage/folders/add", RequirePassword(api.storageFoldersAddHandler, requiredPassword))
router.POST("/host/storage/folders/remove", RequirePassword(api.storageFoldersRemoveHandler, requiredPassword))
router.POST("/host/storage/folders/resize", RequirePassword(api.storageFoldersResizeHandler, requiredPassword))
router.POST("/host/storage/sectors/delete/:merkleroot", RequirePassword(api.storageSectorsDeleteHandler, requiredPassword))
}
// Miner API Calls
if api.miner != nil {
router.GET("/miner", api.minerHandler)
router.GET("/miner/header", RequirePassword(api.minerHeaderHandlerGET, requiredPassword))
router.POST("/miner/header", RequirePassword(api.minerHeaderHandlerPOST, requiredPassword))
router.GET("/miner/start", RequirePassword(api.minerStartHandler, requiredPassword))
router.GET("/miner/stop", RequirePassword(api.minerStopHandler, requiredPassword))
}
// Renter API Calls
if api.renter != nil {
router.GET("/renter", api.renterHandlerGET)
router.POST("/renter", RequirePassword(api.renterHandlerPOST, requiredPassword))
router.GET("/renter/contracts", api.renterContractsHandler)
router.GET("/renter/downloads", api.renterDownloadsHandler)
router.POST("/renter/downloads/clear", RequirePassword(api.renterClearDownloadsHandler, requiredPassword))
router.GET("/renter/files", api.renterFilesHandler)
router.GET("/renter/file/*siapath", api.renterFileHandler)
router.GET("/renter/prices", api.renterPricesHandler)
// TODO: re-enable these routes once the new .sia format has been
// standardized and implemented.
// router.POST("/renter/load", RequirePassword(api.renterLoadHandler, requiredPassword))
// router.POST("/renter/loadascii", RequirePassword(api.renterLoadAsciiHandler, requiredPassword))
// router.GET("/renter/share", RequirePassword(api.renterShareHandler, requiredPassword))
// router.GET("/renter/shareascii", RequirePassword(api.renterShareAsciiHandler, requiredPassword))
router.POST("/renter/delete/*siapath", RequirePassword(api.renterDeleteHandler, requiredPassword))
router.GET("/renter/download/*siapath", RequirePassword(api.renterDownloadHandler, requiredPassword))
router.GET("/renter/downloadasync/*siapath", RequirePassword(api.renterDownloadAsyncHandler, requiredPassword))
router.POST("/renter/rename/*siapath", RequirePassword(api.renterRenameHandler, requiredPassword))
router.GET("/renter/stream/*siapath", api.renterStreamHandler)
router.POST("/renter/upload/*siapath", RequirePassword(api.renterUploadHandler, requiredPassword))
// HostDB endpoints.
router.GET("/hostdb", api.hostdbHandler)
router.GET("/hostdb/active", api.hostdbActiveHandler)
router.GET("/hostdb/all", api.hostdbAllHandler)
router.GET("/hostdb/hosts/:pubkey", api.hostdbHostsHandler)
}
// Transaction pool API Calls
if api.tpool != nil {
router.GET("/tpool/fee", api.tpoolFeeHandlerGET)
router.GET("/tpool/raw/:id", api.tpoolRawHandlerGET)
router.POST("/tpool/raw", api.tpoolRawHandlerPOST)
router.GET("/tpool/confirmed/:id", api.tpoolConfirmedGET)
// TODO: re-enable this route once the transaction pool API has been finalized
//router.GET("/transactionpool/transactions", api.transactionpoolTransactionsHandler)
}
// Wallet API Calls
if api.wallet != nil {
router.GET("/wallet", api.walletHandler)
router.POST("/wallet/033x", RequirePassword(api.wallet033xHandler, requiredPassword))
router.GET("/wallet/address", RequirePassword(api.walletAddressHandler, requiredPassword))
router.GET("/wallet/addresses", api.walletAddressesHandler)
router.GET("/wallet/backup", RequirePassword(api.walletBackupHandler, requiredPassword))
router.POST("/wallet/init", RequirePassword(api.walletInitHandler, requiredPassword))
router.POST("/wallet/init/seed", RequirePassword(api.walletInitSeedHandler, requiredPassword))
router.POST("/wallet/lock", RequirePassword(api.walletLockHandler, requiredPassword))
router.POST("/wallet/seed", RequirePassword(api.walletSeedHandler, requiredPassword))
router.GET("/wallet/seeds", RequirePassword(api.walletSeedsHandler, requiredPassword))
router.POST("/wallet/siacoins", RequirePassword(api.walletSiacoinsHandler, requiredPassword))
router.POST("/wallet/siafunds", RequirePassword(api.walletSiafundsHandler, requiredPassword))
router.POST("/wallet/siagkey", RequirePassword(api.walletSiagkeyHandler, requiredPassword))
router.POST("/wallet/sweep/seed", RequirePassword(api.walletSweepSeedHandler, requiredPassword))
router.GET("/wallet/transaction/:id", api.walletTransactionHandler)
router.GET("/wallet/transactions", api.walletTransactionsHandler)
router.GET("/wallet/transactions/:addr", api.walletTransactionsAddrHandler)
router.GET("/wallet/verify/address/:addr", api.walletVerifyAddressHandler)
router.POST("/wallet/unlock", RequirePassword(api.walletUnlockHandler, requiredPassword))
router.POST("/wallet/changepassword", RequirePassword(api.walletChangePasswordHandler, requiredPassword))
}
// Apply UserAgent middleware and return the Router
api.router = cleanCloseHandler(RequireUserAgent(router, requiredUserAgent))
return
}
// cleanCloseHandler wraps the entire API, ensuring that underlying conns are
// not leaked if the remote end closes the connection before the underlying
// handler finishes.
func cleanCloseHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Close this file handle either when the function completes or when the
// connection is done.
done := make(chan struct{})
go func(w http.ResponseWriter, r *http.Request) {
defer close(done)
next.ServeHTTP(w, r)
}(w, r)
select {
case <-done:
}
// Sanity check - thread should not take more than an hour to return. This
// must be done in a goroutine, otherwise the server will not close the
// underlying socket for this API call.
timer := time.NewTimer(time.Minute * 60)
go func() {
select {
case <-done:
timer.Stop()
case <-timer.C:
build.Severe("api call is taking more than 60 minutes to return:", r.URL.Path)
}
}()
})
}
// RequireUserAgent is middleware that requires all requests to set a
// UserAgent that contains the specified string.
func RequireUserAgent(h http.Handler, ua string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if !strings.Contains(req.UserAgent(), ua) && !isUnrestricted(req) {
WriteError(w, Error{"Browser access disabled due to security vulnerability. Use Sia-UI or siac."}, http.StatusBadRequest)
return
}
h.ServeHTTP(w, req)
})
}
// RequirePassword is middleware that requires a request to authenticate with a
// password using HTTP basic auth. Usernames are ignored. Empty passwords
// indicate no authentication is required.
func RequirePassword(h httprouter.Handle, password string) httprouter.Handle {
// An empty password is equivalent to no password.
if password == "" {
return h
}
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
_, pass, ok := req.BasicAuth()
if !ok || pass != password {
w.Header().Set("WWW-Authenticate", "Basic realm=\"SiaAPI\"")
WriteError(w, Error{"API authentication failed."}, http.StatusUnauthorized)
return
}
h(w, req, ps)
}
}
// isUnrestricted checks if a request may bypass the useragent check.
func isUnrestricted(req *http.Request) bool {
return strings.HasPrefix(req.URL.Path, "/renter/stream/")
}