add redis ip recorder

This commit is contained in:
yuzuki999 2022-11-02 10:26:04 +08:00
parent 5b9cfd51e9
commit e83a56f68d
6 changed files with 100 additions and 4 deletions

View File

@ -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,

View File

@ -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
}

View 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)
}

4
go.mod
View File

@ -17,6 +17,7 @@ require (
)
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/goccy/go-json v0.9.11
gopkg.in/yaml.v3 v3.0.1
)
@ -42,12 +43,14 @@ require (
github.com/aws/aws-sdk-go v1.44.7 // indirect
github.com/boombuler/barcode v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cloudflare/cloudflare-go v0.49.0 // indirect
github.com/cpu/goacmedns v0.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.10.1 // indirect
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/dnsimple/dnsimple-go v0.71.1 // indirect
github.com/exoscale/egoscale v1.19.0 // indirect
@ -102,7 +105,6 @@ require (
github.com/nrdcg/goinwx v0.8.1 // indirect
github.com/nrdcg/namesilo v0.2.1 // indirect
github.com/nrdcg/porkbun v0.1.1 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/ginkgo/v2 v2.4.0 // indirect
github.com/oracle/oci-go-sdk v24.3.0+incompatible // indirect
github.com/ovh/go-ovh v1.1.0 // indirect

7
go.sum
View File

@ -130,6 +130,8 @@ github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInq
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@ -172,6 +174,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165/go.mod h1:c9O8+fpSOX1DM8cPNSkX/qsBWdkD4yd2dpciOWQjpBw=
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 h1:y7y0Oa6UawqTFPCDw9JG6pdKt4F9pAhHv0B7FMGaGD0=
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140/go.mod h1:c9O8+fpSOX1DM8cPNSkX/qsBWdkD4yd2dpciOWQjpBw=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U=
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
@ -235,6 +239,8 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-playground/validator/v10 v10.10.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48/go.mod h1:dZGr0i9PLlaaTD4H/hoZIDjQ+r6xq8mgbRzHZf7f2J8=
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
@ -583,7 +589,6 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs=

View File

@ -103,7 +103,7 @@ func (c *Node) Start() error {
if c.config.EnableIpRecorder {
switch c.config.IpRecorderConfig.Type {
case "Record":
c.ipRecorder = iprecoder.New(c.config.IpRecorderConfig.RecorderConfig)
c.ipRecorder = iprecoder.NewRecorder(c.config.IpRecorderConfig.RecorderConfig)
case "RedisConfig":
}
// report and fetch online ip list task