endpoints.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // checkModuleAvailability checks that all names given in modules are actually
  22. // available API services. It assumes that the MetadataApi module ("rpc") is always available;
  23. // the registration of this "rpc" module happens in NewServer() and is thus common to all endpoints.
  24. func checkModuleAvailability(modules []string, apis []API) (bad, available []string) {
  25. availableSet := make(map[string]struct{})
  26. for _, api := range apis {
  27. if _, ok := availableSet[api.Namespace]; !ok {
  28. availableSet[api.Namespace] = struct{}{}
  29. available = append(available, api.Namespace)
  30. }
  31. }
  32. for _, name := range modules {
  33. if _, ok := availableSet[name]; !ok && name != MetadataApi {
  34. bad = append(bad, name)
  35. }
  36. }
  37. return bad, available
  38. }
  39. // StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules.
  40. func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
  41. if bad, available := checkModuleAvailability(modules, apis); len(bad) > 0 {
  42. log.Error("Unavailable modules in HTTP API list", "unavailable", bad, "available", available)
  43. }
  44. // Generate the whitelist based on the allowed modules
  45. whitelist := make(map[string]bool)
  46. for _, module := range modules {
  47. whitelist[module] = true
  48. }
  49. // Register all the APIs exposed by the services
  50. handler := NewServer()
  51. for _, api := range apis {
  52. if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
  53. if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
  54. return nil, nil, err
  55. }
  56. log.Debug("HTTP registered", "namespace", api.Namespace)
  57. }
  58. }
  59. // All APIs registered, start the HTTP listener
  60. var (
  61. listener net.Listener
  62. err error
  63. )
  64. if listener, err = net.Listen("tcp", endpoint); err != nil {
  65. return nil, nil, err
  66. }
  67. go NewHTTPServer(cors, vhosts, timeouts, handler).Serve(listener)
  68. return listener, handler, err
  69. }
  70. // StartWSEndpoint starts a websocket endpoint.
  71. func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
  72. if bad, available := checkModuleAvailability(modules, apis); len(bad) > 0 {
  73. log.Error("Unavailable modules in WS API list", "unavailable", bad, "available", available)
  74. }
  75. // Generate the whitelist based on the allowed modules
  76. whitelist := make(map[string]bool)
  77. for _, module := range modules {
  78. whitelist[module] = true
  79. }
  80. // Register all the APIs exposed by the services
  81. handler := NewServer()
  82. for _, api := range apis {
  83. if exposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
  84. if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
  85. return nil, nil, err
  86. }
  87. log.Debug("WebSocket registered", "service", api.Service, "namespace", api.Namespace)
  88. }
  89. }
  90. // All APIs registered, start the HTTP listener
  91. var (
  92. listener net.Listener
  93. err error
  94. )
  95. if listener, err = net.Listen("tcp", endpoint); err != nil {
  96. return nil, nil, err
  97. }
  98. go NewWSServer(wsOrigins, handler).Serve(listener)
  99. return listener, handler, err
  100. }
  101. // StartIPCEndpoint starts an IPC endpoint.
  102. func StartIPCEndpoint(ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
  103. // Register all the APIs exposed by the services.
  104. handler := NewServer()
  105. for _, api := range apis {
  106. if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
  107. return nil, nil, err
  108. }
  109. log.Debug("IPC registered", "namespace", api.Namespace)
  110. }
  111. // All APIs registered, start the IPC listener.
  112. listener, err := ipcListen(ipcEndpoint)
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. go handler.ServeListener(listener)
  117. return listener, handler, nil
  118. }