|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "net/http" |
| 6 | + "net/http/httputil" |
| 7 | + "net/url" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "go.opentelemetry.io/otel" |
| 13 | + "go.opentelemetry.io/otel/attribute" |
| 14 | + "go.opentelemetry.io/otel/propagation" |
| 15 | + "go.opentelemetry.io/otel/trace" |
| 16 | + |
| 17 | + "github.com/wuhan005/NekoBox/internal/conf" |
| 18 | + "github.com/wuhan005/NekoBox/internal/context" |
| 19 | +) |
| 20 | + |
| 21 | +func Proxy(ctx context.Context) error { |
| 22 | + span := trace.SpanFromContext(ctx.Request().Context()) |
| 23 | + |
| 24 | + var userID uint |
| 25 | + if ctx.IsLogged { |
| 26 | + userID = ctx.User.ID |
| 27 | + } |
| 28 | + |
| 29 | + if span.SpanContext().IsValid() { |
| 30 | + span.SetAttributes( |
| 31 | + attribute.Int("nekobox.service.user-id", int(userID)), |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + uri := ctx.Param("**") |
| 36 | + basePath := strings.Split(uri, "/")[0] |
| 37 | + forwardPath := strings.TrimPrefix(uri, basePath) |
| 38 | + |
| 39 | + var forwardURLStr string |
| 40 | + for _, backend := range conf.Service.Backends { |
| 41 | + if backend.Prefix == basePath { |
| 42 | + forwardURLStr = backend.ForwardURL |
| 43 | + break |
| 44 | + } |
| 45 | + } |
| 46 | + if len(forwardURLStr) == 0 { |
| 47 | + return ctx.JSONError(http.StatusNotFound, "页面不存在") |
| 48 | + } |
| 49 | + |
| 50 | + forwardURL, err := url.Parse(forwardURLStr) |
| 51 | + if err != nil { |
| 52 | + logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to parse forward URL") |
| 53 | + return ctx.JSONError(http.StatusInternalServerError, "服务网关内部错误") |
| 54 | + } |
| 55 | + |
| 56 | + reverseProxy := httputil.ReverseProxy{ |
| 57 | + Director: func(req *http.Request) { |
| 58 | + req.URL = forwardURL |
| 59 | + req.URL.Path = strings.TrimRight(req.URL.Path, "/") + forwardPath |
| 60 | + req.Host = forwardURL.Host |
| 61 | + |
| 62 | + traceHeaders := http.Header{} |
| 63 | + otel.GetTextMapPropagator().Inject(ctx.Request().Context(), propagation.HeaderCarrier(traceHeaders)) |
| 64 | + for key := range traceHeaders { |
| 65 | + req.Header.Set(key, traceHeaders.Get(key)) |
| 66 | + } |
| 67 | + |
| 68 | + req.Header.Set("X-NekoBox-From", "nekobox-gateway") |
| 69 | + req.Header.Set("X-NekoBox-User-ID", strconv.Itoa(int(userID))) |
| 70 | + }, |
| 71 | + Transport: &http.Transport{ |
| 72 | + TLSClientConfig: &tls.Config{ |
| 73 | + InsecureSkipVerify: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + ErrorHandler: func(writer http.ResponseWriter, request *http.Request, err error) { |
| 77 | + logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to handle reverse proxy request") |
| 78 | + _ = ctx.JSONError(http.StatusInternalServerError, "服务网关内部错误") |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + reverseProxy.ServeHTTP(ctx.ResponseWriter(), ctx.Request().Request) |
| 83 | + return nil |
| 84 | +} |
0 commit comments