Procházet zdrojové kódy

move fundamental interfaces from vmess to common

v2ray před 9 roky
rodič
revize
2147ba5ab3

+ 1 - 1
proxy/vmess/id.go → common/protocol/id.go

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

+ 2 - 2
proxy/vmess/id_test.go → common/protocol/id_test.go

@@ -1,11 +1,11 @@
-package vmess_test
+package protocol_test
 
 import (
 	"testing"
 
+	. "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/serial"
 	"github.com/v2ray/v2ray-core/common/uuid"
-	. "github.com/v2ray/v2ray-core/proxy/vmess"
 	v2testing "github.com/v2ray/v2ray-core/testing"
 	"github.com/v2ray/v2ray-core/testing/assert"
 )

+ 1 - 1
proxy/vmess/user.go → common/protocol/user.go

@@ -1,4 +1,4 @@
-package vmess
+package protocol
 
 import (
 	"github.com/v2ray/v2ray-core/common/dice"

+ 1 - 1
proxy/vmess/user_json.go → common/protocol/user_json.go

@@ -1,6 +1,6 @@
 // +build json
 
-package vmess
+package protocol
 
 import (
 	"encoding/json"

+ 6 - 2
proxy/shadowsocks/shadowsocks.go

@@ -154,7 +154,9 @@ func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
 	buffer := alloc.NewSmallBuffer()
 	defer buffer.Release()
 
-	_, err := io.ReadFull(conn, buffer.Value[:this.config.Cipher.IVSize()])
+	timedReader := v2net.NewTimeOutReader(16, conn)
+
+	_, err := io.ReadFull(timedReader, buffer.Value[:this.config.Cipher.IVSize()])
 	if err != nil {
 		log.Access(conn.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
 		log.Error("Shadowsocks: Failed to read IV: ", err)
@@ -164,7 +166,7 @@ func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
 	iv := buffer.Value[:this.config.Cipher.IVSize()]
 	key := this.config.Key
 
-	reader, err := this.config.Cipher.NewDecodingStream(key, iv, conn)
+	reader, err := this.config.Cipher.NewDecodingStream(key, iv, timedReader)
 	if err != nil {
 		log.Error("Shadowsocks: Failed to create decoding stream: ", err)
 		return
@@ -177,6 +179,8 @@ func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
 		return
 	}
 
+	timedReader.SetTimeOut(300)
+
 	dest := v2net.TCPDestination(request.Address, request.Port)
 	log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, serial.StringLiteral(""))
 	log.Info("Shadowsocks: Tunnelling request to ", dest)

+ 3 - 3
proxy/vmess/command/accounts.go

@@ -4,9 +4,9 @@ import (
 	"io"
 
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/serial"
 	"github.com/v2ray/v2ray-core/common/uuid"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 	"github.com/v2ray/v2ray-core/transport"
 )
 
@@ -27,7 +27,7 @@ type SwitchAccount struct {
 	Port     v2net.Port
 	ID       *uuid.UUID
 	AlterIds serial.Uint16Literal
-	Level    vmess.UserLevel
+	Level    proto.UserLevel
 	ValidMin byte
 }
 
@@ -83,7 +83,7 @@ func (this *SwitchAccount) Unmarshal(data []byte) error {
 	if len(data) < levelStart+1 {
 		return transport.ErrorCorruptedPacket
 	}
-	this.Level = vmess.UserLevel(data[levelStart])
+	this.Level = proto.UserLevel(data[levelStart])
 	timeStart := levelStart + 1
 	if len(data) < timeStart {
 		return transport.ErrorCorruptedPacket

+ 2 - 2
proxy/vmess/inbound/config.go

@@ -1,7 +1,7 @@
 package inbound
 
 import (
-	"github.com/v2ray/v2ray-core/proxy/vmess"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 )
 
 type DetourConfig struct {
@@ -13,6 +13,6 @@ type FeaturesConfig struct {
 }
 
 type Config struct {
-	AllowedUsers []*vmess.User
+	AllowedUsers []*proto.User
 	Features     *FeaturesConfig
 }

+ 2 - 2
proxy/vmess/inbound/config_json.go

@@ -5,8 +5,8 @@ package inbound
 import (
 	"encoding/json"
 
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/proxy/internal/config"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 )
 
 func (this *DetourConfig) UnmarshalJSON(data []byte) error {
@@ -35,7 +35,7 @@ func (this *FeaturesConfig) UnmarshalJSON(data []byte) error {
 
 func (this *Config) UnmarshalJSON(data []byte) error {
 	type JsonConfig struct {
-		Users    []*vmess.User   `json:"clients"`
+		Users    []*proto.User   `json:"clients"`
 		Features *FeaturesConfig `json:"features"`
 	}
 	jsonConfig := new(JsonConfig)

+ 4 - 4
proxy/vmess/inbound/inbound.go

@@ -13,10 +13,10 @@ import (
 	v2io "github.com/v2ray/v2ray-core/common/io"
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/serial"
 	"github.com/v2ray/v2ray-core/proxy"
 	"github.com/v2ray/v2ray-core/proxy/internal"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 	vmessio "github.com/v2ray/v2ray-core/proxy/vmess/io"
 	"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
 	"github.com/v2ray/v2ray-core/transport/hub"
@@ -28,7 +28,7 @@ type VMessInboundHandler struct {
 	packetDispatcher      dispatcher.PacketDispatcher
 	inboundHandlerManager proxyman.InboundHandlerManager
 	clients               protocol.UserSet
-	user                  *vmess.User
+	user                  *proto.User
 	accepting             bool
 	listener              *hub.TCPHub
 	features              *FeaturesConfig
@@ -49,7 +49,7 @@ func (this *VMessInboundHandler) Close() {
 	}
 }
 
-func (this *VMessInboundHandler) GetUser() *vmess.User {
+func (this *VMessInboundHandler) GetUser() *proto.User {
 	return this.user
 }
 
@@ -97,7 +97,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
 	readFinish.Lock()
 	writeFinish.Lock()
 
-	userSettings := vmess.GetUserSettings(request.User.Level)
+	userSettings := proto.GetUserSettings(request.User.Level)
 	connReader.SetTimeOut(userSettings.PayloadReadTimeout)
 	go handleInput(request, connReader, input, &readFinish)
 

+ 2 - 2
proxy/vmess/outbound/command.go

@@ -5,13 +5,13 @@ import (
 
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/serial"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 	"github.com/v2ray/v2ray-core/proxy/vmess/command"
 )
 
 func (this *VMessOutboundHandler) handleSwitchAccount(cmd *command.SwitchAccount) {
-	user := vmess.NewUser(vmess.NewID(cmd.ID), cmd.Level, cmd.AlterIds.Value())
+	user := proto.NewUser(proto.NewID(cmd.ID), cmd.Level, cmd.AlterIds.Value())
 	dest := v2net.TCPDestination(cmd.Host, cmd.Port)
 	this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin)
 }

+ 7 - 7
proxy/vmess/outbound/receiver.go

@@ -6,23 +6,23 @@ import (
 
 	"github.com/v2ray/v2ray-core/common/dice"
 	v2net "github.com/v2ray/v2ray-core/common/net"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 )
 
 type Receiver struct {
 	sync.RWMutex
 	Destination v2net.Destination
-	Accounts    []*vmess.User
+	Accounts    []*proto.User
 }
 
-func NewReceiver(dest v2net.Destination, users ...*vmess.User) *Receiver {
+func NewReceiver(dest v2net.Destination, users ...*proto.User) *Receiver {
 	return &Receiver{
 		Destination: dest,
 		Accounts:    users,
 	}
 }
 
-func (this *Receiver) HasUser(user *vmess.User) bool {
+func (this *Receiver) HasUser(user *proto.User) bool {
 	this.RLock()
 	defer this.RUnlock()
 	for _, u := range this.Accounts {
@@ -34,7 +34,7 @@ func (this *Receiver) HasUser(user *vmess.User) bool {
 	return false
 }
 
-func (this *Receiver) AddUser(user *vmess.User) {
+func (this *Receiver) AddUser(user *proto.User) {
 	if this.HasUser(user) {
 		return
 	}
@@ -43,7 +43,7 @@ func (this *Receiver) AddUser(user *vmess.User) {
 	this.Unlock()
 }
 
-func (this *Receiver) PickUser() *vmess.User {
+func (this *Receiver) PickUser() *proto.User {
 	return this.Accounts[dice.Roll(len(this.Accounts))]
 }
 
@@ -125,7 +125,7 @@ func (this *ReceiverManager) pickStdReceiver() *Receiver {
 	return this.receivers[dice.Roll(len(this.receivers))]
 }
 
-func (this *ReceiverManager) PickReceiver() (v2net.Destination, *vmess.User) {
+func (this *ReceiverManager) PickReceiver() (v2net.Destination, *proto.User) {
 	rec := this.pickDetour()
 	if rec == nil {
 		rec = this.pickStdReceiver()

+ 2 - 2
proxy/vmess/outbound/receiver_json.go

@@ -7,15 +7,15 @@ import (
 
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/proxy/internal"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 )
 
 func (this *Receiver) UnmarshalJSON(data []byte) error {
 	type RawConfigTarget struct {
 		Address *v2net.AddressJson `json:"address"`
 		Port    v2net.Port         `json:"port"`
-		Users   []*vmess.User      `json:"users"`
+		Users   []*proto.User      `json:"users"`
 	}
 	var rawConfig RawConfigTarget
 	if err := json.Unmarshal(data, &rawConfig); err != nil {

+ 5 - 5
proxy/vmess/outbound/receiver_test.go

@@ -4,8 +4,8 @@ import (
 	"testing"
 
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/uuid"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 	. "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
 	v2testing "github.com/v2ray/v2ray-core/testing"
 	"github.com/v2ray/v2ray-core/testing/assert"
@@ -14,14 +14,14 @@ import (
 func TestReceiverUser(t *testing.T) {
 	v2testing.Current(t)
 
-	id := vmess.NewID(uuid.New())
-	user := vmess.NewUser(id, vmess.UserLevel(0), 100)
+	id := proto.NewID(uuid.New())
+	user := proto.NewUser(id, proto.UserLevel(0), 100)
 	rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user)
 	assert.Bool(rec.HasUser(user)).IsTrue()
 	assert.Int(len(rec.Accounts)).Equals(1)
 
-	id2 := vmess.NewID(uuid.New())
-	user2 := vmess.NewUser(id2, vmess.UserLevel(0), 100)
+	id2 := proto.NewID(uuid.New())
+	user2 := proto.NewUser(id2, proto.UserLevel(0), 100)
 	assert.Bool(rec.HasUser(user2)).IsFalse()
 
 	rec.AddUser(user2)

+ 4 - 4
proxy/vmess/protocol/testing/mockuserset.go

@@ -1,22 +1,22 @@
 package mocks
 
 import (
-	"github.com/v2ray/v2ray-core/proxy/vmess"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
 )
 
 type MockUserSet struct {
-	Users      []*vmess.User
+	Users      []*proto.User
 	UserHashes map[string]int
 	Timestamps map[string]protocol.Timestamp
 }
 
-func (us *MockUserSet) AddUser(user *vmess.User) error {
+func (us *MockUserSet) AddUser(user *proto.User) error {
 	us.Users = append(us.Users, user)
 	return nil
 }
 
-func (us *MockUserSet) GetUser(userhash []byte) (*vmess.User, protocol.Timestamp, bool) {
+func (us *MockUserSet) GetUser(userhash []byte) (*proto.User, protocol.Timestamp, bool) {
 	idx, found := us.UserHashes[string(userhash)]
 	if found {
 		return us.Users[idx], us.Timestamps[string(userhash)], true

+ 5 - 5
proxy/vmess/protocol/testing/static_userset.go

@@ -1,21 +1,21 @@
 package mocks
 
 import (
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/uuid"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 	"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
 )
 
 type StaticUserSet struct {
 }
 
-func (us *StaticUserSet) AddUser(user *vmess.User) error {
+func (us *StaticUserSet) AddUser(user *proto.User) error {
 	return nil
 }
 
-func (us *StaticUserSet) GetUser(userhash []byte) (*vmess.User, protocol.Timestamp, bool) {
+func (us *StaticUserSet) GetUser(userhash []byte) (*proto.User, protocol.Timestamp, bool) {
 	id, _ := uuid.ParseString("703e9102-eb57-499c-8b59-faf4f371bb21")
-	return &vmess.User{
-		ID: vmess.NewID(id),
+	return &proto.User{
+		ID: proto.NewID(id),
 	}, 0, true
 }

+ 8 - 8
proxy/vmess/protocol/userset.go

@@ -4,8 +4,8 @@ import (
 	"sync"
 	"time"
 
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/serial"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 )
 
 const (
@@ -30,19 +30,19 @@ func (this Timestamp) HashBytes() []byte {
 }
 
 type idEntry struct {
-	id             *vmess.ID
+	id             *proto.ID
 	userIdx        int
 	lastSec        Timestamp
 	lastSecRemoval Timestamp
 }
 
 type UserSet interface {
-	AddUser(user *vmess.User) error
-	GetUser(timeHash []byte) (*vmess.User, Timestamp, bool)
+	AddUser(user *proto.User) error
+	GetUser(timeHash []byte) (*proto.User, Timestamp, bool)
 }
 
 type TimedUserSet struct {
-	validUsers []*vmess.User
+	validUsers []*proto.User
 	userHash   map[[16]byte]indexTimePair
 	ids        []*idEntry
 	access     sync.RWMutex
@@ -55,7 +55,7 @@ type indexTimePair struct {
 
 func NewTimedUserSet() UserSet {
 	tus := &TimedUserSet{
-		validUsers: make([]*vmess.User, 0, 16),
+		validUsers: make([]*proto.User, 0, 16),
 		userHash:   make(map[[16]byte]indexTimePair, 512),
 		access:     sync.RWMutex{},
 		ids:        make([]*idEntry, 0, 512),
@@ -96,7 +96,7 @@ func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
 	}
 }
 
-func (us *TimedUserSet) AddUser(user *vmess.User) error {
+func (us *TimedUserSet) AddUser(user *proto.User) error {
 	idx := len(us.validUsers)
 	us.validUsers = append(us.validUsers, user)
 
@@ -124,7 +124,7 @@ func (us *TimedUserSet) AddUser(user *vmess.User) error {
 	return nil
 }
 
-func (us *TimedUserSet) GetUser(userHash []byte) (*vmess.User, Timestamp, bool) {
+func (us *TimedUserSet) GetUser(userHash []byte) (*proto.User, Timestamp, bool) {
 	defer us.access.RUnlock()
 	us.access.RLock()
 	var fixedSizeHash [16]byte

+ 3 - 3
proxy/vmess/protocol/vmess.go

@@ -11,8 +11,8 @@ import (
 	v2crypto "github.com/v2ray/v2ray-core/common/crypto"
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/proxy"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 	"github.com/v2ray/v2ray-core/transport"
 )
 
@@ -36,7 +36,7 @@ const (
 // streaming.
 type VMessRequest struct {
 	Version        byte
-	User           *vmess.User
+	User           *proto.User
 	RequestIV      []byte
 	RequestKey     []byte
 	ResponseHeader byte
@@ -76,7 +76,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 	buffer := alloc.NewSmallBuffer()
 	defer buffer.Release()
 
-	nBytes, err := io.ReadFull(reader, buffer.Value[:vmess.IDBytesLen])
+	nBytes, err := io.ReadFull(reader, buffer.Value[:proto.IDBytesLen])
 	if err != nil {
 		log.Debug("VMess: Failed to read request ID (", nBytes, " bytes): ", err)
 		return nil, err

+ 7 - 7
proxy/vmess/protocol/vmess_test.go

@@ -8,8 +8,8 @@ import (
 	"time"
 
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/uuid"
-	"github.com/v2ray/v2ray-core/proxy/vmess"
 	. "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
 	protocoltesting "github.com/v2ray/v2ray-core/proxy/vmess/protocol/testing"
 	v2testing "github.com/v2ray/v2ray-core/testing"
@@ -30,13 +30,13 @@ func TestVMessSerialization(t *testing.T) {
 	id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
 	assert.Error(err).IsNil()
 
-	userId := vmess.NewID(id)
+	userId := proto.NewID(id)
 
-	testUser := &vmess.User{
+	testUser := &proto.User{
 		ID: userId,
 	}
 
-	userSet := protocoltesting.MockUserSet{[]*vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
+	userSet := protocoltesting.MockUserSet{[]*proto.User{}, make(map[string]int), make(map[string]Timestamp)}
 	userSet.AddUser(testUser)
 
 	request := new(VMessRequest)
@@ -91,10 +91,10 @@ func BenchmarkVMessRequestWriting(b *testing.B) {
 	id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
 	assert.Error(err).IsNil()
 
-	userId := vmess.NewID(id)
-	userSet := protocoltesting.MockUserSet{[]*vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
+	userId := proto.NewID(id)
+	userSet := protocoltesting.MockUserSet{[]*proto.User{}, make(map[string]int), make(map[string]Timestamp)}
 
-	testUser := &vmess.User{
+	testUser := &proto.User{
 		ID: userId,
 	}
 	userSet.AddUser(testUser)

+ 2 - 2
proxy/vmess/vmess_test.go

@@ -8,11 +8,11 @@ import (
 	"github.com/v2ray/v2ray-core/app/dispatcher"
 	v2net "github.com/v2ray/v2ray-core/common/net"
 	v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
+	proto "github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/common/uuid"
 	"github.com/v2ray/v2ray-core/proxy"
 	proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
 	proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
-	vmess "github.com/v2ray/v2ray-core/proxy/vmess"
 	_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
 	_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
 	"github.com/v2ray/v2ray-core/shell/point"
@@ -26,7 +26,7 @@ func TestVMessInAndOut(t *testing.T) {
 	id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
 	assert.Error(err).IsNil()
 
-	testAccount := vmess.NewID(id)
+	testAccount := proto.NewID(id)
 
 	portA := v2nettesting.PickPort()
 	portB := v2nettesting.PickPort()