fix HTTP Host header

This commit is contained in:
ginuerzh 2019-06-03 16:14:49 +08:00
parent 49f2ee612a
commit 2e5601bfd6
5 changed files with 18 additions and 14 deletions

View File

@ -56,7 +56,7 @@ func (au *LocalAuthenticator) Add(k, v string) {
au.kvs[k] = v
}
// Reload parses config from r, then live reloads the bypass.
// Reload parses config from r, then live reloads the Authenticator.
func (au *LocalAuthenticator) Reload(r io.Reader) error {
var period time.Duration
kvs := make(map[string]string)

View File

@ -45,12 +45,10 @@ var (
// Load the certificate from cert and key files, will use the default certificate if the provided info are invalid.
func tlsConfig(certFile, keyFile string) (*tls.Config, error) {
if certFile == "" {
certFile = defaultCertFile
}
if keyFile == "" {
keyFile = defaultKeyFile
if certFile == "" || keyFile == "" {
certFile, keyFile = defaultCertFile, defaultKeyFile
}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err

View File

@ -5,10 +5,11 @@ import (
"errors"
"flag"
"fmt"
"net/http"
"os"
"runtime"
// _ "net/http/pprof"
_ "net/http/pprof"
"github.com/ginuerzh/gost"
"github.com/go-log/log"
@ -53,9 +54,11 @@ func init() {
}
func main() {
// go func() {
// log.Log(http.ListenAndServe("localhost:6060", nil))
// }()
if os.Getenv("PROFILING") != "" {
go func() {
log.Log(http.ListenAndServe("127.0.0.1:16060", nil))
}()
}
// NOTE: as of 2.6, you can use custom cert/key files to initialize the default certificate.
tlsConfig, err := tlsConfig(defaultCertFile, defaultKeyFile)

View File

@ -128,14 +128,14 @@ func (h *httpHandler) handleRequest(conn net.Conn, req *http.Request) {
return
}
host := req.Host
// try to get the actual host.
if v := req.Header.Get("Gost-Target"); v != "" {
if h, err := decodeServerName(v); err == nil {
host = h
req.Host = h
}
}
host := req.Host
if _, port, _ := net.SplitHostPort(host); port == "" {
host = net.JoinHostPort(host, "80")
}
@ -152,7 +152,6 @@ func (h *httpHandler) handleRequest(conn net.Conn, req *http.Request) {
log.Logf("[http] %s -> %s\n%s", conn.RemoteAddr(), conn.LocalAddr(), string(dump))
}
req.Host = host
req.Header.Del("Gost-Target")
resp := &http.Response{
@ -305,6 +304,8 @@ func (h *httpHandler) authenticate(conn net.Conn, req *http.Request, resp *http.
// probing resistance is enabled
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 {
resp.StatusCode = http.StatusServiceUnavailable // default status code
switch ss[0] {
case "code":
resp.StatusCode, _ = strconv.Atoi(ss[1])

View File

@ -463,6 +463,9 @@ func (h *http2Handler) authenticate(w http.ResponseWriter, r *http.Request, resp
// probing resistance is enabled
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 {
resp.StatusCode = http.StatusServiceUnavailable // default status code
w.Header().Del("Proxy-Agent")
switch ss[0] {
case "code":
resp.StatusCode, _ = strconv.Atoi(ss[1])
@ -502,7 +505,6 @@ func (h *http2Handler) authenticate(w http.ResponseWriter, r *http.Request, resp
resp.StatusCode = http.StatusProxyAuthRequired
resp.Header.Add("Proxy-Authenticate", "Basic realm=\"gost\"")
} else {
w.Header().Del("Proxy-Agent")
resp.Header = http.Header{}
resp.Header.Set("Server", "nginx/1.14.1")
resp.Header.Set("Date", time.Now().Format(http.TimeFormat))