2 Commits

Author SHA1 Message Date
81193a05ef fix(AntiGateV2): also accept string taskId
All checks were successful
release-tag / release (push) Successful in 1m54s
2023-07-14 04:05:08 +03:00
5f60eb8752 feat: add option for delaying task get requests
All checks were successful
release-tag / release (push) Successful in 2m3s
2023-07-14 03:19:16 +03:00
4 changed files with 33 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"net/http" "net/http"
"reflect" "reflect"
"strconv"
"strings" "strings"
"git.sati.ac/sati.ac/sati-go" "git.sati.ac/sati.ac/sati-go"
@ -83,9 +84,23 @@ var (
) )
func (a *antigateV2Api) getTaskResult(request struct { func (a *antigateV2Api) getTaskResult(request struct {
TaskId uint32 `json:"taskId"` TaskId any `json:"taskId"`
}) any { }) any {
task := a.ctx.Registry.Get(request.TaskId) var taskId uint32
switch id := request.TaskId.(type) {
case float64:
taskId = uint32(id)
case string:
parsed, err := strconv.ParseUint(id, 10, 32)
if err != nil {
return errorBadRequest
}
taskId = uint32(parsed)
default:
return errorBadRequest
}
task := a.ctx.Registry.Get(taskId)
if task == nil { if task == nil {
return errorNoSuchCaptchaId return errorNoSuchCaptchaId
} }

View File

@ -35,19 +35,19 @@ type RegistryStats struct {
type TaskRegistry struct { type TaskRegistry struct {
mu *sync.RWMutex mu *sync.RWMutex
lifetime time.Duration config *config.Config
tasks map[uint32]*Task tasks map[uint32]*Task
idCounter uint32 idCounter uint32
api *sati.Api api *sati.Api
stats RegistryStats stats RegistryStats
} }
func NewTaskRegistry(api *sati.Api, lifetime time.Duration) *TaskRegistry { func NewTaskRegistry(api *sati.Api, config *config.Config) *TaskRegistry {
return &TaskRegistry{ return &TaskRegistry{
mu: &sync.RWMutex{}, mu: &sync.RWMutex{},
tasks: map[uint32]*Task{}, tasks: map[uint32]*Task{},
api: api, api: api,
lifetime: lifetime, config: config,
} }
} }
@ -82,7 +82,7 @@ func (t *TaskRegistry) CreateTask(task sati.AnyTask) uint32 {
entry.EndTime = time.Now().Unix() entry.EndTime = time.Now().Unix()
t.mu.Unlock() t.mu.Unlock()
time.Sleep(t.lifetime) time.Sleep(time.Millisecond * time.Duration(t.config.TaskLifetime))
t.mu.Lock() t.mu.Lock()
delete(t.tasks, id) delete(t.tasks, id)
t.mu.Unlock() t.mu.Unlock()
@ -98,6 +98,10 @@ func (t *TaskRegistry) Stats() RegistryStats {
} }
func (t *TaskRegistry) Get(id uint32) *Task { func (t *TaskRegistry) Get(id uint32) *Task {
if t.config.TaskGetDelay != 0 {
time.Sleep(time.Millisecond * time.Duration(t.config.TaskGetDelay))
}
t.mu.RLock() t.mu.RLock()
defer t.mu.RUnlock() defer t.mu.RUnlock()
task := t.tasks[id] task := t.tasks[id]

View File

@ -16,6 +16,8 @@ type Config struct {
TlsKeyPath string `json:"tlsKeyPath"` TlsKeyPath string `json:"tlsKeyPath"`
Host string `json:"host"` Host string `json:"host"`
TlsHost string `json:"tlsHost"` TlsHost string `json:"tlsHost"`
TaskLifetime int64 `json:"taskLifetime"`
TaskGetDelay int64 `json:"taskGetDelay"`
AntiGateV2 struct { AntiGateV2 struct {
TurnstileUserAgent string `json:"turnstileUserAgent"` TurnstileUserAgent string `json:"turnstileUserAgent"`
Ip string `json:"ip"` Ip string `json:"ip"`
@ -32,6 +34,8 @@ func Default() *Config {
TlsKeyPath: "./data/ca.key", TlsKeyPath: "./data/ca.key",
Host: "127.0.0.1:80", Host: "127.0.0.1:80",
TlsHost: "127.0.0.1:443", TlsHost: "127.0.0.1:443",
TaskLifetime: 60000,
TaskGetDelay: 0,
AntiGateV2: struct { AntiGateV2: struct {
TurnstileUserAgent string `json:"turnstileUserAgent"` TurnstileUserAgent string `json:"turnstileUserAgent"`
Ip string `json:"ip"` Ip string `json:"ip"`

View File

@ -289,7 +289,7 @@ func main() {
satiConfig.Debug = cfg.Debug satiConfig.Debug = cfg.Debug
satiApi := sati.NewApi(satiConfig) satiApi := sati.NewApi(satiConfig)
registry := api.NewTaskRegistry(satiApi, time.Minute) registry := api.NewTaskRegistry(satiApi, cfg)
ctx := api.ApiContext{ ctx := api.ApiContext{
Config: cfg, Config: cfg,