mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-08-06 15:10:40 -04:00
allow dynamic type of user accounts
This commit is contained in:
@@ -1 +1,4 @@
|
||||
package http
|
||||
|
||||
type Client struct {
|
||||
}
|
||||
|
||||
@@ -48,3 +48,6 @@ func (this *Config) IsOwnHost(host v2net.Address) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type ClientConfig struct {
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ func (this *VMessInboundHandler) generateCommand(request *protocol.RequestHeader
|
||||
user := inboundHandler.GetUser(request.User.Email)
|
||||
return &protocol.CommandSwitchAccount{
|
||||
Port: inboundHandler.Port(),
|
||||
ID: user.ID.UUID(),
|
||||
AlterIds: uint16(len(user.AlterIDs)),
|
||||
ID: user.Account.(*protocol.VMessAccount).ID.UUID(),
|
||||
AlterIds: uint16(len(user.Account.(*protocol.VMessAccount).AlterIDs)),
|
||||
Level: user.Level,
|
||||
ValidMin: byte(availableMin),
|
||||
}
|
||||
|
||||
@@ -50,7 +50,11 @@ func (this *userByEmail) Get(email string) (*protocol.User, bool) {
|
||||
if !found {
|
||||
id := protocol.NewID(uuid.New())
|
||||
alterIDs := protocol.NewAlterIDs(id, this.defaultAlterIDs)
|
||||
user = protocol.NewUser(id, alterIDs, this.defaultLevel, email)
|
||||
account := &protocol.VMessAccount{
|
||||
ID: id,
|
||||
AlterIDs: alterIDs,
|
||||
}
|
||||
user = protocol.NewUser(account, this.defaultLevel, email)
|
||||
this.cache[email] = user
|
||||
}
|
||||
this.Unlock()
|
||||
|
||||
@@ -8,7 +8,11 @@ import (
|
||||
func (this *VMessOutboundHandler) handleSwitchAccount(cmd *protocol.CommandSwitchAccount) {
|
||||
primary := protocol.NewID(cmd.ID)
|
||||
alters := protocol.NewAlterIDs(primary, cmd.AlterIds)
|
||||
user := protocol.NewUser(primary, alters, cmd.Level, "")
|
||||
account := &protocol.VMessAccount{
|
||||
ID: primary,
|
||||
AlterIDs: alters,
|
||||
}
|
||||
user := protocol.NewUser(account, cmd.Level, "")
|
||||
dest := v2net.TCPDestination(cmd.Host, cmd.Port)
|
||||
this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin)
|
||||
}
|
||||
|
||||
@@ -25,9 +25,11 @@ func NewReceiver(dest v2net.Destination, users ...*protocol.User) *Receiver {
|
||||
func (this *Receiver) HasUser(user *protocol.User) bool {
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
account := user.Account.(*protocol.VMessAccount)
|
||||
for _, u := range this.Accounts {
|
||||
// TODO: handle AlterIds difference.
|
||||
if u.ID.Equals(user.ID) {
|
||||
uAccount := u.Account.(*protocol.VMessAccount)
|
||||
if uAccount.ID.Equals(account.ID) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/protocol"
|
||||
. "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
@@ -30,5 +31,7 @@ func TestConfigTargetParsing(t *testing.T) {
|
||||
assert.Error(err).IsNil()
|
||||
assert.Destination(receiver.Destination).EqualsString("tcp:127.0.0.1:80")
|
||||
assert.Int(len(receiver.Accounts)).Equals(1)
|
||||
assert.String(receiver.Accounts[0].ID.String()).Equals("e641f5ad-9397-41e3-bf1a-e8740dfed019")
|
||||
|
||||
account := receiver.Accounts[0].Account.(*protocol.VMessAccount)
|
||||
assert.String(account.ID.String()).Equals("e641f5ad-9397-41e3-bf1a-e8740dfed019")
|
||||
}
|
||||
|
||||
@@ -15,14 +15,22 @@ func TestReceiverUser(t *testing.T) {
|
||||
|
||||
id := protocol.NewID(uuid.New())
|
||||
alters := protocol.NewAlterIDs(id, 100)
|
||||
user := protocol.NewUser(id, alters, protocol.UserLevel(0), "")
|
||||
account := &protocol.VMessAccount{
|
||||
ID: id,
|
||||
AlterIDs: alters,
|
||||
}
|
||||
user := protocol.NewUser(account, protocol.UserLevel(0), "")
|
||||
rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user)
|
||||
assert.Bool(rec.HasUser(user)).IsTrue()
|
||||
assert.Int(len(rec.Accounts)).Equals(1)
|
||||
|
||||
id2 := protocol.NewID(uuid.New())
|
||||
alters2 := protocol.NewAlterIDs(id2, 100)
|
||||
user2 := protocol.NewUser(id2, alters2, protocol.UserLevel(0), "")
|
||||
account2 := &protocol.VMessAccount{
|
||||
ID: id2,
|
||||
AlterIDs: alters2,
|
||||
}
|
||||
user2 := protocol.NewUser(account2, protocol.UserLevel(0), "")
|
||||
assert.Bool(rec.HasUser(user2)).IsFalse()
|
||||
|
||||
rec.AddUser(user2)
|
||||
|
||||
Reference in New Issue
Block a user