endpoints.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 node
  17. import (
  18. "net"
  19. "net/http"
  20. "time"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/rpc"
  23. )
  24. // StartHTTPEndpoint starts the HTTP RPC endpoint.
  25. func StartHTTPEndpoint(endpoint string, timeouts rpc.HTTPTimeouts, handler http.Handler) (net.Listener, error) {
  26. // start the HTTP listener
  27. var (
  28. listener net.Listener
  29. err error
  30. )
  31. if listener, err = net.Listen("tcp", endpoint); err != nil {
  32. return nil, err
  33. }
  34. // make sure timeout values are meaningful
  35. CheckTimeouts(&timeouts)
  36. // Bundle and start the HTTP server
  37. httpSrv := &http.Server{
  38. Handler: handler,
  39. ReadTimeout: timeouts.ReadTimeout,
  40. WriteTimeout: timeouts.WriteTimeout,
  41. IdleTimeout: timeouts.IdleTimeout,
  42. }
  43. go httpSrv.Serve(listener)
  44. return listener, err
  45. }
  46. // startWSEndpoint starts a websocket endpoint.
  47. func startWSEndpoint(endpoint string, handler http.Handler) (net.Listener, error) {
  48. // start the HTTP listener
  49. var (
  50. listener net.Listener
  51. err error
  52. )
  53. if listener, err = net.Listen("tcp", endpoint); err != nil {
  54. return nil, err
  55. }
  56. wsSrv := &http.Server{Handler: handler}
  57. go wsSrv.Serve(listener)
  58. return listener, err
  59. }
  60. // checkModuleAvailability checks that all names given in modules are actually
  61. // available API services. It assumes that the MetadataApi module ("rpc") is always available;
  62. // the registration of this "rpc" module happens in NewServer() and is thus common to all endpoints.
  63. func checkModuleAvailability(modules []string, apis []rpc.API) (bad, available []string) {
  64. availableSet := make(map[string]struct{})
  65. for _, api := range apis {
  66. if _, ok := availableSet[api.Namespace]; !ok {
  67. availableSet[api.Namespace] = struct{}{}
  68. available = append(available, api.Namespace)
  69. }
  70. }
  71. for _, name := range modules {
  72. if _, ok := availableSet[name]; !ok && name != rpc.MetadataApi {
  73. bad = append(bad, name)
  74. }
  75. }
  76. return bad, available
  77. }
  78. // CheckTimeouts ensures that timeout values are meaningful
  79. func CheckTimeouts(timeouts *rpc.HTTPTimeouts) {
  80. if timeouts.ReadTimeout < time.Second {
  81. log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadTimeout)
  82. timeouts.ReadTimeout = rpc.DefaultHTTPTimeouts.ReadTimeout
  83. }
  84. if timeouts.WriteTimeout < time.Second {
  85. log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", rpc.DefaultHTTPTimeouts.WriteTimeout)
  86. timeouts.WriteTimeout = rpc.DefaultHTTPTimeouts.WriteTimeout
  87. }
  88. if timeouts.IdleTimeout < time.Second {
  89. log.Warn("Sanitizing invalid HTTP idle timeout", "provided", timeouts.IdleTimeout, "updated", rpc.DefaultHTTPTimeouts.IdleTimeout)
  90. timeouts.IdleTimeout = rpc.DefaultHTTPTimeouts.IdleTimeout
  91. }
  92. }