代码拉取完成,页面将自动刷新
package dialchain
import (
"fmt"
"net"
"time"
"github.com/elastic/beats/heartbeat/look"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/transport"
)
// TCPDialer creates a new NetDialer with constant event fields and default
// connection timeout.
// The fields parameter holds additional constants to be added to the final event
// structure.
//
// The dialer will update the active events with:
//
// {
// "tcp": {
// "port": ...,
// "rtt": { "connect": { "us": ... }}
// }
// }
func TCPDialer(to time.Duration) NetDialer {
return netDialer(to)
}
// UDPDialer creates a new NetDialer with constant event fields and default
// connection timeout.
// The fields parameter holds additional constants to be added to the final event
// structure.
//
// The dialer will update the active events with:
//
// {
// "udp": {
// "port": ...,
// "rtt": { "connect": { "us": ... }}
// }
// }
func UDPDialer(to time.Duration) NetDialer {
return netDialer(to)
}
func netDialer(timeout time.Duration) NetDialer {
return func(event common.MapStr) (transport.Dialer, error) {
return makeDialer(func(network, address string) (net.Conn, error) {
namespace := ""
switch network {
case "tcp", "tcp4", "tcp6":
namespace = "tcp"
case "udp", "udp4", "udp6":
namespace = "udp"
default:
return nil, fmt.Errorf("unsupported network type %v", network)
}
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
addresses, err := net.LookupHost(host)
if err != nil {
logp.Warn(`DNS lookup failure "%s": %v`, host, err)
return nil, err
}
// dial via host IP by randomized iteration of known IPs
dialer := &net.Dialer{Timeout: timeout}
start := time.Now()
conn, err := transport.DialWith(dialer, network, host, addresses, port)
if err != nil {
return nil, err
}
end := time.Now()
event.DeepUpdate(common.MapStr{
namespace: common.MapStr{
"rtt": common.MapStr{
"connect": look.RTT(end.Sub(start)),
},
},
})
return conn, nil
}), nil
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。