Skip to content

Commit

Permalink
clash: set direct rule for all proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Oct 22, 2023
1 parent eee8faf commit 227647a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
25 changes: 23 additions & 2 deletions apps/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.conf import settings
from django.template.loader import render_to_string

from apps.utils import get_clash_direct_rule


class UserSubManager:
"""统一管理用户的订阅"""
Expand Down Expand Up @@ -33,13 +35,14 @@ def _get_clash_sub_yaml(self):
"sub_client": self.sub_client,
"provider_name": settings.SITE_TITLE,
"proxy_provider_url": self.user.clash_proxy_provider_endpoint,
"direct_rules": self.get_clash_direct_rules(),
},
)

def _get_shadowrocket_sub_links(self):
sub_links = ""
# for clean the rule have the same port
# key: relay_node_id+port, value: cfg
# key: relay_node_id+port, value: shadowrocket_sub_link
relay_node_group = {}
for node in self.node_list:
if node.enable_relay:
Expand Down Expand Up @@ -68,7 +71,7 @@ def get_clash_proxy_providers(self):
"""todo support multi provider group"""
node_configs = []
# for clean the rule have the same port
# key: relay_node_id+port, value: cfg
# key: relay_node_id+port, value: clash cfg
relay_node_group = {}
for node in self.node_list:
if node.enable_relay:
Expand All @@ -92,3 +95,21 @@ def get_clash_proxy_providers(self):
"clash/providers.yaml",
{"nodes": sorted(node_configs, key=lambda x: x["name"])},
)

def get_clash_direct_rules(self):
rules = []
# for clean the rule have the same port
# key: relay_node_id+port, value: rule
relay_node_group = {}
for node in self.node_list:
if node.enable_relay:
for rule in node.get_enabled_relay_rules():
key = f"{rule.relay_node.id}{rule.relay_port}"
relay_node_group[key] = get_clash_direct_rule(rule.relay_host)

if node.enable_direct:
rules.append(get_clash_direct_rule(node.server))

for rule in relay_node_group.values():
rules.append(rule)
return rules
11 changes: 11 additions & 0 deletions apps/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hashlib
import json
import random
import re
import time
from functools import wraps

Expand Down Expand Up @@ -105,3 +106,13 @@ def get_client_ip(request):
return x_forwarded_for.split(",")[0]
else:
return request.META.get("REMOTE_ADDR")


def get_clash_direct_rule(addr):
ip_pattern = r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"

# 检查输入字符串是否匹配 IP 地址正则表达式
if re.match(ip_pattern, addr):
return f"IP-CIDR,{addr}/32,DIRECT"
else:
return f"DOMAIN,{addr},DIRECT"
7 changes: 6 additions & 1 deletion templates/clash/rule_sets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ rule-providers:
interval: 86400

rules:
- RULE-SET,applications,DIRECT
# Proxy Node DIRECT
{% autoescape off %}
{% for rule in direct_rules %} - {{ rule }}
{% endfor %}
{% endautoescape%}
- DOMAIN,clash.razord.top,DIRECT
- DOMAIN,yacd.haishan.me,DIRECT
- RULE-SET,applications,DIRECT
- RULE-SET,private,DIRECT
- RULE-SET,reject,REJECT
- RULE-SET,icloud,DIRECT
Expand Down
7 changes: 7 additions & 0 deletions templates/clash/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ rules:
# 自定义规则
## 您可以在此处插入您补充的自定义规则(请注意保持缩进)

# Proxy Node DIRECT
{% autoescape off %}
{% for rule in direct_rules %} - {{ rule }}
{% endfor %}
{% endautoescape%}

# Apple
- DOMAIN,safebrowsing.urlsec.qq.com,DIRECT # 如果您并不信任此服务提供商或防止其下载消耗过多带宽资源,可以进入 Safari 设置,关闭 Fraudulent Website Warning 功能,并使用 REJECT 策略。
- DOMAIN,safebrowsing.googleapis.com,DIRECT # 如果您并不信任此服务提供商或防止其下载消耗过多带宽资源,可以进入 Safari 设置,关闭 Fraudulent Website Warning 功能,并使用 REJECT 策略。
Expand Down Expand Up @@ -36,6 +42,7 @@ rules:
- DOMAIN-SUFFIX,apple-cloudkit.com,DIRECT
- DOMAIN-SUFFIX,apple-mapkit.com,DIRECT
- DOMAIN,e.crashlytics.com,REJECT

# 国内网站
- DOMAIN-SUFFIX,cn,DIRECT
- DOMAIN-KEYWORD,-cn,DIRECT
Expand Down

0 comments on commit 227647a

Please sign in to comment.