909c8b1894
Sanitizer renames tool names and replaces system prompt patterns that Anthropic fingerprints to detect non-Claude-Code clients. Lowercase tool names (bash, read, glob, etc.) combined together trigger rejection — renaming to PascalCase bypasses this. Configurable via YAML sanitize rules for tools, system, and body. Background OAuth token refresh every 30s with 5-minute pre-expiry lead. Uses Chrome TLS fingerprint for refresh endpoint too. Adds /messages route (without /v1 prefix) for OpenCode compat.
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/fujin/anthropic-proxy/internal/auth"
|
|
"github.com/fujin/anthropic-proxy/internal/config"
|
|
"github.com/fujin/anthropic-proxy/internal/proxy"
|
|
"github.com/fujin/anthropic-proxy/internal/server"
|
|
)
|
|
|
|
func run() error {
|
|
log.SetFlags(log.LstdFlags)
|
|
|
|
cfg, err := config.Load("config.yaml")
|
|
if err != nil {
|
|
return fmt.Errorf("load config: %w", err)
|
|
}
|
|
|
|
creds, err := config.LoadCredentials(cfg)
|
|
if err != nil {
|
|
return fmt.Errorf("load credentials: %w", err)
|
|
}
|
|
|
|
if len(creds) == 0 {
|
|
return fmt.Errorf("no credentials found")
|
|
}
|
|
|
|
log.Printf("loaded %d credentials", len(creds))
|
|
|
|
pool := auth.NewPool(creds)
|
|
|
|
pool.RefreshExpiring(context.Background())
|
|
auth.StartBackgroundRefresh(pool)
|
|
|
|
var profile *proxy.SniffedProfile
|
|
if cfg.ClaudeBinary != "" {
|
|
log.Printf("sniffing claude-code at %s...", cfg.ClaudeBinary)
|
|
profile, err = proxy.SniffClaudeCode(cfg.ClaudeBinary)
|
|
if err != nil {
|
|
log.Printf("warning: sniff failed, using defaults: %v", err)
|
|
}
|
|
}
|
|
|
|
log.Printf("starting server on port %d", cfg.Port)
|
|
srv := server.New(cfg, pool, profile)
|
|
return srv.Start()
|
|
}
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
log.Printf("error: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|