diff --git a/README.md b/README.md index 6aab537..8445fb2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # huya-go -虎牙活动积分兑换/抢购的 Go 复刻版。支持两种传输层,CLI 与 GUI 默认都走 `http`: +虎牙活动积分兑换/抢购的 Go 复刻版。支持两种传输层,CLI 与 GUI 默认都走 `wss`: -- `http`(默认):走 `https://cdnws.api.huya.com/`,兼容原有命令。 -- `wss`:实验实现。CLI 需要显式指定 `-transport wss`,GUI 在设置里切换;已按浏览器 HAR 改为 TAF `WebSocketCommand` 外层和 WSS 初始化流程,仍建议先用真实 Cookie 小流量验证。 +- `wss`(默认):浏览器实际使用的通道。先调用 `launch.queryHttpDns` 解析可用 host,再走 TAF `WebSocketCommand` 外层与 WSS 初始化流程(`launch.wsLaunch` → `mobileui.getConfig`)。host 留空即自动解析,解析失败会回退内置默认 host。 +- `http`:走 `https://cdnws.api.huya.com/`,作为可回退通道;CLI 用 `-transport http`,GUI 在设置里切换。 ## 构建 @@ -57,21 +57,23 @@ CLI 示例: 直接兑换指定 PID: ```bash -./huya-go -cmd exchange -sid 2203 -pid 12889 -cookie-file cookie.txt -transport http +./huya-go -cmd exchange -sid 2203 -pid 12889 -cookie-file cookie.txt ``` 按奖品名轮询并抢购: ```bash -./huya-go -cmd monitor -sid 2203 -name 小黄鸭 -cookie-file cookie.txt -transport http -loops 20 -delay 120 -concurrency 5 +./huya-go -cmd monitor -sid 2203 -name 小黄鸭 -cookie-file cookie.txt -loops 20 -delay 120 -concurrency 5 ``` 定时开始: ```bash -./huya-go -cmd monitor -sid 2203 -pid 12889 -cookie-file cookie.txt -transport http -at "2026-07-06 20:00:00" +./huya-go -cmd monitor -sid 2203 -pid 12889 -cookie-file cookie.txt -at "2026-07-06 20:00:00" ``` +以上默认都走 `wss`。需要回退到 http 传输时,给任意命令加上 `-transport http`。 + ## 多开(多实例) 同一台机器可以同时启动多个实例,互不干扰: @@ -103,7 +105,7 @@ GUI 与 CLI 共用同一套结构化日志(分类 + 等级 + 账号标签) - `-min-left`:目标奖品最少剩余库存,`0` 表示不限制(CLI 与 GUI 设置都支持)。 - `-cookie`:单账号 Cookie。 - `-cookie-file`:多账号 Cookie 文件。 -- `-transport`:`http` 或 `wss`,默认 `http`;WSS 仍为实验通道。 +- `-transport`:`wss` 或 `http`,默认 `wss`。 - `-wss-host`:WebSocket host。默认留空,自动调用 `launch.queryHttpDns` 解析(带 TTL 缓存、多 host 轮询),解析失败时回退内置默认 host;需要固定时可显式指定。 - `-timeout`:单次调用超时秒数。 - `-loops`、`-delay`、`-concurrency`:轮询次数、轮询间隔、并发账号数。 diff --git a/cmd/huya-go/main.go b/cmd/huya-go/main.go index 6d8d376..70c2024 100644 --- a/cmd/huya-go/main.go +++ b/cmd/huya-go/main.go @@ -49,7 +49,7 @@ func main() { at = flag.String("at", "", "定时开始时间,支持 RFC3339 或 2006-01-02 15:04:05") leadMS = flag.Int("lead", 2500, "monitor 相对 -at 提前启动毫秒") timeoutSec = flag.Int("timeout", 15, "单次请求超时秒数") - transport = flag.String("transport", "http", "传输层:http/wss") + transport = flag.String("transport", "wss", "传输层:wss/http,默认 wss") wssHost = flag.String("wss-host", "", "WSS host,留空则通过 launch.queryHttpDns 自动解析") prewarm = flag.Bool("prewarm", true, "monitor 开始前预热账号连接") directExchange = flag.Bool("direct-exchange", true, "monitor 已知 PID 时跳过列表直接兑换") diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 4847854..2e0ab77 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -492,7 +492,7 @@ function newCookieID(): string { function pickConfig(values: Partial): UiConfig { return { - transport: values.transport || 'http', + transport: values.transport || 'wss', wssHost: values.wssHost || '', sid: Number(values.sid || 0), pid: Number(values.pid || 0), @@ -1514,7 +1514,7 @@ export default function Dashboard() { > {instanceInfo?.name || '默认实例'} - {String(watchedTransport || 'http').toUpperCase()} + {String(watchedTransport || 'wss').toUpperCase()} diff --git a/internal/gui/app.go b/internal/gui/app.go index b69f2f9..7f12057 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -283,7 +283,7 @@ func (a *App) Shutdown(ctx context.Context) { func (a *App) DefaultConfig() Config { return Config{ - Transport: "http", + Transport: "wss", WSSHost: "", SID: 2203, Loops: 20, @@ -606,7 +606,7 @@ func (a *App) emit(event string, data any) { func normalizeConfig(cfg Config) Config { if strings.TrimSpace(cfg.Transport) == "" { - cfg.Transport = "http" + cfg.Transport = "wss" } // WSSHost 留空表示走 launch.queryHttpDns 自动解析,不在这里补默认值。 if cfg.Loops <= 0 { diff --git a/internal/huya/client.go b/internal/huya/client.go index c6d5170..8cc8e34 100644 --- a/internal/huya/client.go +++ b/internal/huya/client.go @@ -16,15 +16,10 @@ type ActivityClient interface { } // NewActivityClient 按传输层创建活动客户端,供 CLI 和 GUI 共用。 +// 默认走 wss(留空即 wss);需要回退时显式传 "http"。 func NewActivityClient(transport, wssHost string, timeout time.Duration, logger func(string)) (ActivityClient, func(), error) { switch strings.ToLower(strings.TrimSpace(transport)) { - case "", "http": - client := NewHTTPClient(logger) - if timeout > 0 { - client.Client.Timeout = timeout - } - return client, func() {}, nil - case "wss", "ws", "websocket": + case "", "wss", "ws", "websocket": client := NewWSSClient(logger) // 留空(或沿用历史默认 host)表示交给 launch.queryHttpDns 自动解析, // 只有显式指定的其它 host 才固定使用。 @@ -36,6 +31,12 @@ func NewActivityClient(transport, wssHost string, timeout time.Duration, logger client.CallTimeout = timeout } return client, func() { _ = client.Close() }, nil + case "http": + client := NewHTTPClient(logger) + if timeout > 0 { + client.Client.Timeout = timeout + } + return client, func() {}, nil default: return nil, func() {}, fmt.Errorf("未知传输层: %s", transport) }