jsonrpc: Work with browsers

This commit is contained in:
Łukasz Magiera 2019-07-24 19:09:00 +02:00
parent c7b2bf8100
commit 93a8ee11db
2 changed files with 23 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/filecoin-project/go-lotus/api"
lcli "github.com/filecoin-project/go-lotus/cli"
"github.com/filecoin-project/go-lotus/lib/auth"
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
"github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/node/repo"
@ -72,7 +73,13 @@ var runCmd = &cli.Command{
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", api.PermissionedStorMinerAPI(minerapi))
http.Handle("/rpc/v0", rpcServer)
ah := &auth.Handler{
Verify: minerapi.AuthVerify,
Next: rpcServer.ServeHTTP,
}
http.Handle("/rpc/v0", ah)
return http.ListenAndServe("127.0.0.1:"+cctx.String("api"), http.DefaultServeMux)
},
}

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"net/http"
"strings"
"github.com/gorilla/websocket"
)
@ -27,9 +28,21 @@ func NewServer() *RPCServer {
}
}
var upgrader = websocket.Upgrader{}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func (s *RPCServer) handleWS(ctx context.Context, w http.ResponseWriter, r *http.Request) {
// TODO: allow setting
// (note that we still are mostly covered by jwt tokens)
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Header.Get("Sec-WebSocket-Protocol") != "" {
w.Header().Set("Sec-WebSocket-Protocol", r.Header.Get("Sec-WebSocket-Protocol"))
}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error(err)
@ -52,7 +65,7 @@ func (s *RPCServer) handleWS(ctx context.Context, w http.ResponseWriter, r *http
func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Header.Get("Connection") == "Upgrade" {
if strings.Contains(r.Header.Get("Connection"), "Upgrade") {
s.handleWS(ctx, w, r)
return
}