Skip to content

Commit

Permalink
proxy: 添加token机制
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryJi529 committed Jun 7, 2023
1 parent 98fea7a commit 6a0f46f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions Morningstar/settings/jazzmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"poll.question": "fas fa-question",
"poll.choice": "fas fa-arrow-circle-right",
"proxy": "fas fa-project-diagram",
"proxy.account": "fas fa-user-circle",
"proxy.node": "fas fa-link",
"proxy.subscribeurl": "fas fa-external-link-square-alt",
"share": "fas fa-share-alt",
Expand Down
22 changes: 21 additions & 1 deletion apps/proxy/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import base64
import json
import hashlib
import uuid

from django.contrib import admin

Expand All @@ -9,7 +11,7 @@
from import_export.fields import Field
from import_export.admin import ImportExportModelAdmin

from .models import Node, SubscribeUrl
from .models import Node, SubscribeUrl, Account


@admin.register(Node)
Expand Down Expand Up @@ -55,3 +57,21 @@ class SubscribeUrlAdmin(ImportExportModelAdmin):
"id",
"name",
)


@admin.register(Account)
class AccountAdmin(ImportExportModelAdmin):
list_display = ("id", "user", "token")

def save_model(self, request, obj, form, change):
def generate_token(id):
# 使用 uuid4 生成一个随机字符串
salt = uuid.uuid4().hex
# 将 salt 和 id 拼接,然后计算其 SHA-256 哈希值
hashed = hashlib.sha256(salt.encode() + str(id).encode()).hexdigest()
# 将 salt 和 hashed 值拼接,作为最终的 token
return (salt + hashed)[:16]

obj.token = generate_token(obj.user.id) if not obj.token else obj.token
print(generate_token(obj.user.id))
super().save_model(request, obj, form, change)
30 changes: 30 additions & 0 deletions apps/proxy/migrations/0003_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.1 on 2023-06-07 08:10

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('proxy', '0002_subscribeurl'),
]

operations = [
migrations.CreateModel(
name='Account',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('token', models.CharField(max_length=64, unique=True, verbose_name='令牌')),
('updated', models.DateTimeField(auto_now=True, verbose_name='更新时间')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='用户')),
],
options={
'verbose_name': '令牌',
'verbose_name_plural': '令牌',
'ordering': ['-updated'],
},
),
]
18 changes: 18 additions & 0 deletions apps/proxy/migrations/0004_alter_account_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.1 on 2023-06-07 08:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('proxy', '0003_account'),
]

operations = [
migrations.AlterField(
model_name='account',
name='token',
field=models.CharField(blank=True, max_length=64, unique=True, verbose_name='令牌'),
),
]
16 changes: 16 additions & 0 deletions apps/proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
from django.db import models

from Morningstar.models import User


class Node(models.Model):
name = models.CharField("名称", max_length=64, unique=True)
Expand Down Expand Up @@ -30,3 +32,17 @@ class Meta:

def __str__(self):
return self.name


class Account(models.Model):
user = models.ForeignKey(User, verbose_name="用户", on_delete=models.CASCADE)
token = models.CharField("令牌", max_length=64, unique=True, blank=True)
updated = models.DateTimeField("更新时间", auto_now=True)

class Meta:
verbose_name = "账户"
verbose_name_plural = verbose_name
ordering = ["-updated"]

def __str__(self):
return self.user.username
10 changes: 7 additions & 3 deletions apps/proxy/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import base64

from django.http import HttpResponse
from django.http import HttpResponse, HttpRequest
from django.shortcuts import render, redirect

from .lib import Ghelper
from .models import Node, SubscribeUrl
from .models import Node, SubscribeUrl, Account


def index(request):
def index(request: HttpRequest):
if request.GET.get("token") not in [
account.token for account in Account.objects.all()
]:
return HttpResponse("You Need A Token...")
links = [node.link for node in Node.objects.all()]
for ind, subscribeUrl in enumerate(SubscribeUrl.objects.all()):
url = subscribeUrl.link
Expand Down

0 comments on commit 6a0f46f

Please sign in to comment.