frp/pkg/util/net/http.go

116 lines
3.0 KiB
Go
Raw Normal View History

// Copyright 2017 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package net
import (
"compress/gzip"
"io"
"net/http"
"strings"
2023-05-29 00:27:27 +08:00
"time"
"github.com/fatedier/frp/pkg/util/util"
)
2023-11-22 14:30:22 +08:00
type HTTPAuthWrapper struct {
h http.Handler
user string
passwd string
}
2023-11-22 14:30:22 +08:00
func NewHTTPBasicAuthWrapper(h http.Handler, user, passwd string) http.Handler {
return &HTTPAuthWrapper{
h: h,
user: user,
passwd: passwd,
}
}
2023-11-22 14:30:22 +08:00
func (aw *HTTPAuthWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user, passwd, hasAuth := r.BasicAuth()
if (aw.user == "" && aw.passwd == "") || (hasAuth && user == aw.user && passwd == aw.passwd) {
aw.h.ServeHTTP(w, r)
} else {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
}
2020-05-24 17:48:37 +08:00
type HTTPAuthMiddleware struct {
2023-05-29 00:27:27 +08:00
user string
passwd string
authFailDelay time.Duration
2018-05-20 19:06:05 +08:00
}
2020-05-24 17:48:37 +08:00
func NewHTTPAuthMiddleware(user, passwd string) *HTTPAuthMiddleware {
return &HTTPAuthMiddleware{
2018-05-20 19:06:05 +08:00
user: user,
passwd: passwd,
}
}
2023-05-29 00:27:27 +08:00
func (authMid *HTTPAuthMiddleware) SetAuthFailDelay(delay time.Duration) *HTTPAuthMiddleware {
authMid.authFailDelay = delay
return authMid
}
2020-05-24 17:48:37 +08:00
func (authMid *HTTPAuthMiddleware) Middleware(next http.Handler) http.Handler {
2018-05-20 19:06:05 +08:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqUser, reqPasswd, hasAuth := r.BasicAuth()
2018-05-20 19:06:05 +08:00
if (authMid.user == "" && authMid.passwd == "") ||
2023-05-29 00:27:27 +08:00
(hasAuth && util.ConstantTimeEqString(reqUser, authMid.user) &&
util.ConstantTimeEqString(reqPasswd, authMid.passwd)) {
2018-05-20 19:06:05 +08:00
next.ServeHTTP(w, r)
} else {
2023-05-29 00:27:27 +08:00
if authMid.authFailDelay > 0 {
time.Sleep(authMid.authFailDelay)
}
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
2018-05-20 19:06:05 +08:00
})
}
2023-11-22 14:30:22 +08:00
type HTTPGzipWrapper struct {
h http.Handler
}
2023-11-22 14:30:22 +08:00
func (gw *HTTPGzipWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
gw.h.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
gw.h.ServeHTTP(gzr, r)
}
2020-05-24 17:48:37 +08:00
func MakeHTTPGzipHandler(h http.Handler) http.Handler {
2023-11-22 14:30:22 +08:00
return &HTTPGzipWrapper{
h: h,
}
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}