mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
**feat(config): add TLS support for HTTPS server configuration**
- Introduced `TLSConfig` to support HTTPS configurations, including enabling TLS, specifying certificate and key files. - Updated HTTP server logic to handle HTTPS mode when TLS is enabled. - Enhanced `config.example.yaml` with TLS settings example. - Adjusted internal URL generation to respect protocol based on TLS state.
This commit is contained in:
@@ -1,6 +1,12 @@
|
|||||||
# Server port
|
# Server port
|
||||||
port: 8317
|
port: 8317
|
||||||
|
|
||||||
|
# TLS settings for HTTPS. When enabled, the server listens with the provided certificate and key.
|
||||||
|
tls:
|
||||||
|
enable: false
|
||||||
|
cert: ""
|
||||||
|
key: ""
|
||||||
|
|
||||||
# Management API settings
|
# Management API settings
|
||||||
remote-management:
|
remote-management:
|
||||||
# Whether to allow remote (non-localhost) management access.
|
# Whether to allow remote (non-localhost) management access.
|
||||||
|
|||||||
@@ -235,7 +235,11 @@ func (h *Handler) managementCallbackURL(path string) (string, error) {
|
|||||||
if !strings.HasPrefix(path, "/") {
|
if !strings.HasPrefix(path, "/") {
|
||||||
path = "/" + path
|
path = "/" + path
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("http://127.0.0.1:%d%s", h.cfg.Port, path), nil
|
scheme := "http"
|
||||||
|
if h.cfg.TLS.Enable {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s://127.0.0.1:%d%s", scheme, h.cfg.Port, path), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) ListAuthFiles(c *gin.Context) {
|
func (h *Handler) ListAuthFiles(c *gin.Context) {
|
||||||
|
|||||||
@@ -694,17 +694,33 @@ func (s *Server) unifiedModelsHandler(openaiHandler *openai.OpenAIAPIHandler, cl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start begins listening for and serving HTTP requests.
|
// Start begins listening for and serving HTTP or HTTPS requests.
|
||||||
// It's a blocking call and will only return on an unrecoverable error.
|
// It's a blocking call and will only return on an unrecoverable error.
|
||||||
//
|
//
|
||||||
// Returns:
|
// Returns:
|
||||||
// - error: An error if the server fails to start
|
// - error: An error if the server fails to start
|
||||||
func (s *Server) Start() error {
|
func (s *Server) Start() error {
|
||||||
log.Debugf("Starting API server on %s", s.server.Addr)
|
if s == nil || s.server == nil {
|
||||||
|
return fmt.Errorf("failed to start HTTP server: server not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
// Start the HTTP server.
|
useTLS := s.cfg != nil && s.cfg.TLS.Enable
|
||||||
if err := s.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
if useTLS {
|
||||||
return fmt.Errorf("failed to start HTTP server: %v", err)
|
cert := strings.TrimSpace(s.cfg.TLS.Cert)
|
||||||
|
key := strings.TrimSpace(s.cfg.TLS.Key)
|
||||||
|
if cert == "" || key == "" {
|
||||||
|
return fmt.Errorf("failed to start HTTPS server: tls.cert or tls.key is empty")
|
||||||
|
}
|
||||||
|
log.Debugf("Starting API server on %s with TLS", s.server.Addr)
|
||||||
|
if errServeTLS := s.server.ListenAndServeTLS(cert, key); errServeTLS != nil && !errors.Is(errServeTLS, http.ErrServerClosed) {
|
||||||
|
return fmt.Errorf("failed to start HTTPS server: %v", errServeTLS)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Starting API server on %s", s.server.Addr)
|
||||||
|
if errServe := s.server.ListenAndServe(); errServe != nil && !errors.Is(errServe, http.ErrServerClosed) {
|
||||||
|
return fmt.Errorf("failed to start HTTP server: %v", errServe)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ type Config struct {
|
|||||||
// Port is the network port on which the API server will listen.
|
// Port is the network port on which the API server will listen.
|
||||||
Port int `yaml:"port" json:"-"`
|
Port int `yaml:"port" json:"-"`
|
||||||
|
|
||||||
|
// TLS config controls HTTPS server settings.
|
||||||
|
TLS TLSConfig `yaml:"tls" json:"tls"`
|
||||||
|
|
||||||
// AmpUpstreamURL defines the upstream Amp control plane used for non-provider calls.
|
// AmpUpstreamURL defines the upstream Amp control plane used for non-provider calls.
|
||||||
AmpUpstreamURL string `yaml:"amp-upstream-url" json:"amp-upstream-url"`
|
AmpUpstreamURL string `yaml:"amp-upstream-url" json:"amp-upstream-url"`
|
||||||
|
|
||||||
@@ -82,6 +85,16 @@ type Config struct {
|
|||||||
Payload PayloadConfig `yaml:"payload" json:"payload"`
|
Payload PayloadConfig `yaml:"payload" json:"payload"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TLSConfig holds HTTPS server settings.
|
||||||
|
type TLSConfig struct {
|
||||||
|
// Enable toggles HTTPS server mode.
|
||||||
|
Enable bool `yaml:"enable" json:"enable"`
|
||||||
|
// Cert is the path to the TLS certificate file.
|
||||||
|
Cert string `yaml:"cert" json:"cert"`
|
||||||
|
// Key is the path to the TLS private key file.
|
||||||
|
Key string `yaml:"key" json:"key"`
|
||||||
|
}
|
||||||
|
|
||||||
// RemoteManagement holds management API configuration under 'remote-management'.
|
// RemoteManagement holds management API configuration under 'remote-management'.
|
||||||
type RemoteManagement struct {
|
type RemoteManagement struct {
|
||||||
// AllowRemote toggles remote (non-localhost) access to management API.
|
// AllowRemote toggles remote (non-localhost) access to management API.
|
||||||
|
|||||||
Reference in New Issue
Block a user