fix host parsing

This commit is contained in:
ginuerzh
2024-07-10 22:57:49 +08:00
parent f2e32080e4
commit 4a4c64cc66
11 changed files with 76 additions and 56 deletions

View File

@ -110,7 +110,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
}
if _, _, err := net.SplitHostPort(host); err != nil {
host = net.JoinHostPort(host, "0")
host = net.JoinHostPort(strings.Trim(host, "[]"), "0")
}
var target *chain.Node
@ -202,7 +202,7 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, remot
host := req.Host
if _, _, err := net.SplitHostPort(host); err != nil {
host = net.JoinHostPort(host, "80")
host = net.JoinHostPort(strings.Trim(host, "[]"), "80")
}
if bp := h.options.Bypass; bp != nil && bp.Contains(ctx, "tcp", host, bypass.WithPathOption(req.RequestURI)) {
log.Debugf("bypass: %s %s", host, req.RequestURI)

View File

@ -203,7 +203,7 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, remot
host := req.Host
if _, _, err := net.SplitHostPort(host); err != nil {
host = net.JoinHostPort(host, "80")
host = net.JoinHostPort(strings.Trim(host, "[]"), "80")
}
if bp := h.options.Bypass; bp != nil && bp.Contains(ctx, "tcp", host, bypass.WithPathOption(req.RequestURI)) {
log.Debugf("bypass: %s %s", host, req.RequestURI)

View File

@ -136,7 +136,7 @@ func (h *httpHandler) handleRequest(ctx context.Context, conn net.Conn, req *htt
addr := req.Host
if _, port, _ := net.SplitHostPort(addr); port == "" {
addr = net.JoinHostPort(addr, "80")
addr = net.JoinHostPort(strings.Trim(addr, "[]"), "80")
}
fields := map[string]any{

View File

@ -132,7 +132,7 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
addr := req.Host
if _, port, _ := net.SplitHostPort(addr); port == "" {
addr = net.JoinHostPort(addr, "80")
addr = net.JoinHostPort(strings.Trim(addr, "[]"), "80")
}
fields := map[string]any{

View File

@ -7,6 +7,7 @@ import (
"net"
"net/http"
"net/http/httputil"
"strings"
"time"
"github.com/go-gost/core/chain"
@ -88,7 +89,7 @@ func (h *http3Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
func (h *http3Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req *http.Request, log logger.Logger) error {
addr := req.Host
if _, port, _ := net.SplitHostPort(addr); port == "" {
addr = net.JoinHostPort(addr, "80")
addr = net.JoinHostPort(strings.Trim(addr, "[]"), "80")
}
if log.IsLevelEnabled(logger.TraceLevel) {

View File

@ -152,7 +152,7 @@ func (h *redirectHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, radd
host := req.Host
if _, _, err := net.SplitHostPort(host); err != nil {
host = net.JoinHostPort(host, "80")
host = net.JoinHostPort(strings.Trim(host, "[]"), "80")
}
log = log.WithFields(map[string]any{
"host": host,
@ -227,7 +227,7 @@ func (h *redirectHandler) handleHTTPS(ctx context.Context, rw io.ReadWriter, rad
if port == "" {
port = "443"
}
host = net.JoinHostPort(host, port)
host = net.JoinHostPort(strings.Trim(host, "[]"), port)
}
log = log.WithFields(map[string]any{
"host": host,
@ -263,7 +263,7 @@ func (h *redirectHandler) handleHTTPS(ctx context.Context, rw io.ReadWriter, rad
return nil
}
func (h *redirectHandler) getServerName(ctx context.Context, r io.Reader) (host string, err error) {
func (h *redirectHandler) getServerName(_ context.Context, r io.Reader) (host string, err error) {
record, err := dissector.ReadRecord(r)
if err != nil {
return

View File

@ -13,6 +13,7 @@ import (
"net"
"net/http"
"net/http/httputil"
"strings"
"time"
"github.com/go-gost/core/bypass"
@ -105,7 +106,7 @@ func (h *sniHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, raddr net
host := req.Host
if _, _, err := net.SplitHostPort(host); err != nil {
host = net.JoinHostPort(host, "80")
host = net.JoinHostPort(strings.Trim(host, "[]"), "80")
}
log = log.WithFields(map[string]any{
"host": host,
@ -171,7 +172,7 @@ func (h *sniHandler) handleHTTPS(ctx context.Context, rw io.ReadWriter, raddr ne
}
if _, _, err := net.SplitHostPort(host); err != nil {
host = net.JoinHostPort(host, "443")
host = net.JoinHostPort(strings.Trim(host, "[]"), "443")
}
log = log.WithFields(map[string]any{

View File

@ -132,7 +132,7 @@ func (ep *entrypoint) handle(ctx context.Context, conn net.Conn) error {
host := req.Host
if h, _, _ := net.SplitHostPort(host); h == "" {
host = net.JoinHostPort(host, "80")
host = net.JoinHostPort(strings.Trim(host, "[]"), "80")
}
if node == ep.node {