218 lines
7.4 KiB
Python
218 lines
7.4 KiB
Python
from django.db.models import Q
|
|
from django.utils import timezone
|
|
|
|
import yaml
|
|
|
|
from .models import LearnedRouteRule, RouteObservation, Subscription
|
|
|
|
|
|
def desired_subscriptions_for_node(node):
|
|
# Candidate visibility is not permission to install reusable account UUIDs
|
|
# on an office relay. The legacy node-sync protocol carries no enrolled-site
|
|
# proof or per-device lease; leave office provisioning fail-closed until that
|
|
# separate protocol is implemented and accepted.
|
|
if node.office_only:
|
|
return Subscription.objects.none()
|
|
return (
|
|
Subscription.objects.select_related("user", "plan")
|
|
.filter(
|
|
status=Subscription.Status.ACTIVE,
|
|
user__is_active=True,
|
|
user__account_status="active",
|
|
)
|
|
.exclude(
|
|
Q(plan__online_payment_required=True, plan__price_cny__gt=0)
|
|
& ~Q(user__identity_type="employee")
|
|
)
|
|
.filter(
|
|
Q(route_mode__in=[Subscription.RouteMode.GLOBAL, Subscription.RouteMode.CUSTOM])
|
|
| Q(route_mode=Subscription.RouteMode.DEDICATED, allowed_nodes=node)
|
|
)
|
|
.distinct()
|
|
.order_by("pk")
|
|
)
|
|
|
|
|
|
def render_mihomo(subscription):
|
|
proxies = [node.to_mihomo_proxy(subscription) for node in subscription.nodes_for_subscription()]
|
|
node_names = [proxy["name"] for proxy in proxies]
|
|
if not proxies:
|
|
raise ValueError("当前订阅没有可用节点。")
|
|
|
|
rules = []
|
|
policy = subscription.effective_routing_policy()
|
|
if policy:
|
|
rules.extend(rule.strip() for rule in policy.rules if rule and rule.strip())
|
|
if not any(rule.upper().startswith("MATCH,") for rule in rules):
|
|
rules.append("MATCH,节点选择")
|
|
|
|
config = {
|
|
"mixed-port": 7890,
|
|
"allow-lan": False,
|
|
"mode": "rule",
|
|
"log-level": "info",
|
|
"ipv6": True,
|
|
"unified-delay": True,
|
|
"tcp-concurrent": True,
|
|
"proxies": proxies,
|
|
"proxy-groups": [
|
|
{
|
|
"name": "节点选择",
|
|
"type": "select",
|
|
"proxies": ["自动选择", *node_names, "DIRECT"],
|
|
},
|
|
{
|
|
"name": "自动选择",
|
|
"type": "fallback",
|
|
"proxies": node_names,
|
|
"url": "https://www.gstatic.com/generate_204",
|
|
"interval": 300,
|
|
},
|
|
],
|
|
"rules": rules,
|
|
}
|
|
return yaml.safe_dump(config, allow_unicode=True, sort_keys=False, width=120)
|
|
|
|
|
|
def client_routing_rules(route_mode):
|
|
if route_mode not in {"smart", "global", "direct"}:
|
|
raise ValueError("不支持的路由模式。")
|
|
|
|
routing_rules = [
|
|
{
|
|
"type": "field",
|
|
"ip": ["geoip:private"],
|
|
"outboundTag": "direct",
|
|
}
|
|
]
|
|
if route_mode == "smart":
|
|
learned_rules = LearnedRouteRule.objects.filter(
|
|
status=LearnedRouteRule.Status.ACTIVE,
|
|
).exclude(source=LearnedRouteRule.Source.AI).filter(
|
|
Q(valid_until__isnull=True) | Q(valid_until__gt=timezone.now()))
|
|
learned_domains = [
|
|
(
|
|
f"domain:{rule.target}"
|
|
if rule.match_mode == LearnedRouteRule.MatchMode.SUFFIX
|
|
else f"full:{rule.target}"
|
|
)
|
|
for rule in learned_rules.filter(target_type=RouteObservation.TargetType.DOMAIN).only(
|
|
"target", "match_mode"
|
|
)
|
|
]
|
|
learned_ips = [
|
|
f"{target}/{'128' if ':' in target else '32'}"
|
|
for target in learned_rules.filter(target_type=RouteObservation.TargetType.IP).values_list("target", flat=True)
|
|
]
|
|
if learned_domains:
|
|
routing_rules.append({"type": "field", "domain": learned_domains, "outboundTag": "proxy", "ruleTag": "ucvl-learned"})
|
|
if learned_ips:
|
|
routing_rules.append({"type": "field", "ip": learned_ips, "outboundTag": "proxy", "ruleTag": "ucvl-learned"})
|
|
routing_rules.extend(
|
|
[
|
|
{
|
|
"type": "field",
|
|
"domain": ["geosite:category-ads-all"],
|
|
"outboundTag": "block",
|
|
},
|
|
{
|
|
"type": "field",
|
|
"domain": ["geosite:cn"],
|
|
"outboundTag": "direct",
|
|
},
|
|
{
|
|
"type": "field",
|
|
"ip": ["geoip:cn"],
|
|
"outboundTag": "direct",
|
|
},
|
|
{
|
|
"type": "field",
|
|
"network": "tcp,udp",
|
|
"outboundTag": "direct",
|
|
},
|
|
]
|
|
)
|
|
elif route_mode == "direct":
|
|
routing_rules.append(
|
|
{
|
|
"type": "field",
|
|
"network": "tcp,udp",
|
|
"outboundTag": "direct",
|
|
}
|
|
)
|
|
|
|
return routing_rules
|
|
|
|
|
|
def render_xray_client(subscription, node, route_mode="smart"):
|
|
if node.office_only:
|
|
raise ValueError("办公室节点接入验证尚未完成,不能生成普通客户端配置。")
|
|
if node.protocol != node.Protocol.VLESS_REALITY:
|
|
raise ValueError("优创⁺纵横家客户端当前仅支持 VLESS Reality 节点。")
|
|
routing_rules = client_routing_rules(route_mode)
|
|
sniffing = {
|
|
"enabled": True,
|
|
"destOverride": ["http", "tls", "quic"],
|
|
"routeOnly": True,
|
|
}
|
|
return {
|
|
"log": {"loglevel": "warning"},
|
|
"inbounds": [
|
|
{
|
|
"tag": "socks-in",
|
|
"listen": "127.0.0.1",
|
|
"port": 10808,
|
|
"protocol": "socks",
|
|
"settings": {"auth": "noauth", "udp": True},
|
|
"sniffing": sniffing,
|
|
},
|
|
{
|
|
"tag": "http-in",
|
|
"listen": "127.0.0.1",
|
|
"port": 10809,
|
|
"protocol": "http",
|
|
"settings": {},
|
|
"sniffing": sniffing,
|
|
},
|
|
],
|
|
"outbounds": [
|
|
{
|
|
"tag": "proxy",
|
|
"protocol": "vless",
|
|
"settings": {
|
|
"vnext": [
|
|
{
|
|
"address": node.server,
|
|
"port": node.port,
|
|
"users": [
|
|
{
|
|
"id": str(subscription.credential_uuid),
|
|
"encryption": "none",
|
|
"flow": node.flow,
|
|
}
|
|
],
|
|
}
|
|
]
|
|
},
|
|
"streamSettings": {
|
|
"network": node.transport,
|
|
"security": "reality",
|
|
"realitySettings": {
|
|
"show": False,
|
|
"fingerprint": node.client_fingerprint,
|
|
"serverName": node.sni,
|
|
"password": node.reality_public_key,
|
|
"shortId": node.reality_short_id,
|
|
"spiderX": "",
|
|
},
|
|
},
|
|
},
|
|
{"tag": "direct", "protocol": "freedom"},
|
|
{"tag": "block", "protocol": "blackhole"},
|
|
],
|
|
"routing": {
|
|
"domainStrategy": "IPIfNonMatch",
|
|
"rules": routing_rules,
|
|
},
|
|
}
|