本文整理汇总了Golang中net.Dialer.Dial方法的典型用法代码示例。如果您正苦于以下问题:Golang Dialer.Dial方法的具体用法?Golang Dialer.Dial怎么用?Golang Dialer.Dial使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.Dialer
的用法示例。
在下文中一共展示了Dialer.Dial方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
示例2: 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()
}
}
示例3: 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
}
示例4: 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()
}
示例5: 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()
}
示例6: 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())
}
示例7: NewConnection
// NewConnection creates a new connection to the database server
func NewConnection(address string, opts *ConnectOpts) (*Connection, error) {
var err error
c := &Connection{
address: address,
opts: opts,
cursors: make(map[int64]*Cursor),
}
// Connect to Server
nd := net.Dialer{Timeout: c.opts.Timeout, KeepAlive: opts.KeepAlivePeriod}
if c.opts.TLSConfig == nil {
c.Conn, err = nd.Dial("tcp", address)
} else {
c.Conn, err = tls.DialWithDialer(&nd, "tcp", address, c.opts.TLSConfig)
}
if err != nil {
return nil, RQLConnectionError{rqlError(err.Error())}
}
// Send handshake
handshake, err := c.handshake(opts.HandshakeVersion)
if err != nil {
return nil, err
}
if err = handshake.Send(); err != nil {
return nil, err
}
return c, nil
}
示例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: 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)
}
示例11: DialMemdConn
func DialMemdConn(address string, tlsConfig *tls.Config, deadline time.Time) (*memdConn, error) {
d := net.Dialer{
Deadline: deadline,
}
baseConn, err := d.Dial("tcp", address)
if err != nil {
return nil, err
}
tcpConn := baseConn.(*net.TCPConn)
tcpConn.SetNoDelay(false)
var conn io.ReadWriteCloser
if tlsConfig == nil {
conn = tcpConn
} else {
tlsConn := tls.Client(tcpConn, tlsConfig)
if err != nil {
return nil, err
}
conn = tlsConn
}
return &memdConn{
conn: conn,
}, nil
}
示例12: 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
}
示例13: dialAndScrape
// Dial and scrape the basic service parameters
func (s *BasicService) dialAndScrape() (net.Conn, error) {
if s.Timeout == 0 {
log.Warnln("0 deadline set for service. This is probably not what you want as services will flap.")
}
// Set absolute deadline
deadline := time.Now().Add(time.Duration(s.Timeout))
// Dialer deadline
dialer := net.Dialer{
Deadline: deadline,
}
var err error
var conn net.Conn
conn, err = dialer.Dial(s.Protocol, fmt.Sprintf("%s:%d", s.Host().Hostname, s.Port()))
if err != nil {
s.portOpen = FAILED
} else {
s.portOpen = SUCCESS
}
if conn != nil {
// Connection deadline
conn.SetDeadline(deadline)
}
return conn, err
}
示例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: 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
}