feat: SOCKS5代理支持、落地IP国家查询、设置页优化
- 代理支持SOCKS5和HTTP两种类型切换 - 落地IP查询显示国家、城市、ISP信息 - 设置页面不再隐藏已配置的值 - Airwallex API异常统一返回400+详细错误信息
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
"""Airwallex client with proxy support."""
|
||||
"""Airwallex client with proxy support (HTTP and SOCKS5)."""
|
||||
import logging
|
||||
import httpx
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from airwallex.client import AirwallexClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ProxiedAirwallexClient(AirwallexClient):
|
||||
"""AirwallexClient that routes requests through an HTTP proxy."""
|
||||
"""AirwallexClient that routes requests through an HTTP or SOCKS5 proxy."""
|
||||
|
||||
def __init__(self, proxy_url: str | None = None, **kwargs):
|
||||
self._proxy_url = proxy_url
|
||||
@@ -17,16 +21,20 @@ class ProxiedAirwallexClient(AirwallexClient):
|
||||
timeout=self.request_timeout,
|
||||
proxy=proxy_url,
|
||||
)
|
||||
safe_url = proxy_url.split("@")[-1] if "@" in proxy_url else proxy_url
|
||||
logger.info("Airwallex client initialized with proxy: %s", safe_url)
|
||||
else:
|
||||
logger.info("Airwallex client initialized without proxy")
|
||||
|
||||
def authenticate(self) -> None:
|
||||
"""Override authenticate to use proxy for auth requests too."""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
if self._token and self._token_expiry and datetime.now(timezone.utc) < self._token_expiry:
|
||||
return
|
||||
|
||||
# Use a proxied client for authentication
|
||||
auth_kwargs = {"timeout": self.request_timeout}
|
||||
logger.info("Authenticating with Airwallex API at %s (proxy: %s)",
|
||||
self.auth_url, bool(self._proxy_url))
|
||||
|
||||
auth_kwargs: dict = {"timeout": self.request_timeout}
|
||||
if self._proxy_url:
|
||||
auth_kwargs["proxy"] = self._proxy_url
|
||||
|
||||
@@ -41,11 +49,15 @@ class ProxiedAirwallexClient(AirwallexClient):
|
||||
},
|
||||
content="{}",
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
if response.status_code != 200:
|
||||
body = response.text[:300]
|
||||
logger.error("Auth failed: HTTP %d, body: %s", response.status_code, body)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
self._token = data.get("token")
|
||||
# Token valid for 30 minutes, refresh a bit early
|
||||
from datetime import timedelta
|
||||
self._token_expiry = datetime.now(timezone.utc) + timedelta(minutes=28)
|
||||
logger.info("Authentication successful, token expires in 28 minutes")
|
||||
finally:
|
||||
auth_client.close()
|
||||
|
||||
Reference in New Issue
Block a user