832 lines · 26.8 KB · Go
RawDownload
// Package main provides the entry point for the CLI Proxy API server.// This server acts as a proxy that provides OpenAI/Gemini/Claude compatible API interfaces// for CLI models, allowing CLI models to be used with tools and libraries designed for standard AI APIs.package main import (	"context"	"errors"	"flag"	"fmt"	"io"	"io/fs"	"net"	"net/url"	"os"	"path/filepath"	"strconv"	"strings"	"time" 	"github.com/joho/godotenv"	configaccess "github.com/router-for-me/CLIProxyAPI/v7/internal/access/config_access"	"github.com/router-for-me/CLIProxyAPI/v7/internal/buildinfo"	"github.com/router-for-me/CLIProxyAPI/v7/internal/cmd"	"github.com/router-for-me/CLIProxyAPI/v7/internal/config"	"github.com/router-for-me/CLIProxyAPI/v7/internal/home"	"github.com/router-for-me/CLIProxyAPI/v7/internal/logging"	"github.com/router-for-me/CLIProxyAPI/v7/internal/managementasset"	"github.com/router-for-me/CLIProxyAPI/v7/internal/misc"	"github.com/router-for-me/CLIProxyAPI/v7/internal/redisqueue"	"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"	"github.com/router-for-me/CLIProxyAPI/v7/internal/store"	_ "github.com/router-for-me/CLIProxyAPI/v7/internal/translator"	"github.com/router-for-me/CLIProxyAPI/v7/internal/tui"	"github.com/router-for-me/CLIProxyAPI/v7/internal/util"	sdkAuth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"	coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"	log "github.com/sirupsen/logrus") var (	Version           = "dev"	Commit            = "none"	BuildDate         = "unknown"	DefaultConfigPath = "") // init initializes the shared logger setup.func init() {	logging.SetupBaseLogger()	buildinfo.Version = Version	buildinfo.Commit = Commit	buildinfo.BuildDate = BuildDate} func parseHomeFlagConfig(rawAddr string, password string) (config.HomeConfig, error) {	rawAddr = strings.TrimSpace(rawAddr)	if rawAddr == "" {		return config.HomeConfig{}, fmt.Errorf("address is empty")	} 	if strings.Contains(rawAddr, "://") {		return parseHomeURLConfig(rawAddr, password)	} 	host, portStr, errSplit := net.SplitHostPort(rawAddr)	if errSplit != nil {		return config.HomeConfig{}, fmt.Errorf("expected host:port, redis://host:port, or rediss://host:port: %w", errSplit)	} 	host = strings.TrimSpace(host)	if host == "" {		return config.HomeConfig{}, fmt.Errorf("host is empty")	} 	port, errPort := parseHomePort(portStr)	if errPort != nil {		return config.HomeConfig{}, errPort	} 	return config.HomeConfig{		Enabled:  true,		Host:     host,		Port:     port,		Password: password,	}, nil} func parseHomeURLConfig(rawAddr string, password string) (config.HomeConfig, error) {	parsed, errParse := url.Parse(rawAddr)	if errParse != nil {		return config.HomeConfig{}, fmt.Errorf("parse URL: %w", errParse)	} 	scheme := strings.ToLower(strings.TrimSpace(parsed.Scheme))	if scheme != "redis" && scheme != "rediss" {		return config.HomeConfig{}, fmt.Errorf("unsupported URL scheme %q", parsed.Scheme)	} 	host := strings.TrimSpace(parsed.Hostname())	if host == "" {		return config.HomeConfig{}, fmt.Errorf("host is empty")	} 	port, errPort := parseHomePort(parsed.Port())	if errPort != nil {		return config.HomeConfig{}, errPort	} 	if password == "" && parsed.User != nil {		if urlPassword, ok := parsed.User.Password(); ok {			password = urlPassword		}	} 	homeCfg := config.HomeConfig{		Enabled:  true,		Host:     host,		Port:     port,		Password: password,	}	query := parsed.Query()	homeCfg.DisableClusterDiscovery = parseHomeBoolQuery(query, "disable-cluster-discovery", "disable_cluster_discovery") 	if scheme == "rediss" {		homeCfg.TLS.Enable = true		homeCfg.TLS.ServerName = strings.TrimSpace(firstHomeQueryValue(query, "server-name", "server_name"))		homeCfg.TLS.InsecureSkipVerify = parseHomeBoolQuery(query, "insecure-skip-verify", "insecure_skip_verify", "skip_verify")		homeCfg.TLS.CACert = strings.TrimSpace(firstHomeQueryValue(query, "ca-cert", "ca_cert"))	} 	return homeCfg, nil} func parseHomePort(rawPort string) (int, error) {	rawPort = strings.TrimSpace(rawPort)	if rawPort == "" {		return 0, fmt.Errorf("port is empty")	} 	port, errPort := strconv.Atoi(rawPort)	if errPort != nil || port <= 0 || port > 65535 {		return 0, fmt.Errorf("invalid port %q", rawPort)	} 	return port, nil} func firstHomeQueryValue(values url.Values, keys ...string) string {	for _, key := range keys {		if value := values.Get(key); value != "" {			return value		}	}	return ""} func parseHomeBoolQuery(values url.Values, keys ...string) bool {	for _, key := range keys {		value := strings.TrimSpace(values.Get(key))		if value == "" {			continue		}		parsed, errParse := strconv.ParseBool(value)		return errParse == nil && parsed	}	return false} // main is the entry point of the application.// It parses command-line flags, loads configuration, and starts the appropriate// service based on the provided flags (login, codex-login, or server mode).func main() {	fmt.Printf("CLIProxyAPI Version: %s, Commit: %s, BuiltAt: %s\n", buildinfo.Version, buildinfo.Commit, buildinfo.BuildDate) 	// Command-line flags to control the application's behavior.	var login bool	var codexLogin bool	var codexDeviceLogin bool	var claudeLogin bool	var noBrowser bool	var oauthCallbackPort int	var antigravityLogin bool	var kimiLogin bool	var xaiLogin bool	var projectID string	var vertexImport string	var vertexImportPrefix string	var configPath string	var password string	var homeAddr string	var homePassword string	var homeJWT string	var homeDisableClusterDiscovery bool	var tuiMode bool	var standalone bool	var localModel bool 	// Define command-line flags for different operation modes.	flag.BoolVar(&login, "login", false, "Login Google Account")	flag.BoolVar(&codexLogin, "codex-login", false, "Login to Codex using OAuth")	flag.BoolVar(&codexDeviceLogin, "codex-device-login", false, "Login to Codex using device code flow")	flag.BoolVar(&claudeLogin, "claude-login", false, "Login to Claude using OAuth")	flag.BoolVar(&noBrowser, "no-browser", false, "Don't open browser automatically for OAuth")	flag.IntVar(&oauthCallbackPort, "oauth-callback-port", 0, "Override OAuth callback port (defaults to provider-specific port)")	flag.BoolVar(&antigravityLogin, "antigravity-login", false, "Login to Antigravity using OAuth")	flag.BoolVar(&kimiLogin, "kimi-login", false, "Login to Kimi using OAuth")	flag.BoolVar(&xaiLogin, "xai-login", false, "Login to xAI using OAuth")	flag.StringVar(&projectID, "project_id", "", "Project ID (Gemini only, not required)")	flag.StringVar(&configPath, "config", DefaultConfigPath, "Configure File Path")	flag.StringVar(&vertexImport, "vertex-import", "", "Import Vertex service account key JSON file")	flag.StringVar(&vertexImportPrefix, "vertex-import-prefix", "", "Prefix for Vertex model namespacing (use with -vertex-import)")	flag.StringVar(&password, "password", "", "")	flag.StringVar(&homeAddr, "home", "", "Home control plane address in host:port, redis://host:port, or rediss://host:port format (loads config from home and skips local config file)")	flag.StringVar(&homePassword, "home-password", "", "Home control plane password (Redis AUTH)")	flag.StringVar(&homeJWT, "home-jwt", "", "Home control plane JWT for mTLS certificate bootstrap and connection")	flag.BoolVar(&homeDisableClusterDiscovery, "home-disable-cluster-discovery", false, "Disable Home CLUSTER NODES discovery and keep using the configured -home address")	flag.BoolVar(&tuiMode, "tui", false, "Start with terminal management UI")	flag.BoolVar(&standalone, "standalone", false, "In TUI mode, start an embedded local server")	flag.BoolVar(&localModel, "local-model", false, "Use embedded model catalog only, skip remote model fetching") 	flag.CommandLine.Usage = func() {		out := flag.CommandLine.Output()		_, _ = fmt.Fprintf(out, "Usage of %s\n", os.Args[0])		flag.CommandLine.VisitAll(func(f *flag.Flag) {			if f.Name == "password" {				return			}			s := fmt.Sprintf("  -%s", f.Name)			name, unquoteUsage := flag.UnquoteUsage(f)			if name != "" {				s += " " + name			}			if len(s) <= 4 {				s += "	"			} else {				s += "\n    "			}			if unquoteUsage != "" {				s += unquoteUsage			}			if f.DefValue != "" && f.DefValue != "false" && f.DefValue != "0" {				s += fmt.Sprintf(" (default %s)", f.DefValue)			}			_, _ = fmt.Fprint(out, s+"\n")		})	} 	// Parse the command-line flags.	flag.Parse() 	// Core application variables.	var err error	var cfg *config.Config	var isCloudDeploy bool	var configLoadedFromHome bool	var (		usePostgresStore     bool		pgStoreDSN           string		pgStoreSchema        string		pgStoreLocalPath     string		pgStoreInst          *store.PostgresStore		useGitStore          bool		gitStoreRemoteURL    string		gitStoreUser         string		gitStorePassword     string		gitStoreBranch       string		gitStoreLocalPath    string		gitStoreInst         *store.GitTokenStore		gitStoreRoot         string		useObjectStore       bool		objectStoreEndpoint  string		objectStoreAccess    string		objectStoreSecret    string		objectStoreBucket    string		objectStoreLocalPath string		objectStoreInst      *store.ObjectTokenStore	) 	wd, err := os.Getwd()	if err != nil {		log.Errorf("failed to get working directory: %v", err)		return	} 	// Load environment variables from .env if present.	if errLoad := godotenv.Load(filepath.Join(wd, ".env")); errLoad != nil {		if !errors.Is(errLoad, os.ErrNotExist) {			log.WithError(errLoad).Warn("failed to load .env file")		}	} 	lookupEnv := func(keys ...string) (string, bool) {		for _, key := range keys {			if value, ok := os.LookupEnv(key); ok {				if trimmed := strings.TrimSpace(value); trimmed != "" {					return trimmed, true				}			}		}		return "", false	}	writableBase := util.WritablePath() 	// Allow env var fallback for home flags so they can be configured without command args.	if strings.TrimSpace(homeAddr) == "" {		if v, ok := lookupEnv("HOME_ADDR", "home_addr"); ok {			homeAddr = v		}	}	if strings.TrimSpace(homePassword) == "" {		if v, ok := lookupEnv("HOME_PASSWORD", "home_password"); ok {			homePassword = v		}	}	if strings.TrimSpace(homeJWT) == "" {		if v, ok := lookupEnv("HOME_JWT", "home_jwt"); ok {			homeJWT = v		}	} 	if value, ok := lookupEnv("PGSTORE_DSN", "pgstore_dsn"); ok {		usePostgresStore = true		pgStoreDSN = value	}	if usePostgresStore {		if value, ok := lookupEnv("PGSTORE_SCHEMA", "pgstore_schema"); ok {			pgStoreSchema = value		}		if value, ok := lookupEnv("PGSTORE_LOCAL_PATH", "pgstore_local_path"); ok {			pgStoreLocalPath = value		}		if pgStoreLocalPath == "" {			if writableBase != "" {				pgStoreLocalPath = writableBase			} else {				pgStoreLocalPath = wd			}		}		useGitStore = false	}	if value, ok := lookupEnv("GITSTORE_GIT_URL", "gitstore_git_url"); ok {		useGitStore = true		gitStoreRemoteURL = value	}	if value, ok := lookupEnv("GITSTORE_GIT_USERNAME", "gitstore_git_username"); ok {		gitStoreUser = value	}	if value, ok := lookupEnv("GITSTORE_GIT_TOKEN", "gitstore_git_token"); ok {		gitStorePassword = value	}	if value, ok := lookupEnv("GITSTORE_LOCAL_PATH", "gitstore_local_path"); ok {		gitStoreLocalPath = value	}	if value, ok := lookupEnv("GITSTORE_GIT_BRANCH", "gitstore_git_branch"); ok {		gitStoreBranch = value	}	if value, ok := lookupEnv("OBJECTSTORE_ENDPOINT", "objectstore_endpoint"); ok {		useObjectStore = true		objectStoreEndpoint = value	}	if value, ok := lookupEnv("OBJECTSTORE_ACCESS_KEY", "objectstore_access_key"); ok {		objectStoreAccess = value	}	if value, ok := lookupEnv("OBJECTSTORE_SECRET_KEY", "objectstore_secret_key"); ok {		objectStoreSecret = value	}	if value, ok := lookupEnv("OBJECTSTORE_BUCKET", "objectstore_bucket"); ok {		objectStoreBucket = value	}	if value, ok := lookupEnv("OBJECTSTORE_LOCAL_PATH", "objectstore_local_path"); ok {		objectStoreLocalPath = value	} 	// Check for cloud deploy mode only on first execution	// Read env var name in uppercase: DEPLOY	deployEnv := os.Getenv("DEPLOY")	if deployEnv == "cloud" {		isCloudDeploy = true	} 	// Determine and load the configuration file.	// Prefer the Postgres store when configured, otherwise fallback to git or local files.	var configFilePath string	if strings.TrimSpace(homeJWT) != "" {		configLoadedFromHome = true		ctxHome, cancelHome := context.WithTimeout(context.Background(), 30*time.Second)		homeCfg, errHomeCfg := home.ConfigFromJWT(ctxHome, homeJWT)		cancelHome()		if errHomeCfg != nil {			log.Errorf("invalid -home-jwt: %v", errHomeCfg)			return		}		if homeDisableClusterDiscovery {			homeCfg.DisableClusterDiscovery = true		}		homeClient := home.New(homeCfg)		defer homeClient.Close() 		ctxHomeConfig, cancelHomeConfig := context.WithTimeout(context.Background(), 30*time.Second)		raw, errGetConfig := homeClient.GetConfig(ctxHomeConfig)		cancelHomeConfig()		if errGetConfig != nil {			log.Errorf("failed to fetch config from home: %v", errGetConfig)			return		} 		parsed, errParseConfig := config.ParseConfigBytes(raw)		if errParseConfig != nil {			log.Errorf("failed to parse config payload from home: %v", errParseConfig)			return		}		if parsed == nil {			parsed = &config.Config{}		}		parsed.Home = homeCfg		parsed.Port = 8317 // Default to 8317 for home mode, can be overridden by home config		parsed.UsageStatisticsEnabled = true		cfg = parsed 		// Keep a non-empty config path for downstream components (log paths, management assets, etc),		// but do not require the file to exist when loading config from home.		if strings.TrimSpace(configPath) != "" {			configFilePath = configPath		} else {			configFilePath = filepath.Join(wd, "config.yaml")		} 		// Local stores are intentionally disabled when config is loaded from home.		usePostgresStore = false		useObjectStore = false		useGitStore = false	} else if strings.TrimSpace(homeAddr) != "" {		configLoadedFromHome = true		trimmedHomePassword := strings.TrimSpace(homePassword)		homeCfg, errHomeCfg := parseHomeFlagConfig(homeAddr, trimmedHomePassword)		if errHomeCfg != nil {			log.Errorf("invalid -home address %q: %v", homeAddr, errHomeCfg)			return		}		if homeDisableClusterDiscovery {			homeCfg.DisableClusterDiscovery = true		}		homeClient := home.New(homeCfg)		defer homeClient.Close() 		ctxHome, cancelHome := context.WithTimeout(context.Background(), 30*time.Second)		raw, errGetConfig := homeClient.GetConfig(ctxHome)		cancelHome()		if errGetConfig != nil {			log.Errorf("failed to fetch config from home: %v", errGetConfig)			return		} 		parsed, errParseConfig := config.ParseConfigBytes(raw)		if errParseConfig != nil {			log.Errorf("failed to parse config payload from home: %v", errParseConfig)			return		}		if parsed == nil {			parsed = &config.Config{}		}		parsed.Home = homeCfg		parsed.Port = 8317 // Default to 8317 for home mode, can be overridden by home config		parsed.UsageStatisticsEnabled = true		cfg = parsed 		// Keep a non-empty config path for downstream components (log paths, management assets, etc),		// but do not require the file to exist when loading config from home.		if strings.TrimSpace(configPath) != "" {			configFilePath = configPath		} else {			configFilePath = filepath.Join(wd, "config.yaml")		} 		// Local stores are intentionally disabled when config is loaded from home.		usePostgresStore = false		useObjectStore = false		useGitStore = false	} else if usePostgresStore {		if pgStoreLocalPath == "" {			pgStoreLocalPath = wd		}		pgStoreLocalPath = filepath.Join(pgStoreLocalPath, "pgstore")		ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)		pgStoreInst, err = store.NewPostgresStore(ctx, store.PostgresStoreConfig{			DSN:      pgStoreDSN,			Schema:   pgStoreSchema,			SpoolDir: pgStoreLocalPath,		})		cancel()		if err != nil {			log.Errorf("failed to initialize postgres token store: %v", err)			return		}		examplePath := filepath.Join(wd, "config.example.yaml")		ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)		if errBootstrap := pgStoreInst.Bootstrap(ctx, examplePath); errBootstrap != nil {			cancel()			log.Errorf("failed to bootstrap postgres-backed config: %v", errBootstrap)

Lines 1–500 of 832.

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