1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-26 13:56:12 -04:00
v2fly/proxy/freedom/freedom.go

82 lines
1.7 KiB
Go
Raw Normal View History

2015-09-11 08:12:09 -04:00
package freedom
2015-09-09 11:39:06 -04:00
import (
2016-02-01 10:36:33 -05:00
"io"
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-04-25 18:13:26 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
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"
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
}
2016-04-25 18:13:26 -04:00
func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
log.Info("Freedom: Opening connection to ", destination)
2015-12-16 19:19:04 -05:00
2016-04-25 18:13:26 -04:00
defer payload.Release()
2016-04-18 12:44:10 -04:00
defer ray.OutboundInput().Release()
2016-05-06 18:19:06 -04:00
defer ray.OutboundOutput().Close()
2016-04-18 12:44:10 -04:00
2015-12-16 19:19:04 -05:00
var conn net.Conn
err := retry.Timed(5, 100).On(func() error {
2016-04-25 18:13:26 -04:00
rawConn, err := dialer.Dial(destination)
2015-12-16 19:19:04 -05:00
if err != nil {
return err
}
conn = rawConn
return nil
})
2015-09-09 11:39:25 -04:00
if err != nil {
2016-04-25 18:13:26 -04:00
log.Error("Freedom: Failed to open connection to ", destination, ": ", err)
return err
}
2015-12-15 10:38:25 -05:00
defer conn.Close()
2015-10-02 09:32:26 -04:00
input := ray.OutboundInput()
output := ray.OutboundOutput()
var readMutex, writeMutex sync.Mutex
readMutex.Lock()
writeMutex.Lock()
2016-04-25 18:13:26 -04:00
conn.Write(payload.Value)
go func() {
v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release()
2015-09-09 11:39:25 -04:00
2016-04-25 18:13:26 -04:00
v2io.Pipe(input, v2writer)
2015-10-02 09:32:26 -04:00
writeMutex.Unlock()
2016-04-25 18:13:26 -04:00
}()
2015-11-27 15:50:28 -05:00
go func() {
defer readMutex.Unlock()
2016-02-03 15:36:52 -05:00
var reader io.Reader = conn
2016-04-25 18:13:26 -04:00
if destination.IsUDP() {
2016-02-01 09:06:03 -05:00
reader = v2net.NewTimeOutReader(16 /* seconds */, conn)
2015-11-27 15:50:28 -05:00
}
v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release()
v2io.Pipe(v2reader, output)
2016-05-07 03:53:15 -04:00
ray.OutboundOutput().Close()
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
}