allow dynamic type of user accounts

This commit is contained in:
v2ray
2016-05-28 13:44:11 +02:00
parent 87f664048b
commit 3f9cb1136a
18 changed files with 123 additions and 49 deletions
+3
View File
@@ -1 +1,4 @@
package http
type Client struct {
}
+3
View File
@@ -48,3 +48,6 @@ func (this *Config) IsOwnHost(host v2net.Address) bool {
}
return false
}
type ClientConfig struct {
}
+2 -2
View File
@@ -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),
}
+5 -1
View File
@@ -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()
+5 -1
View File
@@ -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)
}
+3 -1
View File
@@ -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
}
}
+4 -1
View File
@@ -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")
}
+10 -2
View File
@@ -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)