server.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. assets.Load(cfg.AssetsDir)
  41. addr := net.JoinHostPort(cfg.Addr, strconv.Itoa(cfg.Port))
  42. if addr == ":" {
  43. addr = ":http"
  44. }
  45. ln, err := net.Listen("tcp", addr)
  46. if err != nil {
  47. return nil, err
  48. }
  49. router := mux.NewRouter()
  50. hs := &http.Server{
  51. Addr: addr,
  52. Handler: router,
  53. ReadTimeout: defaultReadTimeout,
  54. WriteTimeout: defaultWriteTimeout,
  55. }
  56. s := &Server{
  57. addr: addr,
  58. ln: ln,
  59. hs: hs,
  60. router: router,
  61. }
  62. if cfg.PprofEnable {
  63. s.registerPprofHandlers()
  64. }
  65. if cfg.TLS != nil {
  66. cert, err := tls.LoadX509KeyPair(cfg.TLS.CertFile, cfg.TLS.KeyFile)
  67. if err != nil {
  68. return nil, err
  69. }
  70. s.tlsCfg = &tls.Config{
  71. Certificates: []tls.Certificate{cert},
  72. }
  73. }
  74. s.authMiddleware = netpkg.NewHTTPAuthMiddleware(cfg.User, cfg.Password).SetAuthFailDelay(200 * time.Millisecond).Middleware
  75. return s, nil
  76. }
  77. func (s *Server) Address() string {
  78. return s.addr
  79. }
  80. func (s *Server) Run() error {
  81. ln := s.ln
  82. if s.tlsCfg != nil {
  83. ln = tls.NewListener(ln, s.tlsCfg)
  84. }
  85. return s.hs.Serve(ln)
  86. }
  87. func (s *Server) Close() error {
  88. return s.hs.Close()
  89. }
  90. type RouterRegisterHelper struct {
  91. Router *mux.Router
  92. AssetsFS http.FileSystem
  93. AuthMiddleware mux.MiddlewareFunc
  94. }
  95. func (s *Server) RouteRegister(register func(helper *RouterRegisterHelper)) {
  96. register(&RouterRegisterHelper{
  97. Router: s.router,
  98. AssetsFS: assets.FileSystem,
  99. AuthMiddleware: s.authMiddleware,
  100. })
  101. }
  102. func (s *Server) registerPprofHandlers() {
  103. s.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  104. s.router.HandleFunc("/debug/pprof/profile", pprof.Profile)
  105. s.router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  106. s.router.HandleFunc("/debug/pprof/trace", pprof.Trace)
  107. s.router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
  108. }