本文整理汇总了Golang中net.Dialer类的典型用法代码示例。如果您正苦于以下问题:Golang Dialer类的具体用法?Golang Dialer怎么用?Golang Dialer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dialer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: dialContext
// dialContext connects to the address on the named network.
func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
var dialer net.Dialer
if deadline, ok := ctx.Deadline(); ok {
dialer.Timeout = deadline.Sub(time.Now())
}
return dialer.Dial(network, address)
}
示例2: dial
// We accept 'ssl'/'tls' as a protocol; it implies 'tcp' as the underlying
// protocol.
func dial(proto, addr, laddr string, tmout time.Duration) (net.Conn, error) {
// Set up our dialer options; we may need a local address and/or
// a connection timeout.
// TODO: happy eyeballs support, ie dialer.DualStack? This might be
// worth a command line switch.
var dialer net.Dialer
dialer.Timeout = tmout
if laddr != "" {
a, e := ResolveAddr(proto, laddr)
if e != nil {
return nil, e
}
dialer.LocalAddr = a
}
switch proto {
case "ssl", "tls":
// For testing I do not want to have to verify anything
// about the target certificates. I have other tools for
// that.
cfg := tls.Config{InsecureSkipVerify: true}
return tls.DialWithDialer(&dialer, "tcp", addr, &cfg)
case "sslver", "tlsver":
return tls.DialWithDialer(&dialer, "tcp", addr, nil)
}
return dialer.Dial(proto, addr)
}
示例3: checkSourceURI
// checkSourceURI performs a check on the URI associated with the build
// to make sure that it is valid. It also optionally tests the connection
// to the source uri.
func checkSourceURI(git git.Git, rawurl string, testConnection bool, timeout time.Duration) error {
if !git.ValidCloneSpec(rawurl) {
return fmt.Errorf("Invalid git source url: %s", rawurl)
}
if strings.HasPrefix(rawurl, "[email protected]") || strings.HasPrefix(rawurl, "git://") {
return nil
}
srcURL, err := url.Parse(rawurl)
if err != nil {
return err
}
if !testConnection {
return nil
}
host := srcURL.Host
if strings.Index(host, ":") == -1 {
switch srcURL.Scheme {
case "http":
host += ":80"
case "https":
host += ":443"
}
}
dialer := net.Dialer{Timeout: timeout}
conn, err := dialer.Dial("tcp", host)
if err != nil {
return err
}
return conn.Close()
}
示例4: Loop
func (conn *Connection) Loop() {
dialer := net.Dialer{Timeout: DialTimeout}
conn.State = CsDialing
if netconn, err := dialer.Dial(conn.Network, conn.Address); err != nil {
conn.ConnErr <- Error{conn, Dial, err}
conn.State = CsClosed
} else {
conn.conn = netconn.(nt.NetConn)
conn.reader.Init(conn.conn, conn.ReadTimeout, conn.RetCodeType)
conn.writer.Init(conn.conn, conn.WriteTimeout, conn.RetCodeType)
if err = conn.writer.Ping(); err == nil {
if err = conn.writer.Flush(); err == nil {
err = conn.reader.ReadPing()
}
}
if err != nil {
conn.conn.Close()
conn.ConnErr <- Error{conn, Dial, err}
conn.State = CsClosed
return
}
conn.ConnErr <- Error{conn, Dial, nil}
conn.State = CsConnected
go conn.readLoop()
go conn.writeLoop()
go conn.controlLoop()
}
}
示例5: sendAndReceiveState
// sendState is used to initiate a push/pull over TCP with a remote node
func (m *Memberlist) sendAndReceiveState(addr []byte, port uint16, join bool) ([]pushNodeState, []byte, error) {
// Attempt to connect
dialer := net.Dialer{Timeout: m.config.TCPTimeout}
dest := net.TCPAddr{IP: addr, Port: int(port)}
conn, err := dialer.Dial("tcp", dest.String())
if err != nil {
return nil, nil, err
}
defer conn.Close()
m.logger.Printf("[DEBUG] memberlist: Initiating push/pull sync with: %s", conn.RemoteAddr())
metrics.IncrCounter([]string{"memberlist", "tcp", "connect"}, 1)
// Send our state
if err := m.sendLocalState(conn, join); err != nil {
return nil, nil, err
}
// Read remote state
_, remote, userState, err := m.readRemoteState(conn)
if err != nil {
err := fmt.Errorf("Reading remote state failed: %v", err)
return nil, nil, err
}
// Return the remote state
return remote, userState, nil
}
示例6: checkSourceURI
// checkSourceURI performs a check on the URI associated with the build
// to make sure that it is live before proceeding with the build.
func (d *DockerBuilder) checkSourceURI() error {
rawurl := d.build.Spec.Source.Git.URI
if !d.git.ValidCloneSpec(rawurl) {
return fmt.Errorf("Invalid git source url: %s", rawurl)
}
if strings.HasPrefix(rawurl, "git://") || strings.HasPrefix(rawurl, "[email protected]") {
return nil
}
if !strings.HasPrefix(rawurl, "http://") && !strings.HasPrefix(rawurl, "https://") {
rawurl = fmt.Sprintf("https://%s", rawurl)
}
srcURL, err := url.Parse(rawurl)
if err != nil {
return err
}
host := srcURL.Host
if strings.Index(host, ":") == -1 {
switch srcURL.Scheme {
case "http":
host += ":80"
case "https":
host += ":443"
}
}
dialer := net.Dialer{Timeout: d.urlTimeout}
conn, err := dialer.Dial("tcp", host)
if err != nil {
return err
}
return conn.Close()
}
示例7: DialTCPSAddrTimeout
// DialTCPSAddrTimeout 同 DialTCPSAddr 函数,增加了超时功能
func (p *directProxyClient) DialTCPSAddrTimeout(network string, raddr string, timeout time.Duration) (rconn ProxyTCPConn, rerr error) {
switch network {
case "tcp", "tcp4", "tcp6":
default:
return nil, fmt.Errorf("不支持的 network 类型:%v", network)
}
d := net.Dialer{Timeout: timeout, LocalAddr: &p.TCPLocalAddr}
conn, err := d.Dial(network, raddr)
if err != nil {
return nil, err
}
splitHttp := false
if p.splitHttp {
raddr, err := net.ResolveTCPAddr(network, raddr)
if err == nil && raddr.Port == 80 {
splitHttp = true
}
}
wTime := time.Time{}
if p.sleep != 0 {
wTime = time.Now()
wTime = wTime.Add(p.sleep)
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
return &directTCPConn{*tcpConn, splitHttp, wTime, p}, nil
}
return nil, fmt.Errorf("内部错误")
}
示例8: do_axfr
func do_axfr(zone_name string) ([]dns.RR, error) {
result := []dns.RR{}
message := new(dns.Msg)
message.SetAxfr(zone_name)
transfer := &dns.Transfer{DialTimeout: conf.Query_timeout, ReadTimeout: conf.Query_timeout}
if conf.Transfer_source != nil {
d := net.Dialer{LocalAddr: conf.Transfer_source}
c, err := d.Dial("tcp", conf.Master)
if err != nil {
logger.Debug("AXFR ERROR : problem dialing master")
return result, err
}
dnscon := &dns.Conn{Conn: c}
transfer = &dns.Transfer{Conn: dnscon, DialTimeout: conf.Query_timeout, ReadTimeout: conf.Query_timeout}
}
channel, err := transfer.In(message, conf.Master)
if err != nil {
return result, err
}
for envelope := range channel {
result = append(result, envelope.RR...)
}
return result, nil
}
示例9: get_serial
func get_serial(zone_name, query_dest string) (uint32, error) {
var in *dns.Msg
m := new(dns.Msg)
m.SetQuestion(zone_name, dns.TypeSOA)
if conf.Transfer_source != nil {
d := net.Dialer{LocalAddr: conf.Transfer_source}
c, err := d.Dial("tcp", query_dest)
if err != nil {
logger.Error(fmt.Sprintf("QUERY ERROR : problem dialing query_dest %s", query_dest))
return 0, err
}
co := &dns.Conn{Conn: c}
co.WriteMsg(m)
in, err = co.ReadMsg()
if err != nil {
logger.Error(fmt.Sprintf("QUERY ERROR : problem querying query_dest %s", query_dest))
return 0, err
}
co.Close()
} else {
c := &dns.Client{DialTimeout: conf.Query_timeout, ReadTimeout: conf.Query_timeout}
if conf.All_tcp == true {
c.Net = "tcp"
}
// _ is query time, might be useful later
var err error
in, _, err = c.Exchange(m, query_dest)
if err != nil {
logger.Error(fmt.Sprintf("QUERY ERROR : problem querying query_dest %s", query_dest))
return 0, err
}
}
return serial_query_parse(in), nil
}
示例10: sendAndReceiveState
// sendAndReceiveState is used to initiate a push/pull over TCP with a remote node
func (m *Memberlist) sendAndReceiveState(addr []byte, port uint16, join bool) ([]pushNodeState, []byte, error) {
// Attempt to connect
dialer := net.Dialer{Timeout: m.config.TCPTimeout}
dest := net.TCPAddr{IP: addr, Port: int(port)}
conn, err := dialer.Dial("tcp", dest.String())
if err != nil {
return nil, nil, err
}
defer conn.Close()
m.logger.Printf("[DEBUG] memberlist: Initiating push/pull sync with: %s", conn.RemoteAddr())
metrics.IncrCounter([]string{"memberlist", "tcp", "connect"}, 1)
// Send our state
if err := m.sendLocalState(conn, join); err != nil {
return nil, nil, err
}
conn.SetDeadline(time.Now().Add(m.config.TCPTimeout))
msgType, bufConn, dec, err := m.readTCP(conn)
if err != nil {
return nil, nil, err
}
// Quit if not push/pull
if msgType != pushPullMsg {
err := fmt.Errorf("received invalid msgType (%d), expected pushPullMsg (%d) %s", msgType, pushPullMsg, LogConn(conn))
return nil, nil, err
}
// Read remote state
_, remoteNodes, userState, err := m.readRemoteState(bufConn, dec)
return remoteNodes, userState, err
}
示例11: tlsDial
// tlsDial wraps either net.Dial or crypto/tls.Dial, depending on the contents of
// the passed TLS Config.
func tlsDial(network, address string, timeout time.Duration, config *tls.Config) (net.Conn, error) {
defaultDialer := net.Dialer{Timeout: timeout}
if config == nil {
return defaultDialer.Dial(network, address)
}
return tls.DialWithDialer(&defaultDialer, network, address, config)
}
示例12: NewHttpsWriter
func NewHttpsWriter(outputUrl *url.URL, appId string, skipCertVerify bool, dialer *net.Dialer, timeout time.Duration) (w *httpsWriter, err error) {
if dialer == nil {
return nil, errors.New("cannot construct a writer with a nil dialer")
}
if outputUrl.Scheme != "https" {
return nil, errors.New(fmt.Sprintf("Invalid scheme %s, httpsWriter only supports https", outputUrl.Scheme))
}
tlsConfig := &tls.Config{InsecureSkipVerify: skipCertVerify}
tr := &http.Transport{
MaxIdleConnsPerHost: 1,
TLSClientConfig: tlsConfig,
TLSHandshakeTimeout: dialer.Timeout * 2,
Dial: func(network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
},
}
client := &http.Client{Transport: tr, Timeout: timeout}
return &httpsWriter{
appId: appId,
outputUrl: outputUrl,
tlsConfig: tlsConfig,
client: client,
}, nil
}
示例13: sendTCPUserMsg
// sendTCPUserMsg is used to send a TCP userMsg to another host
func (m *Memberlist) sendTCPUserMsg(to net.Addr, sendBuf []byte) error {
dialer := net.Dialer{Timeout: m.config.TCPTimeout}
conn, err := dialer.Dial("tcp", to.String())
if err != nil {
return err
}
defer conn.Close()
bufConn := bytes.NewBuffer(nil)
if err := bufConn.WriteByte(byte(userMsg)); err != nil {
return err
}
// Send our node state
header := userMsgHeader{UserMsgLen: len(sendBuf)}
hd := codec.MsgpackHandle{}
enc := codec.NewEncoder(bufConn, &hd)
if err := enc.Encode(&header); err != nil {
return err
}
if _, err := bufConn.Write(sendBuf); err != nil {
return err
}
return m.rawSendMsgTCP(conn, bufConn.Bytes())
}
示例14: NewClient
// NewVersionedClient returns a Client instance ready for communication with
// the given server endpoint
func NewClient(endpoint string) (*Client, error) {
var httpClient *http.Client
var dialFunc func(network, addr string) (net.Conn, error)
u, err := url.Parse(endpoint)
if err != nil {
return nil, ErrInvalidEndpoint
}
if u.Scheme == "unix" {
d := net.Dialer{}
dialFunc = func(network, addr string) (net.Conn, error) {
return d.Dial("unix", u.Path)
}
httpClient = &http.Client{
Transport: &http.Transport{
Dial: dialFunc,
},
}
} else {
httpClient = http.DefaultClient
}
return &Client{
httpClient: httpClient,
endpoint: endpoint,
endpointURL: u,
dialer: dialFunc,
}, nil
}
示例15: grabber
// Read addresses from addrChan and grab banners from these hosts.
// Sends resultStructs to resultChan. Writes to doneChan when complete.
func grabber(addrChan chan string, resultChan chan resultStruct, doneChan chan int) {
for addr := range addrChan {
deadline := time.Now().Add(time.Duration(*timeoutFlag) * time.Second)
dialer := net.Dialer{Deadline: deadline}
conn, err := dialer.Dial("tcp", net.JoinHostPort(addr, *portFlag))
if err != nil {
resultChan <- resultStruct{addr, nil, err}
continue
}
conn.SetDeadline(deadline)
if len(messageData) > 0 {
s := strings.Replace(string(messageData), "%s", addr, -1)
if _, err := conn.Write([]byte(s)); err != nil {
conn.Close()
resultChan <- resultStruct{addr, nil, err}
continue
}
}
var buf [1024]byte
n, err := conn.Read(buf[:])
conn.Close()
if err != nil && (err != io.EOF || n == 0) {
resultChan <- resultStruct{addr, nil, err}
continue
}
resultChan <- resultStruct{addr, buf[0:n], nil}
}
doneChan <- 1
}