github.com/klaytn/klaytn@v1.10.2/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 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  // StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
    57  func StartFastHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (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 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("FastHTTP registered", "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("tcp4", endpoint); err != nil {
    79  		return nil, nil, err
    80  	}
    81  	go NewFastHTTPServer(cors, vhosts, timeouts, handler).Serve(listener)
    82  	return listener, handler, err
    83  }
    84  
    85  // StartWSEndpoint starts a websocket endpoint
    86  func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
    87  	// Generate the whitelist based on the allowed modules
    88  	whitelist := make(map[string]bool)
    89  	for _, module := range modules {
    90  		whitelist[module] = true
    91  	}
    92  	// Register all the APIs exposed by the services
    93  	handler := NewServer()
    94  	for _, api := range apis {
    95  		if exposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
    96  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
    97  				return nil, nil, err
    98  			}
    99  			logger.Debug("WebSocket registered", "service", api.Service, "namespace", api.Namespace)
   100  		}
   101  	}
   102  	// All APIs registered, start the HTTP listener
   103  	var (
   104  		listener net.Listener
   105  		err      error
   106  	)
   107  	if listener, err = net.Listen("tcp", endpoint); err != nil {
   108  		return nil, nil, err
   109  	}
   110  	go NewWSServer(wsOrigins, handler).Serve(listener)
   111  	return listener, handler, err
   112  }
   113  
   114  func StartFastWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
   115  	// Generate the whitelist based on the allowed modules
   116  	whitelist := make(map[string]bool)
   117  	for _, module := range modules {
   118  		whitelist[module] = true
   119  	}
   120  	// Register all the APIs exposed by the services
   121  	handler := NewServer()
   122  	for _, api := range apis {
   123  		if exposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
   124  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
   125  				return nil, nil, err
   126  			}
   127  			logger.Debug("FastWebSocket registered", "service", api.Service, "namespace", api.Namespace)
   128  		}
   129  	}
   130  	// All APIs registered, start the HTTP listener
   131  	var (
   132  		listener net.Listener
   133  		err      error
   134  	)
   135  	if listener, err = net.Listen("tcp4", endpoint); err != nil {
   136  		return nil, nil, err
   137  	}
   138  	go NewFastWSServer(wsOrigins, handler).Serve(listener)
   139  	return listener, handler, err
   140  }
   141  
   142  // StartIPCEndpoint starts an IPC endpoint.
   143  func StartIPCEndpoint(ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
   144  	// Register all the APIs exposed by the services.
   145  	handler := NewServer()
   146  	for _, api := range apis {
   147  		if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
   148  			return nil, nil, err
   149  		}
   150  		logger.Debug("IPC registered", "namespace", api.Namespace)
   151  	}
   152  	// All APIs registered, start the IPC listener.
   153  	listener, err := ipcListen(ipcEndpoint)
   154  	if err != nil {
   155  		return nil, nil, err
   156  	}
   157  	go handler.ServeListener(listener)
   158  	return listener, handler, nil
   159  }