|
|
@ -60,36 +60,34 @@ 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) |
|
|
|
ray := handler.vPoint.NewInboundConnectionAccepted(request.Address) |
|
|
|
nBytes, err := connection.Write(response[:]) |
|
|
|
input := ray.InboundInput() |
|
|
|
if err != nil { |
|
|
|
output := ray.InboundOutput() |
|
|
|
return log.Error("Failed to write VMess response (%d bytes): %v", nBytes, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requestKey := request.RequestKey[:] |
|
|
|
readFinish := make(chan bool) |
|
|
|
requestIV := request.RequestIV[:] |
|
|
|
writeFinish := make(chan bool) |
|
|
|
responseKey := md5.Sum(requestKey) |
|
|
|
|
|
|
|
responseIV := md5.Sum(requestIV) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requestReader, err := v2io.NewAesDecryptReader(requestKey, requestIV, connection) |
|
|
|
go handleInput(request, connection, input, readFinish) |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return log.Error("Failed to create decrypt reader: %v", err) |
|
|
|
responseKey := md5.Sum(request.RequestKey[:]) |
|
|
|
} |
|
|
|
responseIV := md5.Sum(request.RequestIV[:]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response := vmessio.NewVMessResponse(request) |
|
|
|
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection) |
|
|
|
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return log.Error("Failed to create encrypt writer: %v", err) |
|
|
|
return log.Error("Failed to create encrypt writer: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ray := handler.vPoint.NewInboundConnectionAccepted(request.Address) |
|
|
|
// Optimize for small response packet
|
|
|
|
input := ray.InboundInput() |
|
|
|
buffer := make([]byte, 0, 1024) |
|
|
|
output := ray.InboundOutput() |
|
|
|
buffer = append(buffer, response[:]...) |
|
|
|
|
|
|
|
data, open := <-output |
|
|
|
readFinish := make(chan bool) |
|
|
|
if open { |
|
|
|
writeFinish := make(chan bool) |
|
|
|
buffer = append(buffer, data...) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
responseWriter.Write(buffer) |
|
|
|
|
|
|
|
|
|
|
|
go handler.dumpInput(requestReader, input, readFinish) |
|
|
|
go handleOutput(request, responseWriter, output, writeFinish) |
|
|
|
go handler.dumpOutput(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 { |
|
|
|