The --mode flag was defined but never wired through to the RPC. Add tls_cert and tls_key fields to AddProxyRouteRequest so L7 routes can be created via mcp route add. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.wntrmute.dev/mc/mc-proxy/client/mcproxy"
|
|
mcpv1 "git.wntrmute.dev/mc/mcp/gen/mcp/v1"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
// ListProxyRoutes queries mc-proxy for its current status and routes.
|
|
func (a *Agent) ListProxyRoutes(ctx context.Context, _ *mcpv1.ListProxyRoutesRequest) (*mcpv1.ListProxyRoutesResponse, error) {
|
|
a.Logger.Debug("ListProxyRoutes called")
|
|
|
|
status, err := a.Proxy.GetStatus(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get mc-proxy status: %w", err)
|
|
}
|
|
|
|
resp := &mcpv1.ListProxyRoutesResponse{
|
|
Version: status.Version,
|
|
TotalConnections: status.TotalConnections,
|
|
}
|
|
if !status.StartedAt.IsZero() {
|
|
resp.StartedAt = timestamppb.New(status.StartedAt)
|
|
}
|
|
|
|
for _, ls := range status.Listeners {
|
|
listener := &mcpv1.ProxyListenerInfo{
|
|
Addr: ls.Addr,
|
|
RouteCount: int32(ls.RouteCount), //nolint:gosec // bounded
|
|
ActiveConnections: ls.ActiveConnections,
|
|
}
|
|
for _, r := range ls.Routes {
|
|
listener.Routes = append(listener.Routes, &mcpv1.ProxyRouteInfo{
|
|
Hostname: r.Hostname,
|
|
Backend: r.Backend,
|
|
Mode: r.Mode,
|
|
BackendTls: r.BackendTLS,
|
|
})
|
|
}
|
|
resp.Listeners = append(resp.Listeners, listener)
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
// AddProxyRoute adds a route to mc-proxy.
|
|
func (a *Agent) AddProxyRoute(ctx context.Context, req *mcpv1.AddProxyRouteRequest) (*mcpv1.AddProxyRouteResponse, error) {
|
|
if req.GetListenerAddr() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "listener_addr is required")
|
|
}
|
|
if req.GetHostname() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "hostname is required")
|
|
}
|
|
if req.GetBackend() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "backend is required")
|
|
}
|
|
|
|
if a.Proxy == nil {
|
|
return nil, status.Error(codes.FailedPrecondition, "mc-proxy not configured")
|
|
}
|
|
|
|
route := mcproxy.Route{
|
|
Hostname: req.GetHostname(),
|
|
Backend: req.GetBackend(),
|
|
Mode: req.GetMode(),
|
|
BackendTLS: req.GetBackendTls(),
|
|
TLSCert: req.GetTlsCert(),
|
|
TLSKey: req.GetTlsKey(),
|
|
}
|
|
|
|
if err := a.Proxy.AddRoute(ctx, req.GetListenerAddr(), route); err != nil {
|
|
return nil, fmt.Errorf("add route: %w", err)
|
|
}
|
|
|
|
a.Logger.Info("route added",
|
|
"listener", req.GetListenerAddr(),
|
|
"hostname", req.GetHostname(),
|
|
"backend", req.GetBackend(),
|
|
"mode", req.GetMode(),
|
|
)
|
|
|
|
return &mcpv1.AddProxyRouteResponse{}, nil
|
|
}
|
|
|
|
// RemoveProxyRoute removes a route from mc-proxy.
|
|
func (a *Agent) RemoveProxyRoute(ctx context.Context, req *mcpv1.RemoveProxyRouteRequest) (*mcpv1.RemoveProxyRouteResponse, error) {
|
|
if req.GetListenerAddr() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "listener_addr is required")
|
|
}
|
|
if req.GetHostname() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "hostname is required")
|
|
}
|
|
|
|
if a.Proxy == nil {
|
|
return nil, status.Error(codes.FailedPrecondition, "mc-proxy not configured")
|
|
}
|
|
|
|
if err := a.Proxy.RemoveRoute(ctx, req.GetListenerAddr(), req.GetHostname()); err != nil {
|
|
return nil, fmt.Errorf("remove route: %w", err)
|
|
}
|
|
|
|
a.Logger.Info("route removed",
|
|
"listener", req.GetListenerAddr(),
|
|
"hostname", req.GetHostname(),
|
|
)
|
|
|
|
return &mcpv1.RemoveProxyRouteResponse{}, nil
|
|
}
|