當前位置: 首頁>>代碼示例>>Golang>>正文


Golang net.Dialer類代碼示例

本文整理匯總了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)
}
開發者ID:kubernetes,項目名稱:kubernetes,代碼行數:8,代碼來源:pre_go16.go

示例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)
}
開發者ID:siebenmann,項目名稱:call,代碼行數:29,代碼來源:call.go

示例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()
}
開發者ID:hloganathan,項目名稱:origin,代碼行數:33,代碼來源:source.go

示例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()
	}
}
開發者ID:moxian,項目名稱:go-iproto,代碼行數:28,代碼來源:connection.go

示例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
}
開發者ID:DaveDaCoda,項目名稱:docker,代碼行數:28,代碼來源:net.go

示例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()

}
開發者ID:nikkomega,項目名稱:origin,代碼行數:34,代碼來源:docker.go

示例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("內部錯誤")
}
開發者ID:GameXG,項目名稱:ProxyClient,代碼行數:31,代碼來源:directproxy.go

示例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
}
開發者ID:rackerlabs,項目名稱:slappy,代碼行數:26,代碼來源:slapdns.go

示例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
}
開發者ID:rackerlabs,項目名稱:slappy,代碼行數:35,代碼來源:slapdns.go

示例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
}
開發者ID:PagerDuty,項目名稱:nomad,代碼行數:34,代碼來源:net.go

示例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)
}
開發者ID:mbertschler,項目名稱:cockroach,代碼行數:9,代碼來源:tls.go

示例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
}
開發者ID:rakutentech,項目名稱:loggregator,代碼行數:25,代碼來源:https_writer.go

示例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())
}
開發者ID:PagerDuty,項目名稱:nomad,代碼行數:30,代碼來源:net.go

示例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
}
開發者ID:morfeo8marc,項目名稱:docker-multi-tenancy,代碼行數:33,代碼來源:client.go

示例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
}
開發者ID:nulng,項目名稱:zmap,代碼行數:31,代碼來源:banner.go


注:本文中的net.Dialer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。