mirror of https://github.com/fatedier/frp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.1 KiB
116 lines
3.1 KiB
7 years ago
|
// 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"
|
||
|
)
|
||
|
|
||
5 years ago
|
type HTTPAuthWraper struct {
|
||
7 years ago
|
h http.Handler
|
||
|
user string
|
||
|
passwd string
|
||
|
}
|
||
|
|
||
5 years ago
|
func NewHTTPBasicAuthWraper(h http.Handler, user, passwd string) http.Handler {
|
||
|
return &HTTPAuthWraper{
|
||
7 years ago
|
h: h,
|
||
|
user: user,
|
||
|
passwd: passwd,
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func (aw *HTTPAuthWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
7 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
type HTTPAuthMiddleware struct {
|
||
7 years ago
|
user string
|
||
|
passwd string
|
||
|
}
|
||
|
|
||
5 years ago
|
func NewHTTPAuthMiddleware(user, passwd string) *HTTPAuthMiddleware {
|
||
|
return &HTTPAuthMiddleware{
|
||
7 years ago
|
user: user,
|
||
|
passwd: passwd,
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func (authMid *HTTPAuthMiddleware) Middleware(next http.Handler) http.Handler {
|
||
7 years ago
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
7 years ago
|
reqUser, reqPasswd, hasAuth := r.BasicAuth()
|
||
7 years ago
|
if (authMid.user == "" && authMid.passwd == "") ||
|
||
|
(hasAuth && reqUser == authMid.user && reqPasswd == authMid.passwd) {
|
||
|
next.ServeHTTP(w, r)
|
||
7 years ago
|
} else {
|
||
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||
|
}
|
||
7 years ago
|
})
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
func HTTPBasicAuth(h http.HandlerFunc, user, passwd string) http.HandlerFunc {
|
||
7 years ago
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
7 years ago
|
reqUser, reqPasswd, hasAuth := r.BasicAuth()
|
||
|
if (user == "" && passwd == "") ||
|
||
|
(hasAuth && reqUser == user && reqPasswd == passwd) {
|
||
7 years ago
|
h.ServeHTTP(w, r)
|
||
7 years ago
|
} else {
|
||
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
type HTTPGzipWraper struct {
|
||
7 years ago
|
h http.Handler
|
||
|
}
|
||
|
|
||
5 years ago
|
func (gw *HTTPGzipWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
7 years ago
|
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)
|
||
|
}
|
||
|
|
||
5 years ago
|
func MakeHTTPGzipHandler(h http.Handler) http.Handler {
|
||
|
return &HTTPGzipWraper{
|
||
7 years ago
|
h: h,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type gzipResponseWriter struct {
|
||
|
io.Writer
|
||
|
http.ResponseWriter
|
||
|
}
|
||
|
|
||
|
func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
||
|
return w.Writer.Write(b)
|
||
|
}
|