Skip to content

Commit

Permalink
re-org vmess content
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Dec 7, 2015
1 parent 0a4d9fb commit af84121
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 107 deletions.
5 changes: 0 additions & 5 deletions proxy/vmess/config/inbound.go

This file was deleted.

14 changes: 0 additions & 14 deletions proxy/vmess/config/outbound.go

This file was deleted.

2 changes: 1 addition & 1 deletion proxy/vmess/config/id.go → proxy/vmess/id.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package vmess

import (
"crypto/md5"
Expand Down
2 changes: 1 addition & 1 deletion proxy/vmess/config/id_test.go → proxy/vmess/id_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package vmess

import (
"testing"
Expand Down
9 changes: 9 additions & 0 deletions proxy/vmess/inbound/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package inbound

import (
"github.com/v2ray/v2ray-core/proxy/vmess"
)

type Config interface {
AllowedUsers() []vmess.User
}
6 changes: 3 additions & 3 deletions proxy/vmess/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
)
Expand Down Expand Up @@ -85,7 +85,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
readFinish.Lock()
writeFinish.Lock()

userSettings := config.GetUserSettings(request.User.Level())
userSettings := vmess.GetUserSettings(request.User.Level())
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
go handleInput(request, connReader, input, &readFinish)

Expand Down Expand Up @@ -143,7 +143,7 @@ type VMessInboundHandlerFactory struct {
}

func (this *VMessInboundHandlerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
config := rawConfig.(config.Inbound)
config := rawConfig.(Config)

allowedClients := user.NewTimedUserSet()
for _, user := range config.AllowedUsers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package json

import (
"github.com/v2ray/v2ray-core/proxy/common/config/json"
vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
)

type Inbound struct {
AllowedClients []*ConfigUser `json:"clients"`
AllowedClients []*vmessjson.ConfigUser `json:"clients"`
}

func (c *Inbound) AllowedUsers() []vmessconfig.User {
users := make([]vmessconfig.User, 0, len(c.AllowedClients))
func (c *Inbound) AllowedUsers() []vmess.User {
users := make([]vmess.User, 0, len(c.AllowedClients))
for _, rawUser := range c.AllowedClients {
users = append(users, rawUser)
}
Expand Down
14 changes: 7 additions & 7 deletions proxy/vmess/config/json/user.go → proxy/vmess/json/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package json
import (
"encoding/json"

"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
)

// ConfigUser is an user account in VMess configuration.
type ConfigUser struct {
Id *config.ID
Id *vmess.ID
Email string
LevelValue config.UserLevel
LevelValue vmess.UserLevel
}

func (u *ConfigUser) UnmarshalJSON(data []byte) error {
Expand All @@ -23,20 +23,20 @@ func (u *ConfigUser) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &rawUserValue); err != nil {
return err
}
id, err := config.NewID(rawUserValue.IdString)
id, err := vmess.NewID(rawUserValue.IdString)
if err != nil {
return err
}
u.Id = id
u.Email = rawUserValue.EmailString
u.LevelValue = config.UserLevel(rawUserValue.LevelInt)
u.LevelValue = vmess.UserLevel(rawUserValue.LevelInt)
return nil
}

func (u *ConfigUser) ID() *config.ID {
func (u *ConfigUser) ID() *vmess.ID {
return u.Id
}

func (this *ConfigUser) Level() config.UserLevel {
func (this *ConfigUser) Level() vmess.UserLevel {
return this.LevelValue
}
5 changes: 5 additions & 0 deletions proxy/vmess/outbound/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package outbound

type Config interface {
Receivers() []*Receiver
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import (
v2net "github.com/v2ray/v2ray-core/common/net"
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
"github.com/v2ray/v2ray-core/proxy/vmess/outbound"
)

type ConfigTarget struct {
Address v2net.Address
Users []*ConfigUser
Users []*vmessjson.ConfigUser
}

func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
type RawConfigTarget struct {
Address string `json:"address"`
Port v2net.Port `json:"port"`
Users []*ConfigUser `json:"users"`
Address string `json:"address"`
Port v2net.Port `json:"port"`
Users []*vmessjson.ConfigUser `json:"users"`
}
var rawConfig RawConfigTarget
if err := json.Unmarshal(data, &rawConfig); err != nil {
Expand Down Expand Up @@ -61,14 +63,14 @@ func (this *Outbound) UnmarshalJSON(data []byte) error {
return nil
}

func (o *Outbound) Receivers() []*vmessconfig.Receiver {
targets := make([]*vmessconfig.Receiver, 0, 2*len(o.TargetList))
func (o *Outbound) Receivers() []*outbound.Receiver {
targets := make([]*outbound.Receiver, 0, 2*len(o.TargetList))
for _, rawTarget := range o.TargetList {
users := make([]vmessconfig.User, 0, len(rawTarget.Users))
users := make([]vmess.User, 0, len(rawTarget.Users))
for _, rawUser := range rawTarget.Users {
users = append(users, rawUser)
}
targets = append(targets, &vmessconfig.Receiver{
targets = append(targets, &outbound.Receiver{
Address: rawTarget.Address,
Accounts: users,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"testing"

jsonconfig "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
jsonconfig "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
Expand Down
3 changes: 1 addition & 2 deletions proxy/vmess/outbound/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
"github.com/v2ray/v2ray-core/transport/ray"
Expand Down Expand Up @@ -177,7 +176,7 @@ type VMessOutboundHandlerFactory struct {
}

func (this *VMessOutboundHandlerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
vOutConfig := rawConfig.(config.Outbound)
vOutConfig := rawConfig.(Config)
return &VMessOutboundHandler{
space: space,
receiverManager: NewReceiverManager(vOutConfig.Receivers()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ import (
"math/rand"

v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
)

type Receiver struct {
Address v2net.Address
Accounts []vmess.User
}

type ReceiverManager struct {
receivers []*config.Receiver
receivers []*Receiver
}

func NewReceiverManager(receivers []*config.Receiver) *ReceiverManager {
func NewReceiverManager(receivers []*Receiver) *ReceiverManager {
return &ReceiverManager{
receivers: receivers,
}
}

func (this *ReceiverManager) PickReceiver() (v2net.Address, config.User) {
func (this *ReceiverManager) PickReceiver() (v2net.Address, vmess.User) {
receiverLen := len(this.receivers)
receiverIdx := 0
if receiverLen > 1 {
Expand Down
8 changes: 4 additions & 4 deletions proxy/vmess/protocol/user/testing/mocks/mockuserset.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package mocks

import (
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
)

type MockUserSet struct {
Users []config.User
Users []vmess.User
UserHashes map[string]int
Timestamps map[string]int64
}

func (us *MockUserSet) AddUser(user config.User) error {
func (us *MockUserSet) AddUser(user vmess.User) error {
us.Users = append(us.Users, user)
return nil
}

func (us *MockUserSet) GetUser(userhash []byte) (config.User, int64, bool) {
func (us *MockUserSet) GetUser(userhash []byte) (vmess.User, int64, bool) {
idx, found := us.UserHashes[string(userhash)]
if found {
return us.Users[idx], us.Timestamps[string(userhash)], true
Expand Down
16 changes: 8 additions & 8 deletions proxy/vmess/protocol/user/testing/mocks/static_userset.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package mocks

import (
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
)

type StaticUser struct {
id *config.ID
id *vmess.ID
}

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

func (this *StaticUser) Level() config.UserLevel {
return config.UserLevelUntrusted
func (this *StaticUser) Level() vmess.UserLevel {
return vmess.UserLevelUntrusted
}

type StaticUserSet struct {
}

func (us *StaticUserSet) AddUser(user config.User) error {
func (us *StaticUserSet) AddUser(user vmess.User) error {
return nil
}

func (us *StaticUserSet) GetUser(userhash []byte) (config.User, int64, bool) {
id, _ := config.NewID("703e9102-eb57-499c-8b59-faf4f371bb21")
func (us *StaticUserSet) GetUser(userhash []byte) (vmess.User, int64, bool) {
id, _ := vmess.NewID("703e9102-eb57-499c-8b59-faf4f371bb21")
return &StaticUser{id: id}, 0, true
}
16 changes: 8 additions & 8 deletions proxy/vmess/protocol/user/userset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/v2ray/v2ray-core/common/collect"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
)

const (
Expand All @@ -14,12 +14,12 @@ const (
)

type UserSet interface {
AddUser(user config.User) error
GetUser(timeHash []byte) (config.User, int64, bool)
AddUser(user vmess.User) error
GetUser(timeHash []byte) (vmess.User, int64, bool)
}

type TimedUserSet struct {
validUsers []config.User
validUsers []vmess.User
userHash map[string]indexTimePair
userHashDeleteQueue *collect.TimedQueue
access sync.RWMutex
Expand All @@ -32,7 +32,7 @@ type indexTimePair struct {

func NewTimedUserSet() UserSet {
tus := &TimedUserSet{
validUsers: make([]config.User, 0, 16),
validUsers: make([]vmess.User, 0, 16),
userHash: make(map[string]indexTimePair, 512),
userHashDeleteQueue: collect.NewTimedQueue(updateIntervalSec),
access: sync.RWMutex{},
Expand All @@ -50,7 +50,7 @@ func (us *TimedUserSet) removeEntries(entries <-chan interface{}) {
}
}

func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id *config.ID) {
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id *vmess.ID) {
idHash := NewTimeHash(HMACHash{})
for lastSec < nowSec {
idHash := idHash.Hash(id.Bytes[:], lastSec)
Expand All @@ -74,7 +74,7 @@ func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
}
}

func (us *TimedUserSet) AddUser(user config.User) error {
func (us *TimedUserSet) AddUser(user vmess.User) error {
id := user.ID()
idx := len(us.validUsers)
us.validUsers = append(us.validUsers, user)
Expand All @@ -86,7 +86,7 @@ func (us *TimedUserSet) AddUser(user config.User) error {
return nil
}

func (us *TimedUserSet) GetUser(userHash []byte) (config.User, int64, bool) {
func (us *TimedUserSet) GetUser(userHash []byte) (vmess.User, int64, bool) {
defer us.access.RUnlock()
us.access.RLock()
pair, found := us.userHash[string(userHash)]
Expand Down
6 changes: 3 additions & 3 deletions proxy/vmess/protocol/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
"github.com/v2ray/v2ray-core/transport"
)
Expand All @@ -35,7 +35,7 @@ const (
// streaming.
type VMessRequest struct {
Version byte
User config.User
User vmess.User
RequestIV []byte
RequestKey []byte
ResponseHeader []byte
Expand Down Expand Up @@ -68,7 +68,7 @@ func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader {
func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
buffer := alloc.NewSmallBuffer()

nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:config.IDBytesLen])
nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:vmess.IDBytesLen])
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit af84121

Please sign in to comment.