mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-15 08:46:34 -05:00
parent
2ad12084ba
commit
cb2658f2bf
@ -5,7 +5,6 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/chacha20poly1305"
|
"golang.org/x/crypto/chacha20poly1305"
|
||||||
|
|
||||||
@ -39,16 +38,12 @@ type ClientSession struct {
|
|||||||
responseHeader byte
|
responseHeader byte
|
||||||
}
|
}
|
||||||
|
|
||||||
var clientSessionPool = sync.Pool{
|
|
||||||
New: func() interface{} { return &ClientSession{} },
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewClientSession creates a new ClientSession.
|
// NewClientSession creates a new ClientSession.
|
||||||
func NewClientSession(idHash protocol.IDHash) *ClientSession {
|
func NewClientSession(idHash protocol.IDHash) *ClientSession {
|
||||||
randomBytes := make([]byte, 33) // 16 + 16 + 1
|
randomBytes := make([]byte, 33) // 16 + 16 + 1
|
||||||
common.Must2(rand.Read(randomBytes))
|
common.Must2(rand.Read(randomBytes))
|
||||||
|
|
||||||
session := clientSessionPool.Get().(*ClientSession)
|
session := &ClientSession{}
|
||||||
copy(session.requestBodyKey[:], randomBytes[:16])
|
copy(session.requestBodyKey[:], randomBytes[:16])
|
||||||
copy(session.requestBodyIV[:], randomBytes[16:32])
|
copy(session.requestBodyIV[:], randomBytes[16:32])
|
||||||
session.responseHeader = randomBytes[32]
|
session.responseHeader = randomBytes[32]
|
||||||
@ -59,12 +54,6 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession {
|
|||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReleaseClientSession(session *ClientSession) {
|
|
||||||
session.idHash = nil
|
|
||||||
session.responseReader = nil
|
|
||||||
clientSessionPool.Put(session)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error {
|
func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error {
|
||||||
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
|
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
|
||||||
account := header.User.Account.(*vmess.InternalAccount)
|
account := header.User.Account.(*vmess.InternalAccount)
|
||||||
|
@ -44,8 +44,6 @@ func TestRequestSerialization(t *testing.T) {
|
|||||||
|
|
||||||
buffer := buf.New()
|
buffer := buf.New()
|
||||||
client := NewClientSession(protocol.DefaultIDHash)
|
client := NewClientSession(protocol.DefaultIDHash)
|
||||||
defer ReleaseClientSession(client)
|
|
||||||
|
|
||||||
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
||||||
|
|
||||||
buffer2 := buf.New()
|
buffer2 := buf.New()
|
||||||
@ -59,7 +57,6 @@ func TestRequestSerialization(t *testing.T) {
|
|||||||
defer common.Close(userValidator)
|
defer common.Close(userValidator)
|
||||||
|
|
||||||
server := NewServerSession(userValidator, sessionHistory)
|
server := NewServerSession(userValidator, sessionHistory)
|
||||||
defer ReleaseServerSession(server)
|
|
||||||
actualRequest, err := server.DecodeRequestHeader(buffer)
|
actualRequest, err := server.DecodeRequestHeader(buffer)
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
@ -100,8 +97,6 @@ func TestInvalidRequest(t *testing.T) {
|
|||||||
|
|
||||||
buffer := buf.New()
|
buffer := buf.New()
|
||||||
client := NewClientSession(protocol.DefaultIDHash)
|
client := NewClientSession(protocol.DefaultIDHash)
|
||||||
defer ReleaseClientSession(client)
|
|
||||||
|
|
||||||
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
||||||
|
|
||||||
buffer2 := buf.New()
|
buffer2 := buf.New()
|
||||||
@ -115,7 +110,6 @@ func TestInvalidRequest(t *testing.T) {
|
|||||||
defer common.Close(userValidator)
|
defer common.Close(userValidator)
|
||||||
|
|
||||||
server := NewServerSession(userValidator, sessionHistory)
|
server := NewServerSession(userValidator, sessionHistory)
|
||||||
defer ReleaseServerSession(server)
|
|
||||||
_, err := server.DecodeRequestHeader(buffer)
|
_, err := server.DecodeRequestHeader(buffer)
|
||||||
assert(err, IsNotNil)
|
assert(err, IsNotNil)
|
||||||
}
|
}
|
||||||
@ -156,7 +150,6 @@ func TestMuxRequest(t *testing.T) {
|
|||||||
defer common.Close(userValidator)
|
defer common.Close(userValidator)
|
||||||
|
|
||||||
server := NewServerSession(userValidator, sessionHistory)
|
server := NewServerSession(userValidator, sessionHistory)
|
||||||
defer ReleaseServerSession(server)
|
|
||||||
actualRequest, err := server.DecodeRequestHeader(buffer)
|
actualRequest, err := server.DecodeRequestHeader(buffer)
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
|
@ -99,24 +99,13 @@ type ServerSession struct {
|
|||||||
responseHeader byte
|
responseHeader byte
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverSessionPool = sync.Pool{
|
|
||||||
New: func() interface{} { return &ServerSession{} },
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewServerSession creates a new ServerSession, using the given UserValidator.
|
// NewServerSession creates a new ServerSession, using the given UserValidator.
|
||||||
// The ServerSession instance doesn't take ownership of the validator.
|
// The ServerSession instance doesn't take ownership of the validator.
|
||||||
func NewServerSession(validator *vmess.TimedUserValidator, sessionHistory *SessionHistory) *ServerSession {
|
func NewServerSession(validator *vmess.TimedUserValidator, sessionHistory *SessionHistory) *ServerSession {
|
||||||
session := serverSessionPool.Get().(*ServerSession)
|
return &ServerSession{
|
||||||
session.userValidator = validator
|
userValidator: validator,
|
||||||
session.sessionHistory = sessionHistory
|
sessionHistory: sessionHistory,
|
||||||
return session
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func ReleaseServerSession(session *ServerSession) {
|
|
||||||
session.responseWriter = nil
|
|
||||||
session.userValidator = nil
|
|
||||||
session.sessionHistory = nil
|
|
||||||
serverSessionPool.Put(session)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSecurityType(b byte) protocol.SecurityType {
|
func parseSecurityType(b byte) protocol.SecurityType {
|
||||||
|
@ -221,8 +221,6 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i
|
|||||||
|
|
||||||
reader := &buf.BufferedReader{Reader: buf.NewReader(connection)}
|
reader := &buf.BufferedReader{Reader: buf.NewReader(connection)}
|
||||||
svrSession := encoding.NewServerSession(h.clients, h.sessionHistory)
|
svrSession := encoding.NewServerSession(h.clients, h.sessionHistory)
|
||||||
defer encoding.ReleaseServerSession(svrSession)
|
|
||||||
|
|
||||||
request, err := svrSession.DecodeRequestHeader(reader)
|
request, err := svrSession.DecodeRequestHeader(reader)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -106,8 +106,6 @@ func (v *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia
|
|||||||
output := link.Writer
|
output := link.Writer
|
||||||
|
|
||||||
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
||||||
defer encoding.ReleaseClientSession(session)
|
|
||||||
|
|
||||||
sessionPolicy := v.v.PolicyManager().ForLevel(request.User.Level)
|
sessionPolicy := v.v.PolicyManager().ForLevel(request.User.Level)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
Loading…
Reference in New Issue
Block a user