forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsapi.go
239 lines (202 loc) · 6.26 KB
/
wsapi.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package wsapi
import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/FactomProject/btcutil/certs"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
var Servers map[string]*Server
var ServersMutex sync.Mutex
func Start(state interfaces.IState) {
RegisterPrometheus()
ServersMutex.Lock()
defer ServersMutex.Unlock()
if Servers == nil {
Servers = make(map[string]*Server)
}
port := strconv.Itoa(state.GetPort())
if Servers[port] == nil {
server := InitServer(state)
server.AddRootEndpoints()
server.AddV1Endpoints()
server.AddV2Endpoints()
Servers[port] = server
rpcUser := state.GetRpcUser()
rpcPass := state.GetRpcPass()
h := sha256.New()
h.Write(httpBasicAuth(rpcUser, rpcPass))
// TODO verify if there already runs a Server on the port, this code isn't executed, would change behavior.
state.SetRpcAuthHash(h.Sum(nil)) //set this in the beginning to prevent timing attacks
server.Start()
}
}
func Stop(state interfaces.IState) {
ServersMutex.Lock()
defer ServersMutex.Unlock()
port := strconv.Itoa(state.GetPort())
Servers[port].Stop()
}
func SetState(state interfaces.IState) {
wait := func() {
port := strconv.Itoa(state.GetPort())
ServersMutex.Lock()
defer ServersMutex.Unlock()
//todo: Should wait() instead of sleep but that requires plumbing a wait group....
for Servers == nil && Servers[port] != nil && Servers[port].State != nil {
ServersMutex.Unlock()
time.Sleep(10 * time.Millisecond)
ServersMutex.Lock()
}
if Servers == nil {
wsLog.Error("Got here early need synchronization")
}
if Servers[port] == nil {
wsLog.Error("Got here early need synchronization")
}
if Servers[port].State == nil {
wsLog.Error("Got here early need synchronization")
}
Servers[port].State = state
}
go wait()
}
func GetState(r *http.Request) (state interfaces.IState, err error) {
ServersMutex.Lock()
defer ServersMutex.Unlock()
port := r.Header.Get("factomd-port")
if server, ok := Servers[port]; ok {
return server.State, nil
} else {
return nil, errors.New(fmt.Sprintf("failed to get state, server initialization on port: %s failed", port))
}
}
/*********************************************************
* Support Functions
*********************************************************/
func returnMsg(writer http.ResponseWriter, msg interface{}, success bool) {
r := msg
if p, err := json.Marshal(r); err != nil {
wsLog.Error(err)
return
} else {
_, err := writer.Write(p)
if err != nil {
wsLog.Errorf("failed to write response: %v", err)
}
}
}
func returnV1Msg(writer http.ResponseWriter, msg string, success bool) {
/* V1 requires call specific case changes that can't be handled with
interfaces for example. Block Height needs to return height as the json item name
in golang, lower case names are private so won't be returned.
Deal with the responses in the call specific v1 handlers until they are depricated.
*/
bMsg := []byte(msg)
_, err := writer.Write(bMsg)
if err != nil {
wsLog.Errorf("failed to write v1 repsonse: %v", err)
}
}
func handleV1Error(writer http.ResponseWriter, err *primitives.JSONError) {
writer.WriteHeader(http.StatusBadRequest)
return
}
func returnV1(writer http.ResponseWriter, jsonResp *primitives.JSON2Response, jsonError *primitives.JSONError) {
if jsonError != nil {
handleV1Error(writer, jsonError)
return
}
returnMsg(writer, jsonResp.Result, true)
}
// httpBasicAuth returns the UTF-8 bytes of the HTTP Basic authentication
// string:
//
// "Basic " + base64(username + ":" + password)
func httpBasicAuth(username, password string) []byte {
const header = "Basic "
base64 := base64.StdEncoding
b64InputLen := len(username) + len(":") + len(password)
b64Input := make([]byte, 0, b64InputLen)
b64Input = append(b64Input, username...)
b64Input = append(b64Input, ':')
b64Input = append(b64Input, password...)
output := make([]byte, len(header)+base64.EncodedLen(b64InputLen))
copy(output, header)
base64.Encode(output[len(header):], b64Input)
return output
}
func checkAuthHeader(state interfaces.IState, request *http.Request) error {
if "" == state.GetRpcUser() {
//no username was specified in the config file or command line, meaning factomd API is open access
return nil
}
authhdr := request.Header["Authorization"]
if len(authhdr) == 0 {
return errors.New("no auth")
}
correctAuth := state.GetRpcAuthHash()
h := sha256.New()
h.Write([]byte(authhdr[0]))
presentedPassHash := h.Sum(nil)
cmp := subtle.ConstantTimeCompare(presentedPassHash, correctAuth) //compare hashes because ConstantTimeCompare takes a constant time based on the slice size. hashing gives a constant slice size.
if cmp != 1 {
return errors.New("bad auth")
}
return nil
}
func handleUnauthorized(request *http.Request, writer http.ResponseWriter) {
remoteIP := ""
remoteIP += strings.Split(request.RemoteAddr, ":")[0]
wsLog.Debugf("Unauthorized V2 API client connection attempt from %s\n", remoteIP)
writer.Header().Add("WWW-Authenticate", `Basic realm="factomd RPC"`)
http.Error(writer, "401 Unauthorized.", http.StatusUnauthorized)
}
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func genCertPair(certFile string, keyFile string, extraAddress string) error {
wsLog.Info("Generating TLS certificates...")
org := "factom autogenerated cert"
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
var externalAddresses []string
if extraAddress != "" {
externalAddresses = strings.Split(extraAddress, ",")
for _, i := range externalAddresses {
wsLog.Infof("adding %s to certificate\n", i)
}
}
cert, key, err := certs.NewTLSCertPair(org, validUntil, externalAddresses)
if err != nil {
return err
}
// Write cert and key files.
if err = ioutil.WriteFile(certFile, cert, 0666); err != nil {
return err
}
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile)
return err
}
wsLog.Info("Done generating TLS certificates")
return nil
}