本文整理汇总了Golang中github.com/v2ray/v2ray-core/common/net.Destination.IsUDP方法的典型用法代码示例。如果您正苦于以下问题:Golang Destination.IsUDP方法的具体用法?Golang Destination.IsUDP怎么用?Golang Destination.IsUDP使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/v2ray/v2ray-core/common/net.Destination
的用法示例。
在下文中一共展示了Destination.IsUDP方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: startCommunicate
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray core.OutboundRay, firstPacket v2net.Packet) error {
conn, err := net.Dial(dest.Network(), dest.Address().String())
if err != nil {
log.Error("Failed to open %s: %v", dest.String(), err)
if ray != nil {
close(ray.OutboundOutput())
}
return err
}
log.Info("VMessOut: Tunneling request to %s via %s", request.Address.String(), dest.String())
defer conn.Close()
input := ray.OutboundInput()
output := ray.OutboundOutput()
var requestFinish, responseFinish sync.Mutex
requestFinish.Lock()
responseFinish.Lock()
go handleRequest(conn, request, firstPacket, input, &requestFinish)
go handleResponse(conn, request, output, &responseFinish, dest.IsUDP())
requestFinish.Lock()
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
responseFinish.Lock()
return nil
}
示例2: Dial
func Dial(dest v2net.Destination) (net.Conn, error) {
if dest.Address().IsDomain() {
dialer := &net.Dialer{
Timeout: time.Second * 60,
DualStack: true,
}
network := "tcp"
if dest.IsUDP() {
network = "udp"
}
return dialer.Dial(network, dest.NetAddr())
}
ip := dest.Address().IP()
if dest.IsTCP() {
return net.DialTCP("tcp", nil, &net.TCPAddr{
IP: ip,
Port: int(dest.Port()),
})
} else {
return net.DialUDP("udp", nil, &net.UDPAddr{
IP: ip,
Port: int(dest.Port()),
})
}
}
示例3: Dispatch
func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
defer ray.OutboundInput().Release()
defer ray.OutboundOutput().Close()
var rec *protocol.ServerSpec
var conn internet.Connection
err := retry.Timed(5, 100).On(func() error {
rec = this.serverPicker.PickServer()
rawConn, err := internet.Dial(this.meta.Address, rec.Destination(), this.meta.StreamSettings)
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
log.Error("VMess|Outbound: Failed to find an available destination:", err)
return err
}
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination)
command := protocol.RequestCommandTCP
if target.IsUDP() {
command = protocol.RequestCommandUDP
}
request := &protocol.RequestHeader{
Version: encoding.Version,
User: rec.PickUser(),
Command: command,
Address: target.Address(),
Port: target.Port(),
Option: protocol.RequestOptionChunkStream,
}
defer conn.Close()
conn.SetReusable(true)
if conn.Reusable() { // Conn reuse may be disabled on transportation layer
request.Option.Set(protocol.RequestOptionConnectionReuse)
}
input := ray.OutboundInput()
output := ray.OutboundOutput()
var requestFinish, responseFinish sync.Mutex
requestFinish.Lock()
responseFinish.Lock()
session := encoding.NewClientSession(protocol.DefaultIDHash)
go this.handleRequest(session, conn, request, payload, input, &requestFinish)
go this.handleResponse(session, conn, request, rec.Destination(), output, &responseFinish)
requestFinish.Lock()
responseFinish.Lock()
return nil
}
示例4: Dispatch
func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
log.Info("Freedom: Opening connection to ", destination)
defer payload.Release()
defer ray.OutboundInput().Release()
defer ray.OutboundOutput().Close()
var conn internet.Connection
if this.domainStrategy == DomainStrategyUseIP && destination.Address().IsDomain() {
destination = this.ResolveIP(destination)
}
err := retry.Timed(5, 100).On(func() error {
rawConn, err := internet.Dial(this.meta.Address, destination, this.meta.StreamSettings)
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
return err
}
defer conn.Close()
input := ray.OutboundInput()
output := ray.OutboundOutput()
var readMutex, writeMutex sync.Mutex
readMutex.Lock()
writeMutex.Lock()
conn.Write(payload.Value)
go func() {
v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release()
v2io.Pipe(input, v2writer)
writeMutex.Unlock()
}()
go func() {
defer readMutex.Unlock()
var reader io.Reader = conn
timeout := this.timeout
if destination.IsUDP() {
timeout = 16
}
if timeout > 0 {
reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn)
}
v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release()
v2io.Pipe(v2reader, output)
ray.OutboundOutput().Close()
}()
writeMutex.Lock()
if tcpConn, ok := conn.(*tcp.RawConnection); ok {
tcpConn.CloseWrite()
}
readMutex.Lock()
return nil
}