Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
81193a05ef | |||
5f60eb8752 | |||
5a40fb593c | |||
e47a96a597 | |||
4d861f037e |
@ -5,6 +5,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.sati.ac/sati.ac/sati-go"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
@ -82,9 +84,23 @@ var (
|
||||
)
|
||||
|
||||
func (a *antigateV2Api) getTaskResult(request struct {
|
||||
TaskId uint32 `json:"taskId"`
|
||||
TaskId any `json:"taskId"`
|
||||
}) 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 {
|
||||
return errorNoSuchCaptchaId
|
||||
}
|
||||
@ -147,8 +163,8 @@ func (a *antigateV2Api) createTask(request struct {
|
||||
|
||||
var id uint32
|
||||
|
||||
switch taskType {
|
||||
case "TurnstileTask", "TurnstileTaskProxyless":
|
||||
switch strings.ToLower(taskType) {
|
||||
case "turnstiletask", "turnstiletaskproxyless":
|
||||
var task struct {
|
||||
WebsiteURL string `json:"websiteURL"`
|
||||
WebsiteKey string `json:"websiteKey"`
|
||||
@ -160,7 +176,7 @@ func (a *antigateV2Api) createTask(request struct {
|
||||
SiteKey: task.WebsiteKey,
|
||||
Action: task.Action,
|
||||
})
|
||||
case "RecaptchaV2Task", "RecaptchaV2TaskProxyless":
|
||||
case "recaptchav2task", "recaptchav2taskproxyless":
|
||||
var task struct {
|
||||
WebsiteURL string `json:"websiteURL"`
|
||||
WebsiteKey string `json:"websiteKey"`
|
||||
@ -170,7 +186,7 @@ func (a *antigateV2Api) createTask(request struct {
|
||||
PageUrl: task.WebsiteURL,
|
||||
SiteKey: task.WebsiteKey,
|
||||
})
|
||||
case "FunCaptchaTask", "FunCaptchaTaskProxyless":
|
||||
case "funcaptchatask", "funcaptchataskproxyless":
|
||||
var task struct {
|
||||
WebsiteURL string `json:"websiteURL"`
|
||||
WebsitePublicKey string `json:"websitePublicKey"`
|
||||
@ -209,7 +225,7 @@ func (a *antigateV2Api) getBalance(struct{}) any {
|
||||
}{0, 0, balance.InexactFloat64()}
|
||||
}
|
||||
|
||||
func jsonHandler[T any](handler func(data T) any) func(http.ResponseWriter, *http.Request) {
|
||||
func jsonHandler[T any](api *antigateV2Api, handler func(data T) any) func(http.ResponseWriter, *http.Request) {
|
||||
emptyRequest := reflect.Zero(reflect.TypeOf(handler).In(0)).Interface().(T)
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@ -223,7 +239,9 @@ func jsonHandler[T any](handler func(data T) any) func(http.ResponseWriter, *htt
|
||||
return
|
||||
}
|
||||
|
||||
api.ctx.Logger.WithFields(logrus.Fields{"handler": api.Name(), "request": request}).Debug("request")
|
||||
response := handler(request)
|
||||
api.ctx.Logger.WithFields(logrus.Fields{"handler": api.Name(), "response": response}).Debug("response")
|
||||
|
||||
marshaled, _ := json.Marshal(response)
|
||||
w.Write(marshaled)
|
||||
@ -233,9 +251,9 @@ func jsonHandler[T any](handler func(data T) any) func(http.ResponseWriter, *htt
|
||||
func newAntigateV2Api(ctx *ApiContext) ApiHandler {
|
||||
api := &antigateV2Api{ctx, http.NewServeMux()}
|
||||
|
||||
api.mux.HandleFunc("/createTask", jsonHandler(api.createTask))
|
||||
api.mux.HandleFunc("/getTaskResult", jsonHandler(api.getTaskResult))
|
||||
api.mux.HandleFunc("/getBalance", jsonHandler(api.getBalance))
|
||||
api.mux.HandleFunc("/createTask", jsonHandler(api, api.createTask))
|
||||
api.mux.HandleFunc("/getTaskResult", jsonHandler(api, api.getTaskResult))
|
||||
api.mux.HandleFunc("/getBalance", jsonHandler(api, api.getBalance))
|
||||
|
||||
return api
|
||||
}
|
||||
|
18
api/api.go
18
api/api.go
@ -35,19 +35,19 @@ type RegistryStats struct {
|
||||
|
||||
type TaskRegistry struct {
|
||||
mu *sync.RWMutex
|
||||
lifetime time.Duration
|
||||
config *config.Config
|
||||
tasks map[uint32]*Task
|
||||
idCounter uint32
|
||||
api *sati.Api
|
||||
stats RegistryStats
|
||||
}
|
||||
|
||||
func NewTaskRegistry(api *sati.Api, lifetime time.Duration) *TaskRegistry {
|
||||
func NewTaskRegistry(api *sati.Api, config *config.Config) *TaskRegistry {
|
||||
return &TaskRegistry{
|
||||
mu: &sync.RWMutex{},
|
||||
tasks: map[uint32]*Task{},
|
||||
api: api,
|
||||
lifetime: lifetime,
|
||||
mu: &sync.RWMutex{},
|
||||
tasks: map[uint32]*Task{},
|
||||
api: api,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,7 +82,7 @@ func (t *TaskRegistry) CreateTask(task sati.AnyTask) uint32 {
|
||||
entry.EndTime = time.Now().Unix()
|
||||
t.mu.Unlock()
|
||||
|
||||
time.Sleep(t.lifetime)
|
||||
time.Sleep(time.Millisecond * time.Duration(t.config.TaskLifetime))
|
||||
t.mu.Lock()
|
||||
delete(t.tasks, id)
|
||||
t.mu.Unlock()
|
||||
@ -98,6 +98,10 @@ func (t *TaskRegistry) Stats() RegistryStats {
|
||||
}
|
||||
|
||||
func (t *TaskRegistry) Get(id uint32) *Task {
|
||||
if t.config.TaskGetDelay != 0 {
|
||||
time.Sleep(time.Millisecond * time.Duration(t.config.TaskGetDelay))
|
||||
}
|
||||
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
task := t.tasks[id]
|
||||
|
@ -16,6 +16,8 @@ type Config struct {
|
||||
TlsKeyPath string `json:"tlsKeyPath"`
|
||||
Host string `json:"host"`
|
||||
TlsHost string `json:"tlsHost"`
|
||||
TaskLifetime int64 `json:"taskLifetime"`
|
||||
TaskGetDelay int64 `json:"taskGetDelay"`
|
||||
AntiGateV2 struct {
|
||||
TurnstileUserAgent string `json:"turnstileUserAgent"`
|
||||
Ip string `json:"ip"`
|
||||
@ -32,6 +34,8 @@ func Default() *Config {
|
||||
TlsKeyPath: "./data/ca.key",
|
||||
Host: "127.0.0.1:80",
|
||||
TlsHost: "127.0.0.1:443",
|
||||
TaskLifetime: 60000,
|
||||
TaskGetDelay: 0,
|
||||
AntiGateV2: struct {
|
||||
TurnstileUserAgent string `json:"turnstileUserAgent"`
|
||||
Ip string `json:"ip"`
|
||||
|
4
main.go
4
main.go
@ -207,7 +207,7 @@ func addDomainsToHosts(ctx *api.ApiContext) error {
|
||||
|
||||
hosts = hostsModRE.ReplaceAll(hosts, []byte{}) // remove old entries
|
||||
|
||||
hostIp := "127.0.0.1"
|
||||
hostIp := strings.SplitN(ctx.Config.Host, ":", 2)[0]
|
||||
suffix := "\r\n#sati-bridge start, DO NOT MODIFY\r\n"
|
||||
for _, domain := range ctx.Server.GetDomains() {
|
||||
suffix += hostIp + " " + domain + "\r\n"
|
||||
@ -289,7 +289,7 @@ func main() {
|
||||
satiConfig.Debug = cfg.Debug
|
||||
satiApi := sati.NewApi(satiConfig)
|
||||
|
||||
registry := api.NewTaskRegistry(satiApi, time.Minute)
|
||||
registry := api.NewTaskRegistry(satiApi, cfg)
|
||||
|
||||
ctx := api.ApiContext{
|
||||
Config: cfg,
|
||||
|
Reference in New Issue
Block a user