Skip to content

Commit

Permalink
add local/proxy login switcher
Browse files Browse the repository at this point in the history
Signed-off-by: pengzhile <[email protected]>
  • Loading branch information
pengzhile committed Apr 3, 2023
1 parent 2a69b9c commit 1a07518
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
* `-t``--token_file` 指定一个存放`Access Token`的文件,使用`Access Token`登录。
* `-s``--server``http`服务方式启动,格式:`ip:port`
* `-a``--api` 使用`gpt-3.5-turbo`API请求,**你可能需要向`OpenAI`支付费用**
* `-l``--local` 使用本地环境登录,**你可能需要一个合适的代理IP以避免账号被风控!**
* `--sentry` 启用`sentry`框架来发送错误报告供作者查错,敏感信息**不会被发送**
* `-v``--verbose` 显示调试信息,且出错时打印异常堆栈信息,供查错使用。

Expand All @@ -118,6 +119,7 @@
* `PANDORA_PROXY` 指定代理,格式:`protocol://user:pass@ip:port`
* `PANDORA_SERVER``http`服务方式启动,格式:`ip:port`
* `PANDORA_API` 使用`gpt-3.5-turbo`API请求,**你可能需要向`OpenAI`支付费用**
* `PANDORA_LOGIN_LOCAL` 使用本地环境登录,**你可能需要一个合适的代理IP以避免账号被风控!**
* `PANDORA_SENTRY` 启用`sentry`框架来发送错误报告供作者查错,敏感信息**不会被发送**
* `PANDORA_VERBOSE` 显示调试信息,且出错时打印异常堆栈信息,供查错使用。
* 使用Docker方式,设置环境变量即可,无视上述`程序参数`
Expand Down
4 changes: 4 additions & 0 deletions bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ if [ -n "${PANDORA_API}" ]; then
PANDORA_ARGS="${PANDORA_ARGS} -a"
fi

if [ -n "${PANDORA_LOGIN_LOCAL}" ]; then
PANDORA_ARGS="${PANDORA_ARGS} -l"
fi

if [ -n "${PANDORA_SENTRY}" ]; then
PANDORA_ARGS="${PANDORA_ARGS} --sentry"
fi
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
install_requires=requirements,
extras_require={
'api': requirements_api,
'cloud': ['pandora-cloud~=0.0.1'],
'cloud': ['pandora-cloud~=0.0.4'],
},
entry_points={
'console_scripts': [
Expand Down
2 changes: 1 addition & 1 deletion src/pandora/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = '0.9.6'
__version__ = '0.9.7'
8 changes: 7 additions & 1 deletion src/pandora/cloud_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def main():
type=str,
default='127.0.0.1:8018',
)
parser.add_argument(
'-l',
'--local',
help='Login locally. Pay attention to the risk control of the login ip!',
action='store_true',
)
parser.add_argument(
'--sentry',
help='Enable sentry to send error reports when errors occur.',
Expand All @@ -60,7 +66,7 @@ def main():
try:
from pandora_cloud.server import ChatBot as CloudServer

return CloudServer(args.proxy, args.verbose, args.sentry).run(args.server)
return CloudServer(args.proxy, args.verbose, args.sentry, args.local).run(args.server)
except (ImportError, ModuleNotFoundError):
Console.error_bh('### You need `pip install Pandora-ChatGPT[cloud]` to support cloud mode.')

Expand Down
10 changes: 9 additions & 1 deletion src/pandora/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ def main():
help='Use gpt-3.5-turbo chat api. Note: OpenAI will bill you.',
action='store_true',
)
parser.add_argument(
'-l',
'--local',
help='Login locally. Pay attention to the risk control of the login ip!',
action='store_true',
)
parser.add_argument(
'--sentry',
help='Enable sentry to send error reports when errors occur.',
Expand Down Expand Up @@ -161,10 +167,12 @@ def main():
access_token, need_save = confirm_access_token(args.token_file, args.server, args.api)
if not access_token:
Console.info_b('Please enter your email and password to log in ChatGPT!')
if not args.local:
Console.warn('We login via https://chat.gateway.do, but it doesn\'t retain your data.')
email = getenv('OPENAI_EMAIL') or Prompt.ask(' Email')
password = getenv('OPENAI_PASSWORD') or Prompt.ask(' Password', password=True)
Console.warn('### Do login, please wait...')
access_token = Auth0(email, password, args.proxy).auth()
access_token = Auth0(email, password, args.proxy).auth(args.local)

if not check_access_token_out(access_token, args.api):
return
Expand Down
26 changes: 24 additions & 2 deletions src/pandora/openai/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ def __check_email(email: str):
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
return re.fullmatch(regex, email)

def auth(self) -> str:
def auth(self, login_local=False) -> str:
if self.use_cache and self.access_token and self.expires and self.expires > dt.now():
return self.access_token

if not self.__check_email(self.email) or not self.password:
raise Exception('invalid email or password.')

return self.__part_two()
return self.__part_two() if login_local else self.get_access_token_proxy()

def __part_two(self) -> str:
url = 'https://chat.gateway.do/auth/endpoint'
Expand Down Expand Up @@ -145,3 +145,25 @@ def get_access_token(self, state1: str, callback_url: str) -> str:
return self.access_token
else:
raise Exception(resp.text)

def get_access_token_proxy(self) -> str:
url = 'https://chat.gateway.do/api/auth/login'
headers = {
'User-Agent': self.user_agent,
}
data = {
'username': self.email,
'password': self.password,
}
resp = self.session.post(url=url, headers=headers, data=data, allow_redirects=False, **self.req_kwargs)

if resp.status_code == 200:
json = resp.json()
if 'accessToken' not in json:
raise Exception('Get access token failed.')

self.access_token = json['accessToken']
self.expires = dt.strptime(json['expires'], '%Y-%m-%dT%H:%M:%S.%fZ') - datetime.timedelta(minutes=5)
return self.access_token
else:
raise Exception('Error get access token.')

0 comments on commit 1a07518

Please sign in to comment.