fix #77, add ua config

This commit is contained in:
felinae98 2022-05-24 00:08:41 +08:00
parent 1e62b6beca
commit 1254995b3c
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
4 changed files with 9 additions and 7 deletions

View File

@ -137,6 +137,7 @@ sidebar: auto
:::
- `BISON_PROXY`: 使用的代理连接,形如`http://<ip>:<port>`(可选)
- `BISON_UA`: 使用的 User-Agent默认为 Chrome
## 使用

View File

@ -18,6 +18,7 @@ class PlugConfig(BaseSettings):
# 0不启用1首条消息单独发送剩余照片合并转发2以及以上所有消息全部合并转发
bison_resend_times: int = 0
bison_proxy: Optional[str]
bison_ua: str = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
class Config:
extra = "ignore"

View File

@ -4,9 +4,8 @@ import httpx
from ..plugin_config import plugin_config
if plugin_config.bison_proxy:
http_client = functools.partial(
httpx.AsyncClient, proxies=plugin_config.bison_proxy
)
else:
http_client = httpx.AsyncClient
http_client = functools.partial(
httpx.AsyncClient,
proxies=plugin_config.bison_proxy or None,
headers={"user-agent": plugin_config.bison_ua},
)

View File

@ -1,6 +1,5 @@
import pytest
from nonebug import App
from nonebug.fixture import nonebug_init
async def test_without_proxy(app: App):
@ -8,6 +7,8 @@ async def test_without_proxy(app: App):
c = http_client()
assert not c._mounts
req = c.build_request("GET", "http://example.com")
assert "Chrome" in req.headers["User-Agent"]
@pytest.mark.parametrize(