Skip to content

Commit

Permalink
feat: support record relay rule traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Feb 5, 2024
1 parent ddd7477 commit 0ebe86f
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 93 deletions.
79 changes: 71 additions & 8 deletions apps/api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import uuid
from typing import List

import pendulum
from django.conf import settings
Expand Down Expand Up @@ -50,7 +51,11 @@ def post(self, request):
if success := request.user.update_proxy_config_from_dict(
data=dict(request.POST.items())
):
data = {"title": "修改成功!", "status": "success", "subtitle": "请及时更换客户端配置!"}
data = {
"title": "修改成功!",
"status": "success",
"subtitle": "请及时更换客户端配置!",
}
else:
data = {
"title": "修改失败!",
Expand Down Expand Up @@ -167,6 +172,10 @@ def post(self, request, node_id):
class EhcoRelayConfigView(View):
"""中转机器"""

@csrf_exempt
def dispatch(self, *args, **kwargs):
return super(EhcoRelayConfigView, self).dispatch(*args, **kwargs)

@method_decorator(api_authorized)
def get(self, request, node_id):
node: m.RelayNode = m.RelayNode.get_or_none(node_id)
Expand All @@ -176,6 +185,28 @@ def get(self, request, node_id):
else HttpResponseBadRequest()
)

@method_decorator(handle_json_request)
@method_decorator(api_authorized)
def post(self, request, node_id):
node: m.RelayNode = m.RelayNode.get_or_none(node_id)
if not node:
return HttpResponseBadRequest()
if not request.json:
return JsonResponse(data={})

# TODO make this async
rules: List[m.RelayRule] = node.relay_rules.all()
name_rule_map = {rule.name: rule for rule in rules}
for data in request.json:
name = data["relay_label"]
if name in name_rule_map:
rule = name_rule_map[name]
rule.up_traffic += data["stats"]["up"]
rule.down_traffic += data["stats"]["down"]
for rule in rules:
rule.save()
return JsonResponse(data={})


class UserCheckInView(View):
@method_decorator(login_required)
Expand All @@ -190,7 +221,11 @@ def post(self, request):
"status": "success",
}
else:
data = {"title": "签到失败!", "subtitle": "今天已经签到过了", "status": "error"}
data = {
"title": "签到失败!",
"subtitle": "今天已经签到过了",
"status": "error",
}
return JsonResponse(data)


Expand All @@ -200,9 +235,17 @@ def get(self, request):
user = request.user
order = UserOrder.get_and_check_recent_created_order(user)
if order and order.status != UserOrder.STATUS_CREATED:
info = {"title": "充值成功!", "subtitle": "请去商品界面购买商品!", "status": "success"}
info = {
"title": "充值成功!",
"subtitle": "请去商品界面购买商品!",
"status": "success",
}
else:
info = {"title": "支付查询失败!", "subtitle": "亲,确认支付了么?", "status": "error"}
info = {
"title": "支付查询失败!",
"subtitle": "亲,确认支付了么?",
"status": "error",
}
return JsonResponse({"info": info})

@method_decorator(login_required)
Expand All @@ -213,7 +256,13 @@ def post(self, request):
raise ValueError
except ValueError:
return JsonResponse(
{"info": {"title": "校验失败", "subtitle": "请保证金额正确", "status": "error"}},
{
"info": {
"title": "校验失败",
"subtitle": "请保证金额正确",
"status": "error",
}
},
)

if settings.CHECK_PAY_REQ_IP_FROM_CN:
Expand Down Expand Up @@ -247,7 +296,13 @@ def purchase(request):
good_id = request.POST.get("goodId")
good = Goods.objects.get(id=good_id)
return (
JsonResponse({"title": "购买成功", "status": "success", "subtitle": "重新订阅即可获取所有节点"})
JsonResponse(
{
"title": "购买成功",
"status": "success",
"subtitle": "重新订阅即可获取所有节点",
}
)
if good.purchase_by_user(request.user)
else JsonResponse({"title": "余额不足", "status": "error", "subtitle": "先去捐赠充值那充值"})
)
Expand All @@ -262,7 +317,11 @@ def change_theme(request):
user = request.user
user.theme = theme
user.save()
res = {"title": "修改成功!", "subtitle": "主题更换成功,刷新页面可见", "status": "success"}
res = {
"title": "修改成功!",
"subtitle": "主题更换成功,刷新页面可见",
"status": "success",
}
return JsonResponse(res)


