2022-10-20 23:07:04 -04:00
|
|
|
package iprecoder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/Yuzuki616/V2bX/conf"
|
2023-05-15 21:15:29 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/limiter"
|
2022-10-20 23:07:04 -04:00
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Recorder struct {
|
|
|
|
client *resty.Client
|
|
|
|
*conf.RecorderConfig
|
|
|
|
}
|
|
|
|
|
2022-11-01 22:26:04 -04:00
|
|
|
func NewRecorder(c *conf.RecorderConfig) *Recorder {
|
2022-10-20 23:07:04 -04:00
|
|
|
return &Recorder{
|
|
|
|
client: resty.New().SetTimeout(time.Duration(c.Timeout) * time.Second),
|
|
|
|
RecorderConfig: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (r *Recorder) SyncOnlineIp(ips []limiter.UserIpList) ([]limiter.UserIpList, error) {
|
2022-10-20 23:07:04 -04:00
|
|
|
rsp, err := r.client.R().
|
|
|
|
SetBody(ips).
|
|
|
|
Post(r.Url + "/api/v1/SyncOnlineIp?token=" + r.Token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.StatusCode() != 200 {
|
|
|
|
return nil, errors.New(rsp.String())
|
|
|
|
}
|
2023-05-15 21:15:29 -04:00
|
|
|
ips = []limiter.UserIpList{}
|
2022-10-20 23:07:04 -04:00
|
|
|
err = json.Unmarshal(rsp.Body(), &ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ips, nil
|
|
|
|
}
|