2020-11-09 16:29:04 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2020 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-10-19 15:00:55 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/ipld-eth-server/pkg/prom"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StartWSEndpoint starts a websocket endpoint.
|
|
|
|
func StartWSEndpoint(endpoint string, apis []rpc.API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *rpc.Server, error) {
|
|
|
|
if bad, available := checkModuleAvailability(modules, apis); len(bad) > 0 {
|
|
|
|
log.Error("Unavailable modules in WS API list", "unavailable", bad, "available", available)
|
|
|
|
}
|
|
|
|
// Generate the whitelist based on the allowed modules
|
|
|
|
whitelist := make(map[string]bool)
|
|
|
|
for _, module := range modules {
|
|
|
|
whitelist[module] = true
|
|
|
|
}
|
|
|
|
// Register all the APIs exposed by the services
|
|
|
|
handler := rpc.NewServer()
|
|
|
|
for _, api := range apis {
|
|
|
|
if exposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
|
|
|
|
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
log.Debug("WebSocket registered", "service", api.Service, "namespace", api.Namespace)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// All APIs registered, start the HTTP listener
|
|
|
|
var (
|
|
|
|
listener net.Listener
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if listener, err = net.Listen("tcp", endpoint); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wsServer := rpc.NewWSServer(wsOrigins, handler)
|
|
|
|
wsServer.Handler = prom.WSMiddleware(wsServer.Handler)
|
|
|
|
go wsServer.Serve(listener)
|
|
|
|
|
|
|
|
return listener, handler, err
|
|
|
|
|
|
|
|
}
|