Refactor Mihomo proxy to support WebSocket upgrades and enhance alias forwarding
Publish telemt-api gateway Docker image / test (push) Successful in 29s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 2m17s

- 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:
Denozordec
2026-03-31 01:13:36 +07:00
parent b8bb5bede8
commit 0aa440477e
2 changed files with 45 additions and 9 deletions
+13 -1
View File
@@ -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"
}