1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-05 08:45:22 -05:00

Introduce Packet to unify TCP and UDP communication

This commit is contained in:
V2Ray
2015-09-22 14:45:03 +02:00
parent 4320c14efd
commit 4874cd54a4
12 changed files with 124 additions and 41 deletions

View File

@@ -9,25 +9,38 @@ import (
)
type FreedomConnection struct {
dest v2net.Destination
packet v2net.Packet
}
func NewFreedomConnection(dest v2net.Destination) *FreedomConnection {
func NewFreedomConnection(firstPacket v2net.Packet) *FreedomConnection {
return &FreedomConnection{
dest: dest,
packet: firstPacket,
}
}
func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
input := ray.OutboundInput()
output := ray.OutboundOutput()
conn, err := net.Dial(vconn.dest.Network(), vconn.dest.Address().String())
log.Info("Freedom: Opening connection to %s", vconn.dest.String())
conn, err := net.Dial(vconn.packet.Destination().Network(), vconn.packet.Destination().Address().String())
log.Info("Freedom: Opening connection to %s", vconn.packet.Destination().String())
if err != nil {
close(output)
return log.Error("Freedom: Failed to open connection: %s : %v", vconn.dest.String(), err)
if ray != nil {
close(ray.OutboundOutput())
}
return log.Error("Freedom: Failed to open connection: %s : %v", vconn.packet.Destination().String(), err)
}
if chunk := vconn.packet.Chunk(); chunk != nil {
conn.Write(chunk)
}
if !vconn.packet.MoreChunks() {
if ray != nil {
close(ray.OutboundOutput())
}
return nil
}
input := ray.OutboundInput()
output := ray.OutboundOutput()
readFinish := make(chan bool)
writeFinish := make(chan bool)

View File

@@ -8,8 +8,8 @@ import (
type FreedomFactory struct {
}
func (factory FreedomFactory) Create(vp *core.Point, config []byte, dest v2net.Destination) (core.OutboundConnectionHandler, error) {
return NewFreedomConnection(dest), nil
func (factory FreedomFactory) Create(vp *core.Point, config []byte, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
return NewFreedomConnection(firstPacket), nil
}
func init() {

View File

@@ -170,7 +170,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
dest = request.Destination()
}
ray := server.vPoint.NewInboundConnectionAccepted(dest)
ray := server.vPoint.DispatchToOutbound(v2net.NewTCPPacket(dest))
input := ray.InboundInput()
output := ray.InboundOutput()
readFinish := make(chan bool)

View File

@@ -69,7 +69,7 @@ func TestVMessInAndOut(t *testing.T) {
assert.Error(err).IsNil()
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
ich.Communicate(dest)
ich.Communicate(v2net.NewTCPPacket(dest))
assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
}

View File

@@ -74,7 +74,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
// Clear read timeout
connection.SetReadDeadline(zeroTime)
ray := handler.vPoint.NewInboundConnectionAccepted(request.Destination())
ray := handler.vPoint.DispatchToOutbound(v2net.NewTCPPacket(request.Destination()))
input := ray.InboundInput()
output := ray.InboundOutput()

View File

@@ -27,14 +27,14 @@ type VNextServer struct {
type VMessOutboundHandler struct {
vPoint *core.Point
dest v2net.Destination
packet v2net.Packet
vNextList []VNextServer
}
func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net.Destination) *VMessOutboundHandler {
func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, firstPacket v2net.Packet) *VMessOutboundHandler {
return &VMessOutboundHandler{
vPoint: vp,
dest: dest,
packet: firstPacket,
vNextList: vNextList,
}
}
@@ -66,37 +66,50 @@ func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
vNextAddress, vNextUser := handler.pickVNext()
command := protocol.CmdTCP
if handler.dest.IsUDP() {
if handler.packet.Destination().IsUDP() {
command = protocol.CmdUDP
}
request := &protocol.VMessRequest{
Version: protocol.Version,
UserId: vNextUser.Id,
Command: command,
Address: handler.dest.Address(),
Address: handler.packet.Destination().Address(),
}
rand.Read(request.RequestIV[:])
rand.Read(request.RequestKey[:])
rand.Read(request.ResponseHeader[:])
go startCommunicate(request, vNextAddress, ray)
go startCommunicate(request, vNextAddress, ray, handler.packet)
return nil
}
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray core.OutboundRay) error {
input := ray.OutboundInput()
output := ray.OutboundOutput()
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray core.OutboundRay, firstPacket v2net.Packet) error {
conn, err := net.DialTCP(dest.Network(), nil, &net.TCPAddr{dest.Address().IP(), int(dest.Address().Port()), ""})
if err != nil {
log.Error("Failed to open tcp (%s): %v", dest.String(), err)
close(output)
if ray != nil {
close(ray.OutboundOutput())
}
return err
}
log.Info("VMessOut: Tunneling request for %s", request.Address.String())
defer conn.Close()
if chunk := firstPacket.Chunk(); chunk != nil {
conn.Write(chunk)
}
if !firstPacket.MoreChunks() {
if ray != nil {
close(ray.OutboundOutput())
}
return nil
}
input := ray.OutboundInput()
output := ray.OutboundOutput()
requestFinish := make(chan bool)
responseFinish := make(chan bool)
@@ -171,7 +184,7 @@ func handleResponse(conn *net.TCPConn, request *protocol.VMessRequest, output ch
type VMessOutboundHandlerFactory struct {
}
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig []byte, destination v2net.Destination) (core.OutboundConnectionHandler, error) {
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig []byte, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
config, err := loadOutboundConfig(rawConfig)
if err != nil {
panic(log.Error("Failed to load VMess outbound config: %v", err))
@@ -180,7 +193,7 @@ func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig []b
for _, server := range config.VNextList {
servers = append(servers, server.ToVNextServer())
}
return NewVMessOutboundHandler(vp, servers, destination), nil
return NewVMessOutboundHandler(vp, servers, firstPacket), nil
}
func init() {