mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.9 KiB
83 lines
1.9 KiB
package freedom
|
|
|
|
import (
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
"github.com/v2ray/v2ray-core/transport/ray"
|
|
)
|
|
|
|
type FreedomConnection struct {
|
|
}
|
|
|
|
func NewFreedomConnection() *FreedomConnection {
|
|
return &FreedomConnection{}
|
|
}
|
|
|
|
func (vconn *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
|
|
conn, err := net.Dial(firstPacket.Destination().Network(), firstPacket.Destination().Address().String())
|
|
log.Info("Freedom: Opening connection to %s", firstPacket.Destination().String())
|
|
if err != nil {
|
|
close(ray.OutboundOutput())
|
|
log.Error("Freedom: Failed to open connection: %s : %v", firstPacket.Destination().String(), err)
|
|
return err
|
|
}
|
|
|
|
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 dumpInput(conn, input, &writeMutex)
|
|
}
|
|
|
|
go dumpOutput(conn, output, &readMutex, firstPacket.Destination().IsUDP())
|
|
|
|
writeMutex.Lock()
|
|
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
|
tcpConn.CloseWrite()
|
|
}
|
|
readMutex.Lock()
|
|
conn.Close()
|
|
|
|
return nil
|
|
}
|
|
|
|
func dumpInput(conn net.Conn, input <-chan *alloc.Buffer, finish *sync.Mutex) {
|
|
v2net.ChanToWriter(conn, input)
|
|
finish.Unlock()
|
|
}
|
|
|
|
func dumpOutput(conn net.Conn, output chan<- *alloc.Buffer, finish *sync.Mutex, udp bool) {
|
|
defer finish.Unlock()
|
|
defer close(output)
|
|
|
|
response, err := v2net.ReadFrom(conn, nil)
|
|
log.Info("Freedom receives %d bytes from %s", response.Len(), conn.RemoteAddr().String())
|
|
if response.Len() > 0 {
|
|
output <- response
|
|
} else {
|
|
response.Release()
|
|
}
|
|
if err != nil {
|
|
return
|
|
}
|
|
if udp {
|
|
return
|
|
}
|
|
|
|
v2net.ReaderToChan(output, conn)
|
|
}
|