Expand All @@ -273,7 +332,11 @@ def reset_sub_uid(request):
"""
user = request.user
user.reset_sub_uid()
res = {"title": "修改成功!", "subtitle": "订阅更换成功,刷新页面可见", "status": "success"}
res = {
"title": "修改成功!",
"subtitle": "订阅更换成功,刷新页面可见",
"status": "success",
}
return JsonResponse(res)


Expand Down
51 changes: 35 additions & 16 deletions apps/proxy/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,6 @@ def get_formset(self, request, obj=None, **kwargs):
return super().get_formset(request, obj, **kwargs)


class RelayRuleInline(admin.TabularInline):
model = models.RelayRule
verbose_name = "中转规则配置"
extra = 0
fields = [
"rule_name",
"proxy_node",
"relay_node",
"relay_port",
"listen_type",
"transport_type",
]


class ProxyNodeAdmin(admin.ModelAdmin):
list_display = [
"__str__",
Expand All @@ -78,11 +64,10 @@ class ProxyNodeAdmin(admin.ModelAdmin):
"sequence",
"api_endpoint",
]
inlines = [RelayRuleInline, OccupancyConfigInline]
inlines = [OccupancyConfigInline]
all_inlines = [
TrojanConfigInline,
SSConfigInline,
RelayRuleInline,
OccupancyConfigInline,
]
list_filter = ["provider_remark", "country"]
Expand Down Expand Up @@ -181,6 +166,20 @@ def duplicate(self, request, queryset):
duplicate.type = "warning"


class RelayRuleInline(admin.TabularInline):
model = models.RelayRule
verbose_name = "中转规则配置"
extra = 0
fields = [
"name",
"relay_node",
"relay_port",
"proxy_nodes",
"listen_type",
"transport_type",
]


class RelayNodeAdmin(admin.ModelAdmin):
list_display = [
"__str__",
Expand Down Expand Up @@ -219,6 +218,24 @@ def toggle_enable(self, request, queryset):
toggle_enable.type = "danger"


class RelayRuleAdmin(admin.ModelAdmin):
list_display = [
"name",
"relay_node",
"relay_port",
"listen_type",
"transport_type",
"traffic_info",
]
list_filter = ["relay_node"]
list_per_page = 10
show_full_result_count = False

@admin.display(description="流量")
def traffic_info(self, instance):
return f"up:{traffic_format(instance.up_traffic) }/down:{traffic_format(instance.down_traffic)}"


class UserTrafficLogAdmin(admin.ModelAdmin):
list_display = [
"username",
Expand Down Expand Up @@ -334,6 +351,8 @@ def get_form(self, request, obj=None, **kwargs):
# Register your models here.
admin.site.register(models.ProxyNode, ProxyNodeAdmin)
admin.site.register(models.RelayNode, RelayNodeAdmin)
admin.site.register(models.RelayRule, RelayRuleAdmin)

admin.site.register(models.UserTrafficLog, UserTrafficLogAdmin)
admin.site.register(models.UserProxyNodeOccupancy, UserProxyNodeOccupancyAdmin)
admin.site.register(models.OccupancyConfig, OccupancyConfigAdmin)
44 changes: 44 additions & 0 deletions apps/proxy/migrations/0027_remove_relayrule_proxy_node_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 4.2.6 on 2024-02-05 01:48

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("proxy", "0026_alter_userproxynodeoccupancy_options_and_more"),
]

operations = [
migrations.RemoveField(
model_name="relayrule",
name="proxy_node",
),
migrations.RemoveField(
model_name="relayrule",
name="rule_name",
),
migrations.AddField(
model_name="relayrule",
name="down_traffic",
field=models.BigIntegerField(default=0, verbose_name="下载流量"),
),
migrations.AddField(
model_name="relayrule",
name="name",
field=models.CharField(
blank=True, default="", max_length=64, unique=True, verbose_name="规则名"
),
),
migrations.AddField(
model_name="relayrule",
name="proxy_nodes",
field=models.ManyToManyField(
related_name="relay_rules", to="proxy.proxynode", verbose_name="代理节点"
),
),
migrations.AddField(
model_name="relayrule",
name="up_traffic",
field=models.BigIntegerField(default=0, verbose_name="上传流量"),
),
]
Loading

0 comments on commit 0ebe86f

Please sign in to comment.