mirror of https://github.com/v2ray/v2ray-core
remove proxy/vmess/protocol/user
parent
dc1fbecdfb
commit
bed5235772
|
@ -16,19 +16,18 @@ import (
|
||||||
"github.com/v2ray/v2ray-core/proxy/internal"
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Inbound connection handler that handles messages in VMess format.
|
// Inbound connection handler that handles messages in VMess format.
|
||||||
type VMessInboundHandler struct {
|
type VMessInboundHandler struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
space app.Space
|
space app.Space
|
||||||
clients user.UserSet
|
clients protocol.UserSet
|
||||||
accepting bool
|
accepting bool
|
||||||
listener *net.TCPListener
|
listener *net.TCPListener
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVMessInboundHandler(space app.Space, clients user.UserSet) *VMessInboundHandler {
|
func NewVMessInboundHandler(space app.Space, clients protocol.UserSet) *VMessInboundHandler {
|
||||||
return &VMessInboundHandler{
|
return &VMessInboundHandler{
|
||||||
space: space,
|
space: space,
|
||||||
clients: clients,
|
clients: clients,
|
||||||
|
@ -170,7 +169,7 @@ func init() {
|
||||||
func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
|
func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
|
||||||
config := rawConfig.(Config)
|
config := rawConfig.(Config)
|
||||||
|
|
||||||
allowedClients := user.NewTimedUserSet()
|
allowedClients := protocol.NewTimedUserSet()
|
||||||
for _, user := range config.AllowedUsers() {
|
for _, user := range config.AllowedUsers() {
|
||||||
allowedClients.AddUser(user)
|
allowedClients.AddUser(user)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@ import (
|
||||||
"github.com/v2ray/v2ray-core/proxy"
|
"github.com/v2ray/v2ray-core/proxy"
|
||||||
"github.com/v2ray/v2ray-core/proxy/internal"
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
|
||||||
"github.com/v2ray/v2ray-core/transport/ray"
|
"github.com/v2ray/v2ray-core/transport/ray"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -105,7 +104,7 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2
|
||||||
|
|
||||||
buffer := alloc.NewBuffer().Clear()
|
buffer := alloc.NewBuffer().Clear()
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
buffer, err = request.ToBytes(user.NewRandomTimestampGenerator(user.Timestamp(time.Now().Unix()), 30), buffer)
|
buffer, err = request.ToBytes(protocol.NewRandomTimestampGenerator(protocol.Timestamp(time.Now().Unix()), 30), buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
|
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package user
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
|
@ -1,4 +1,4 @@
|
||||||
package user
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
|
@ -1,9 +1,10 @@
|
||||||
package user
|
package protocol_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
)
|
)
|
||||||
|
@ -13,10 +14,7 @@ func TestGenerateRandomInt64InRange(t *testing.T) {
|
||||||
|
|
||||||
base := time.Now().Unix()
|
base := time.Now().Unix()
|
||||||
delta := 100
|
delta := 100
|
||||||
generator := &RealRandomTimestampGenerator{
|
generator := NewRandomTimestampGenerator(Timestamp(base), delta)
|
||||||
base: Timestamp(base),
|
|
||||||
delta: delta,
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
v := int64(generator.Next())
|
v := int64(generator.Next())
|
|
@ -2,13 +2,13 @@ package mocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockUserSet struct {
|
type MockUserSet struct {
|
||||||
Users []vmess.User
|
Users []vmess.User
|
||||||
UserHashes map[string]int
|
UserHashes map[string]int
|
||||||
Timestamps map[string]user.Timestamp
|
Timestamps map[string]protocol.Timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *MockUserSet) AddUser(user vmess.User) error {
|
func (us *MockUserSet) AddUser(user vmess.User) error {
|
||||||
|
@ -16,7 +16,7 @@ func (us *MockUserSet) AddUser(user vmess.User) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *MockUserSet) GetUser(userhash []byte) (vmess.User, user.Timestamp, bool) {
|
func (us *MockUserSet) GetUser(userhash []byte) (vmess.User, protocol.Timestamp, bool) {
|
||||||
idx, found := us.UserHashes[string(userhash)]
|
idx, found := us.UserHashes[string(userhash)]
|
||||||
if found {
|
if found {
|
||||||
return us.Users[idx], us.Timestamps[string(userhash)], true
|
return us.Users[idx], us.Timestamps[string(userhash)], true
|
|
@ -3,7 +3,7 @@ package mocks
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core/common/uuid"
|
"github.com/v2ray/v2ray-core/common/uuid"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StaticUser struct {
|
type StaticUser struct {
|
||||||
|
@ -33,7 +33,7 @@ func (us *StaticUserSet) AddUser(user vmess.User) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *StaticUserSet) GetUser(userhash []byte) (vmess.User, user.Timestamp, bool) {
|
func (us *StaticUserSet) GetUser(userhash []byte) (vmess.User, protocol.Timestamp, bool) {
|
||||||
id, _ := uuid.ParseString("703e9102-eb57-499c-8b59-faf4f371bb21")
|
id, _ := uuid.ParseString("703e9102-eb57-499c-8b59-faf4f371bb21")
|
||||||
return &StaticUser{id: vmess.NewID(id)}, 0, true
|
return &StaticUser{id: vmess.NewID(id)}, 0, true
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package user
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
|
@ -13,7 +13,6 @@ import (
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/proxy"
|
"github.com/v2ray/v2ray-core/proxy"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
|
||||||
"github.com/v2ray/v2ray-core/transport"
|
"github.com/v2ray/v2ray-core/transport"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -55,11 +54,11 @@ func (this *VMessRequest) Destination() v2net.Destination {
|
||||||
|
|
||||||
// VMessRequestReader is a parser to read VMessRequest from a byte stream.
|
// VMessRequestReader is a parser to read VMessRequest from a byte stream.
|
||||||
type VMessRequestReader struct {
|
type VMessRequestReader struct {
|
||||||
vUserSet user.UserSet
|
vUserSet UserSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVMessRequestReader creates a new VMessRequestReader with a given UserSet
|
// NewVMessRequestReader creates a new VMessRequestReader with a given UserSet
|
||||||
func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader {
|
func NewVMessRequestReader(vUserSet UserSet) *VMessRequestReader {
|
||||||
return &VMessRequestReader{
|
return &VMessRequestReader{
|
||||||
vUserSet: vUserSet,
|
vUserSet: vUserSet,
|
||||||
}
|
}
|
||||||
|
@ -81,7 +80,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||||
return nil, proxy.InvalidAuthentication
|
return nil, proxy.InvalidAuthentication
|
||||||
}
|
}
|
||||||
|
|
||||||
timestampHash := user.TimestampHash()
|
timestampHash := TimestampHash()
|
||||||
timestampHash.Write(timeSec.HashBytes())
|
timestampHash.Write(timeSec.HashBytes())
|
||||||
iv := timestampHash.Sum(nil)
|
iv := timestampHash.Sum(nil)
|
||||||
aesStream, err := v2crypto.NewAesDecryptionStream(userObj.ID().CmdKey(), iv)
|
aesStream, err := v2crypto.NewAesDecryptionStream(userObj.ID().CmdKey(), iv)
|
||||||
|
@ -172,13 +171,13 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToBytes returns a VMessRequest in the form of byte array.
|
// ToBytes returns a VMessRequest in the form of byte array.
|
||||||
func (this *VMessRequest) ToBytes(timestampGenerator user.RandomTimestampGenerator, buffer *alloc.Buffer) (*alloc.Buffer, error) {
|
func (this *VMessRequest) ToBytes(timestampGenerator RandomTimestampGenerator, buffer *alloc.Buffer) (*alloc.Buffer, error) {
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
buffer = alloc.NewSmallBuffer().Clear()
|
buffer = alloc.NewSmallBuffer().Clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp := timestampGenerator.Next()
|
timestamp := timestampGenerator.Next()
|
||||||
idHash := user.IDHash(this.User.AnyValidID().Bytes())
|
idHash := IDHash(this.User.AnyValidID().Bytes())
|
||||||
idHash.Write(timestamp.Bytes())
|
idHash.Write(timestamp.Bytes())
|
||||||
|
|
||||||
buffer.Append(idHash.Sum(nil))
|
buffer.Append(idHash.Sum(nil))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package protocol
|
package protocol_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -10,17 +10,17 @@ import (
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/common/uuid"
|
"github.com/v2ray/v2ray-core/common/uuid"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
. "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user/testing/mocks"
|
protocoltesting "github.com/v2ray/v2ray-core/proxy/vmess/protocol/testing"
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeTimestampGenerator struct {
|
type FakeTimestampGenerator struct {
|
||||||
timestamp user.Timestamp
|
timestamp Timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *FakeTimestampGenerator) Next() user.Timestamp {
|
func (this *FakeTimestampGenerator) Next() Timestamp {
|
||||||
return this.timestamp
|
return this.timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ func TestVMessSerialization(t *testing.T) {
|
||||||
id: userId,
|
id: userId,
|
||||||
}
|
}
|
||||||
|
|
||||||
userSet := mocks.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]user.Timestamp)}
|
userSet := protocoltesting.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
|
||||||
userSet.AddUser(testUser)
|
userSet.AddUser(testUser)
|
||||||
|
|
||||||
request := new(VMessRequest)
|
request := new(VMessRequest)
|
||||||
|
@ -75,7 +75,7 @@ func TestVMessSerialization(t *testing.T) {
|
||||||
request.Address = v2net.DomainAddress("v2ray.com")
|
request.Address = v2net.DomainAddress("v2ray.com")
|
||||||
request.Port = v2net.Port(80)
|
request.Port = v2net.Port(80)
|
||||||
|
|
||||||
mockTime := user.Timestamp(1823730)
|
mockTime := Timestamp(1823730)
|
||||||
|
|
||||||
buffer, err := request.ToBytes(&FakeTimestampGenerator{timestamp: mockTime}, nil)
|
buffer, err := request.ToBytes(&FakeTimestampGenerator{timestamp: mockTime}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -113,7 +113,7 @@ func BenchmarkVMessRequestWriting(b *testing.B) {
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
userId := vmess.NewID(id)
|
userId := vmess.NewID(id)
|
||||||
userSet := mocks.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]user.Timestamp)}
|
userSet := protocoltesting.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
|
||||||
|
|
||||||
testUser := &TestUser{
|
testUser := &TestUser{
|
||||||
id: userId,
|
id: userId,
|
||||||
|
@ -135,6 +135,6 @@ func BenchmarkVMessRequestWriting(b *testing.B) {
|
||||||
request.Port = v2net.Port(80)
|
request.Port = v2net.Port(80)
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
request.ToBytes(user.NewRandomTimestampGenerator(user.Timestamp(time.Now().Unix()), 30), nil)
|
request.ToBytes(NewRandomTimestampGenerator(Timestamp(time.Now().Unix()), 30), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue