mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-19 10:56:06 -05:00
100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
package freedom
|
|
|
|
import (
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
|
"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() {
|
|
v2net.ChanToWriter(conn, input)
|
|
writeMutex.Unlock()
|
|
}()
|
|
}
|
|
|
|
go func() {
|
|
defer readMutex.Unlock()
|
|
defer close(output)
|
|
|
|
response, err := v2net.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
|
|
}
|
|
|
|
v2net.ReaderToChan(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
|
|
}
|