mirror of
https://github.com/ginuerzh/gost.git
synced 2024-04-21 12:31:34 +00:00
add obfuscation support for SNI
This commit is contained in:
@@ -134,6 +134,16 @@ func (h *httpHandler) Handle(conn net.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
// try to get the actual host.
|
||||
if req.Host != "" {
|
||||
if index := strings.IndexByte(req.Host, '.'); index > 0 {
|
||||
// try to decode the prefix
|
||||
if name, err := decodeServerName(req.Host[:index]); err == nil {
|
||||
req.Host = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// forward http request
|
||||
lastNode := h.options.Chain.LastNode()
|
||||
if req.Method != http.MethodConnect && lastNode.Protocol == "http" {
|
||||
@@ -142,17 +152,18 @@ func (h *httpHandler) Handle(conn net.Conn) {
|
||||
}
|
||||
|
||||
host := req.Host
|
||||
if !strings.Contains(req.Host, ":") {
|
||||
if !strings.Contains(host, ":") {
|
||||
host += ":80"
|
||||
}
|
||||
|
||||
cc, err := h.options.Chain.Dial(host)
|
||||
if err != nil {
|
||||
log.Logf("[http] %s -> %s : %s", conn.RemoteAddr(), req.Host, err)
|
||||
log.Logf("[http] %s -> %s : %s", conn.RemoteAddr(), host, err)
|
||||
|
||||
b := []byte("HTTP/1.1 503 Service unavailable\r\n" +
|
||||
"Proxy-Agent: gost/" + Version + "\r\n\r\n")
|
||||
if Debug {
|
||||
log.Logf("[http] %s <- %s\n%s", conn.RemoteAddr(), req.Host, string(b))
|
||||
log.Logf("[http] %s <- %s\n%s", conn.RemoteAddr(), host, string(b))
|
||||
}
|
||||
conn.Write(b)
|
||||
return
|
||||
@@ -163,21 +174,21 @@ func (h *httpHandler) Handle(conn net.Conn) {
|
||||
b := []byte("HTTP/1.1 200 Connection established\r\n" +
|
||||
"Proxy-Agent: gost/" + Version + "\r\n\r\n")
|
||||
if Debug {
|
||||
log.Logf("[http] %s <- %s\n%s", conn.RemoteAddr(), req.Host, string(b))
|
||||
log.Logf("[http] %s <- %s\n%s", conn.RemoteAddr(), host, string(b))
|
||||
}
|
||||
conn.Write(b)
|
||||
} else {
|
||||
req.Header.Del("Proxy-Connection")
|
||||
|
||||
if err = req.Write(cc); err != nil {
|
||||
log.Logf("[http] %s -> %s : %s", conn.RemoteAddr(), req.Host, err)
|
||||
log.Logf("[http] %s -> %s : %s", conn.RemoteAddr(), host, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log.Logf("[http] %s <-> %s", cc.LocalAddr(), req.Host)
|
||||
log.Logf("[http] %s <-> %s", cc.LocalAddr(), host)
|
||||
transport(conn, cc)
|
||||
log.Logf("[http] %s >-< %s", cc.LocalAddr(), req.Host)
|
||||
log.Logf("[http] %s >-< %s", cc.LocalAddr(), host)
|
||||
}
|
||||
|
||||
func (h *httpHandler) forwardRequest(conn net.Conn, req *http.Request) {
|
||||
@@ -210,6 +221,9 @@ func (h *httpHandler) forwardRequest(conn net.Conn, req *http.Request) {
|
||||
}
|
||||
|
||||
cc.SetWriteDeadline(time.Now().Add(WriteTimeout))
|
||||
if !req.URL.IsAbs() {
|
||||
req.URL.Scheme = "http" // make sure that the URL is absolute
|
||||
}
|
||||
if err = req.WriteProxy(cc); err != nil {
|
||||
log.Logf("[http] %s -> %s : %s", conn.RemoteAddr(), req.Host, err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user