jsonrpc: Work with browsers
This commit is contained in:
parent
c7b2bf8100
commit
93a8ee11db
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/filecoin-project/go-lotus/api"
|
"github.com/filecoin-project/go-lotus/api"
|
||||||
lcli "github.com/filecoin-project/go-lotus/cli"
|
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/lib/jsonrpc"
|
||||||
"github.com/filecoin-project/go-lotus/node"
|
"github.com/filecoin-project/go-lotus/node"
|
||||||
"github.com/filecoin-project/go-lotus/node/repo"
|
"github.com/filecoin-project/go-lotus/node/repo"
|
||||||
@ -72,7 +73,13 @@ var runCmd = &cli.Command{
|
|||||||
|
|
||||||
rpcServer := jsonrpc.NewServer()
|
rpcServer := jsonrpc.NewServer()
|
||||||
rpcServer.Register("Filecoin", api.PermissionedStorMinerAPI(minerapi))
|
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)
|
return http.ListenAndServe("127.0.0.1:"+cctx.String("api"), http.DefaultServeMux)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"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) {
|
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)
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
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) {
|
func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
if r.Header.Get("Connection") == "Upgrade" {
|
if strings.Contains(r.Header.Get("Connection"), "Upgrade") {
|
||||||
s.handleWS(ctx, w, r)
|
s.handleWS(ctx, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user