2015-09-11 12:12:09 +00:00
|
|
|
package freedom
|
2015-09-09 15:39:06 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-09 15:39:25 +00:00
|
|
|
"io"
|
2015-09-11 12:12:26 +00:00
|
|
|
"log"
|
2015-09-09 15:39:25 +00:00
|
|
|
"net"
|
2015-09-09 22:50:21 +00:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-09-10 22:24:18 +00:00
|
|
|
v2net "github.com/v2ray/v2ray-core/net"
|
2015-09-09 15:39:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type VFreeConnection struct {
|
2015-09-12 09:51:42 +00:00
|
|
|
dest v2net.VAddress
|
2015-09-09 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 09:51:42 +00:00
|
|
|
func NewVFreeConnection(dest v2net.VAddress) *VFreeConnection {
|
2015-09-09 15:39:25 +00:00
|
|
|
conn := new(VFreeConnection)
|
2015-09-10 22:24:18 +00:00
|
|
|
conn.dest = dest
|
2015-09-09 15:39:25 +00:00
|
|
|
return conn
|
2015-09-09 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 22:50:21 +00:00
|
|
|
func (vconn *VFreeConnection) Start(vRay core.OutboundVRay) error {
|
|
|
|
input := vRay.OutboundInput()
|
|
|
|
output := vRay.OutboundOutput()
|
2015-09-10 22:24:18 +00:00
|
|
|
conn, err := net.Dial("tcp", vconn.dest.String())
|
2015-09-09 15:39:25 +00:00
|
|
|
if err != nil {
|
2015-09-11 12:12:26 +00:00
|
|
|
log.Print(err)
|
2015-09-09 22:50:21 +00:00
|
|
|
return err
|
2015-09-09 15:39:25 +00:00
|
|
|
}
|
2015-09-11 12:12:26 +00:00
|
|
|
log.Print("Working on tcp:" + vconn.dest.String())
|
2015-09-09 15:39:25 +00:00
|
|
|
|
|
|
|
finish := make(chan bool, 2)
|
|
|
|
go vconn.DumpInput(conn, input, finish)
|
|
|
|
go vconn.DumpOutput(conn, output, finish)
|
|
|
|
go vconn.CloseConn(conn, finish)
|
2015-09-09 22:50:21 +00:00
|
|
|
return nil
|
2015-09-09 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vconn *VFreeConnection) DumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {
|
2015-09-09 15:39:25 +00:00
|
|
|
for {
|
|
|
|
data, open := <-input
|
|
|
|
if !open {
|
|
|
|
finish <- true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Write(data)
|
|
|
|
}
|
2015-09-09 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vconn *VFreeConnection) DumpOutput(conn net.Conn, output chan<- []byte, finish chan<- bool) {
|
2015-09-09 15:39:25 +00:00
|
|
|
for {
|
|
|
|
buffer := make([]byte, 128)
|
|
|
|
nBytes, err := conn.Read(buffer)
|
|
|
|
if err == io.EOF {
|
2015-09-11 12:12:26 +00:00
|
|
|
close(output)
|
2015-09-09 15:39:25 +00:00
|
|
|
finish <- true
|
|
|
|
break
|
|
|
|
}
|
2015-09-11 12:12:26 +00:00
|
|
|
log.Print(buffer[:nBytes])
|
2015-09-09 15:39:25 +00:00
|
|
|
output <- buffer[:nBytes]
|
|
|
|
}
|
2015-09-09 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vconn *VFreeConnection) CloseConn(conn net.Conn, finish <-chan bool) {
|
2015-09-09 15:39:25 +00:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
<-finish
|
|
|
|
}
|
|
|
|
conn.Close()
|
2015-09-09 15:39:06 +00:00
|
|
|
}
|