feat: 添加IP竞速拨号器与IP偏好设置,连接状态显示域名与实际IP
- 新增竞速拨号器(racedial.go):域名连接时并行解析所有A/AAAA记录, 同时对所有IP发起TCP连接,第一个成功的胜出,确保选择延迟最低的地址 - ServerProfile新增IPPreference字段(auto/v4/v6),支持用户指定IP版本偏好 - 填写服务器IP时跳过域名解析直接使用IP,顺序failover - 连接成功后状态栏显示「已连接 (域名 -> 实际IP)」 - DB迁移v6:server_profiles表添加ip_preference列 - WebSocket拨号与HTTP登录均使用竞速拨号器 - 补充中英文i18n翻译
This commit is contained in:
+12
-4
@@ -14,6 +14,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lmvpn/internal/transport"
|
||||
)
|
||||
|
||||
// LoginResult holds the response from a successful /api/login call.
|
||||
@@ -49,19 +51,25 @@ type errorResponse struct {
|
||||
// host will be an IP address, but the certificate must be verified
|
||||
// against the real hostname (set tlsCfg.ServerName).
|
||||
//
|
||||
// ipPreference controls which IP address families are used when
|
||||
// resolving the server hostname ("auto", "v4", "v6").
|
||||
//
|
||||
// ctx allows cancellation of the HTTP request (e.g. when the VPN
|
||||
// session is disconnected while login is in flight).
|
||||
func Login(ctx context.Context, baseURL, username, password string, tlsCfg *tls.Config) (*LoginResult, error) {
|
||||
func Login(ctx context.Context, baseURL, username, password string, tlsCfg *tls.Config, ipPreference string) (*LoginResult, error) {
|
||||
body, err := json.Marshal(loginRequest{Username: username, Password: password})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := strings.TrimRight(baseURL, "/") + "/api/login"
|
||||
client := &http.Client{Timeout: 15 * time.Second}
|
||||
if tlsCfg != nil {
|
||||
client.Transport = &http.Transport{TLSClientConfig: tlsCfg}
|
||||
httpTransport := &http.Transport{
|
||||
DialContext: transport.NewRaceDialer(ipPreference),
|
||||
}
|
||||
if tlsCfg != nil {
|
||||
httpTransport.TLSClientConfig = tlsCfg
|
||||
}
|
||||
client := &http.Client{Timeout: 15 * time.Second, Transport: httpTransport}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user