github.com/klaytn/klaytn@v1.12.1/networks/rpc/endpoints.go (about)

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