from decimal import Decimal from django.db import migrations, models PRESET_DOMAINS = ( ("openai.com", "OpenAI / Codex"), ("chatgpt.com", "ChatGPT"), ("oaistatic.com", "OpenAI 静态资源"), ("oaiusercontent.com", "OpenAI 用户内容"), ("google.com", "Google"), ("googleapis.com", "Google API"), ("gstatic.com", "Google 静态资源"), ("googleusercontent.com", "Google 用户内容"), ("ggpht.com", "Google 图片资源"), ("googlevideo.com", "Google 视频资源"), ("youtube.com", "YouTube"), ("youtu.be", "YouTube 短链接"), ("ytimg.com", "YouTube 图片资源"), ) def seed_preset_route_rules(apps, schema_editor): LearnedRouteRule = apps.get_model("control", "LearnedRouteRule") for target, label in PRESET_DOMAINS: LearnedRouteRule.objects.update_or_create( target_type="domain", target=target, defaults={ "source": "preset", "match_mode": "suffix", "status": "active", "confidence": Decimal("1.0000"), "failure_reports": 0, "proxy_success_reports": 0, "distinct_devices": 0, "reason": f"系统预置:{label} 的已知需代理域名;直接下发,不等待失败学习。", "valid_until": None, "manually_locked": True, }, ) def remove_preset_route_rules(apps, schema_editor): LearnedRouteRule = apps.get_model("control", "LearnedRouteRule") LearnedRouteRule.objects.filter( source="preset", target__in=[target for target, _ in PRESET_DOMAINS], ).delete() class Migration(migrations.Migration): dependencies = [("control", "0004_vpn_ai")] operations = [ migrations.AlterModelOptions( name="learnedrouterule", options={ "ordering": ["-status", "-confidence", "target"], "verbose_name": "智能代理规则", "verbose_name_plural": "智能代理规则", }, ), migrations.AddField( model_name="routeobservation", name="access_kind", field=models.CharField( choices=[("http", "HTTP/HTTPS 网页"), ("socket", "Socket 连接")], db_index=True, default="socket", max_length=10, verbose_name="访问方式", ), ), migrations.AlterField( model_name="learnedrouterule", name="status", field=models.CharField( choices=[("pending", "学习中"), ("active", "已生效"), ("disabled", "已禁用")], db_index=True, default="pending", max_length=12, verbose_name="状态", ), ), migrations.AddField( model_name="learnedrouterule", name="source", field=models.CharField( choices=[("preset", "系统预置"), ("ai", "AI 学习"), ("manual", "管理员添加")], db_index=True, default="ai", max_length=12, verbose_name="规则来源", ), ), migrations.AddField( model_name="learnedrouterule", name="match_mode", field=models.CharField( choices=[("exact", "精确目标"), ("suffix", "域名及其子域")], default="exact", help_text="域名预置规则通常选择“域名及其子域”;IP 地址只能精确匹配。", max_length=12, verbose_name="匹配方式", ), ), migrations.RunPython(seed_preset_route_rules, remove_preset_route_rules), ]