http.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 HTTPAuthWraper struct {
  24. h http.Handler
  25. user string
  26. passwd string
  27. }
  28. func NewHTTPBasicAuthWraper(h http.Handler, user, passwd string) http.Handler {
  29. return &HTTPAuthWraper{
  30. h: h,
  31. user: user,
  32. passwd: passwd,
  33. }
  34. }
  35. func (aw *HTTPAuthWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  36. user, passwd, hasAuth := r.BasicAuth()
  37. if (aw.user == "" && aw.passwd == "") || (hasAuth && user == aw.user && passwd == aw.passwd) {
  38. aw.h.ServeHTTP(w, r)
  39. } else {
  40. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  41. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  42. }
  43. }
  44. type HTTPAuthMiddleware struct {
  45. user string
  46. passwd string
  47. authFailDelay time.Duration
  48. }
  49. func NewHTTPAuthMiddleware(user, passwd string) *HTTPAuthMiddleware {
  50. return &HTTPAuthMiddleware{
  51. user: user,
  52. passwd: passwd,
  53. }
  54. }
  55. func (authMid *HTTPAuthMiddleware) SetAuthFailDelay(delay time.Duration) *HTTPAuthMiddleware {
  56. authMid.authFailDelay = delay
  57. return authMid
  58. }
  59. func (authMid *HTTPAuthMiddleware) Middleware(next http.Handler) http.Handler {
  60. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  61. reqUser, reqPasswd, hasAuth := r.BasicAuth()
  62. if (authMid.user == "" && authMid.passwd == "") ||
  63. (hasAuth && util.ConstantTimeEqString(reqUser, authMid.user) &&
  64. util.ConstantTimeEqString(reqPasswd, authMid.passwd)) {
  65. next.ServeHTTP(w, r)
  66. } else {
  67. if authMid.authFailDelay > 0 {
  68. time.Sleep(authMid.authFailDelay)
  69. }
  70. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  71. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  72. }
  73. })
  74. }
  75. type HTTPGzipWraper struct {
  76. h http.Handler
  77. }
  78. func (gw *HTTPGzipWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  79. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  80. gw.h.ServeHTTP(w, r)
  81. return
  82. }
  83. w.Header().Set("Content-Encoding", "gzip")
  84. gz := gzip.NewWriter(w)
  85. defer gz.Close()
  86. gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
  87. gw.h.ServeHTTP(gzr, r)
  88. }
  89. func MakeHTTPGzipHandler(h http.Handler) http.Handler {
  90. return &HTTPGzipWraper{
  91. h: h,
  92. }
  93. }
  94. type gzipResponseWriter struct {
  95. io.Writer
  96. http.ResponseWriter
  97. }
  98. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  99. return w.Writer.Write(b)
  100. }