http.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  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 net
  15. import (
  16. "compress/gzip"
  17. "io"
  18. "net/http"
  19. "strings"
  20. "time"
  21. "github.com/fatedier/frp/pkg/util/util"
  22. )
  23. type HTTPAuthMiddleware struct {
  24. user string
  25. passwd string
  26. authFailDelay time.Duration
  27. }
  28. func NewHTTPAuthMiddleware(user, passwd string) *HTTPAuthMiddleware {
  29. return &HTTPAuthMiddleware{
  30. user: user,
  31. passwd: passwd,
  32. }
  33. }
  34. func (authMid *HTTPAuthMiddleware) SetAuthFailDelay(delay time.Duration) *HTTPAuthMiddleware {
  35. authMid.authFailDelay = delay
  36. return authMid
  37. }
  38. func (authMid *HTTPAuthMiddleware) Middleware(next http.Handler) http.Handler {
  39. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  40. reqUser, reqPasswd, hasAuth := r.BasicAuth()
  41. if (authMid.user == "" && authMid.passwd == "") ||
  42. (hasAuth && util.ConstantTimeEqString(reqUser, authMid.user) &&
  43. util.ConstantTimeEqString(reqPasswd, authMid.passwd)) {
  44. next.ServeHTTP(w, r)
  45. } else {
  46. if authMid.authFailDelay > 0 {
  47. time.Sleep(authMid.authFailDelay)
  48. }
  49. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  50. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  51. }
  52. })
  53. }
  54. type HTTPGzipWrapper struct {
  55. h http.Handler
  56. }
  57. func (gw *HTTPGzipWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  58. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  59. gw.h.ServeHTTP(w, r)
  60. return
  61. }
  62. w.Header().Set("Content-Encoding", "gzip")
  63. gz := gzip.NewWriter(w)
  64. defer gz.Close()
  65. gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
  66. gw.h.ServeHTTP(gzr, r)
  67. }
  68. func MakeHTTPGzipHandler(h http.Handler) http.Handler {
  69. return &HTTPGzipWrapper{
  70. h: h,
  71. }
  72. }
  73. type gzipResponseWriter struct {
  74. io.Writer
  75. http.ResponseWriter
  76. }
  77. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  78. return w.Writer.Write(b)
  79. }