feat: add debug log stream support
This commit is contained in:
@@ -37,11 +37,17 @@ func NewWK(username, password, host string, cookies []*http.Cookie) *WK {
|
||||
return nil
|
||||
}
|
||||
|
||||
req := request.NewClient(&request.Config{
|
||||
reqCfg := &request.Config{
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0",
|
||||
// Proxy: "http://127.0.0.1:9000",
|
||||
// VerifySSL: false,
|
||||
})
|
||||
VerifySSL: true,
|
||||
Debug: conf.IsDebugMode(),
|
||||
}
|
||||
if conf.IsDebugMode() {
|
||||
reqCfg.Proxy = conf.DebugProxy
|
||||
reqCfg.VerifySSL = !conf.DebugSkipSSLVerify
|
||||
}
|
||||
|
||||
req := request.NewClient(reqCfg)
|
||||
if len(cookies) > 0 {
|
||||
req.SetCookies(cookies)
|
||||
}
|
||||
|
||||
@@ -37,8 +37,14 @@ func (m *SessionManager) Store(wk *WK) string {
|
||||
userKey := wk.Host + ":" + wk.Username
|
||||
if oldID, exists := m.userToSession[userKey]; exists {
|
||||
item := m.sessions[oldID]
|
||||
if item.cancel != nil {
|
||||
item.cancel()
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
item.LastValue = time.Now()
|
||||
item.Instance = wk
|
||||
item.cancel = cancel
|
||||
m.sessions[oldID] = item
|
||||
|
||||
log.Info("用户已存在,复用旧 Session",
|
||||
@@ -46,6 +52,8 @@ func (m *SessionManager) Store(wk *WK) string {
|
||||
zap.String("user", userKey),
|
||||
)
|
||||
|
||||
go m.KeepAlive(ctx, oldID)
|
||||
|
||||
return oldID
|
||||
}
|
||||
|
||||
@@ -61,18 +69,20 @@ func (m *SessionManager) Store(wk *WK) string {
|
||||
|
||||
log.Info("创建新 Session", zap.String("id", sessionID))
|
||||
|
||||
go m.KeepAlive(ctx, sessionID, wk)
|
||||
go m.KeepAlive(ctx, sessionID)
|
||||
|
||||
return sessionID
|
||||
}
|
||||
|
||||
// Get: 获取指定 session id 的 wk
|
||||
func (m *SessionManager) Get(sessionID string) (*WK, bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
item, ok := m.sessions[sessionID]
|
||||
if ok {
|
||||
item.LastValue = time.Now()
|
||||
m.sessions[sessionID] = item
|
||||
return item.Instance, true
|
||||
}
|
||||
return nil, false
|
||||
@@ -96,7 +106,7 @@ func (m *SessionManager) Del(sessionID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *SessionManager) KeepAlive(ctx context.Context, id string, wk *WK) {
|
||||
func (m *SessionManager) KeepAlive(ctx context.Context, id string) {
|
||||
ticker := time.NewTicker(2 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
@@ -108,7 +118,15 @@ func (m *SessionManager) KeepAlive(ctx context.Context, id string, wk *WK) {
|
||||
log.Info("KeepAlive 已停止", zap.String("id", id))
|
||||
return
|
||||
case <-ticker.C:
|
||||
_, err := wk.Online()
|
||||
m.mu.RLock()
|
||||
item, ok := m.sessions[id]
|
||||
m.mu.RUnlock()
|
||||
if !ok || item.Instance == nil {
|
||||
log.Info("Session 已不存在,停止 KeepAlive", zap.String("id", id))
|
||||
return
|
||||
}
|
||||
|
||||
_, err := item.Instance.Online()
|
||||
if err != nil {
|
||||
log.Error("自动保活请求失败", zap.Error(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user