server.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2023 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package http
  15. import (
  16. "crypto/tls"
  17. "net"
  18. "net/http"
  19. "net/http/pprof"
  20. "strconv"
  21. "time"
  22. "github.com/gorilla/mux"
  23. "github.com/fatedier/frp/assets"
  24. v1 "github.com/fatedier/frp/pkg/config/v1"
  25. netpkg "github.com/fatedier/frp/pkg/util/net"
  26. )
  27. var (
  28. defaultReadTimeout = 60 * time.Second
  29. defaultWriteTimeout = 60 * time.Second
  30. )
  31. type Server struct {
  32. addr string
  33. ln net.Listener
  34. tlsCfg *tls.Config
  35. router *mux.Router
  36. hs *http.Server
  37. authMiddleware mux.MiddlewareFunc
  38. }
  39. func NewServer(cfg v1.WebServerConfig) (*Server, error) {
  40. if cfg.AssetsDir != "" {
  41. assets.Load(cfg.AssetsDir)
  42. }
  43. addr := net.JoinHostPort(cfg.Addr, strconv.Itoa(cfg.Port))
  44. if addr == ":" {
  45. addr = ":http"
  46. }
  47. ln, err := net.Listen("tcp", addr)
  48. if err != nil {
  49. return nil, err
  50. }
  51. router := mux.NewRouter()
  52. hs := &http.Server{
  53. Addr: addr,
  54. Handler: router,
  55. ReadTimeout: defaultReadTimeout,
  56. WriteTimeout: defaultWriteTimeout,
  57. }
  58. s := &Server{
  59. addr: addr,
  60. ln: ln,
  61. hs: hs,
  62. router: router,
  63. }
  64. if cfg.PprofEnable {
  65. s.registerPprofHandlers()
  66. }
  67. if cfg.TLS != nil {
  68. cert, err := tls.LoadX509KeyPair(cfg.TLS.CertFile, cfg.TLS.KeyFile)
  69. if err != nil {
  70. return nil, err
  71. }
  72. s.tlsCfg = &tls.Config{
  73. Certificates: []tls.Certificate{cert},
  74. }
  75. }
  76. s.authMiddleware = netpkg.NewHTTPAuthMiddleware(cfg.User, cfg.Password).SetAuthFailDelay(200 * time.Millisecond).Middleware
  77. return s, nil
  78. }
  79. func (s *Server) Address() string {
  80. return s.addr
  81. }
  82. func (s *Server) Run() error {
  83. ln := s.ln
  84. if s.tlsCfg != nil {
  85. ln = tls.NewListener(ln, s.tlsCfg)
  86. }
  87. return s.hs.Serve(ln)
  88. }
  89. func (s *Server) Close() error {
  90. return s.hs.Close()
  91. }
  92. type RouterRegisterHelper struct {
  93. Router *mux.Router
  94. AssetsFS http.FileSystem
  95. AuthMiddleware mux.MiddlewareFunc
  96. }
  97. func (s *Server) RouteRegister(register func(helper *RouterRegisterHelper)) {
  98. register(&RouterRegisterHelper{
  99. Router: s.router,
  100. AssetsFS: assets.FileSystem,
  101. AuthMiddleware: s.authMiddleware,
  102. })
  103. }
  104. func (s *Server) registerPprofHandlers() {
  105. s.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  106. s.router.HandleFunc("/debug/pprof/profile", pprof.Profile)
  107. s.router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  108. s.router.HandleFunc("/debug/pprof/trace", pprof.Trace)
  109. s.router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
  110. }