增加connect host伪装

This commit is contained in:
wenyifan
2026-06-28 12:41:23 +08:00
parent 3993c95700
commit cb5d5c590b
2 changed files with 28 additions and 2 deletions
+24 -2
View File
@@ -2,9 +2,12 @@ package http
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"hash/crc32"
"net"
"net/http"
"net/http/httputil"
@@ -53,10 +56,16 @@ func (c *httpConnector) Connect(ctx context.Context, conn net.Conn, network, add
})
log.Debugf("connect %s/%s", address, network)
connectHost := address
if c.md.host != "" {
// 伪装的地址
connectHost = c.md.host
}
req := &http.Request{
Method: http.MethodConnect,
URL: &url.URL{Host: address},
Host: address,
URL: &url.URL{Host: connectHost},
Host: connectHost,
ProtoMajor: 1,
ProtoMinor: 1,
Header: c.md.header,
@@ -67,6 +76,12 @@ func (c *httpConnector) Connect(ctx context.Context, conn net.Conn, network, add
}
req.Header.Set("Proxy-Connection", "keep-alive")
// 真实连接的地址
if c.md.host != "" {
targetAddr := encodeServerName(address)
req.Header.Set("Gost-Target", targetAddr)
}
if user := c.options.Auth; user != nil {
u := user.Username()
p, _ := user.Password()
@@ -129,3 +144,10 @@ func (c *httpConnector) Connect(ctx context.Context, conn net.Conn, network, add
return conn, nil
}
func encodeServerName(name string) string {
buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, crc32.ChecksumIEEE([]byte(name)))
buf.WriteString(base64.RawURLEncoding.EncodeToString([]byte(name)))
return base64.RawURLEncoding.EncodeToString(buf.Bytes())
}