endpoints.go 3.4 KB

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