mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-05 16:55:27 -05:00
reuse outbound connection handler
This commit is contained in:
@@ -10,23 +10,20 @@ import (
|
||||
)
|
||||
|
||||
type FreedomConnection struct {
|
||||
packet v2net.Packet
|
||||
}
|
||||
|
||||
func NewFreedomConnection(firstPacket v2net.Packet) *FreedomConnection {
|
||||
return &FreedomConnection{
|
||||
packet: firstPacket,
|
||||
}
|
||||
func NewFreedomConnection() *FreedomConnection {
|
||||
return &FreedomConnection{}
|
||||
}
|
||||
|
||||
func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
|
||||
conn, err := net.Dial(vconn.packet.Destination().Network(), vconn.packet.Destination().Address().String())
|
||||
log.Info("Freedom: Opening connection to %s", vconn.packet.Destination().String())
|
||||
func (vconn *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray core.OutboundRay) error {
|
||||
conn, err := net.Dial(firstPacket.Destination().Network(), firstPacket.Destination().Address().String())
|
||||
log.Info("Freedom: Opening connection to %s", firstPacket.Destination().String())
|
||||
if err != nil {
|
||||
if ray != nil {
|
||||
close(ray.OutboundOutput())
|
||||
}
|
||||
return log.Error("Freedom: Failed to open connection: %s : %v", vconn.packet.Destination().String(), err)
|
||||
return log.Error("Freedom: Failed to open connection: %s : %v", firstPacket.Destination().String(), err)
|
||||
}
|
||||
|
||||
input := ray.OutboundInput()
|
||||
@@ -35,17 +32,17 @@ func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
|
||||
readMutex.Lock()
|
||||
writeMutex.Lock()
|
||||
|
||||
if chunk := vconn.packet.Chunk(); chunk != nil {
|
||||
if chunk := firstPacket.Chunk(); chunk != nil {
|
||||
conn.Write(chunk)
|
||||
}
|
||||
|
||||
if !vconn.packet.MoreChunks() {
|
||||
if !firstPacket.MoreChunks() {
|
||||
writeMutex.Unlock()
|
||||
} else {
|
||||
go dumpInput(conn, input, &writeMutex)
|
||||
}
|
||||
|
||||
go dumpOutput(conn, output, &readMutex, vconn.packet.Destination().IsUDP())
|
||||
go dumpOutput(conn, output, &readMutex, firstPacket.Destination().IsUDP())
|
||||
|
||||
go func() {
|
||||
writeMutex.Lock()
|
||||
|
||||
@@ -2,14 +2,13 @@ package freedom
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
|
||||
type FreedomFactory struct {
|
||||
}
|
||||
|
||||
func (factory FreedomFactory) Create(vp *core.Point, config interface{}, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
|
||||
return NewFreedomConnection(firstPacket), nil
|
||||
func (factory FreedomFactory) Create(vp *core.Point, config interface{}) (core.OutboundConnectionHandler, error) {
|
||||
return NewFreedomConnection(), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -28,15 +28,13 @@ type VNextServer struct {
|
||||
|
||||
type VMessOutboundHandler struct {
|
||||
vPoint *core.Point
|
||||
packet v2net.Packet
|
||||
vNextList []VNextServer
|
||||
vNextListUDP []VNextServer
|
||||
}
|
||||
|
||||
func NewVMessOutboundHandler(vp *core.Point, vNextList, vNextListUDP []VNextServer, firstPacket v2net.Packet) *VMessOutboundHandler {
|
||||
func NewVMessOutboundHandler(vp *core.Point, vNextList, vNextListUDP []VNextServer) *VMessOutboundHandler {
|
||||
return &VMessOutboundHandler{
|
||||
vPoint: vp,
|
||||
packet: firstPacket,
|
||||
vNextList: vNextList,
|
||||
vNextListUDP: vNextListUDP,
|
||||
}
|
||||
@@ -65,28 +63,28 @@ func pickVNext(serverList []VNextServer) (v2net.Destination, user.User) {
|
||||
return vNext.Destination, vNextUser
|
||||
}
|
||||
|
||||
func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
|
||||
func (handler *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray core.OutboundRay) error {
|
||||
vNextList := handler.vNextList
|
||||
if handler.packet.Destination().IsUDP() {
|
||||
if firstPacket.Destination().IsUDP() {
|
||||
vNextList = handler.vNextListUDP
|
||||
}
|
||||
vNextAddress, vNextUser := pickVNext(vNextList)
|
||||
|
||||
command := protocol.CmdTCP
|
||||
if handler.packet.Destination().IsUDP() {
|
||||
if firstPacket.Destination().IsUDP() {
|
||||
command = protocol.CmdUDP
|
||||
}
|
||||
request := &protocol.VMessRequest{
|
||||
Version: protocol.Version,
|
||||
UserId: vNextUser.Id,
|
||||
Command: command,
|
||||
Address: handler.packet.Destination().Address(),
|
||||
Address: firstPacket.Destination().Address(),
|
||||
}
|
||||
rand.Read(request.RequestIV[:])
|
||||
rand.Read(request.RequestKey[:])
|
||||
rand.Read(request.ResponseHeader[:])
|
||||
|
||||
go startCommunicate(request, vNextAddress, ray, handler.packet)
|
||||
go startCommunicate(request, vNextAddress, ray, firstPacket)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -195,7 +193,7 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
|
||||
type VMessOutboundHandlerFactory struct {
|
||||
}
|
||||
|
||||
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig interface{}, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
|
||||
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig interface{}) (core.OutboundConnectionHandler, error) {
|
||||
config := rawConfig.(*VMessOutboundConfig)
|
||||
servers := make([]VNextServer, 0, len(config.VNextList))
|
||||
udpServers := make([]VNextServer, 0, len(config.VNextList))
|
||||
@@ -207,7 +205,7 @@ func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig int
|
||||
udpServers = append(udpServers, server.ToVNextServer("udp"))
|
||||
}
|
||||
}
|
||||
return NewVMessOutboundHandler(vp, servers, udpServers, firstPacket), nil
|
||||
return NewVMessOutboundHandler(vp, servers, udpServers), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user