Skip to content

Commit

Permalink
alternative user ids
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 8, 2016
1 parent 43f76d4 commit 4ca43c3
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 16 deletions.
27 changes: 26 additions & 1 deletion common/uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package uuid

import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"errors"
Expand All @@ -22,7 +24,30 @@ func (this *UUID) String() string {
}

func (this *UUID) Bytes() []byte {
return this.byteValue[:]
return this.byteValue
}

func (this *UUID) Equals(another *UUID) bool {
if this == nil && another == nil {
return true
}
if this == nil || another == nil {
return false
}
return bytes.Equal(this.Bytes(), another.Bytes())
}

// Next generates a deterministic random UUID based on this UUID.
func (this *UUID) Next() *UUID {
md5hash := md5.New()
md5hash.Write(this.Bytes())
md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
for {
newid, _ := ParseBytes(md5hash.Sum(nil))
if !newid.Equals(this) {
return newid
}
}
}

func bytesToString(bytes []byte) string {
Expand Down
4 changes: 4 additions & 0 deletions proxy/vmess/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (this *ID) String() string {
return this.uuid.String()
}

func (this *ID) UUID() *uuid.UUID {
return this.uuid
}

func NewID(uuid *uuid.UUID) *ID {
md5hash := md5.New()
md5hash.Write(uuid.Bytes())
Expand Down
4 changes: 4 additions & 0 deletions proxy/vmess/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (this *VMessInboundHandler) Close() {
}
}

func (this *VMessInboundHandler) AddUser(user vmess.User) {

}

func (this *VMessInboundHandler) Listen(port v2net.Port) error {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Expand Down
36 changes: 33 additions & 3 deletions proxy/vmess/json/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package json

import (
"encoding/json"
"math/rand"

"github.com/v2ray/v2ray-core/common/uuid"
"github.com/v2ray/v2ray-core/proxy/vmess"
Expand All @@ -12,13 +13,15 @@ type ConfigUser struct {
Id *vmess.ID
Email string
LevelValue vmess.UserLevel
AlterIds []*vmess.ID
}

func (u *ConfigUser) UnmarshalJSON(data []byte) error {
type rawUser struct {
IdString string `json:"id"`
EmailString string `json:"email"`
LevelInt int `json:"level"`
IdString string `json:"id"`
EmailString string `json:"email"`
LevelInt int `json:"level"`
AlterIdCount int `json:"alterId"`
}
var rawUserValue rawUser
if err := json.Unmarshal(data, &rawUserValue); err != nil {
Expand All @@ -31,6 +34,18 @@ func (u *ConfigUser) UnmarshalJSON(data []byte) error {
u.Id = vmess.NewID(id)
u.Email = rawUserValue.EmailString
u.LevelValue = vmess.UserLevel(rawUserValue.LevelInt)

if rawUserValue.AlterIdCount > 0 {
prevId := u.Id.UUID()
// TODO: check duplicate
u.AlterIds = make([]*vmess.ID, rawUserValue.AlterIdCount)
for idx, _ := range u.AlterIds {
newid := prevId.Next()
u.AlterIds[idx] = vmess.NewID(newid)
prevId = newid
}
}

return nil
}

Expand All @@ -41,3 +56,18 @@ func (u *ConfigUser) ID() *vmess.ID {
func (this *ConfigUser) Level() vmess.UserLevel {
return this.LevelValue
}

func (this *ConfigUser) AlterIDs() []*vmess.ID {
return this.AlterIds
}

func (this *ConfigUser) AnyValidID() *vmess.ID {
if len(this.AlterIds) == 0 {
return this.ID()
}
if len(this.AlterIds) == 1 {
return this.AlterIds[0]
}
idIdx := rand.Intn(len(this.AlterIds))
return this.AlterIds[idIdx]
}
8 changes: 8 additions & 0 deletions proxy/vmess/protocol/user/testing/mocks/static_userset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func (this *StaticUser) Level() vmess.UserLevel {
return vmess.UserLevelUntrusted
}

func (this *StaticUser) AlterIDs() []*vmess.ID {
return nil
}

func (this *StaticUser) AnyValidID() *vmess.ID {
return this.id
}

type StaticUserSet struct {
}

Expand Down
6 changes: 6 additions & 0 deletions proxy/vmess/protocol/user/userset.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
nowSec := now.Unix() + cacheDurationSec
for idx, user := range us.validUsers {
us.generateNewHashes(lastSec, nowSec, idx, user.ID())
for _, alterid := range user.AlterIDs() {
us.generateNewHashes(lastSec, nowSec, idx, alterid)
}
}
lastSec = nowSec
}
Expand All @@ -82,6 +85,9 @@ func (us *TimedUserSet) AddUser(user vmess.User) error {
nowSec := time.Now().Unix()
lastSec := nowSec - cacheDurationSec
us.generateNewHashes(lastSec, nowSec+cacheDurationSec, idx, id)
for _, alterid := range user.AlterIDs() {
us.generateNewHashes(lastSec, nowSec+cacheDurationSec, idx, alterid)
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion proxy/vmess/protocol/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (this *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user
}

counter := randomRangeInt64(time.Now().Unix(), 30)
hash := idHash.Hash(this.User.ID().Bytes(), counter)
hash := idHash.Hash(this.User.AnyValidID().Bytes(), counter)

buffer.Append(hash)

Expand Down
11 changes: 2 additions & 9 deletions proxy/vmess/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package vmess

import (
"time"
)

type UserLevel int

const (
Expand All @@ -13,12 +9,9 @@ const (

type User interface {
ID() *ID
AlterIDs() []*ID
Level() UserLevel
}

type SecondaryID interface {
ID() *ID
ValidUntil() time.Time
AnyValidID() *ID
}

type UserSettings struct {
Expand Down
5 changes: 4 additions & 1 deletion testing/scenarios/data/test_1_client.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"address": "127.0.0.1",
"port": 50001,
"users": [
{"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"}
{
"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f",
"alterId": 10
}
]
}
]
Expand Down
3 changes: 2 additions & 1 deletion testing/scenarios/data/test_1_server.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"clients": [
{
"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f",
"level": 1
"level": 1,
"alterId": 10
}
]
}
Expand Down

0 comments on commit 4ca43c3

Please sign in to comment.