History

Merge pull request #422 from router-for-me/amp

Amp

Luis Pater committed 11 months agov6.5.43

Commit c5df8e72 parents d4d5298 caa48e76 files · +100 −60Browse files

Unified · Split

6 files
  1. internal/api/middleware/request_logging.go +5 −0
  2. internal/api/modules/amp/amp.go +42 −51
  3. internal/api/modules/amp/fallback_handlers.go +4 −4
  4. internal/api/modules/amp/routes.go +21 −5
  5. internal/api/modules/amp/routes_test.go +1 −0
  6. internal/logging/gin_logger.go +27 −0
internal/api/middleware/request_logging.go+5−0
@@ -112,5 +112,10 @@ func shouldLogRequest(path string) bool { · 111 unmodified lines
112112 if strings.HasPrefix(path, "/v0/management") || strings.HasPrefix(path, "/management") {
113113 return false
114114 }
115+
116+ if strings.HasPrefix(path, "/api") {
117+ return strings.HasPrefix(path, "/api/provider")
118+ }
119+
115120 return true
116121 }
internal/api/modules/amp/amp.go+42−51
@@ -126,6 +126,9 @@ func (m *AmpModule) Register(ctx modules.Context) error { · 125 unmodified lines
126126 // Always register provider aliases - these work without an upstream
127127 m.registerProviderAliases(ctx.Engine, ctx.BaseHandler, auth)
128128
129+ // Register management proxy routes once; middleware will gate access when upstream is unavailable.
130+ m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler)
131+
129132 // If no upstream URL, skip proxy routes but provider aliases are still available
130133 if upstreamURL == "" {
131134 log.Debug("amp upstream proxy disabled (no upstream URL configured)")
@@ -134,27 +137,11 @@ func (m *AmpModule) Register(ctx modules.Context) error { · 2 unmodified lines
134137 return
135138 }
136139
137− // Create secret source with precedence: config > env > file
138− // Cache secrets for 5 minutes to reduce file I/O
139− if m.secretSource == nil {
140− m.secretSource = NewMultiSourceSecret(settings.UpstreamAPIKey, 0 /* default 5min */)
141− }
142−
143− // Create reverse proxy with gzip handling via ModifyResponse
144− proxy, err := createReverseProxy(upstreamURL, m.secretSource)
145− if err != nil {
140+ if err := m.enableUpstreamProxy(upstreamURL, &settings); err != nil {
146141 regErr = fmt.Errorf("failed to create amp proxy: %w", err)
147142 return
148143 }
149144
150− m.setProxy(proxy)
151− m.enabled = true
152−
153− // Register management proxy routes (requires upstream)
154− // Uses dynamic middleware that checks m.IsRestrictedToLocalhost() for hot-reload support
155− m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler)
156−
157− log.Infof("amp upstream proxy enabled for: %s", upstreamURL)
158145 log.Debug("amp provider alias routes registered")
159146 })
160147
@@ -188,18 +175,30 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 27 unmodified lines
188175 oldSettings := m.lastConfig
189176 m.configMu.RUnlock()
190177
191− // Track what changed for logging
192− var changes []string
178+ if oldSettings != nil && oldSettings.RestrictManagementToLocalhost != newSettings.RestrictManagementToLocalhost {
179+ m.setRestrictToLocalhost(newSettings.RestrictManagementToLocalhost)
180+ if !newSettings.RestrictManagementToLocalhost {
181+ log.Warnf("amp management routes now accessible from any IP - this is insecure!")
182+ }
183+ }
184+
185+ newUpstreamURL := strings.TrimSpace(newSettings.UpstreamURL)
186+ oldUpstreamURL := ""
187+ if oldSettings != nil {
188+ oldUpstreamURL = strings.TrimSpace(oldSettings.UpstreamURL)
189+ }
190+
191+ if !m.enabled && newUpstreamURL != "" {
192+ if err := m.enableUpstreamProxy(newUpstreamURL, &newSettings); err != nil {
193+ log.Errorf("amp config: failed to enable upstream proxy for %s: %v", newUpstreamURL, err)
194+ }
195+ }
193196
194197 // Check model mappings change
195198 modelMappingsChanged := m.hasModelMappingsChanged(oldSettings, &newSettings)
196199 if modelMappingsChanged {
197200 if m.modelMapper != nil {
198201 m.modelMapper.UpdateMappings(newSettings.ModelMappings)
199− changes = append(changes, "model-mappings")
200− if m.enabled {
201− log.Infof("amp config partial reload: model mappings updated (%d entries)", len(newSettings.ModelMappings))
202− }
203202 } else if m.enabled {
204203 log.Warnf("amp model mapper not initialized, skipping model mapping update")
205204 }
@@ -207,25 +206,16 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 1 unmodified line
207206
208207 if m.enabled {
209208 // Check upstream URL change - now supports hot-reload
210− newUpstreamURL := strings.TrimSpace(newSettings.UpstreamURL)
211− oldUpstreamURL := ""
212− if oldSettings != nil {
213− oldUpstreamURL = strings.TrimSpace(oldSettings.UpstreamURL)
214− }
215−
216209 if newUpstreamURL == "" && oldUpstreamURL != "" {
217− log.Warn("amp upstream URL removed from config, proxy has been disabled")
218210 m.setProxy(nil)
219− changes = append(changes, "upstream-url(disabled)")
220− } else if newUpstreamURL != oldUpstreamURL && newUpstreamURL != "" {
211+ m.enabled = false
212+ } else if oldUpstreamURL != "" && newUpstreamURL != oldUpstreamURL && newUpstreamURL != "" {
221213 // Recreate proxy with new URL
222214 proxy, err := createReverseProxy(newUpstreamURL, m.secretSource)
223215 if err != nil {
224216 log.Errorf("amp config: failed to create proxy for new upstream URL %s: %v", newUpstreamURL, err)
225217 } else {
226218 m.setProxy(proxy)
227− changes = append(changes, "upstream-url")
228− log.Infof("amp config partial reload: upstream URL updated (%s -> %s)", oldUpstreamURL, newUpstreamURL)
229219 }
230220 }
231221
@@ -236,22 +226,10 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 4 unmodified lines
236226 if ms, ok := m.secretSource.(*MultiSourceSecret); ok {
237227 ms.UpdateExplicitKey(newSettings.UpstreamAPIKey)
238228 ms.InvalidateCache()
239− changes = append(changes, "upstream-api-key")
240− log.Debug("amp config partial reload: secret cache invalidated")
241229 }
242230 }
243231 }
244232
245− // Check restrict-management-to-localhost change - now supports hot-reload
246− if oldSettings != nil && oldSettings.RestrictManagementToLocalhost != newSettings.RestrictManagementToLocalhost {
247− m.setRestrictToLocalhost(newSettings.RestrictManagementToLocalhost)
248− changes = append(changes, "restrict-management-to-localhost")
249− if newSettings.RestrictManagementToLocalhost {
250− log.Infof("amp config partial reload: management routes now restricted to localhost")
251− } else {
252− log.Warnf("amp config partial reload: management routes now accessible from any IP - this is insecure!")
253− }
254− }
255233 }
256234
257235 // Store current config for next comparison
@@ -260,13 +238,26 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 2 unmodified lines
260238 m.lastConfig = &settingsCopy
261239 m.configMu.Unlock()
262240
263− // Log summary if any changes detected
264− if len(changes) > 0 {
265− log.Debugf("amp config partial reload completed: %v", changes)
266− } else {
267− log.Debug("amp config checked: no changes detected")
241+ return nil
242+}
243+
244+func (m *AmpModule) enableUpstreamProxy(upstreamURL string, settings *config.AmpCode) error {
245+ if m.secretSource == nil {
246+ m.secretSource = NewMultiSourceSecret(settings.UpstreamAPIKey, 0 /* default 5min */)
247+ } else if ms, ok := m.secretSource.(*MultiSourceSecret); ok {
248+ ms.UpdateExplicitKey(settings.UpstreamAPIKey)
249+ ms.InvalidateCache()
268250 }
269251
252+ proxy, err := createReverseProxy(upstreamURL, m.secretSource)
253+ if err != nil {
254+ return err
255+ }
256+
257+ m.setProxy(proxy)
258+ m.enabled = true
259+
260+ log.Infof("amp upstream proxy enabled for: %s", upstreamURL)
270261 return nil
271262 }
272263
internal/api/modules/amp/fallback_handlers.go+4−4
@@ -48,25 +48,25 @@ func logAmpRouting(routeType AmpRouteType, requestedModel, resolvedModel, provid · 47 unmodified lines
4848 case RouteTypeLocalProvider:
4949 fields["cost"] = "free"
5050 fields["source"] = "local_oauth"
51− log.WithFields(fields).Infof("[amp] using local provider for model: %s", requestedModel)
51+ log.WithFields(fields).Debugf("amp using local provider for model: %s", requestedModel)
5252
5353 case RouteTypeModelMapping:
5454 fields["cost"] = "free"
5555 fields["source"] = "local_oauth"
5656 fields["mapping"] = requestedModel + " -> " + resolvedModel
57− log.WithFields(fields).Infof("[amp] model mapped: %s -> %s", requestedModel, resolvedModel)
57+ // model mapping already logged in mapper; avoid duplicate here
5858
5959 case RouteTypeAmpCredits:
6060 fields["cost"] = "amp_credits"
6161 fields["source"] = "ampcode.com"
6262 fields["model_id"] = requestedModel // Explicit model_id for easy config reference
63− log.WithFields(fields).Warnf("[amp] forwarding to ampcode.com (uses amp credits) - model_id: %s | To use local proxy, add to config: amp-model-mappings: [{from: \"%s\", to: \"<your-local-model>\"}]", requestedModel, requestedModel)
63+ log.WithFields(fields).Warnf("forwarding to ampcode.com (uses amp credits) - model_id: %s | To use local proxy, add to config: amp-model-mappings: [{from: \"%s\", to: \"<your-local-model>\"}]", requestedModel, requestedModel)
6464
6565 case RouteTypeNoProvider:
6666 fields["cost"] = "none"
6767 fields["source"] = "error"
6868 fields["model_id"] = requestedModel // Explicit model_id for easy config reference
69− log.WithFields(fields).Warnf("[amp] no provider available for model_id: %s", requestedModel)
69+ log.WithFields(fields).Warnf("no provider available for model_id: %s", requestedModel)
7070 }
7171 }
7272
internal/api/modules/amp/routes.go+21−5
@@ -2,10 +2,12 @@ package amp · 1 unmodified line
22
33 import (
44 "net"
5+ "net/http"
56 "net/http/httputil"
67 "strings"
78
89 "github.com/gin-gonic/gin"
10+ "github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
911 "github.com/router-for-me/CLIProxyAPI/v6/internal/util"
1012 "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
1113 "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/claude"
@@ -78,6 +80,21 @@ func noCORSMiddleware() gin.HandlerFunc { · 66 unmodified lines
7880 }
7981 }
8082
83+// managementAvailabilityMiddleware short-circuits management routes when the upstream
84+// proxy is disabled, preventing noisy localhost warnings and accidental exposure.
85+func (m *AmpModule) managementAvailabilityMiddleware() gin.HandlerFunc {
86+ return func(c *gin.Context) {
87+ if m.getProxy() == nil {
88+ logging.SkipGinRequestLogging(c)
89+ c.AbortWithStatusJSON(http.StatusServiceUnavailable, gin.H{
90+ "error": "amp upstream proxy not available",
91+ })
92+ return
93+ }
94+ c.Next()
95+ }
96+}
97+
8198 // registerManagementRoutes registers Amp management proxy routes
8299 // These routes proxy through to the Amp control plane for OAuth, user management, etc.
83100 // Uses dynamic middleware and proxy getter for hot-reload support.
@@ -85,14 +102,12 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha · 1 unmodified line
85102 ampAPI := engine.Group("/api")
86103
87104 // Always disable CORS for management routes to prevent browser-based attacks
88− ampAPI.Use(noCORSMiddleware())
105+ ampAPI.Use(m.managementAvailabilityMiddleware(), noCORSMiddleware())
89106
90107 // Apply dynamic localhost-only restriction (hot-reloadable via m.IsRestrictedToLocalhost())
91108 ampAPI.Use(m.localhostOnlyMiddleware())
92109
93− if m.IsRestrictedToLocalhost() {
94− log.Info("amp management routes restricted to localhost only (CORS disabled)")
95− } else {
110+ if !m.IsRestrictedToLocalhost() {
96111 log.Warn("amp management routes are NOT restricted to localhost - this is insecure!")
97112 }
98113
@@ -127,7 +142,8 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha · 28 unmodified lines
127142
128143 // Root-level routes that AMP CLI expects without /api prefix
129144 // These need the same security middleware as the /api/* routes (dynamic for hot-reload)
130− rootMiddleware := []gin.HandlerFunc{noCORSMiddleware(), m.localhostOnlyMiddleware()}
145+ rootMiddleware := []gin.HandlerFunc{m.managementAvailabilityMiddleware(), noCORSMiddleware(), m.localhostOnlyMiddleware()}
146+ engine.GET("/threads/*path", append(rootMiddleware, proxyHandler)...)
131147 engine.GET("/threads.rss", append(rootMiddleware, proxyHandler)...)
132148
133149 // Root-level auth routes for CLI login flow
internal/api/modules/amp/routes_test.go+1−0
@@ -47,6 +47,7 @@ func TestRegisterManagementRoutes(t *testing.T) { · 46 unmodified lines
4747 {"/api/meta", http.MethodGet},
4848 {"/api/telemetry", http.MethodGet},
4949 {"/api/threads", http.MethodGet},
50+ {"/threads/", http.MethodGet},
5051 {"/threads.rss", http.MethodGet}, // Root-level route (no /api prefix)
5152 {"/api/otel", http.MethodGet},
5253 {"/api/tab", http.MethodGet},
internal/logging/gin_logger.go+27−0
@@ -14,6 +14,8 @@ import ( · 13 unmodified lines
1414 log "github.com/sirupsen/logrus"
1515 )
1616
17+const skipGinLogKey = "__gin_skip_request_logging__"
18+
1719 // GinLogrusLogger returns a Gin middleware handler that logs HTTP requests and responses
1820 // using logrus. It captures request details including method, path, status code, latency,
1921 // client IP, and any error messages, formatting them in a Gin-style log format.
@@ -28,6 +30,10 @@ func GinLogrusLogger() gin.HandlerFunc { · 8 unmodified lines
2830
2931 c.Next()
3032
33+ if shouldSkipGinRequestLogging(c) {
34+ return
35+ }
36+
3137 if raw != "" {
3238 path = path + "?" + raw
3339 }
@@ -77,3 +83,24 @@ func GinLogrusRecovery() gin.HandlerFunc { · 43 unmodified lines
7783 c.AbortWithStatus(http.StatusInternalServerError)
7884 })
7985 }
86+
87+// SkipGinRequestLogging marks the provided Gin context so that GinLogrusLogger
88+// will skip emitting a log line for the associated request.
89+func SkipGinRequestLogging(c *gin.Context) {
90+ if c == nil {
91+ return
92+ }
93+ c.Set(skipGinLogKey, true)
94+}
95+
96+func shouldSkipGinRequestLogging(c *gin.Context) bool {
97+ if c == nil {
98+ return false
99+ }
100+ val, exists := c.Get(skipGinLogKey)
101+ if !exists {
102+ return false
103+ }
104+ flag, ok := val.(bool)
105+ return ok && flag
106+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t