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