mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 17:46:58 -05:00
reuse outbound connection handler
This commit is contained in:
parent
054f56820a
commit
cd81e5531b
39
point.go
39
point.go
@ -26,10 +26,8 @@ func RegisterOutboundConnectionHandlerFactory(name string, factory OutboundConne
|
|||||||
// Point is an single server in V2Ray system.
|
// Point is an single server in V2Ray system.
|
||||||
type Point struct {
|
type Point struct {
|
||||||
port uint16
|
port uint16
|
||||||
ichFactory InboundConnectionHandlerFactory
|
ich InboundConnectionHandler
|
||||||
ichConfig interface{}
|
och OutboundConnectionHandler
|
||||||
ochFactory OutboundConnectionHandlerFactory
|
|
||||||
ochConfig interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPoint returns a new Point server based on given configuration.
|
// NewPoint returns a new Point server based on given configuration.
|
||||||
@ -42,16 +40,25 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
panic(log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol()))
|
panic(log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol()))
|
||||||
}
|
}
|
||||||
vpoint.ichFactory = ichFactory
|
ichConfig := pConfig.InboundConfig().Settings(config.TypeInbound)
|
||||||
vpoint.ichConfig = pConfig.InboundConfig().Settings(config.TypeInbound)
|
ich, err := ichFactory.Create(vpoint, ichConfig)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to create inbound connection handler: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
vpoint.ich = ich
|
||||||
|
|
||||||
ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()]
|
ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()]
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol))
|
panic(log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol))
|
||||||
}
|
}
|
||||||
|
ochConfig := pConfig.OutboundConfig().Settings(config.TypeOutbound)
|
||||||
vpoint.ochFactory = ochFactory
|
och, err := ochFactory.Create(vpoint, ochConfig)
|
||||||
vpoint.ochConfig = pConfig.OutboundConfig().Settings(config.TypeOutbound)
|
if err != nil {
|
||||||
|
log.Error("Failed to create outbound connection handler: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
vpoint.och = och
|
||||||
|
|
||||||
return vpoint, nil
|
return vpoint, nil
|
||||||
}
|
}
|
||||||
@ -65,11 +72,11 @@ type InboundConnectionHandler interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type OutboundConnectionHandlerFactory interface {
|
type OutboundConnectionHandlerFactory interface {
|
||||||
Create(VP *Point, config interface{}, firstPacket v2net.Packet) (OutboundConnectionHandler, error)
|
Create(VP *Point, config interface{}) (OutboundConnectionHandler, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type OutboundConnectionHandler interface {
|
type OutboundConnectionHandler interface {
|
||||||
Start(ray OutboundRay) error
|
Dispatch(firstPacket v2net.Packet, ray OutboundRay) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start starts the Point server, and return any error during the process.
|
// Start starts the Point server, and return any error during the process.
|
||||||
@ -79,18 +86,14 @@ func (vp *Point) Start() error {
|
|||||||
return log.Error("Invalid port %d", vp.port)
|
return log.Error("Invalid port %d", vp.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
|
err := vp.ich.Listen(vp.port)
|
||||||
if err != nil {
|
// TODO: handle error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = inboundConnectionHandler.Listen(vp.port)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
|
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
|
||||||
ray := NewRay()
|
ray := NewRay()
|
||||||
// TODO: handle error
|
// TODO: handle error
|
||||||
och, _ := p.ochFactory.Create(p, p.ochConfig, packet)
|
p.och.Dispatch(packet, ray)
|
||||||
_ = och.Start(ray)
|
|
||||||
return ray
|
return ray
|
||||||
}
|
}
|
||||||
|
@ -10,23 +10,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type FreedomConnection struct {
|
type FreedomConnection struct {
|
||||||
packet v2net.Packet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFreedomConnection(firstPacket v2net.Packet) *FreedomConnection {
|
func NewFreedomConnection() *FreedomConnection {
|
||||||
return &FreedomConnection{
|
return &FreedomConnection{}
|
||||||
packet: firstPacket,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
|
func (vconn *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray core.OutboundRay) error {
|
||||||
conn, err := net.Dial(vconn.packet.Destination().Network(), vconn.packet.Destination().Address().String())
|
conn, err := net.Dial(firstPacket.Destination().Network(), firstPacket.Destination().Address().String())
|
||||||
log.Info("Freedom: Opening connection to %s", vconn.packet.Destination().String())
|
log.Info("Freedom: Opening connection to %s", firstPacket.Destination().String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ray != nil {
|
if ray != nil {
|
||||||
close(ray.OutboundOutput())
|
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()
|
input := ray.OutboundInput()
|
||||||
@ -35,17 +32,17 @@ func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
|
|||||||
readMutex.Lock()
|
readMutex.Lock()
|
||||||
writeMutex.Lock()
|
writeMutex.Lock()
|
||||||
|
|
||||||
if chunk := vconn.packet.Chunk(); chunk != nil {
|
if chunk := firstPacket.Chunk(); chunk != nil {
|
||||||
conn.Write(chunk)
|
conn.Write(chunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !vconn.packet.MoreChunks() {
|
if !firstPacket.MoreChunks() {
|
||||||
writeMutex.Unlock()
|
writeMutex.Unlock()
|
||||||
} else {
|
} else {
|
||||||
go dumpInput(conn, input, &writeMutex)
|
go dumpInput(conn, input, &writeMutex)
|
||||||
}
|
}
|
||||||
|
|
||||||
go dumpOutput(conn, output, &readMutex, vconn.packet.Destination().IsUDP())
|
go dumpOutput(conn, output, &readMutex, firstPacket.Destination().IsUDP())
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
writeMutex.Lock()
|
writeMutex.Lock()
|
||||||
|
@ -2,14 +2,13 @@ package freedom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreedomFactory struct {
|
type FreedomFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory FreedomFactory) Create(vp *core.Point, config interface{}, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
|
func (factory FreedomFactory) Create(vp *core.Point, config interface{}) (core.OutboundConnectionHandler, error) {
|
||||||
return NewFreedomConnection(firstPacket), nil
|
return NewFreedomConnection(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -28,15 +28,13 @@ type VNextServer struct {
|
|||||||
|
|
||||||
type VMessOutboundHandler struct {
|
type VMessOutboundHandler struct {
|
||||||
vPoint *core.Point
|
vPoint *core.Point
|
||||||
packet v2net.Packet
|
|
||||||
vNextList []VNextServer
|
vNextList []VNextServer
|
||||||
vNextListUDP []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{
|
return &VMessOutboundHandler{
|
||||||
vPoint: vp,
|
vPoint: vp,
|
||||||
packet: firstPacket,
|
|
||||||
vNextList: vNextList,
|
vNextList: vNextList,
|
||||||
vNextListUDP: vNextListUDP,
|
vNextListUDP: vNextListUDP,
|
||||||
}
|
}
|
||||||
@ -65,28 +63,28 @@ func pickVNext(serverList []VNextServer) (v2net.Destination, user.User) {
|
|||||||
return vNext.Destination, vNextUser
|
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
|
vNextList := handler.vNextList
|
||||||
if handler.packet.Destination().IsUDP() {
|
if firstPacket.Destination().IsUDP() {
|
||||||
vNextList = handler.vNextListUDP
|
vNextList = handler.vNextListUDP
|
||||||
}
|
}
|
||||||
vNextAddress, vNextUser := pickVNext(vNextList)
|
vNextAddress, vNextUser := pickVNext(vNextList)
|
||||||
|
|
||||||
command := protocol.CmdTCP
|
command := protocol.CmdTCP
|
||||||
if handler.packet.Destination().IsUDP() {
|
if firstPacket.Destination().IsUDP() {
|
||||||
command = protocol.CmdUDP
|
command = protocol.CmdUDP
|
||||||
}
|
}
|
||||||
request := &protocol.VMessRequest{
|
request := &protocol.VMessRequest{
|
||||||
Version: protocol.Version,
|
Version: protocol.Version,
|
||||||
UserId: vNextUser.Id,
|
UserId: vNextUser.Id,
|
||||||
Command: command,
|
Command: command,
|
||||||
Address: handler.packet.Destination().Address(),
|
Address: firstPacket.Destination().Address(),
|
||||||
}
|
}
|
||||||
rand.Read(request.RequestIV[:])
|
rand.Read(request.RequestIV[:])
|
||||||
rand.Read(request.RequestKey[:])
|
rand.Read(request.RequestKey[:])
|
||||||
rand.Read(request.ResponseHeader[:])
|
rand.Read(request.ResponseHeader[:])
|
||||||
|
|
||||||
go startCommunicate(request, vNextAddress, ray, handler.packet)
|
go startCommunicate(request, vNextAddress, ray, firstPacket)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,7 +193,7 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
|
|||||||
type VMessOutboundHandlerFactory struct {
|
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)
|
config := rawConfig.(*VMessOutboundConfig)
|
||||||
servers := make([]VNextServer, 0, len(config.VNextList))
|
servers := make([]VNextServer, 0, len(config.VNextList))
|
||||||
udpServers := 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"))
|
udpServers = append(udpServers, server.ToVNextServer("udp"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NewVMessOutboundHandler(vp, servers, udpServers, firstPacket), nil
|
return NewVMessOutboundHandler(vp, servers, udpServers), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
jsonconf "github.com/v2ray/v2ray-core/config/json"
|
jsonconf "github.com/v2ray/v2ray-core/config/json"
|
||||||
|
|
||||||
// The following are neccesary as they register handlers in their init functions.
|
// The following are neccesary as they register handlers in their init functions.
|
||||||
_ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
|
|
||||||
_ "github.com/v2ray/v2ray-core/proxy/freedom"
|
_ "github.com/v2ray/v2ray-core/proxy/freedom"
|
||||||
|
_ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
|
||||||
_ "github.com/v2ray/v2ray-core/proxy/socks"
|
_ "github.com/v2ray/v2ray-core/proxy/socks"
|
||||||
_ "github.com/v2ray/v2ray-core/proxy/vmess"
|
_ "github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
)
|
)
|
||||||
|
@ -13,10 +13,15 @@ type OutboundConnectionHandler struct {
|
|||||||
Destination v2net.Destination
|
Destination v2net.Destination
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *OutboundConnectionHandler) Start(ray core.OutboundRay) error {
|
func (handler *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray core.OutboundRay) error {
|
||||||
input := ray.OutboundInput()
|
input := ray.OutboundInput()
|
||||||
output := ray.OutboundOutput()
|
output := ray.OutboundOutput()
|
||||||
|
|
||||||
|
handler.Destination = packet.Destination()
|
||||||
|
if packet.Chunk() != nil {
|
||||||
|
handler.Data2Send.Write(packet.Chunk())
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
data, open := <-input
|
data, open := <-input
|
||||||
@ -34,11 +39,6 @@ func (handler *OutboundConnectionHandler) Start(ray core.OutboundRay) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *OutboundConnectionHandler) Create(point *core.Point, config interface{}, packet v2net.Packet) (core.OutboundConnectionHandler, error) {
|
func (handler *OutboundConnectionHandler) Create(point *core.Point, config interface{}) (core.OutboundConnectionHandler, error) {
|
||||||
handler.Destination = packet.Destination()
|
|
||||||
if packet.Chunk() != nil {
|
|
||||||
handler.Data2Send.Write(packet.Chunk())
|
|
||||||
}
|
|
||||||
|
|
||||||
return handler, nil
|
return handler, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user