You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/proxy/freedom/freedom.go

72 lines
1.6 KiB

package freedom
import (
9 years ago
"net"
9 years ago
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type FreedomConnection struct {
packet v2net.Packet
}
func NewFreedomConnection(firstPacket v2net.Packet) *FreedomConnection {
return &FreedomConnection{
packet: firstPacket,
}
}
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())
9 years ago
if err != nil {
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)
9 years ago
}
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)
9 years ago
go dumpInput(conn, input, writeFinish)
go dumpOutput(conn, output, readFinish)
9 years ago
9 years ago
go func() {
<-writeFinish
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
<-readFinish
conn.Close()
}()
9 years ago
return nil
}
9 years ago
func dumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {
v2net.ChanToWriter(conn, input)
9 years ago
close(finish)
}
9 years ago
func dumpOutput(conn net.Conn, output chan<- []byte, finish chan<- bool) {
v2net.ReaderToChan(output, conn)
close(output)
9 years ago
close(finish)
}