2021-06-15 17:13:40 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-09-30 16:30:07 +08:00
|
|
|
"fmt"
|
2021-06-15 17:13:40 +08:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
2023-12-02 17:07:36 +08:00
|
|
|
"github.com/metacubex/mihomo/common/lru"
|
2023-11-03 21:01:45 +08:00
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
authStore "github.com/metacubex/mihomo/listener/auth"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2021-06-15 17:13:40 +08:00
|
|
|
)
|
|
|
|
|
2023-12-02 17:07:36 +08:00
|
|
|
func HandleConn(c net.Conn, tunnel C.Tunnel, cache *lru.LruCache[string, bool], additions ...inbound.Addition) {
|
2023-10-23 16:45:22 +08:00
|
|
|
client := newClient(c, tunnel, additions...)
|
2021-06-15 17:13:40 +08:00
|
|
|
defer client.CloseIdleConnections()
|
|
|
|
|
2022-04-25 19:50:20 +08:00
|
|
|
conn := N.NewBufferedConn(c)
|
2021-06-15 17:13:40 +08:00
|
|
|
|
|
|
|
keepAlive := true
|
2023-12-02 17:07:36 +08:00
|
|
|
trusted := cache == nil // disable authenticate if lru is nil
|
2021-06-15 17:13:40 +08:00
|
|
|
|
|
|
|
for keepAlive {
|
2021-08-20 23:38:47 +08:00
|
|
|
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 {
|
|
|
|
resp = authenticate(request, cache)
|
|
|
|
|
|
|
|
trusted = resp == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if trusted {
|
|
|
|
if request.Method == http.MethodConnect {
|
2021-09-30 16:30:07 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
tunnel.HandleTCPConn(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 = ""
|
|
|
|
|
2022-04-25 19:50:20 +08:00
|
|
|
if isUpgradeRequest(request) {
|
2023-09-28 18:59:31 +08:00
|
|
|
handleUpgrade(conn, request, tunnel, additions...)
|
2022-04-25 19:50:20 +08:00
|
|
|
|
|
|
|
return // hijack connection
|
2022-04-25 19:50:20 +08:00
|
|
|
}
|
|
|
|
|
2021-09-14 00:08:23 +08:00
|
|
|
removeHopByHopHeaders(request.Header)
|
|
|
|
removeExtraHTTPHostPort(request)
|
2021-06-15 17:13:40 +08:00
|
|
|
|
|
|
|
if request.URL.Scheme == "" || request.URL.Host == "" {
|
2021-09-30 16:30:07 +08:00
|
|
|
resp = responseWith(request, http.StatusBadRequest)
|
2021-06-15 17:13:40 +08:00
|
|
|
} else {
|
|
|
|
resp, err = client.Do(request)
|
|
|
|
if err != nil {
|
2021-09-30 16:30:07 +08:00
|
|
|
resp = responseWith(request, http.StatusBadGateway)
|
2021-06-15 17:13:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 00:08:23 +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
|
|
|
}
|
|
|
|
|
2023-12-02 17:07:36 +08:00
|
|
|
func authenticate(request *http.Request, cache *lru.LruCache[string, bool]) *http.Response {
|
2021-06-15 17:13:40 +08:00
|
|
|
authenticator := authStore.Authenticator()
|
2023-10-11 22:54:19 +08:00
|
|
|
if inbound.SkipAuthRemoteAddress(request.RemoteAddr) {
|
2023-10-10 19:43:26 +08:00
|
|
|
authenticator = nil
|
|
|
|
}
|
2021-06-15 17:13:40 +08:00
|
|
|
if authenticator != nil {
|
2021-09-14 00:08:23 +08:00
|
|
|
credential := parseBasicProxyAuthorization(request)
|
2021-06-15 17:13:40 +08:00
|
|
|
if credential == "" {
|
2021-09-30 16:30:07 +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 {
|
2021-09-14 00:08:23 +08:00
|
|
|
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)
|
|
|
|
|
2021-09-30 16:30:07 +08:00
|
|
|
return responseWith(request, http.StatusForbidden)
|
2021-06-15 17:13:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-30 16:30:07 +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),
|
2021-09-30 16:30:07 +08:00
|
|
|
Proto: request.Proto,
|
|
|
|
ProtoMajor: request.ProtoMajor,
|
|
|
|
ProtoMinor: request.ProtoMinor,
|
2021-06-15 17:13:40 +08:00
|
|
|
Header: http.Header{},
|
|
|
|
}
|
|
|
|
}
|