add obfuscation support for SNI

This commit is contained in:
rui.zheng
2017-10-25 17:40:57 +08:00
parent 6a65b32b71
commit 65c0ce36bc
12 changed files with 6419 additions and 99 deletions
+21 -7
View File
@@ -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