1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-27 04:27:20 -05:00
v2fly/net/freedom/freedom.go
2015-09-18 13:19:12 +02:00

59 lines
1.4 KiB
Go

package freedom
import (
"net"
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/log"
v2net "github.com/v2ray/v2ray-core/net"
)
type FreedomConnection struct {
dest v2net.Address
}
func NewFreedomConnection(dest v2net.Address) *FreedomConnection {
return &FreedomConnection{
dest: dest,
}
}
func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
input := ray.OutboundInput()
output := ray.OutboundOutput()
conn, err := net.Dial("tcp", vconn.dest.String())
if err != nil {
close(output)
return log.Error("Freedom: Failed to open tcp connection: %s : %v", vconn.dest.String(), err)
}
log.Info("Freedom: Sending outbound tcp: %s", vconn.dest.String())
readFinish := make(chan bool)
writeFinish := make(chan bool)
go vconn.DumpInput(conn, input, writeFinish)
go vconn.DumpOutput(conn, output, readFinish)
go vconn.CloseConn(conn, readFinish, writeFinish)
return nil
}
func (vconn *FreedomConnection) DumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {
v2net.ChanToWriter(conn, input)
finish <- true
}
func (vconn *FreedomConnection) DumpOutput(conn net.Conn, output chan<- []byte, finish chan<- bool) {
v2net.ReaderToChan(output, conn)
close(output)
finish <- true
}
func (vconn *FreedomConnection) CloseConn(conn net.Conn, readFinish <-chan bool, writeFinish <-chan bool) {
<-writeFinish
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
<-readFinish
conn.Close()
}