160 lines
3.7 KiB
Go
160 lines
3.7 KiB
Go
package algos
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// AlgorithmInterface 算法接口
|
|
type AlgorithmInterface interface {
|
|
GetAlgorithmName() string
|
|
GenerateHeaders(url string, headers map[string]string) (map[string]string, error)
|
|
}
|
|
|
|
// AlgorithmSwitcher 算法切换器
|
|
type AlgorithmSwitcher struct {
|
|
mu sync.RWMutex
|
|
currentAlgorithm string
|
|
algorithms map[string]AlgorithmInterface
|
|
}
|
|
|
|
// NewAlgorithmSwitcher 创建算法切换器
|
|
func NewAlgorithmSwitcher() *AlgorithmSwitcher {
|
|
switcher := &AlgorithmSwitcher{
|
|
algorithms: make(map[string]AlgorithmInterface),
|
|
}
|
|
|
|
// 注册所有算法
|
|
switcher.algorithms["gorgon"] = &GorgonAlgorithmAdapter{}
|
|
switcher.algorithms["8404"] = &Algo8404Adapter{}
|
|
|
|
// 设置默认算法为gorgon
|
|
switcher.currentAlgorithm = "gorgon"
|
|
|
|
return switcher
|
|
}
|
|
|
|
// SwitchAlgorithm 切换算法
|
|
func (s *AlgorithmSwitcher) SwitchAlgorithm(algorithmType string) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
if _, exists := s.algorithms[algorithmType]; !exists {
|
|
return fmt.Errorf("不支持的算法类型: %s", algorithmType)
|
|
}
|
|
|
|
s.currentAlgorithm = algorithmType
|
|
return nil
|
|
}
|
|
|
|
// GetCurrentAlgorithm 获取当前算法名称
|
|
func (s *AlgorithmSwitcher) GetCurrentAlgorithm() string {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.currentAlgorithm
|
|
}
|
|
|
|
// GetAvailableAlgorithms 获取可用算法列表
|
|
func (s *AlgorithmSwitcher) GetAvailableAlgorithms() []string {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
algorithms := make([]string, 0, len(s.algorithms))
|
|
for name := range s.algorithms {
|
|
algorithms = append(algorithms, name)
|
|
}
|
|
return algorithms
|
|
}
|
|
|
|
// GetCurrentAlgorithmInstance 获取当前算法实例
|
|
func (s *AlgorithmSwitcher) GetCurrentAlgorithmInstance() AlgorithmInterface {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.algorithms[s.currentAlgorithm]
|
|
}
|
|
|
|
// GetAlgorithmInstance 获取指定算法实例
|
|
func (s *AlgorithmSwitcher) GetAlgorithmInstance(algorithmType string) (AlgorithmInterface, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
algorithm, exists := s.algorithms[algorithmType]
|
|
if !exists {
|
|
return nil, fmt.Errorf("不支持的算法类型: %s", algorithmType)
|
|
}
|
|
|
|
return algorithm, nil
|
|
}
|
|
|
|
// GorgonAlgorithmAdapter Gorgon算法适配器(兼容旧版)
|
|
type GorgonAlgorithmAdapter struct{}
|
|
|
|
func (g *GorgonAlgorithmAdapter) GetAlgorithmName() string {
|
|
return "Gorgon算法"
|
|
}
|
|
|
|
func (g *GorgonAlgorithmAdapter) GenerateHeaders(url string, headers map[string]string) (map[string]string, error) {
|
|
// 使用Gorgon算法
|
|
parts := strings.Split(url, "?")
|
|
if len(parts) < 2 {
|
|
return headers, fmt.Errorf("URL格式错误")
|
|
}
|
|
|
|
xSSStub := headers["x-ss-stub"]
|
|
cookie := headers["Cookie"]
|
|
if cookie == "" {
|
|
cookie = headers["cookie"]
|
|
}
|
|
|
|
gorgon := NewGorgon(parts[1], time.Now().Unix(), xSSStub, cookie)
|
|
result := gorgon.GetValue()
|
|
|
|
finalHeaders := make(map[string]string)
|
|
for k, v := range headers {
|
|
finalHeaders[k] = v
|
|
}
|
|
|
|
for k, v := range result {
|
|
finalHeaders[k] = v
|
|
}
|
|
|
|
return finalHeaders, nil
|
|
}
|
|
|
|
// Algo8404Adapter 8404算法适配器
|
|
type Algo8404Adapter struct{}
|
|
|
|
func (a *Algo8404Adapter) GetAlgorithmName() string {
|
|
return "8404算法"
|
|
}
|
|
|
|
func (a *Algo8404Adapter) GenerateHeaders(url string, headers map[string]string) (map[string]string, error) {
|
|
// 使用8404算法
|
|
parts := strings.Split(url, "?")
|
|
if len(parts) < 2 {
|
|
return headers, fmt.Errorf("URL格式错误")
|
|
}
|
|
|
|
xSSStub := headers["x-ss-stub"]
|
|
algo := NewAlgo8404(parts[1], time.Now().Unix(), xSSStub)
|
|
result := algo.GetValue()
|
|
|
|
finalHeaders := make(map[string]string)
|
|
for k, v := range headers {
|
|
finalHeaders[k] = v
|
|
}
|
|
|
|
for k, v := range result {
|
|
finalHeaders[k] = v
|
|
}
|
|
|
|
return finalHeaders, nil
|
|
}
|
|
|
|
// GlobalAlgorithmSwitcher 全局算法切换器实例
|
|
var GlobalAlgorithmSwitcher = NewAlgorithmSwitcher()
|
|
|