Skip to content

Commit

Permalink
feat: Filter nodes by location in UserNodeBaseView and UserSubManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jul 22, 2024
1 parent c02f6ba commit f33191b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
4 changes: 4 additions & 0 deletions apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def get_user_and_nodes(self, request):
native_ip = request.GET.get("native_ip")
if native_ip:
node_list = node_list.filter(native_ip=True)
location = request.GET.get("location")
if location:
node_list = node_list.filter(country=location)

if node_list.count() == 0:
return None, HttpResponseBadRequest("no active nodes for you")

Expand Down
50 changes: 50 additions & 0 deletions apps/proxy/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,56 @@ class ProxyNodeAdmin(admin.ModelAdmin):
list_editable = ["sequence"]
search_fields = ["name", "provider_remark"]

fieldsets = (
(
None,
{
"fields": (
"name",
"server",
"node_type",
"info",
"country",
"provider_remark",
"level",
"enable",
"enable_direct",
"enable_udp",
"native_ip",
)
},
),
(
"Traffic and Bandwidth",
{
"fields": (
"used_traffic",
"total_traffic",
"upload_bandwidth_bytes",
"download_bandwidth_bytes",
"current_used_upload_bandwidth_bytes",
"current_used_download_bandwidth_bytes",
)
},
),
(
"Xray and Ehco Configuration",
{
"fields": (
"xray_grpc_port",
"ehco_listen_host",
"ehco_listen_port",
"ehco_listen_type",
"ehco_transport_type",
"ehco_web_port",
"ehco_web_token",
"ehco_log_level",
"ehco_reload_interval",
)
},
),
)

def get_form(self, request, obj=None, **kwargs):
if obj:
help_texts = {
Expand Down
5 changes: 5 additions & 0 deletions apps/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def _get_clash_sub_yaml(self):
direct_ip_rule_set_url = user.direct_ip_rule_set_endpoint
direct_domain_rule_set_url = user.direct_domain_rule_set_endpoint

node_location_set = set()
for node in self.node_list:
node_location_set.add(node.country)

return render_to_string(
"clash/main.yaml",
{
Expand All @@ -48,6 +52,7 @@ def _get_clash_sub_yaml(self):
"native_ip_proxy_provider_url": native_ip_proxy_provider_url,
"direct_ip_rule_set_url": direct_ip_rule_set_url,
"direct_domain_rule_set_url": direct_domain_rule_set_url,
"node_location_set": node_location_set,
},
)

Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ django-cors-headers = "^4.2.0"
django-countries = "^7.1"
django-debug-toolbar = "^4.2.0"
django-prometheus = "^2.1.0"
django-simpleui = "^2023.8.28"
django-simpleui = "^2024.4.1"
django-telegram-login = "^0.2.3"
ipicn = "^2021.5.5"
ipython = "^8.19.0"
Expand Down
27 changes: 24 additions & 3 deletions templates/clash/main.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# see https://github.com/Loyalsoldier/clash-rules

mode: rule
port: 7890
socks-port: 7891
allow-lan: true
log-level: info
external-controller: 0.0.0.0:9090

proxy-groups:
- name: PROXY
type: select
proxies:
- auto
{% for loc in node_location_set %}- {{ loc }}
{% endfor %}
use:
- {{ provider_name }}-all

Expand All @@ -20,15 +20,22 @@ proxy-groups:
use:
- {{ provider_name }}-native-ip

{% for loc in node_location_set %}
- name: {{ loc }}
type: select
use:
- {{ provider_name }}-{{ loc }}
{% endfor %}

- name: auto
type: url-test
use:
- {{ provider_name }}-all
- {{ provider_name }}-native-ip
url: "http://www.gstatic.com/generate_204"
interval: 300
tolerance: 150
lazy: true

- name: Final
type: select
proxies:
Expand Down Expand Up @@ -59,6 +66,19 @@ proxy-providers:
tolerance: 150
lazy: true
url: http://www.gstatic.com/generate_204
{% for loc in node_location_set %}
{{ provider_name }}-{{ loc }}:
type: http
url: {{ all_proxy_provider_url|safe }}&location={{ loc }}
interval: 3600
path: ./providers/{{ provider_name }}-native-ip.yaml
health-check:
enable: true
interval: 300
tolerance: 150
lazy: true
url: http://www.gstatic.com/generate_204
{% endfor %}

rule-providers:
reject:
Expand Down Expand Up @@ -189,3 +209,4 @@ rules:
- GEOIP,LAN,DIRECT
- GEOIP,CN,DIRECT
- MATCH,Final

2 changes: 1 addition & 1 deletion templates/web/user_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ <h2 class="subtitle">根据您的客户端一键选择</h2>
<div class="icon is-small is-left">
<i class="fas fa-desktop"></i>
</div>
<span class="display-content">默认使用 clash</span>
<span class="display-content">默认使用 shadowrocket</span>
<span class="icon is-small">
<i class="fas fa-angle-down" aria-hidden="true"></i>
</span>
Expand Down

0 comments on commit f33191b

Please # to comment.