2015-09-11 08:12:09 -04:00
|
|
|
package freedom
|
2015-09-09 11:39:06 -04:00
|
|
|
|
|
|
|
import (
|
2015-09-09 11:39:25 -04:00
|
|
|
"net"
|
2015-09-23 08:14:53 -04:00
|
|
|
"sync"
|
2015-09-09 18:50:21 -04:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-09-09 11:39:06 -04:00
|
|
|
)
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
type FreedomConnection struct {
|
2015-09-22 08:45:03 -04:00
|
|
|
packet v2net.Packet
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 08:45:03 -04:00
|
|
|
func NewFreedomConnection(firstPacket v2net.Packet) *FreedomConnection {
|
2015-09-16 10:27:36 -04:00
|
|
|
return &FreedomConnection{
|
2015-09-22 08:45:03 -04:00
|
|
|
packet: firstPacket,
|
2015-09-16 10:27:36 -04:00
|
|
|
}
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
|
2015-09-22 08:45:03 -04:00
|
|
|
conn, err := net.Dial(vconn.packet.Destination().Network(), vconn.packet.Destination().Address().String())
|
|
|
|
log.Info("Freedom: Opening connection to %s", vconn.packet.Destination().String())
|
2015-09-09 11:39:25 -04:00
|
|
|
if err != nil {
|
2015-09-22 08:45:03 -04:00
|
|
|
if ray != nil {
|
|
|
|
close(ray.OutboundOutput())
|
|
|
|
}
|
|
|
|
return log.Error("Freedom: Failed to open connection: %s : %v", vconn.packet.Destination().String(), err)
|
|
|
|
}
|
|
|
|
|
2015-10-02 09:32:26 -04:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
|
|
|
var readMutex, writeMutex sync.Mutex
|
|
|
|
readMutex.Lock()
|
|
|
|
writeMutex.Lock()
|
|
|
|
|
2015-09-22 08:45:03 -04:00
|
|
|
if chunk := vconn.packet.Chunk(); chunk != nil {
|
|
|
|
conn.Write(chunk)
|
2015-09-09 11:39:25 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 08:45:03 -04:00
|
|
|
if !vconn.packet.MoreChunks() {
|
2015-10-02 09:32:26 -04:00
|
|
|
writeMutex.Unlock()
|
|
|
|
} else {
|
|
|
|
go dumpInput(conn, input, &writeMutex)
|
2015-09-22 08:45:03 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 18:58:07 -04:00
|
|
|
go dumpOutput(conn, output, &readMutex, vconn.packet.Destination().IsUDP())
|
2015-09-22 12:31:06 -04:00
|
|
|
|
2015-09-22 12:43:30 -04:00
|
|
|
go func() {
|
2015-09-23 08:14:53 -04:00
|
|
|
writeMutex.Lock()
|
2015-09-22 12:43:30 -04:00
|
|
|
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
|
|
|
tcpConn.CloseWrite()
|
|
|
|
}
|
2015-09-23 08:14:53 -04:00
|
|
|
readMutex.Lock()
|
2015-09-22 12:43:30 -04:00
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
|
2015-09-09 18:50:21 -04:00
|
|
|
return nil
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 11:13:50 -04:00
|
|
|
func dumpInput(conn net.Conn, input <-chan []byte, finish *sync.Mutex) {
|
2015-09-13 14:01:50 -04:00
|
|
|
v2net.ChanToWriter(conn, input)
|
2015-09-23 08:14:53 -04:00
|
|
|
finish.Unlock()
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 18:58:07 -04:00
|
|
|
func dumpOutput(conn net.Conn, output chan<- []byte, finish *sync.Mutex, udp bool) {
|
|
|
|
defer finish.Unlock()
|
|
|
|
defer close(output)
|
|
|
|
|
|
|
|
response, err := v2net.ReadFrom(conn)
|
|
|
|
if len(response) > 0 {
|
|
|
|
output <- response
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if udp {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-13 14:01:50 -04:00
|
|
|
v2net.ReaderToChan(output, conn)
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|