endpoints.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "context"
  19. "crypto/tls"
  20. "fmt"
  21. "net"
  22. "net/http"
  23. "time"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/plugin/security"
  26. "github.com/ethereum/go-ethereum/rpc"
  27. )
  28. // StartHTTPEndpoint starts the HTTP RPC endpoint.
  29. func StartHTTPEndpoint(endpoint string, timeouts rpc.HTTPTimeouts, handler http.Handler, tlsConfigSource security.TLSConfigurationSource) (*http.Server, net.Addr, bool, error) {
  30. // start the HTTP listener
  31. var (
  32. listener net.Listener
  33. err error
  34. isTlsEnabled bool
  35. )
  36. if isTlsEnabled, listener, err = startListener(endpoint, tlsConfigSource); err != nil {
  37. return nil, nil, isTlsEnabled, err
  38. }
  39. // make sure timeout values are meaningful
  40. CheckTimeouts(&timeouts)
  41. // Bundle and start the HTTP server
  42. httpSrv := &http.Server{
  43. Handler: handler,
  44. ReadTimeout: timeouts.ReadTimeout,
  45. WriteTimeout: timeouts.WriteTimeout,
  46. IdleTimeout: timeouts.IdleTimeout,
  47. // Ensure to Disable HTTP/2
  48. // this configuration and customized tls.Config is to follow: https://blog.bracebin.com/achieving-perfect-ssl-labs-score-with-go
  49. TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
  50. }
  51. go httpSrv.Serve(listener)
  52. return httpSrv, listener.Addr(), isTlsEnabled, err
  53. }
  54. // checkModuleAvailability checks that all names given in modules are actually
  55. // available API services. It assumes that the MetadataApi module ("rpc") is always available;
  56. // the registration of this "rpc" module happens in NewServer() and is thus common to all endpoints.
  57. func checkModuleAvailability(modules []string, apis []rpc.API) (bad, available []string) {
  58. availableSet := make(map[string]struct{})
  59. for _, api := range apis {
  60. if _, ok := availableSet[api.Namespace]; !ok {
  61. availableSet[api.Namespace] = struct{}{}
  62. available = append(available, api.Namespace)
  63. }
  64. }
  65. for _, name := range modules {
  66. if _, ok := availableSet[name]; !ok && name != rpc.MetadataApi {
  67. bad = append(bad, name)
  68. }
  69. }
  70. return bad, available
  71. }
  72. // Quorum
  73. // Produce net.Listener instance with TLS support if tlsConfigSource provides the config
  74. func startListener(endpoint string, tlsConfigSource security.TLSConfigurationSource) (bool, net.Listener, error) {
  75. var tlsConfig *tls.Config
  76. var err error
  77. var listener net.Listener
  78. isTlsEnabled := true
  79. if tlsConfigSource != nil {
  80. if tlsConfig, err = tlsConfigSource.Get(context.Background()); err != nil {
  81. isTlsEnabled = false
  82. }
  83. } else {
  84. isTlsEnabled = false
  85. err = fmt.Errorf("no TLSConfigurationSource found")
  86. }
  87. if isTlsEnabled {
  88. if listener, err = tls.Listen("tcp", endpoint, tlsConfig); err != nil {
  89. return isTlsEnabled, nil, err
  90. }
  91. } else {
  92. log.Info("Security: TLS not enabled", "endpoint", endpoint, "reason", err)
  93. if listener, err = net.Listen("tcp", endpoint); err != nil {
  94. return isTlsEnabled, nil, err
  95. }
  96. }
  97. return isTlsEnabled, listener, nil
  98. }
  99. // CheckTimeouts ensures that timeout values are meaningful
  100. func CheckTimeouts(timeouts *rpc.HTTPTimeouts) {
  101. if timeouts.ReadTimeout < time.Second {
  102. log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadTimeout)
  103. timeouts.ReadTimeout = rpc.DefaultHTTPTimeouts.ReadTimeout
  104. }
  105. if timeouts.WriteTimeout < time.Second {
  106. log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", rpc.DefaultHTTPTimeouts.WriteTimeout)
  107. timeouts.WriteTimeout = rpc.DefaultHTTPTimeouts.WriteTimeout
  108. }
  109. if timeouts.IdleTimeout < time.Second {
  110. log.Warn("Sanitizing invalid HTTP idle timeout", "provided", timeouts.IdleTimeout, "updated", rpc.DefaultHTTPTimeouts.IdleTimeout)
  111. timeouts.IdleTimeout = rpc.DefaultHTTPTimeouts.IdleTimeout
  112. }
  113. }