File size: 11,318 Bytes
3d2357d
3f31b27
3d2357d
3f31b27
 
 
 
7769832
3d2357d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f31b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7769832
3f31b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7769832
3d2357d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f31b27
 
 
 
 
 
 
 
 
 
3d2357d
 
 
 
 
 
 
 
 
 
 
 
 
3f31b27
7769832
 
 
 
 
 
 
 
 
 
 
 
 
 
3f31b27
 
 
7769832
3f31b27
 
 
 
 
3d2357d
 
 
 
7769832
3f31b27
 
7769832
3f31b27
7769832
4f5d5ba
3f31b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7769832
3f31b27
4f5d5ba
3f31b27
3d2357d
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import os
import time
import urllib.parse
import requests
import random
import hashlib
import json
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify, Response
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from werkzeug.security import check_password_hash, generate_password_hash
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()

app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'starsky_secret_key')

# 登录管理配置
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

# 用户类
class User(UserMixin):
    def __init__(self, id, username, password_hash):
        self.id = id
        self.username = username
        self.password_hash = password_hash

# 默认用户
default_username = os.environ.get('ADMIN_USERNAME', 'admin')
default_password = os.environ.get('ADMIN_PASSWORD', 'admin123')

# 用户存储
users = {
    default_username: User(
        default_username, 
        default_username, 
        generate_password_hash(default_password)
    )
}

# 代理和缓存系统
class ProxySystem:
    def __init__(self):
        # 可用的API列表
        self.apis = [
            "https://api.v1.mk/sub",
            "https://pub-api-1.bianyuan.xyz/sub"
        ]
        
        # 简单缓存系统
        self._cache_file = "cache.json"
        self._cache = {}
        self._cache_time = {}
        self._cache_ttl = 3600  # 1小时缓存
        self._load_cache()
        
        # 统计信息
        self.stats = {
            "cache_hits": 0,
            "cache_misses": 0,
            "api_successes": 0,
            "api_failures": 0
        }
    
    def _load_cache(self):
        """从磁盘加载缓存"""
        try:
            if os.path.exists(self._cache_file):
                with open(self._cache_file, 'r') as f:
                    cache_data = json.load(f)
                    self._cache = cache_data.get('cache', {})
                    self._cache_time = cache_data.get('cache_time', {})
                    print(f"已加载 {len(self._cache)} 条缓存数据")
        except Exception as e:
            print(f"加载缓存失败: {e}")
            self._cache = {}
            self._cache_time = {}
    
    def _save_cache(self):
        """保存缓存到磁盘"""
        try:
            # 限制缓存大小,最多保存100条
            if len(self._cache) > 100:
                # 移除最旧的缓存
                sorted_keys = sorted(self._cache_time.items(), key=lambda x: x[1])
                keys_to_remove = [k for k, v in sorted_keys[:len(self._cache) - 100]]
                for key in keys_to_remove:
                    self._cache.pop(key, None)
                    self._cache_time.pop(key, None)
            
            cache_data = {
                'cache': self._cache,
                'cache_time': self._cache_time
            }
            
            with open(self._cache_file, 'w') as f:
                json.dump(cache_data, f)
        except Exception as e:
            print(f"保存缓存失败: {e}")
    
    def get_random_api(self):
        """获取随机API"""
        return random.choice(self.apis)
    
    def get_cache_key(self, params):
        """生成缓存键"""
        # 排序参数以保持一致性
        sorted_params = sorted(params.items())
        param_str = urllib.parse.urlencode(sorted_params)
        return hashlib.md5(param_str.encode()).hexdigest()
    
    def get_from_cache(self, params):
        """从缓存获取结果"""
        key = self.get_cache_key(params)
        current_time = time.time()
        
        if key in self._cache and current_time - self._cache_time.get(key, 0) < self._cache_ttl:
            self.stats["cache_hits"] += 1
            return self._cache.get(key)
            
        self.stats["cache_misses"] += 1
        return None
    
    def save_to_cache(self, params, data):
        """保存到缓存"""
        key = self.get_cache_key(params)
        self._cache[key] = data
        self._cache_time[key] = time.time()
        
        # 定期保存缓存
        if self.stats["cache_misses"] % 5 == 0:  # 每5次未命中缓存时保存
            self._save_cache()
    
    def convert(self, params):
        """执行转换,优先使用缓存"""
        # 尝试从缓存获取
        cache_result = self.get_from_cache(params)
        if cache_result:
            return cache_result
        
        # 缓存未命中,请求API
        errors = []
        
        # 随机API顺序
        apis = list(self.apis)
        random.shuffle(apis)
        
        for api_url in apis:
            try:
                # 发送请求
                response = requests.get(api_url, params=params, timeout=30)
                
                if response.status_code == 200:
                    # 获取内容和类型
                    content = response.content
                    content_type = response.headers.get('Content-Type', 'text/plain')
                    
                    # 保存到缓存
                    cache_data = {
                        'content': content.decode('utf-8', errors='ignore'),  # 保存为字符串
                        'content_type': content_type
                    }
                    self.save_to_cache(params, cache_data)
                    
                    self.stats["api_successes"] += 1
                    return content, content_type
                else:
                    errors.append(f"API {api_url} 返回状态码: {response.status_code}")
            except Exception as e:
                errors.append(f"API {api_url} 请求失败: {str(e)}")
                self.stats["api_failures"] += 1
        
        # 所有API都失败
        error_message = "所有转换API均不可用:\n" + "\n".join(errors)
        raise Exception(error_message)
    
    def get_stats(self):
        """获取统计信息"""
        return {
            "cache_size": len(self._cache),
            "cache_hits": self.stats["cache_hits"],
            "cache_misses": self.stats["cache_misses"],
            "api_successes": self.stats["api_successes"],
            "api_failures": self.stats["api_failures"],
            "cache_hit_ratio": self.stats["cache_hits"] / (self.stats["cache_hits"] + self.stats["cache_misses"]) if (self.stats["cache_hits"] + self.stats["cache_misses"]) > 0 else 0
        }

