refactor limiter
fix getLink bug
add connection limit
move limit config to ControllerConfig
del dynamic speed limit (next version will be re add)
del online ip sync (next version will be re add)
This commit is contained in:
yuzuki999
2023-05-16 09:15:29 +08:00
parent 2d7aaef066
commit 15c36a9580
35 changed files with 564 additions and 617 deletions

View File

@@ -1,7 +1,9 @@
package iprecoder
import "github.com/Yuzuki616/V2bX/core/app/dispatcher"
import (
"github.com/Yuzuki616/V2bX/limiter"
)
type IpRecorder interface {
SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error)
SyncOnlineIp(Ips []limiter.UserIpList) ([]limiter.UserIpList, error)
}

View File

@@ -3,7 +3,7 @@ package iprecoder
import (
"errors"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core/app/dispatcher"
"github.com/Yuzuki616/V2bX/limiter"
"github.com/go-resty/resty/v2"
"github.com/goccy/go-json"
"time"
@@ -21,7 +21,7 @@ func NewRecorder(c *conf.RecorderConfig) *Recorder {
}
}
func (r *Recorder) SyncOnlineIp(ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error) {
func (r *Recorder) SyncOnlineIp(ips []limiter.UserIpList) ([]limiter.UserIpList, error) {
rsp, err := r.client.R().
SetBody(ips).
Post(r.Url + "/api/v1/SyncOnlineIp?token=" + r.Token)
@@ -31,7 +31,7 @@ func (r *Recorder) SyncOnlineIp(ips []dispatcher.UserIpList) ([]dispatcher.UserI
if rsp.StatusCode() != 200 {
return nil, errors.New(rsp.String())
}
ips = []dispatcher.UserIpList{}
ips = []limiter.UserIpList{}
err = json.Unmarshal(rsp.Body(), &ips)
if err != nil {
return nil, err

View File

@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core/app/dispatcher"
"github.com/Yuzuki616/V2bX/limiter"
"github.com/go-redis/redis/v8"
"strconv"
"time"
@@ -26,7 +26,7 @@ func NewRedis(c *conf.RedisConfig) *Redis {
}
}
func (r *Redis) SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error) {
func (r *Redis) SyncOnlineIp(Ips []limiter.UserIpList) ([]limiter.UserIpList, error) {
ctx := context.Background()
for i := range Ips {
err := r.client.SAdd(ctx, "UserList", Ips[i].Uid).Err()
@@ -46,7 +46,7 @@ func (r *Redis) SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpLi
if c.Err() != nil {
return nil, fmt.Errorf("get user list failed: %s", c.Err())
}
Ips = make([]dispatcher.UserIpList, 0, len(c.Val()))
Ips = make([]limiter.UserIpList, 0, len(c.Val()))
for _, uid := range c.Val() {
uidInt, err := strconv.Atoi(uid)
if err != nil {
@@ -56,7 +56,7 @@ func (r *Redis) SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpLi
if ips.Err() != nil {
return nil, fmt.Errorf("get ip list failed: %s", ips.Err())
}
Ips = append(Ips, dispatcher.UserIpList{
Ips = append(Ips, limiter.UserIpList{
Uid: uidInt,
IpList: ips.Val(),
})

View File

@@ -2,7 +2,7 @@ package iprecoder
import (
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core/app/dispatcher"
"github.com/Yuzuki616/V2bX/limiter"
"log"
"testing"
)
@@ -13,7 +13,7 @@ func TestRedis_SyncOnlineIp(t *testing.T) {
Password: "",
Db: 0,
})
users, err := r.SyncOnlineIp([]dispatcher.UserIpList{
users, err := r.SyncOnlineIp([]limiter.UserIpList{
{1, []string{"5.5.5.5", "4.4.4.4"}},
})
if err != nil {

View File

@@ -36,12 +36,8 @@ type DestinationRule struct {
Pattern *regexp.Regexp
}
type localNodeConfig struct {
NodeId int
NodeType string
EnableVless bool
EnableTls bool
SpeedLimit int
DeviceLimit int
NodeId int
NodeType string
}
func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {

View File

@@ -28,8 +28,6 @@ type Client struct {
Key string
NodeType string
NodeId int
SpeedLimit int
DeviceLimit int
LocalRuleList []DestinationRule
etag string
}
@@ -69,8 +67,6 @@ func New(c *conf.ApiConfig) (Panel, error) {
Key: c.Key,
APIHost: c.APIHost,
NodeType: c.NodeType,
SpeedLimit: c.SpeedLimit,
DeviceLimit: c.DeviceLimit,
NodeId: c.NodeID,
LocalRuleList: localRuleList,
}, nil

View File

@@ -10,18 +10,9 @@ type OnlineUser struct {
IP string
}
type V2RayUserInfo struct {
Uuid string `json:"uuid"`
Email string `json:"email"`
AlterId int `json:"alter_id"`
}
type TrojanUserInfo struct {
Password string `json:"password"`
}
type UserInfo struct {
Id int `json:"id"`
Uuid string `json:"uuid"`
Email string `json:"-"`
SpeedLimit int `json:"speed_limit"`
Traffic int64 `json:"-"`
}