From 0ba255e783266b71ae983ed54bcbc8e130445daa Mon Sep 17 00:00:00 2001 From: liangliangyy Date: Tue, 4 Apr 2023 13:38:42 +0800 Subject: [PATCH] add proxy --- oauth/oauthmanager.py | 41 +++++++++++++++++++++++++--------- servermanager/api/commonapi.py | 5 +++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/oauth/oauthmanager.py b/oauth/oauthmanager.py index 285cee5..6d7ebf9 100644 --- a/oauth/oauthmanager.py +++ b/oauth/oauthmanager.py @@ -1,5 +1,6 @@ import json import logging +import os import urllib.parse from abc import ABCMeta, abstractmethod @@ -145,7 +146,28 @@ class WBOauthManager(BaseOauthManager): return datas['avatar_large'] -class GoogleOauthManager(BaseOauthManager): +class ProxyManagerMixin: + def __init__(self, *args, **kwargs): + if os.environ.get("HTTP_PROXY"): + self.proxies = { + "http": os.environ.get("HTTP_PROXY"), + "https": os.environ.get("HTTP_PROXY") + } + else: + self.proxies = None + + def do_get(self, url, params, headers=None): + rsp = requests.get(url=url, params=params, headers=headers, proxies=self.proxies) + logger.info(rsp.text) + return rsp.text + + def do_post(self, url, params, headers=None): + rsp = requests.post(url, params, headers=headers, proxies=self.proxies) + logger.info(rsp.text) + return rsp.text + + +class GoogleOauthManager(ProxyManagerMixin, BaseOauthManager): AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth' TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token' API_URL = 'https://www.googleapis.com/oauth2/v3/userinfo' @@ -223,7 +245,7 @@ class GoogleOauthManager(BaseOauthManager): return datas['picture'] -class GitHubOauthManager(BaseOauthManager): +class GitHubOauthManager(ProxyManagerMixin, BaseOauthManager): AUTH_URL = 'https://github.com/login/oauth/authorize' TOKEN_URL = 'https://github.com/login/oauth/access_token' API_URL = 'https://api.github.com/user' @@ -240,14 +262,13 @@ class GitHubOauthManager(BaseOauthManager): access_token=access_token, openid=openid) - def get_authorization_url(self, nexturl='/'): + def get_authorization_url(self, next_url='/'): params = { 'client_id': self.client_id, 'response_type': 'code', - 'redirect_uri': self.callback_url + '&next_url=' + nexturl, + 'redirect_uri': f'{self.callback_url}&next_url={next_url}', 'scope': 'user' } - # url = self.AUTH_URL + "?" + urllib.parse.urlencode(params, quote_via=urllib.parse.quote) url = self.AUTH_URL + "?" + urllib.parse.urlencode(params) return url @@ -297,7 +318,7 @@ class GitHubOauthManager(BaseOauthManager): return datas['avatar_url'] -class FaceBookOauthManager(BaseOauthManager): +class FaceBookOauthManager(ProxyManagerMixin, BaseOauthManager): AUTH_URL = 'https://www.facebook.com/v2.10/dialog/oauth' TOKEN_URL = 'https://graph.facebook.com/v2.10/oauth/access_token' API_URL = 'https://graph.facebook.com/me' @@ -314,11 +335,11 @@ class FaceBookOauthManager(BaseOauthManager): access_token=access_token, openid=openid) - def get_authorization_url(self, nexturl='/'): + def get_authorization_url(self, next_url='/'): params = { 'client_id': self.client_id, 'response_type': 'code', - 'redirect_uri': self.callback_url, # + '&next_url=' + nexturl, + 'redirect_uri': self.callback_url, 'scope': 'email,public_profile' } url = self.AUTH_URL + "?" + urllib.parse.urlencode(params) @@ -389,11 +410,11 @@ class QQOauthManager(BaseOauthManager): access_token=access_token, openid=openid) - def get_authorization_url(self, nexturl='/'): + def get_authorization_url(self, next_url='/'): params = { 'response_type': 'code', 'client_id': self.client_id, - 'redirect_uri': self.callback_url + '&next_url=' + nexturl, + 'redirect_uri': self.callback_url + '&next_url=' + next_url, } url = self.AUTH_URL + "?" + urllib.parse.urlencode(params) return url diff --git a/servermanager/api/commonapi.py b/servermanager/api/commonapi.py index 3181500..fa146af 100644 --- a/servermanager/api/commonapi.py +++ b/servermanager/api/commonapi.py @@ -30,6 +30,11 @@ class CommandHandler: self.commands = commands.objects.all() def run(self, title): + """ + 运行命令 + :param title: 命令 + :return: 返回命令执行结果 + """ cmd = list( filter( lambda x: x.title.upper() == title.upper(),