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

74 lines
1.6 KiB

package freedom
import (
9 years ago
"net"
"sync"
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()
var readMutex, writeMutex sync.Mutex
readMutex.Lock()
writeMutex.Lock()
go dumpInput(conn, input, &writeMutex)
go dumpOutput(conn, output, &readMutex)
9 years ago
9 years ago
go func() {
writeMutex.Lock()
9 years ago
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
readMutex.Lock()
9 years ago
conn.Close()
}()
9 years ago
return nil
}
func dumpInput(conn net.Conn, input <-chan []byte, finish *sync.Mutex) {
v2net.ChanToWriter(conn, input)
finish.Unlock()
}
func dumpOutput(conn net.Conn, output chan<- []byte, finish *sync.Mutex) {
v2net.ReaderToChan(output, conn)
finish.Unlock()
close(output)
}