mihomo/listener/http/proxy.go

137 lines
3.3 KiB
Go
Raw Permalink Normal View History

2021-06-15 17:13:40 +08:00
package http
import (
"fmt"
2021-06-15 17:13:40 +08:00
"net"
"net/http"
"strings"
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/common/cache"
N "github.com/Dreamacro/clash/common/net"
C "github.com/Dreamacro/clash/constant"
authStore "github.com/Dreamacro/clash/listener/auth"
"github.com/Dreamacro/clash/log"
)
func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool], additions ...inbound.Addition) {
client := newClient(c.RemoteAddr(), in, additions...)
2021-06-15 17:13:40 +08:00
defer client.CloseIdleConnections()
conn := N.NewBufferedConn(c)
2021-06-15 17:13:40 +08:00
keepAlive := true
trusted := cache == nil // disable authenticate if cache is nil
for keepAlive {
request, err := ReadRequest(conn.Reader())
2021-06-15 17:13:40 +08:00
if err != nil {
break
}
request.RemoteAddr = conn.RemoteAddr().String()
keepAlive = strings.TrimSpace(strings.ToLower(request.Header.Get("Proxy-Connection"))) == "keep-alive"
var resp *http.Response
if !trusted {
2022-04-10 03:59:27 +08:00
resp = Authenticate(request, cache)
2021-06-15 17:13:40 +08:00
trusted = resp == nil
}
if trusted {
if request.Method == http.MethodConnect {
// Manual writing to support CONNECT for http 1.0 (workaround for uplay client)
if _, err = fmt.Fprintf(conn, "HTTP/%d.%d %03d %s\r\n\r\n", request.ProtoMajor, request.ProtoMinor, http.StatusOK, "Connection established"); err != nil {
2021-06-15 17:13:40 +08:00
break // close connection
}
in <- inbound.NewHTTPS(request, conn, additions...)
2021-06-15 17:13:40 +08:00
return // hijack connection
}
host := request.Header.Get("Host")
if host != "" {
request.Host = host
}
request.RequestURI = ""
if isUpgradeRequest(request) {
handleUpgrade(conn, request, in, additions...)
return // hijack connection
}
2022-04-10 03:59:27 +08:00
RemoveHopByHopHeaders(request.Header)
RemoveExtraHTTPHostPort(request)
2021-06-15 17:13:40 +08:00
if request.URL.Scheme == "" || request.URL.Host == "" {
2022-04-10 03:59:27 +08:00
resp = ResponseWith(request, http.StatusBadRequest)
2021-06-15 17:13:40 +08:00
} else {
resp, err = client.Do(request)
if err != nil {
2022-04-10 03:59:27 +08:00
resp = ResponseWith(request, http.StatusBadGateway)
2021-06-15 17:13:40 +08:00
}
}
2022-04-10 03:59:27 +08:00
RemoveHopByHopHeaders(resp.Header)
}
2021-06-15 17:13:40 +08:00
if keepAlive {
resp.Header.Set("Proxy-Connection", "keep-alive")
resp.Header.Set("Connection", "keep-alive")
resp.Header.Set("Keep-Alive", "timeout=4")
}
resp.Close = !keepAlive
err = resp.Write(conn)
if err != nil {
break // close connection
}
}
2022-04-27 05:14:03 +08:00
_ = conn.Close()
2021-06-15 17:13:40 +08:00
}
2022-04-10 03:59:27 +08:00
func Authenticate(request *http.Request, cache *cache.LruCache[string, bool]) *http.Response {
2021-06-15 17:13:40 +08:00
authenticator := authStore.Authenticator()
if authenticator != nil {
credential := parseBasicProxyAuthorization(request)
2021-06-15 17:13:40 +08:00
if credential == "" {
2022-04-10 03:59:27 +08:00
resp := ResponseWith(request, http.StatusProxyAuthRequired)
2021-06-15 17:13:40 +08:00
resp.Header.Set("Proxy-Authenticate", "Basic")
return resp
}
2022-08-17 11:43:13 +08:00
authed, exist := cache.Get(credential)
if !exist {
user, pass, err := decodeBasicProxyAuthorization(credential)
2021-06-15 17:13:40 +08:00
authed = err == nil && authenticator.Verify(user, pass)
2022-08-17 11:43:13 +08:00
cache.Set(credential, authed)
2021-06-15 17:13:40 +08:00
}
2022-04-05 23:29:52 +08:00
if !authed {
2021-06-15 17:13:40 +08:00
log.Infoln("Auth failed from %s", request.RemoteAddr)
2022-04-10 03:59:27 +08:00
return ResponseWith(request, http.StatusForbidden)
2021-06-15 17:13:40 +08:00
}
}
return nil
}
2022-04-10 03:59:27 +08:00
func ResponseWith(request *http.Request, statusCode int) *http.Response {
2021-06-15 17:13:40 +08:00
return &http.Response{
StatusCode: statusCode,
Status: http.StatusText(statusCode),
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
2021-06-15 17:13:40 +08:00
Header: http.Header{},
}
}