mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 12:40:11 +00:00
add redis ip recorder
This commit is contained in:
@@ -14,7 +14,7 @@ type Recorder struct {
|
||||
*conf.RecorderConfig
|
||||
}
|
||||
|
||||
func New(c *conf.RecorderConfig) *Recorder {
|
||||
func NewRecorder(c *conf.RecorderConfig) *Recorder {
|
||||
return &Recorder{
|
||||
client: resty.New().SetTimeout(time.Duration(c.Timeout) * time.Second),
|
||||
RecorderConfig: c,
|
||||
|
||||
@@ -1 +1,67 @@
|
||||
package iprecoder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/Yuzuki616/V2bX/conf"
|
||||
"github.com/Yuzuki616/V2bX/core/app/dispatcher"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Redis struct {
|
||||
*conf.RedisConfig
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewRedis(c *conf.RedisConfig) *Redis {
|
||||
return &Redis{
|
||||
RedisConfig: c,
|
||||
client: redis.NewClient(&redis.Options{
|
||||
Addr: c.Address,
|
||||
Password: c.Password,
|
||||
DB: c.Db,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Redis) SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error) {
|
||||
ctx := context.Background()
|
||||
for i := range Ips {
|
||||
if !r.client.SIsMember(ctx, "UserList", strconv.Itoa(Ips[i].Uid)).Val() {
|
||||
err := r.client.SAdd(ctx, "UserList", Ips[i].Uid).Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add user failed: %s", err)
|
||||
}
|
||||
}
|
||||
r.client.Expire(ctx, "UserList", time.Duration(2)*time.Minute)
|
||||
for _, ip := range Ips[i].IpList {
|
||||
err := r.client.SAdd(ctx, strconv.Itoa(Ips[i].Uid), ip).Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add ip failed: %s", err)
|
||||
}
|
||||
r.client.Expire(ctx, strconv.Itoa(Ips[i].Uid), time.Duration(2)*time.Minute)
|
||||
}
|
||||
}
|
||||
c := r.client.SMembers(ctx, "UserList")
|
||||
if c.Err() != nil {
|
||||
return nil, fmt.Errorf("get user list failed: %s", c.Err())
|
||||
}
|
||||
Ips = make([]dispatcher.UserIpList, 0, len(c.Val()))
|
||||
for _, uid := range c.Val() {
|
||||
uidInt, err := strconv.Atoi(uid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert uid failed: %s", err)
|
||||
}
|
||||
ips := r.client.SMembers(ctx, uid)
|
||||
if ips.Err() != nil {
|
||||
return nil, fmt.Errorf("get ip list failed: %s", ips.Err())
|
||||
}
|
||||
Ips = append(Ips, dispatcher.UserIpList{
|
||||
Uid: uidInt,
|
||||
IpList: ips.Val(),
|
||||
})
|
||||
}
|
||||
return Ips, nil
|
||||
}
|
||||
|
||||
23
api/iprecoder/redis_test.go
Normal file
23
api/iprecoder/redis_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package iprecoder
|
||||
|
||||
import (
|
||||
"github.com/Yuzuki616/V2bX/conf"
|
||||
"github.com/Yuzuki616/V2bX/core/app/dispatcher"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRedis_SyncOnlineIp(t *testing.T) {
|
||||
r := NewRedis(&conf.RedisConfig{
|
||||
Address: "127.0.0.1:6379",
|
||||
Password: "",
|
||||
Db: 0,
|
||||
})
|
||||
users, err := r.SyncOnlineIp([]dispatcher.UserIpList{
|
||||
{2, []string{"3.3.3.3", "4.4.4.4"}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
log.Println(users)
|
||||
}
|
||||
Reference in New Issue
Block a user