mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-04 09:17:32 -05:00
Combine VMess response with first packet for performance
This commit is contained in:
parent
8ed15ce847
commit
1b2b5b6cb1
@ -18,10 +18,12 @@ type Address struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func IPAddress(ip []byte, port uint16) Address {
|
func IPAddress(ip []byte, port uint16) Address {
|
||||||
|
ipCopy := make([]byte, len(ip))
|
||||||
|
copy(ipCopy, ip)
|
||||||
// TODO: check IP length
|
// TODO: check IP length
|
||||||
return Address{
|
return Address{
|
||||||
Type: AddrTypeIP,
|
Type: AddrTypeIP,
|
||||||
IP: net.IP(ip),
|
IP: net.IP(ipCopy),
|
||||||
Domain: "",
|
Domain: "",
|
||||||
Port: port,
|
Port: port,
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
|
|||||||
output := ray.OutboundOutput()
|
output := ray.OutboundOutput()
|
||||||
conn, err := net.Dial("tcp", vconn.dest.String())
|
conn, err := net.Dial("tcp", vconn.dest.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
close(output)
|
||||||
return log.Error("Failed to open tcp: %s : %v", vconn.dest.String(), err)
|
return log.Error("Failed to open tcp: %s : %v", vconn.dest.String(), err)
|
||||||
}
|
}
|
||||||
log.Debug("Sending outbound tcp: %s", vconn.dest.String())
|
log.Debug("Sending outbound tcp: %s", vconn.dest.String())
|
||||||
|
@ -60,27 +60,6 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
|
|||||||
}
|
}
|
||||||
log.Debug("Received request for %s", request.Address.String())
|
log.Debug("Received request for %s", request.Address.String())
|
||||||
|
|
||||||
response := vmessio.NewVMessResponse(request)
|
|
||||||
nBytes, err := connection.Write(response[:])
|
|
||||||
if err != nil {
|
|
||||||
return log.Error("Failed to write VMess response (%d bytes): %v", nBytes, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
requestKey := request.RequestKey[:]
|
|
||||||
requestIV := request.RequestIV[:]
|
|
||||||
responseKey := md5.Sum(requestKey)
|
|
||||||
responseIV := md5.Sum(requestIV)
|
|
||||||
|
|
||||||
requestReader, err := v2io.NewAesDecryptReader(requestKey, requestIV, connection)
|
|
||||||
if err != nil {
|
|
||||||
return log.Error("Failed to create decrypt reader: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
|
|
||||||
if err != nil {
|
|
||||||
return log.Error("Failed to create encrypt writer: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ray := handler.vPoint.NewInboundConnectionAccepted(request.Address)
|
ray := handler.vPoint.NewInboundConnectionAccepted(request.Address)
|
||||||
input := ray.InboundInput()
|
input := ray.InboundInput()
|
||||||
output := ray.InboundOutput()
|
output := ray.InboundOutput()
|
||||||
@ -88,8 +67,27 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
|
|||||||
readFinish := make(chan bool)
|
readFinish := make(chan bool)
|
||||||
writeFinish := make(chan bool)
|
writeFinish := make(chan bool)
|
||||||
|
|
||||||
go handler.dumpInput(requestReader, input, readFinish)
|
go handleInput(request, connection, input, readFinish)
|
||||||
go handler.dumpOutput(responseWriter, output, writeFinish)
|
|
||||||
|
responseKey := md5.Sum(request.RequestKey[:])
|
||||||
|
responseIV := md5.Sum(request.RequestIV[:])
|
||||||
|
|
||||||
|
response := vmessio.NewVMessResponse(request)
|
||||||
|
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
|
||||||
|
if err != nil {
|
||||||
|
return log.Error("Failed to create encrypt writer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optimize for small response packet
|
||||||
|
buffer := make([]byte, 0, 1024)
|
||||||
|
buffer = append(buffer, response[:]...)
|
||||||
|
data, open := <-output
|
||||||
|
if open {
|
||||||
|
buffer = append(buffer, data...)
|
||||||
|
}
|
||||||
|
responseWriter.Write(buffer)
|
||||||
|
|
||||||
|
go handleOutput(request, responseWriter, output, writeFinish)
|
||||||
|
|
||||||
<-writeFinish
|
<-writeFinish
|
||||||
if tcpConn, ok := connection.(*net.TCPConn); ok {
|
if tcpConn, ok := connection.(*net.TCPConn); ok {
|
||||||
@ -101,17 +99,22 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessInboundHandler) dumpInput(reader io.Reader, input chan<- []byte, finish chan<- bool) {
|
func handleInput(request *vmessio.VMessRequest, reader io.Reader, input chan<- []byte, finish chan<- bool) {
|
||||||
v2net.ReaderToChan(input, reader)
|
defer close(input)
|
||||||
close(input)
|
defer close(finish)
|
||||||
log.Debug("VMessIn closing input")
|
|
||||||
finish <- true
|
requestReader, err := v2io.NewAesDecryptReader(request.RequestKey[:], request.RequestIV[:], reader)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to create decrypt reader: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
v2net.ReaderToChan(input, requestReader)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessInboundHandler) dumpOutput(writer io.Writer, output <-chan []byte, finish chan<- bool) {
|
func handleOutput(request *vmessio.VMessRequest, writer io.Writer, output <-chan []byte, finish chan<- bool) {
|
||||||
v2net.ChanToWriter(writer, output)
|
v2net.ChanToWriter(writer, output)
|
||||||
log.Debug("VMessOut closing output")
|
close(finish)
|
||||||
finish <- true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMessInboundHandlerFactory struct {
|
type VMessInboundHandlerFactory struct {
|
||||||
|
@ -79,8 +79,10 @@ func startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray cor
|
|||||||
close(output)
|
close(output)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug("VMessOut: Tunneling request for %s", request.Address.String())
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
defer close(output)
|
|
||||||
|
|
||||||
requestFinish := make(chan bool)
|
requestFinish := make(chan bool)
|
||||||
responseFinish := make(chan bool)
|
responseFinish := make(chan bool)
|
||||||
@ -115,24 +117,25 @@ func handleRequest(conn *net.TCPConn, request *vmessio.VMessRequest, input <-cha
|
|||||||
|
|
||||||
func handleResponse(conn *net.TCPConn, request *vmessio.VMessRequest, output chan<- []byte, finish chan<- bool) error {
|
func handleResponse(conn *net.TCPConn, request *vmessio.VMessRequest, output chan<- []byte, finish chan<- bool) error {
|
||||||
defer close(finish)
|
defer close(finish)
|
||||||
|
defer close(output)
|
||||||
responseKey := md5.Sum(request.RequestKey[:])
|
responseKey := md5.Sum(request.RequestKey[:])
|
||||||
responseIV := md5.Sum(request.RequestIV[:])
|
responseIV := md5.Sum(request.RequestIV[:])
|
||||||
|
|
||||||
response := vmessio.VMessResponse{}
|
|
||||||
nBytes, err := conn.Read(response[:])
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed to read VMess response (%d bytes): %v", nBytes, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Debug("Got response %v", response)
|
|
||||||
// TODO: check response
|
|
||||||
|
|
||||||
decryptResponseReader, err := v2io.NewAesDecryptReader(responseKey[:], responseIV[:], conn)
|
decryptResponseReader, err := v2io.NewAesDecryptReader(responseKey[:], responseIV[:], conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create decrypt reader: %v", err)
|
log.Error("Failed to create decrypt reader: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response := vmessio.VMessResponse{}
|
||||||
|
nBytes, err := decryptResponseReader.Read(response[:])
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to read VMess response (%d bytes): %v", nBytes, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Debug("Got response %v", response)
|
||||||
|
// TODO: check response
|
||||||
|
|
||||||
v2net.ReaderToChan(output, decryptResponseReader)
|
v2net.ReaderToChan(output, decryptResponseReader)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user