-
Notifications
You must be signed in to change notification settings - Fork 34
route: service gateway #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||||||||||||||||||
| package service | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import ( | ||||||||||||||||||||||
| "crypto/tls" | ||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||
| "net/http/httputil" | ||||||||||||||||||||||
| "net/url" | ||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| "github.com/sirupsen/logrus" | ||||||||||||||||||||||
| "go.opentelemetry.io/otel" | ||||||||||||||||||||||
| "go.opentelemetry.io/otel/attribute" | ||||||||||||||||||||||
| "go.opentelemetry.io/otel/propagation" | ||||||||||||||||||||||
| "go.opentelemetry.io/otel/trace" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| "github.com/wuhan005/NekoBox/internal/conf" | ||||||||||||||||||||||
| "github.com/wuhan005/NekoBox/internal/context" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func Proxy(ctx context.Context) error { | ||||||||||||||||||||||
| span := trace.SpanFromContext(ctx.Request().Context()) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var userID uint | ||||||||||||||||||||||
| if ctx.IsLogged { | ||||||||||||||||||||||
| userID = ctx.User.ID | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if span.SpanContext().IsValid() { | ||||||||||||||||||||||
| span.SetAttributes( | ||||||||||||||||||||||
| attribute.Int("nekobox.service.user-id", int(userID)), | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| uri := ctx.Param("**") | ||||||||||||||||||||||
| basePath := strings.Split(uri, "/")[0] | ||||||||||||||||||||||
| forwardPath := strings.TrimPrefix(uri, basePath) | ||||||||||||||||||||||
wuhan005 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var forwardURLStr string | ||||||||||||||||||||||
| for _, backend := range conf.Service.Backends { | ||||||||||||||||||||||
| if backend.Prefix == basePath { | ||||||||||||||||||||||
| forwardURLStr = backend.ForwardURL | ||||||||||||||||||||||
| break | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if len(forwardURLStr) == 0 { | ||||||||||||||||||||||
| return ctx.JSONError(http.StatusNotFound, "页面不存在") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| forwardURL, err := url.Parse(forwardURLStr) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to parse forward URL") | ||||||||||||||||||||||
| return ctx.JSONError(http.StatusInternalServerError, "服务网关内部错误") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| reverseProxy := httputil.ReverseProxy{ | ||||||||||||||||||||||
| Director: func(req *http.Request) { | ||||||||||||||||||||||
| req.URL = forwardURL | ||||||||||||||||||||||
| req.URL.Path = strings.TrimRight(req.URL.Path, "/") + forwardPath | ||||||||||||||||||||||
| req.Host = forwardURL.Host | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| traceHeaders := http.Header{} | ||||||||||||||||||||||
| otel.GetTextMapPropagator().Inject(ctx.Request().Context(), propagation.HeaderCarrier(traceHeaders)) | ||||||||||||||||||||||
| for key := range traceHeaders { | ||||||||||||||||||||||
| req.Header.Set(key, traceHeaders.Get(key)) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| req.Header.Set("X-NekoBox-From", "nekobox-gateway") | ||||||||||||||||||||||
| req.Header.Set("X-NekoBox-User-ID", strconv.Itoa(int(userID))) | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| Transport: &http.Transport{ | ||||||||||||||||||||||
| TLSClientConfig: &tls.Config{ | ||||||||||||||||||||||
| InsecureSkipVerify: true, | ||||||||||||||||||||||
|
||||||||||||||||||||||
| }, | ||||||||||||||||||||||
wuhan005 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
| }, | ||||||||||||||||||||||
|
Comment on lines
+71
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: TLS certificate verification is disabled. The Additionally, the TLS configuration is missing 🔎 Required security fixRemove Transport: &http.Transport{
TLSClientConfig: &tls.Config{
- InsecureSkipVerify: true,
+ MinVersion: tls.VersionTLS12,
},
},If you need to support self-signed certificates in development environments, use a proper certificate pool or make this configurable per-backend with clear warnings, but never use Based on static analysis hints: CWE-327, OWASP A02:2021 - Cryptographic Failures. 📝 Committable suggestion
Suggested change
🧰 Tools🪛 ast-grep (0.40.3)[warning] 71-73: MinVersion (missing-ssl-minversion-go) 🪛 GitHub Check: CodeQL[failure] 73-73: Disabled TLS certificate check 🤖 Prompt for AI Agents |
||||||||||||||||||||||
| ErrorHandler: func(writer http.ResponseWriter, request *http.Request, err error) { | ||||||||||||||||||||||
| logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to handle reverse proxy request") | ||||||||||||||||||||||
| _ = ctx.JSONError(http.StatusInternalServerError, "服务网关内部错误") | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| reverseProxy.ServeHTTP(ctx.ResponseWriter(), ctx.Request().Request) | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.