File size: 2,681 Bytes
bbb1195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use serde::{Deserialize, Serialize};

/// Proxy service configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProxyConfig {
    /// Whether proxy service is enabled
    pub enabled: bool,

    /// Whether to allow LAN access
    /// - false: localhost only 127.0.0.1 (default, privacy first)
    /// - true: allow LAN access 0.0.0.0
    #[serde(default)]
    pub allow_lan_access: bool,

    /// Listen port
    pub port: u16,

    /// API key
    pub api_key: String,

    /// Whether to auto start
    pub auto_start: bool,

    /// Anthropic model mapping (key: Claude model name, value: Gemini model name)
    #[serde(default)]
    pub anthropic_mapping: std::collections::HashMap<String, String>,

    /// OpenAI model mapping (key: OpenAI model group, value: Gemini model name)
    #[serde(default)]
    pub openai_mapping: std::collections::HashMap<String, String>,

    /// Custom exact model mapping (key: original model name, value: target model name)
    #[serde(default)]
    pub custom_mapping: std::collections::HashMap<String, String>,

    /// API request timeout in seconds
    #[serde(default = "default_request_timeout")]
    pub request_timeout: u64,

    /// Upstream proxy configuration
    #[serde(default)]
    pub upstream_proxy: UpstreamProxyConfig,
}

/// Upstream proxy configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpstreamProxyConfig {
    /// Whether enabled
    pub enabled: bool,
    /// Proxy URL (http://, https://, socks5://)
    pub url: String,
}

impl Default for ProxyConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            allow_lan_access: true, // Cloud deployment should allow external access
            port: 7860, // HuggingFace Spaces default port
            api_key: format!("sk-{}", uuid::Uuid::new_v4().simple()),
            auto_start: true, // Auto start in cloud environment
            anthropic_mapping: std::collections::HashMap::new(),
            openai_mapping: std::collections::HashMap::new(),
            custom_mapping: std::collections::HashMap::new(),
            request_timeout: default_request_timeout(),
            upstream_proxy: UpstreamProxyConfig::default(),
        }
    }
}

fn default_request_timeout() -> u64 {
    120  // Default 120 seconds
}

impl ProxyConfig {
    /// Get actual bind address
    /// - allow_lan_access = false: return "127.0.0.1" (default, privacy first)
    /// - allow_lan_access = true: return "0.0.0.0" (allow LAN access)
    pub fn get_bind_address(&self) -> &str {
        if self.allow_lan_access {
            "0.0.0.0"
        } else {
            "127.0.0.1"
        }
    }
}