-
Notifications
You must be signed in to change notification settings - Fork 963
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
178 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
app_name = "openapi" | ||
urlpatterns = [ | ||
path("proxy_nodes/search/", views.ProxyNodeSearchView.as_view()), | ||
path("proxy_nodes/<int:node_id>/", views.ProxyNodeDetailView.as_view()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from functools import wraps | ||
|
||
from django.http import JsonResponse | ||
|
||
from apps.openapi.models import UserOpenAPIKey | ||
|
||
|
||
def gen_common_error_response(msg: str, status=400) -> JsonResponse: | ||
return JsonResponse({"error_msg": msg}, status=status) | ||
|
||
|
||
def openapi_authorized(view_func): | ||
@wraps(view_func) | ||
def wrapper(request, *args, **kwargs): | ||
key = request.META.get("HTTP_X_API_KEY", "") | ||
if not key: | ||
return gen_common_error_response( | ||
"x-api-key in header not found", status=401 | ||
) | ||
user_key = UserOpenAPIKey.get_by_key(key) | ||
if not user_key: | ||
return gen_common_error_response("x-api-key is invalid", status=401) | ||
request.user = user_key.user | ||
return view_func(request, *args, **kwargs) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,47 @@ | ||
# Create your views here. | ||
from django.forms import model_to_dict | ||
from django.http import JsonResponse | ||
from django.utils.decorators import method_decorator | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
|
||
from apps.openapi.utils import gen_common_error_response, openapi_authorized | ||
from apps.proxy.models import ProxyNode | ||
from apps.utils import handle_json_request | ||
|
||
|
||
class ProxyNodeSearchView(View): | ||
@csrf_exempt | ||
@method_decorator(openapi_authorized) | ||
def dispatch(self, *args, **kwargs): | ||
return super(ProxyNodeSearchView, self).dispatch(*args, **kwargs) | ||
|
||
def get(self, request): | ||
ip = request.GET.get("ip") | ||
if not ip: | ||
return gen_common_error_response("ip in query is required") | ||
|
||
node = ProxyNode.get_by_ip(ip) | ||
if not node: | ||
return gen_common_error_response( | ||
f"node with ip:{ip} not found", status=404 | ||
) | ||
return JsonResponse(model_to_dict(node)) | ||
|
||
|
||
class ProxyNodeDetailView(View): | ||
@csrf_exempt | ||
@method_decorator(openapi_authorized) | ||
@method_decorator(handle_json_request) | ||
def dispatch(self, *args, **kwargs): | ||
return super(ProxyNodeDetailView, self).dispatch(*args, **kwargs) | ||
|
||
def patch(self, request, node_id): | ||
node = ProxyNode.get_by_id(node_id) | ||
if not node: | ||
return gen_common_error_response( | ||
f"node with id:{node_id} not found", status=404 | ||
) | ||
enable = request.json.get("enable") | ||
node.enable = enable | ||
node.save() | ||
return JsonResponse(model_to_dict(node)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters