1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 09:55:22 +00:00
v2fly/proxy/freedom/freedom.go
2016-01-29 13:39:55 +00:00

101 lines
2.2 KiB
Go

package freedom
import (
"net"
"sync"
"github.com/v2ray/v2ray-core/app"
v2io "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/transport/dialer"
"github.com/v2ray/v2ray-core/transport/ray"
)
type FreedomConnection struct {
space app.Space
}
func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
log.Info("Freedom: Opening connection to ", firstPacket.Destination())
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
})
if err != nil {
close(ray.OutboundOutput())
log.Error("Freedom: Failed to open connection to ", firstPacket.Destination(), ": ", err)
return err
}
defer conn.Close()
input := ray.OutboundInput()
output := ray.OutboundOutput()
var readMutex, writeMutex sync.Mutex
readMutex.Lock()
writeMutex.Lock()
if chunk := firstPacket.Chunk(); chunk != nil {
conn.Write(chunk.Value)
chunk.Release()
}
if !firstPacket.MoreChunks() {
writeMutex.Unlock()
} else {
go func() {
v2io.ChanToWriter(conn, input)
writeMutex.Unlock()
}()
}
go func() {
defer readMutex.Unlock()
defer close(output)
response, err := v2io.ReadFrom(conn, nil)
log.Info("Freedom receives ", response.Len(), " bytes from ", conn.RemoteAddr())
if response.Len() > 0 {
output <- response
} else {
response.Release()
}
if err != nil {
return
}
if firstPacket.Destination().IsUDP() {
return
}
v2io.RawReaderToChan(output, conn)
}()
if this.space.HasDnsCache() {
if firstPacket.Destination().Address().IsDomain() {
domain := firstPacket.Destination().Address().Domain()
addr := conn.RemoteAddr()
switch typedAddr := addr.(type) {
case *net.TCPAddr:
this.space.DnsCache().Add(domain, typedAddr.IP)
case *net.UDPAddr:
this.space.DnsCache().Add(domain, typedAddr.IP)
}
}
}
writeMutex.Lock()
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
readMutex.Lock()
return nil
}