优化 CK 导入和监控配置
This commit is contained in:
+162
-48
@@ -34,18 +34,29 @@ type App struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Transport string `json:"transport"`
|
||||
WSSHost string `json:"wssHost"`
|
||||
SID int32 `json:"sid"`
|
||||
PID int32 `json:"pid"`
|
||||
Name string `json:"name"`
|
||||
CookieText string `json:"cookieText"`
|
||||
Loops int `json:"loops"`
|
||||
DelayMS int `json:"delayMs"`
|
||||
Concurrency int `json:"concurrency"`
|
||||
StopAfterSuccess bool `json:"stopAfterSuccess"`
|
||||
TimeoutSec int `json:"timeoutSec"`
|
||||
StartAt string `json:"startAt"`
|
||||
Transport string `json:"transport"`
|
||||
WSSHost string `json:"wssHost"`
|
||||
SID int32 `json:"sid"`
|
||||
PID int32 `json:"pid"`
|
||||
Name string `json:"name"`
|
||||
TargetText string `json:"targetText"`
|
||||
CookieText string `json:"cookieText"`
|
||||
SelectedAccountLines []int `json:"selectedAccountLines"`
|
||||
Loops int `json:"loops"`
|
||||
DelayMS int `json:"delayMs"`
|
||||
Concurrency int `json:"concurrency"`
|
||||
StopAfterSuccess bool `json:"stopAfterSuccess"`
|
||||
StopAccountAfterSuccess bool `json:"stopAccountAfterSuccess"`
|
||||
Prewarm bool `json:"prewarm"`
|
||||
DirectExchange bool `json:"directExchange"`
|
||||
SharePrizeList bool `json:"sharePrizeList"`
|
||||
AutoSelect bool `json:"autoSelect"`
|
||||
RefreshAfterSuccess bool `json:"refreshAfterSuccess"`
|
||||
LeadMS int `json:"leadMs"`
|
||||
FrequentBackoffMS int `json:"frequentBackoffMs"`
|
||||
ErrorBackoffMS int `json:"errorBackoffMs"`
|
||||
TimeoutSec int `json:"timeoutSec"`
|
||||
StartAt string `json:"startAt"`
|
||||
}
|
||||
|
||||
type AccountPreview struct {
|
||||
@@ -114,16 +125,23 @@ func (a *App) Shutdown(ctx context.Context) {
|
||||
|
||||
func (a *App) DefaultConfig() Config {
|
||||
return Config{
|
||||
Transport: "http",
|
||||
WSSHost: huya.DefaultWSSHost,
|
||||
SID: 2203,
|
||||
PID: 12889,
|
||||
Name: "小黄鸭",
|
||||
Loops: 20,
|
||||
DelayMS: 120,
|
||||
Concurrency: 5,
|
||||
StopAfterSuccess: true,
|
||||
TimeoutSec: 15,
|
||||
Transport: "http",
|
||||
WSSHost: huya.DefaultWSSHost,
|
||||
SID: 2203,
|
||||
Loops: 20,
|
||||
DelayMS: 120,
|
||||
Concurrency: 5,
|
||||
StopAfterSuccess: false,
|
||||
StopAccountAfterSuccess: true,
|
||||
Prewarm: true,
|
||||
DirectExchange: true,
|
||||
SharePrizeList: true,
|
||||
AutoSelect: false,
|
||||
RefreshAfterSuccess: true,
|
||||
LeadMS: 2500,
|
||||
FrequentBackoffMS: 300,
|
||||
ErrorBackoffMS: 300,
|
||||
TimeoutSec: 15,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +172,7 @@ func (a *App) IsRunning() bool {
|
||||
|
||||
func (a *App) ListPrizes(cfg Config) (PrizeListResult, error) {
|
||||
cfg = normalizeConfig(cfg)
|
||||
accounts, err := parseAccounts(cfg.CookieText)
|
||||
accounts, err := parseAccounts(cfg)
|
||||
if err != nil {
|
||||
return PrizeListResult{}, err
|
||||
}
|
||||
@@ -186,7 +204,7 @@ func (a *App) ListPrizes(cfg Config) (PrizeListResult, error) {
|
||||
|
||||
func (a *App) QueryScores(cfg Config) ([]ScoreResult, error) {
|
||||
cfg = normalizeConfig(cfg)
|
||||
accounts, err := parseAccounts(cfg.CookieText)
|
||||
accounts, err := parseAccounts(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -216,7 +234,7 @@ func (a *App) QueryScores(cfg Config) ([]ScoreResult, error) {
|
||||
|
||||
func (a *App) GetRecords(cfg Config) ([]RecordResult, error) {
|
||||
cfg = normalizeConfig(cfg)
|
||||
accounts, err := parseAccounts(cfg.CookieText)
|
||||
accounts, err := parseAccounts(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -246,7 +264,7 @@ func (a *App) GetRecords(cfg Config) ([]RecordResult, error) {
|
||||
|
||||
func (a *App) Exchange(cfg Config) ([]ExchangeResult, error) {
|
||||
cfg = normalizeConfig(cfg)
|
||||
accounts, err := parseAccounts(cfg.CookieText)
|
||||
accounts, err := parseAccounts(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -272,14 +290,15 @@ func (a *App) Exchange(cfg Config) ([]ExchangeResult, error) {
|
||||
|
||||
func (a *App) StartMonitor(cfg Config) error {
|
||||
cfg = normalizeConfig(cfg)
|
||||
accounts, err := parseAccounts(cfg.CookieText)
|
||||
accounts, err := parseAccounts(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg.SID <= 0 {
|
||||
return errors.New("SID 不能为空")
|
||||
}
|
||||
if cfg.PID <= 0 && strings.TrimSpace(cfg.Name) == "" {
|
||||
targets := parseTargets(cfg)
|
||||
if len(targets) == 0 {
|
||||
return errors.New("PID 和名称至少填写一个")
|
||||
}
|
||||
startAt, err := parseStartTime(cfg.StartAt)
|
||||
@@ -305,34 +324,48 @@ func (a *App) StartMonitor(cfg Config) error {
|
||||
}
|
||||
|
||||
a.emitState(true, "运行中")
|
||||
a.addLog("info", "启动监控: sid=%d pid=%d name=%s accounts=%d", cfg.SID, cfg.PID, cfg.Name, len(accounts))
|
||||
a.addLog("info", "启动监控: sid=%d targets=%d accounts=%d prewarm=%v direct=%v", cfg.SID, len(targets), len(accounts), cfg.Prewarm, cfg.DirectExchange)
|
||||
|
||||
go func() {
|
||||
defer closeClient()
|
||||
defer a.finishMonitor("已停止")
|
||||
|
||||
runner := sniper.Runner{Client: client}
|
||||
emit := func(result sniper.Result) {
|
||||
a.emit(eventMonitorResult, result)
|
||||
a.addLog("info", "监控结果: loop=%d uid=%d action=%s category=%s msg=%s error=%s", result.Loop, result.UID, result.Action, result.Category, result.Message, result.Error)
|
||||
}
|
||||
runnerCfg := sniper.Config{
|
||||
SID: cfg.SID,
|
||||
Loops: cfg.Loops,
|
||||
Delay: time.Duration(cfg.DelayMS) * time.Millisecond,
|
||||
Concurrency: cfg.Concurrency,
|
||||
StopAfterSuccess: cfg.StopAfterSuccess,
|
||||
StopAccountAfterSuccess: cfg.StopAccountAfterSuccess,
|
||||
Prewarm: false,
|
||||
DirectExchange: cfg.DirectExchange,
|
||||
SharePrizeList: cfg.SharePrizeList,
|
||||
RefreshAfterSuccess: cfg.RefreshAfterSuccess,
|
||||
TooFrequentBackoff: time.Duration(cfg.FrequentBackoffMS) * time.Millisecond,
|
||||
ErrorBackoff: time.Duration(cfg.ErrorBackoffMS) * time.Millisecond,
|
||||
}
|
||||
if cfg.Prewarm {
|
||||
a.addLog("info", "开始预热连接: accounts=%d", len(accounts))
|
||||
if err := runner.Prewarm(ctx, accounts, runnerCfg, emit); err != nil {
|
||||
a.addLog("error", "连接预热异常: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if !startAt.IsZero() {
|
||||
a.addLog("info", "等待定时开始: %s", startAt.Format("2006-01-02 15:04:05"))
|
||||
if err := waitUntil(ctx, startAt); err != nil {
|
||||
when := startAt.Add(-time.Duration(cfg.LeadMS) * time.Millisecond)
|
||||
a.addLog("info", "等待定时开始: %s 提前=%dms", when.Format("2006-01-02 15:04:05.000"), cfg.LeadMS)
|
||||
if err := waitUntil(ctx, when); err != nil {
|
||||
a.addLog("info", "定时等待已取消")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
runner := sniper.Runner{Client: client}
|
||||
err := runner.Run(ctx, accounts, sniper.Target{
|
||||
PID: cfg.PID,
|
||||
Name: strings.TrimSpace(cfg.Name),
|
||||
}, sniper.Config{
|
||||
SID: cfg.SID,
|
||||
Loops: cfg.Loops,
|
||||
Delay: time.Duration(cfg.DelayMS) * time.Millisecond,
|
||||
Concurrency: cfg.Concurrency,
|
||||
StopAfterSuccess: cfg.StopAfterSuccess,
|
||||
}, func(result sniper.Result) {
|
||||
a.emit(eventMonitorResult, result)
|
||||
a.addLog("info", "监控结果: loop=%d uid=%d action=%s error=%s", result.Loop, result.UID, result.Action, result.Error)
|
||||
})
|
||||
err := runner.RunTargets(ctx, accounts, targets, runnerCfg, emit)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
a.addLog("error", "监控结束异常: %v", err)
|
||||
}
|
||||
@@ -426,33 +459,95 @@ func normalizeConfig(cfg Config) Config {
|
||||
if cfg.Concurrency <= 0 {
|
||||
cfg.Concurrency = 1
|
||||
}
|
||||
if cfg.LeadMS < 0 {
|
||||
cfg.LeadMS = 0
|
||||
}
|
||||
if cfg.FrequentBackoffMS <= 0 {
|
||||
cfg.FrequentBackoffMS = 300
|
||||
}
|
||||
if cfg.ErrorBackoffMS <= 0 {
|
||||
cfg.ErrorBackoffMS = 300
|
||||
}
|
||||
if cfg.TimeoutSec <= 0 {
|
||||
cfg.TimeoutSec = 15
|
||||
}
|
||||
cfg.Name = strings.TrimSpace(cfg.Name)
|
||||
cfg.TargetText = strings.TrimSpace(cfg.TargetText)
|
||||
cfg.CookieText = strings.TrimSpace(cfg.CookieText)
|
||||
cfg.StartAt = strings.TrimSpace(cfg.StartAt)
|
||||
return cfg
|
||||
}
|
||||
|
||||
func parseAccounts(cookieText string) ([]sniper.Account, error) {
|
||||
lines := splitCookieLines(cookieText)
|
||||
func parseTargets(cfg Config) []sniper.Target {
|
||||
targets := make([]sniper.Target, 0)
|
||||
if cfg.PID > 0 || strings.TrimSpace(cfg.Name) != "" {
|
||||
targets = append(targets, sniper.Target{PID: cfg.PID, Name: strings.TrimSpace(cfg.Name)})
|
||||
}
|
||||
if cfg.AutoSelect {
|
||||
targets = append(targets, sniper.Target{Auto: true})
|
||||
}
|
||||
for _, item := range splitTargetText(cfg.TargetText) {
|
||||
if item == "auto" || item == "自动" || item == "自动选择" {
|
||||
targets = append(targets, sniper.Target{Auto: true})
|
||||
continue
|
||||
}
|
||||
if pid, err := strconv.ParseInt(item, 10, 32); err == nil && pid > 0 {
|
||||
targets = append(targets, sniper.Target{PID: int32(pid)})
|
||||
continue
|
||||
}
|
||||
targets = append(targets, sniper.Target{Name: item})
|
||||
}
|
||||
return targets
|
||||
}
|
||||
|
||||
func splitTargetText(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 parseAccounts(cfg Config) ([]sniper.Account, error) {
|
||||
lines := splitCookieLines(cfg.CookieText)
|
||||
if len(lines) == 0 {
|
||||
return nil, errors.New("Cookie 不能为空")
|
||||
}
|
||||
selected := selectedLineSet(cfg.SelectedAccountLines)
|
||||
accounts := make([]sniper.Account, 0, len(lines))
|
||||
for _, cookie := range lines {
|
||||
if len(selected) > 0 && !selected[cookie.lineNo] {
|
||||
continue
|
||||
}
|
||||
uid := huya.ResolveUID(cookie.text)
|
||||
if uid <= 0 {
|
||||
return nil, fmt.Errorf("第 %d 行 Cookie 无法解析 yyuid/udb_uid", cookie.lineNo)
|
||||
}
|
||||
accounts = append(accounts, sniper.Account{UID: uid, Cookie: cookie.text})
|
||||
}
|
||||
if len(accounts) == 0 {
|
||||
return nil, errors.New("未选择有效账号")
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func selectedLineSet(lines []int) map[int]bool {
|
||||
out := make(map[int]bool, len(lines))
|
||||
for _, line := range lines {
|
||||
if line > 0 {
|
||||
out[line] = true
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func splitCookieLines(cookieText string) []cookieLine {
|
||||
lines := strings.Split(cookieText, "\n")
|
||||
lines := strings.Split(normalizeCookieText(cookieText), "\n")
|
||||
out := make([]cookieLine, 0, len(lines))
|
||||
for i, line := range lines {
|
||||
item := strings.TrimSpace(line)
|
||||
@@ -464,6 +559,25 @@ func splitCookieLines(cookieText string) []cookieLine {
|
||||
return out
|
||||
}
|
||||
|
||||
func normalizeCookieText(cookieText string) string {
|
||||
normalized := strings.NewReplacer(
|
||||
"\r\n", "\n",
|
||||
"\r", "\n",
|
||||
`\r\n`, "\n",
|
||||
`\r`, "\n",
|
||||
`\n`, "\n",
|
||||
).Replace(cookieText)
|
||||
if strings.Contains(normalized, "\n") ||
|
||||
(!strings.Contains(normalized, "/r/n") && !strings.Contains(normalized, "/r")) {
|
||||
return normalized
|
||||
}
|
||||
return strings.NewReplacer(
|
||||
"/r/n", "\n",
|
||||
"/r", "\n",
|
||||
"/n", "\n",
|
||||
).Replace(normalized)
|
||||
}
|
||||
|
||||
func requestContext(timeoutSec int) (context.Context, context.CancelFunc) {
|
||||
if timeoutSec <= 0 {
|
||||
timeoutSec = 15
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package gui
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSplitCookieLinesNormalizesLineBreaks(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "crlf_and_cr",
|
||||
input: "ck-a\r\nck-b\rck-c",
|
||||
want: []string{"ck-a", "ck-b", "ck-c"},
|
||||
},
|
||||
{
|
||||
name: "escaped_line_breaks",
|
||||
input: `ck-a\r\nck-b\rck-c\nck-d`,
|
||||
want: []string{"ck-a", "ck-b", "ck-c", "ck-d"},
|
||||
},
|
||||
{
|
||||
name: "slash_r_line_breaks",
|
||||
input: "ck-a/r/nck-b/rck-c/nck-d",
|
||||
want: []string{"ck-a", "ck-b", "ck-c", "ck-d"},
|
||||
},
|
||||
{
|
||||
name: "slash_n_only_is_preserved",
|
||||
input: "ck-a/nck-b",
|
||||
want: []string{"ck-a/nck-b"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
lines := splitCookieLines(tt.input)
|
||||
if len(lines) != len(tt.want) {
|
||||
t.Fatalf("len(splitCookieLines()) = %d, want %d", len(lines), len(tt.want))
|
||||
}
|
||||
for i, line := range lines {
|
||||
if line.text != tt.want[i] {
|
||||
t.Fatalf("line[%d] = %q, want %q", i, line.text, tt.want[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user