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
|
|
|
|
2016-01-29 08:39:55 -05:00
|
|
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
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-12-16 19:19:04 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/retry"
|
|
|
|
"github.com/v2ray/v2ray-core/transport/dialer"
|
2015-10-14 08:51:19 -04:00
|
|
|
"github.com/v2ray/v2ray-core/transport/ray"
|
2015-09-09 11:39:06 -04:00
|
|
|
)
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
type FreedomConnection struct {
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 15:50:28 -05:00
|
|
|
func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Info("Freedom: Opening connection to ", firstPacket.Destination())
|
2015-12-16 19:19:04 -05:00
|
|
|
|
|
|
|
var conn net.Conn
|
|
|
|
err := retry.Timed(5, 100).On(func() error {
|
|
|
|
rawConn, err := dialer.Dial(firstPacket.Destination())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn = rawConn
|
|
|
|
return nil
|
|
|
|
})
|
2015-09-09 11:39:25 -04:00
|
|
|
if err != nil {
|
2015-10-10 14:52:31 -04:00
|
|
|
close(ray.OutboundOutput())
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Freedom: Failed to open connection to ", firstPacket.Destination(), ": ", err)
|
2015-10-13 12:27:29 -04:00
|
|
|
return err
|
2015-09-22 08:45:03 -04:00
|
|
|
}
|
2015-12-15 10:38:25 -05:00
|
|
|
defer conn.Close()
|
2015-09-22 08:45:03 -04:00
|
|
|
|
2015-10-02 09:32:26 -04:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
|
|
|
var readMutex, writeMutex sync.Mutex
|
|
|
|
readMutex.Lock()
|
|
|
|
writeMutex.Lock()
|
|
|
|
|
2015-10-06 18:30:44 -04:00
|
|
|
if chunk := firstPacket.Chunk(); chunk != nil {
|
2015-10-08 08:46:18 -04:00
|
|
|
conn.Write(chunk.Value)
|
|
|
|
chunk.Release()
|
2015-09-09 11:39:25 -04:00
|
|
|
}
|
|
|
|
|
2015-10-06 18:30:44 -04:00
|
|
|
if !firstPacket.MoreChunks() {
|
2015-10-02 09:32:26 -04:00
|
|
|
writeMutex.Unlock()
|
|
|
|
} else {
|
2015-11-27 15:50:28 -05:00
|
|
|
go func() {
|
2016-01-29 08:39:55 -05:00
|
|
|
v2io.ChanToWriter(conn, input)
|
2015-11-27 15:50:28 -05:00
|
|
|
writeMutex.Unlock()
|
|
|
|
}()
|
2015-09-22 08:45:03 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 15:50:28 -05:00
|
|
|
go func() {
|
|
|
|
defer readMutex.Unlock()
|
|
|
|
defer close(output)
|
|
|
|
|
2016-01-29 08:39:55 -05:00
|
|
|
response, err := v2io.ReadFrom(conn, nil)
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Info("Freedom receives ", response.Len(), " bytes from ", conn.RemoteAddr())
|
2015-11-27 15:50:28 -05:00
|
|
|
if response.Len() > 0 {
|
|
|
|
output <- response
|
|
|
|
} else {
|
|
|
|
response.Release()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if firstPacket.Destination().IsUDP() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-29 08:39:55 -05:00
|
|
|
v2io.RawReaderToChan(output, conn)
|
2015-11-27 15:50:28 -05:00
|
|
|
}()
|
2015-09-22 12:31:06 -04:00
|
|
|
|
2015-10-09 19:25:12 -04:00
|
|
|
writeMutex.Lock()
|
2015-12-15 10:38:25 -05:00
|
|
|
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
|
|
|
tcpConn.CloseWrite()
|
|
|
|
}
|
2015-10-09 19:25:12 -04:00
|
|
|
readMutex.Lock()
|
2015-09-22 12:43:30 -04:00
|
|
|
|
2015-09-09 18:50:21 -04:00
|
|
|
return nil
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|