1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

re-org vmess content

This commit is contained in:
Darien Raymond 2015-12-07 19:32:38 +00:00
parent 0a4d9fb037
commit af8412175e
24 changed files with 113 additions and 108 deletions

View File

@ -1,5 +0,0 @@
package config
type Inbound interface {
AllowedUsers() []User
}

View File

@ -1,14 +0,0 @@
package config
import (
v2net "github.com/v2ray/v2ray-core/common/net"
)
type Receiver struct {
Address v2net.Address
Accounts []User
}
type Outbound interface {
Receivers() []*Receiver
}

View File

@ -1,4 +1,4 @@
package config
package vmess
import (
"crypto/md5"

View File

@ -1,4 +1,4 @@
package config
package vmess
import (
"testing"

View File

@ -0,0 +1,9 @@
package inbound
import (
"github.com/v2ray/v2ray-core/proxy/vmess"
)
type Config interface {
AllowedUsers() []vmess.User
}

View File

@ -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"
)
@ -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)
@ -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() {

View File

@ -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)
}

View File

@ -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 {
@ -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
}

View File

@ -0,0 +1,5 @@
package outbound
type Config interface {
Receivers() []*Receiver
}

View File

@ -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 {
@ -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,
})

View File

@ -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"
)

View File

@ -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"
@ -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()),

View File

@ -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 ReceiverManager struct {
receivers []*config.Receiver
type Receiver struct {
Address v2net.Address
Accounts []vmess.User
}
func NewReceiverManager(receivers []*config.Receiver) *ReceiverManager {
type ReceiverManager struct {
receivers []*Receiver
}
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 {

View File

@ -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

View File

@ -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
}

View File

@ -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 (
@ -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
@ -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{},
@ -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)
@ -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)
@ -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)]

View File

@ -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"
)
@ -35,7 +35,7 @@ const (
// streaming.
type VMessRequest struct {
Version byte
User config.User
User vmess.User
RequestIV []byte
RequestKey []byte
ResponseHeader []byte
@ -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
}

View File

@ -7,7 +7,7 @@ import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
"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/proxy/vmess/protocol/user/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing"
@ -15,22 +15,22 @@ import (
)
type TestUser struct {
id *config.ID
level config.UserLevel
id *vmess.ID
level vmess.UserLevel
}
func (u *TestUser) ID() *config.ID {
func (u *TestUser) ID() *vmess.ID {
return u.id
}
func (this *TestUser) Level() config.UserLevel {
func (this *TestUser) Level() vmess.UserLevel {
return this.level
}
func TestVMessSerialization(t *testing.T) {
v2testing.Current(t)
userId, err := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
userId, err := vmess.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
if err != nil {
t.Fatal(err)
}
@ -39,7 +39,7 @@ func TestVMessSerialization(t *testing.T) {
id: userId,
}
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
userSet := mocks.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]int64)}
userSet.AddUser(testUser)
request := new(VMessRequest)
@ -90,8 +90,8 @@ func TestReadSingleByte(t *testing.T) {
}
func BenchmarkVMessRequestWriting(b *testing.B) {
userId, _ := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
userId, _ := vmess.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
userSet := mocks.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]int64)}
testUser := &TestUser{
id: userId,

View File

@ -1,4 +1,4 @@
package config
package vmess
type UserLevel int

View File

@ -4,9 +4,3 @@
// together with 'freedom' to talk to final destination, while VMess outbound is usually used on
// clients with 'socks' for proxying.
package vmess
// The actual implementation is in the following packages respectively.
import (
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
)

View File

@ -1,4 +1,4 @@
package vmess
package vmess_test
import (
"bytes"
@ -8,8 +8,12 @@ import (
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess/config/json"
vmess "github.com/v2ray/v2ray-core/proxy/vmess"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
inboundjson "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json"
vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
outboundjson "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
"github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing"
@ -19,7 +23,7 @@ import (
func TestVMessInAndOut(t *testing.T) {
v2testing.Current(t)
testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
testAccount, err := vmess.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
assert.Error(err).IsNil()
portA := v2nettesting.PickPort()
@ -42,12 +46,12 @@ func TestVMessInAndOut(t *testing.T) {
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
SettingsValue: &json.Outbound{
[]*json.ConfigTarget{
&json.ConfigTarget{
SettingsValue: &outboundjson.Outbound{
[]*outboundjson.ConfigTarget{
&outboundjson.ConfigTarget{
Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
Users: []*json.ConfigUser{
&json.ConfigUser{Id: testAccount},
Users: []*vmessjson.ConfigUser{
&vmessjson.ConfigUser{Id: testAccount},
},
},
},
@ -74,9 +78,9 @@ func TestVMessInAndOut(t *testing.T) {
PortValue: portB,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
SettingsValue: &json.Inbound{
AllowedClients: []*json.ConfigUser{
&json.ConfigUser{Id: testAccount},
SettingsValue: &inboundjson.Inbound{
AllowedClients: []*vmessjson.ConfigUser{
&vmessjson.ConfigUser{Id: testAccount},
},
},
},

View File

@ -23,8 +23,10 @@ import (
_ "github.com/v2ray/v2ray-core/proxy/freedom/json"
_ "github.com/v2ray/v2ray-core/proxy/socks"
_ "github.com/v2ray/v2ray-core/proxy/socks/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess"
_ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
)
var (

View File

@ -9,7 +9,8 @@ import (
_ "github.com/v2ray/v2ray-core/proxy/dokodemo/json"
_ "github.com/v2ray/v2ray-core/proxy/freedom/json"
_ "github.com/v2ray/v2ray-core/proxy/socks/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
"github.com/v2ray/v2ray-core/shell/point/json"
v2testing "github.com/v2ray/v2ray-core/testing"

View File

@ -20,8 +20,10 @@ import (
_ "github.com/v2ray/v2ray-core/proxy/freedom/json"
_ "github.com/v2ray/v2ray-core/proxy/socks"
_ "github.com/v2ray/v2ray-core/proxy/socks/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess"
_ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
)
var (