Refactor Mihomo proxy to support WebSocket upgrades and enhance alias forwarding
- Introduced a new function `newAliasForward` to handle alias remapping for users, improving the flexibility of the proxy. - Updated `NewMihomoForward` to differentiate between HTTP and WebSocket requests, ensuring proper handling of both types. - Enhanced comments for clarity on the proxy behavior and request handling, improving maintainability.
This commit is contained in:
@@ -17,6 +17,18 @@ func NewAliasForward(
|
||||
stripPrefix, pathPrefix, auth string,
|
||||
rt http.RoundTripper,
|
||||
errHandler func(http.ResponseWriter, *http.Request, error),
|
||||
) http.Handler {
|
||||
return newAliasForward(target, stripPrefix, pathPrefix, auth, rt, errHandler, true)
|
||||
}
|
||||
|
||||
// newAliasForward is the shared HTTP forwarder. remapUsers maps top-level "users" → "stats/users"
|
||||
// for Telemt; Mihomo must pass false so /users is not rewritten.
|
||||
func newAliasForward(
|
||||
target *url.URL,
|
||||
stripPrefix, pathPrefix, auth string,
|
||||
rt http.RoundTripper,
|
||||
errHandler func(http.ResponseWriter, *http.Request, error),
|
||||
remapUsers bool,
|
||||
) http.Handler {
|
||||
if errHandler == nil {
|
||||
errHandler = defaultForwardErrorHandler
|
||||
@@ -35,7 +47,7 @@ func NewAliasForward(
|
||||
}
|
||||
rest := strings.TrimPrefix(p, stripPrefix)
|
||||
rest = strings.TrimPrefix(rest, "/")
|
||||
if (r.Method == http.MethodGet || r.Method == http.MethodHead) && rest == "users" {
|
||||
if remapUsers && (r.Method == http.MethodGet || r.Method == http.MethodHead) && rest == "users" {
|
||||
rest = "stats/users"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user