优化 CK 导入和监控配置
This commit is contained in:
+97
-11
@@ -24,15 +24,27 @@ func main() {
|
||||
cookieFile = flag.String("cookie-file", "", "Cookie 文件,一行一个账号")
|
||||
sid = flag.Int("sid", 0, "活动 SID")
|
||||
pid = flag.Int("pid", 0, "奖品 PID")
|
||||
pids = flag.String("pids", "", "多个奖品 PID,逗号分隔")
|
||||
name = flag.String("name", "", "按奖品名包含匹配")
|
||||
names = flag.String("names", "", "多个奖品名,逗号分隔")
|
||||
autoSelect = flag.Bool("auto", false, "monitor 自动选择列表里的可兑奖品")
|
||||
loops = flag.Int("loops", 1, "monitor 循环次数")
|
||||
delayMS = flag.Int("delay", 150, "monitor 每轮间隔毫秒")
|
||||
concurrency = flag.Int("concurrency", 5, "monitor/exchange 并发账号数")
|
||||
stopAfterSuccess = flag.Bool("stop-after-success", true, "monitor 成功后停止")
|
||||
stopAfterSuccess = flag.Bool("stop-after-success", false, "monitor 任意账号成功后停止全部")
|
||||
accountStop = flag.Bool("account-stop", true, "monitor 单账号成功后停止该账号该奖品")
|
||||
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")
|
||||
wssHost = flag.String("wss-host", huya.DefaultWSSHost, "WSS host,来自 launch.queryHttpDns 或抓包")
|
||||
prewarm = flag.Bool("prewarm", true, "monitor 开始前预热账号连接")
|
||||
directExchange = flag.Bool("direct-exchange", true, "monitor 已知 PID 时跳过列表直接兑换")
|
||||
listBefore = flag.Bool("list-before-exchange", false, "monitor 即使已知 PID 也先查询列表")
|
||||
shareList = flag.Bool("share-list", true, "monitor 多账号共享同一轮奖品列表")
|
||||
refreshSuccess = flag.Bool("refresh-after-success", true, "monitor 成功后异步刷新积分和记录")
|
||||
freqBackoffMS = flag.Int("freq-backoff", 300, "操作频繁后的账号退避毫秒")
|
||||
errorBackoffMS = flag.Int("error-backoff", 300, "请求异常后的账号退避毫秒")
|
||||
verbose = flag.Bool("v", false, "输出调试日志")
|
||||
)
|
||||
flag.Parse()
|
||||
@@ -65,22 +77,25 @@ func main() {
|
||||
defer closeClient()
|
||||
ctx := context.Background()
|
||||
|
||||
var startAt time.Time
|
||||
if *at != "" {
|
||||
when, err := parseTime(*at)
|
||||
if err != nil {
|
||||
exitErr(err)
|
||||
}
|
||||
waitUntil(ctx, when)
|
||||
startAt = when
|
||||
}
|
||||
|
||||
switch *cmd {
|
||||
case "list":
|
||||
waitForCommand(ctx, startAt)
|
||||
resp, err := client.GetActPrizeList(ctx, accounts[0].UID, accounts[0].Cookie, int32(*sid))
|
||||
if err != nil {
|
||||
exitErr(err)
|
||||
}
|
||||
printJSON(resp)
|
||||
case "score":
|
||||
waitForCommand(ctx, startAt)
|
||||
results := make([]map[string]any, 0, len(accounts))
|
||||
for _, account := range accounts {
|
||||
resp, err := client.QueryUserScore(ctx, account.UID, account.Cookie, int32(*sid))
|
||||
@@ -93,6 +108,7 @@ func main() {
|
||||
}
|
||||
printJSON(results)
|
||||
case "records":
|
||||
waitForCommand(ctx, startAt)
|
||||
results := make([]map[string]any, 0, len(accounts))
|
||||
for _, account := range accounts {
|
||||
resp, err := client.GetUserPrizeRecords(ctx, account.UID, account.Cookie, int32(*sid))
|
||||
@@ -107,24 +123,42 @@ func main() {
|
||||
if *pid <= 0 {
|
||||
exitErr(errors.New("exchange 必须提供 -pid"))
|
||||
}
|
||||
waitForCommand(ctx, startAt)
|
||||
runExchange(ctx, client, accounts, int32(*sid), int32(*pid), *concurrency)
|
||||
case "monitor":
|
||||
if *pid <= 0 && strings.TrimSpace(*name) == "" {
|
||||
targets := parseTargets(*pid, *pids, *name, *names, *autoSelect)
|
||||
if len(targets) == 0 {
|
||||
exitErr(errors.New("monitor 必须提供 -pid 或 -name"))
|
||||
}
|
||||
runner := sniper.Runner{Client: client}
|
||||
var outMu sync.Mutex
|
||||
err := runner.Run(ctx, accounts, sniper.Target{PID: int32(*pid), Name: strings.TrimSpace(*name)}, sniper.Config{
|
||||
SID: int32(*sid),
|
||||
Loops: *loops,
|
||||
Delay: time.Duration(*delayMS) * time.Millisecond,
|
||||
Concurrency: *concurrency,
|
||||
StopAfterSuccess: *stopAfterSuccess,
|
||||
}, func(result sniper.Result) {
|
||||
emit := func(result sniper.Result) {
|
||||
outMu.Lock()
|
||||
defer outMu.Unlock()
|
||||
_ = json.NewEncoder(os.Stdout).Encode(result)
|
||||
})
|
||||
}
|
||||
cfg := sniper.Config{
|
||||
SID: int32(*sid),
|
||||
Loops: *loops,
|
||||
Delay: time.Duration(*delayMS) * time.Millisecond,
|
||||
Concurrency: *concurrency,
|
||||
StopAfterSuccess: *stopAfterSuccess,
|
||||
StopAccountAfterSuccess: *accountStop,
|
||||
Prewarm: false,
|
||||
DirectExchange: *directExchange,
|
||||
ListBeforeExchange: *listBefore,
|
||||
SharePrizeList: *shareList,
|
||||
RefreshAfterSuccess: *refreshSuccess,
|
||||
TooFrequentBackoff: time.Duration(*freqBackoffMS) * time.Millisecond,
|
||||
ErrorBackoff: time.Duration(*errorBackoffMS) * time.Millisecond,
|
||||
}
|
||||
if *prewarm {
|
||||
if err := runner.Prewarm(ctx, accounts, cfg, emit); err != nil && !errors.Is(err, context.Canceled) {
|
||||
exitErr(err)
|
||||
}
|
||||
}
|
||||
waitForMonitor(ctx, startAt, time.Duration(*leadMS)*time.Millisecond)
|
||||
err := runner.RunTargets(ctx, accounts, targets, cfg, emit)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
exitErr(err)
|
||||
}
|
||||
@@ -133,6 +167,20 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func waitForCommand(ctx context.Context, startAt time.Time) {
|
||||
if !startAt.IsZero() {
|
||||
waitUntil(ctx, startAt)
|
||||
}
|
||||
}
|
||||
|
||||
func waitForMonitor(ctx context.Context, startAt time.Time, lead time.Duration) {
|
||||
if startAt.IsZero() {
|
||||
return
|
||||
}
|
||||
when := startAt.Add(-lead)
|
||||
waitUntil(ctx, when)
|
||||
}
|
||||
|
||||
func runExchange(ctx context.Context, client huya.ActivityClient, accounts []sniper.Account, sid, pid int32, concurrency int) {
|
||||
if concurrency <= 0 {
|
||||
concurrency = 1
|
||||
@@ -189,6 +237,44 @@ func loadCookies(cookie, file string) ([]string, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseTargets(pid int, pids, name, names string, autoSelect bool) []sniper.Target {
|
||||
targets := make([]sniper.Target, 0)
|
||||
name = strings.TrimSpace(name)
|
||||
if pid > 0 || name != "" {
|
||||
targets = append(targets, sniper.Target{PID: int32(pid), Name: name})
|
||||
}
|
||||
if autoSelect {
|
||||
targets = append(targets, sniper.Target{Auto: true})
|
||||
}
|
||||
for _, text := range splitList(pids) {
|
||||
value, err := strconv.Atoi(text)
|
||||
if err == nil && value > 0 {
|
||||
targets = append(targets, sniper.Target{PID: int32(value)})
|
||||
}
|
||||
}
|
||||
for _, text := range splitList(names) {
|
||||
if text == "auto" || text == "自动" || text == "自动选择" {
|
||||
targets = append(targets, sniper.Target{Auto: true})
|
||||
continue
|
||||
}
|
||||
targets = append(targets, sniper.Target{Name: text})
|
||||
}
|
||||
return targets
|
||||
}
|
||||
|
||||
func splitList(text string) []string {
|
||||
replacer := strings.NewReplacer(",", ",", "\n", ",", "\t", ",", " ", ",")
|
||||
parts := strings.Split(replacer.Replace(text), ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
item := strings.TrimSpace(part)
|
||||
if item != "" {
|
||||
out = append(out, item)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseTime(text string) (time.Time, error) {
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
|
||||
Reference in New Issue
Block a user