github.com/julesgoullee/go-ethereum@v1.9.7/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 "github.com/ethereum/go-ethereum/log" 23 ) 24 25 // StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules 26 func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) { 27 // Generate the whitelist based on the allowed modules 28 whitelist := make(map[string]bool) 29 for _, module := range modules { 30 whitelist[module] = true 31 } 32 // Register all the APIs exposed by the services 33 handler := NewServer() 34 for _, api := range apis { 35 if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) { 36 if err := handler.RegisterName(api.Namespace, api.Service); err != nil { 37 return nil, nil, err 38 } 39 log.Debug("HTTP registered", "namespace", api.Namespace) 40 } 41 } 42 // All APIs registered, start the HTTP listener 43 var ( 44 listener net.Listener 45 err error 46 ) 47 if listener, err = net.Listen("tcp", endpoint); err != nil { 48 return nil, nil, err 49 } 50 go NewHTTPServer(cors, vhosts, timeouts, handler).Serve(listener) 51 return listener, handler, err 52 } 53 54 // StartWSEndpoint starts a websocket endpoint 55 func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) { 56 57 // Generate the whitelist based on the allowed modules 58 whitelist := make(map[string]bool) 59 for _, module := range modules { 60 whitelist[module] = true 61 } 62 // Register all the APIs exposed by the services 63 handler := NewServer() 64 for _, api := range apis { 65 if exposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) { 66 if err := handler.RegisterName(api.Namespace, api.Service); err != nil { 67 return nil, nil, err 68 } 69 log.Debug("WebSocket registered", "service", api.Service, "namespace", api.Namespace) 70 } 71 } 72 // All APIs registered, start the HTTP listener 73 var ( 74 listener net.Listener 75 err error 76 ) 77 if listener, err = net.Listen("tcp", endpoint); err != nil { 78 return nil, nil, err 79 } 80 go NewWSServer(wsOrigins, handler).Serve(listener) 81 return listener, handler, err 82 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 log.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 }