git.pirl.io/community/pirl@v0.0.0-20201111064343-9d3d31ff74be/rpc/endpoints.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import (
    20  	"net"
    21  
    22  	"git.pirl.io/community/pirl/log"
    23  )
    24  
    25  // checkModuleAvailability check that all names given in modules are actually
    26  // available API services.
    27  func checkModuleAvailability(modules []string, apis []API) (bad, available []string) {
    28  	availableSet := make(map[string]struct{})
    29  	for _, api := range apis {
    30  		if _, ok := availableSet[api.Namespace]; !ok {
    31  			availableSet[api.Namespace] = struct{}{}
    32  			available = append(available, api.Namespace)
    33  		}
    34  	}
    35  	for _, name := range modules {
    36  		if _, ok := availableSet[name]; !ok {
    37  			bad = append(bad, name)
    38  		}
    39  	}
    40  	return bad, available
    41  }
    42  
    43  // StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules.
    44  func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
    45  	if bad, available := checkModuleAvailability(modules, apis); len(bad) > 0 {
    46  		log.Error("Unavailable modules in HTTP API list", "unavailable", bad, "available", available)
    47  	}
    48  	// Generate the whitelist based on the allowed modules
    49  	whitelist := make(map[string]bool)
    50  	for _, module := range modules {
    51  		whitelist[module] = true
    52  	}
    53  	// Register all the APIs exposed by the services
    54  	handler := NewServer()
    55  	for _, api := range apis {
    56  		if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
    57  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
    58  				return nil, nil, err
    59  			}
    60  			log.Debug("HTTP registered", "namespace", api.Namespace)
    61  		}
    62  	}
    63  	// All APIs registered, start the HTTP listener
    64  	var (
    65  		listener net.Listener
    66  		err      error
    67  	)
    68  	if listener, err = net.Listen("tcp", endpoint); err != nil {
    69  		return nil, nil, err
    70  	}
    71  	go NewHTTPServer(cors, vhosts, timeouts, handler).Serve(listener)
    72  	return listener, handler, err
    73  }
    74  
    75  // StartWSEndpoint starts a websocket endpoint.
    76  func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
    77  	if bad, available := checkModuleAvailability(modules, apis); len(bad) > 0 {
    78  		log.Error("Unavailable modules in WS API list", "unavailable", bad, "available", available)
    79  	}
    80  	// Generate the whitelist based on the allowed modules
    81  	whitelist := make(map[string]bool)
    82  	for _, module := range modules {
    83  		whitelist[module] = true
    84  	}
    85  	// Register all the APIs exposed by the services
    86  	handler := NewServer()
    87  	for _, api := range apis {
    88  		if exposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
    89  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
    90  				return nil, nil, err
    91  			}
    92  			log.Debug("WebSocket registered", "service", api.Service, "namespace", api.Namespace)
    93  		}
    94  	}
    95  	// All APIs registered, start the HTTP listener
    96  	var (
    97  		listener net.Listener
    98  		err      error
    99  	)
   100  	if listener, err = net.Listen("tcp", endpoint); err != nil {
   101  		return nil, nil, err
   102  	}
   103  	go NewWSServer(wsOrigins, handler).Serve(listener)
   104  	return listener, handler, err
   105  
   106  }
   107  
   108  // StartIPCEndpoint starts an IPC endpoint.
   109  func StartIPCEndpoint(ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
   110  	// Register all the APIs exposed by the services.
   111  	handler := NewServer()
   112  	for _, api := range apis {
   113  		if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
   114  			return nil, nil, err
   115  		}
   116  		log.Debug("IPC registered", "namespace", api.Namespace)
   117  	}
   118  	// All APIs registered, start the IPC listener.
   119  	listener, err := ipcListen(ipcEndpoint)
   120  	if err != nil {
   121  		return nil, nil, err
   122  	}
   123  	go handler.ServeListener(listener)
   124  	return listener, handler, nil
   125  }