# 初始化代理系统
proxy_system = ProxySystem()

@login_manager.user_loader
def load_user(user_id):
    return users.get(user_id)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        
        user = users.get(username)
        if user and check_password_hash(user.password_hash, password):
            login_user(user)
            return redirect(url_for('index'))
        else:
            flash('用户名或密码错误')
    
    return render_template('login.html')

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('login'))

@app.route('/')
@login_required
def index():
    # 获取缓存统计信息
    try:
        stats = proxy_system.get_stats()
        cache_hit_ratio = f"{stats['cache_hit_ratio']*100:.1f}%" if stats['cache_hit_ratio'] > 0 else "0%"
    except:
        cache_hit_ratio = "计算中"
        
    return render_template('index.html', 
                         conversion_mode="隐私代理+本地缓存", 
                         cache_hit_ratio=cache_hit_ratio)

@app.route('/convert', methods=['POST'])
@login_required
def convert():
    # 获取参数
    backend_url = request.form.get('backend_url', 'https://raw.githubusercontent.com/yuanwangokk-1/subscribe/refs/heads/main/ACL4SSR/ACL4SSR.ini')
    target = request.form.get('target', 'clash')
    original_url = request.form.get('original_url', '')
    
    if not original_url:
        return jsonify({"status": "error", "message": "订阅链接不能为空"})
    
    try:
        # 构建参数
        params = {
            'target': target,
            'url': original_url,
            'config': backend_url,
            'emoji': 'true',
            'list': 'false',
            'udp': 'false',
            'tfo': 'false',
            'expand': 'true',
            'scv': 'false',
            'fdn': 'false',
            'new_name': 'true'
        }
        
        # 构建转换URL (隐私代理URL)
        server_url = request.url_root.rstrip('/')
        proxy_url = f"{server_url}/api/sub?{urllib.parse.urlencode(params)}"
        
        return jsonify({
            "status": "success", 
            "result": proxy_url,
            "mode": "proxy"
        })
        
    except Exception as e:
        return jsonify({"status": "error", "message": f"处理失败: {str(e)}"})

@app.route('/api/sub')
def api_sub_proxy():
    """智能代理API - 带缓存"""
    try:
        # 获取所有请求参数
        params = request.args.to_dict()
        
        # 使用代理系统处理请求
        try:
            # 从缓存中获取
            cache_result = proxy_system.get_from_cache(params)
            if cache_result:
                # 返回缓存内容
                content = cache_result.get('content', '').encode('utf-8')
                content_type = cache_result.get('content_type', 'text/plain')
                
                headers = {
                    'Content-Type': content_type,
                    'X-Proxy-By': 'Your Private Space',
                    'X-Cache': 'HIT',
                    'X-Content-Type-Options': 'nosniff',
                    'Cache-Control': 'max-age=3600'
                }
                
                return Response(content, headers=headers)
            
            # 缓存未命中,使用API
            content, content_type = proxy_system.convert(params)
            
            # 构建响应
            headers = {
                'Content-Type': content_type,
                'X-Proxy-By': 'Your Private Space',
                'X-Cache': 'MISS',
                'X-Content-Type-Options': 'nosniff',
                'Cache-Control': 'max-age=3600'
            }
            
            return Response(content, headers=headers)
        except Exception as e:
            return f"转换失败: {str(e)}", 500
            
    except Exception as e:
        return f"请求处理失败: {str(e)}", 500

# 添加统计接口
@app.route('/stats')
@login_required
def show_stats():
    stats = proxy_system.get_stats()
    return jsonify(stats)

# 添加清除缓存接口
@app.route('/clear-cache', methods=['POST'])
@login_required
def clear_cache():
    try:
        proxy_system._cache = {}
        proxy_system._cache_time = {}
        proxy_system._save_cache()
        return jsonify({"status": "success", "message": "缓存已清除"})
    except Exception as e:
        return jsonify({"status": "error", "message": f"清除缓存失败: {str(e)}"})

# 仅在本地开发时使用
if __name__ == '__main__' and os.environ.get('DEVELOPMENT') == 'true':
    app.run(host='0.0.0.0', port=7860, debug=True)