// 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) }
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() } }
// 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 }
// 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, "git@") { 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() }
// 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, "git@") || 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() }
// 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()) }
// 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 }
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 }
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 }
// 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) }
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 }
// 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 }
// 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 }
// 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 }
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 }
// sendState is used to initiate a push/pull over TCP with a remote node func (m *Memberlist) sendAndReceiveState(addr []byte) ([]pushNodeState, []byte, error) { // Attempt to connect dialer := net.Dialer{Timeout: m.config.TCPTimeout} dest := net.TCPAddr{IP: addr, Port: m.config.TCPPort} conn, err := dialer.Dial("tcp", dest.String()) if err != nil { return nil, nil, err } defer conn.Close() m.logger.Printf("[INFO] Initiating push/pull sync with: %s", conn.RemoteAddr()) // Send our state if err := m.sendLocalState(conn); 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 }
// DialWithDialer is shorthand for using dialer.Dial() then NewConnection() func DialWithDialer(dialer *net.Dialer, network, addr string, opts ...ConnectionOption) (c Connection, err error) { conn, err := dialer.Dial(network, addr) if err == nil { c, err = NewConnection(conn, opts...) } return }
func NewClient(originURL, targetURL string, settings map[string]interface{}) (c *websocket.Conn, response *http.Response, err error) { u, err := url.Parse(targetURL) if err != nil { return nil, nil, err } dialer := net.Dialer{} timeoutInterface, ok := settings["Timeout"] if ok { dialer.Timeout = timeoutInterface.(time.Duration) } keepAliveInterface, ok := settings["KeepAlive"] if ok { dialer.KeepAlive = keepAliveInterface.(time.Duration) } rawConn, err := dialer.Dial("tcp", u.Host) if err != nil { logrus.WithFields(logrus.Fields{ "Error": err.Error(), }).Errorf("TCP connection error when dialing %v", u.Host) return nil, nil, err } wsHeaders := http.Header{ "Origin": {originURL}, // your milage may differ "Sec-WebSocket-Extensions": {"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"}, } return websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024) }
func (p *peer) connect() error { var dialer net.Dialer var backoff = backoff{Max: 10 * time.Second} for { dialer.Deadline = time.Now().Add(3 * time.Second) if p.sock.debug { println("Connecting", p.network, p.address, dialer.Deadline.String()) } conn, err := dialer.Dial(p.network, p.address) if err != nil { backoff.Wait() continue } p.conn = conn if p.sock.debug { fmt.Printf("\nConnected %s (%s) <-> %s (%s)..\n", conn.LocalAddr(), p.sock.ident, conn.RemoteAddr(), p.ident) } break } return nil }
// NewRemotePassAuthRunnerWithTimeouts is one of functions for creating remote // runner. Use this one instead of NewRemotePassAuthRunner if you need to setup // nondefault timeouts for ssh connection func NewRemotePassAuthRunnerWithTimeouts( user, host, password string, timeouts Timeouts, ) (*Remote, error) { config := &ssh.ClientConfig{ User: user, Auth: []ssh.AuthMethod{ssh.Password(password)}, } dialer := net.Dialer{ Timeout: timeouts.ConnectionTimeout, Deadline: time.Now().Add(timeouts.ConnectionTimeout), KeepAlive: timeouts.KeepAlive, } conn, err := dialer.Dial("tcp", host) if err != nil { return nil, err } connection := &timeBoundedConnection{ Conn: conn, readTimeout: timeouts.SendTimeout, writeTimeout: timeouts.ReceiveTimeout, } sshConnection, channels, requests, err := ssh.NewClientConn( connection, host, config, ) if err != nil { return nil, err } return &Remote{ssh.NewClient(sshConnection, channels, requests)}, nil }
// 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("内部错误") }
// DialWithDialer connects to the fcgi responder at the specified network address, using custom net.Dialer. // See func net.Dial for a description of the network and address parameters. func DialWithDialer(network, address string, dialer net.Dialer) (fcgi *FCGIClient, err error) { var conn net.Conn conn, err = dialer.Dial(network, address) if err != nil { return } tmp, err := ioutil.TempDir("", "fcgi") if err != nil { return nil, err } binlog, err := os.Create(filepath.Join(tmp, "bin.log")) if err != nil { return nil, err } strlog, err := os.Create(filepath.Join(tmp, "str.log")) if err != nil { return nil, err } fcgi = &FCGIClient{ rwc: rwcWrapper{conn, binlog, strlog}, keepAlive: false, reqID: 1, } fmt.Printf("FASTCGI: Dumping to %v\n", tmp) return }
func (c *Client) tcpConnect(dialer *net.Dialer) (err error) { c.connMutex.RLock() defer c.connMutex.RUnlock() tcpaddr, err := net.ResolveTCPAddr("tcp", c.addr) if err != nil { return err } c.tcpaddr = tcpaddr if c.conn_count <= 0 { return BadNumberOfConnections } else if conn := <-c.conns; conn == nil { // Create multiple connections to Riak and send these to the conns channel for later use for i := 0; i < c.conn_count; i++ { conn, err := dialer.Dial("tcp", tcpaddr.String()) if err != nil { // Empty the conns channel before returning, in case an error appeared after a few // successful connections. for j := 0; j < i; j++ { (<-c.conns).Close() } c.conns <- nil return err } c.conns <- conn.(*net.TCPConn) } } else { c.conns <- conn } return nil }
// 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) }
func XSISubscribeCH(Config ConfigT, def DefHead) (net.Conn, string) { var CPOST string = "POST /com.broadsoft.async/com.broadsoft.xsi-events/v2.0/channel HTTP/1.1" var CSET string = ConcatStr("", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Channel xmlns=\"http://schema.broadsoft.com/xsi\"><channelSetId>", def.CHANID, "</channelSetId><priority>1</priority><weight>100</weight><expires>", Config.Main.Expires, "</expires></Channel>") var CLEN string = ConcatStr("", "Content-Length: ", strconv.Itoa(len(CSET))) var dialer net.Dialer dialer.Timeout = time.Second chandesc, err := dialer.Dial("tcp", ConcatStr(":", Config.Main.Host, Config.Main.HTTPPort)) if err != nil { LogErr(err, "chan dial") } fmt.Fprintf(chandesc, "%s\n%s\n%s\n%s\n%s\n\n%s\n", CPOST, def.AUTHORIZATION, def.HOSTH, CLEN, def.CTYPE, CSET) chanreader := bufio.NewReader(chandesc) status, err := chanreader.ReadString('\n') if err != nil { LogErr(err, "chan read") } if !strings.Contains(status, "200") { LogErr(nil, "chan status", status) } //get new chan id data := make([]byte, 1024) _, err = chanreader.Read(data) chanID := GetChanID(data) return chandesc, chanID }
// Dial connects to a remote address and pipes all os.Stdin to the remote end. // If localAddr is set, uses it to Dial from. func Dial(localAddr, remoteAddr string) (net.Conn, error) { var laddr net.Addr var err error if localAddr != "" { laddr, err = net.ResolveTCPAddr("tcp", localAddr) if err != nil { return nil, fmt.Errorf("failed to resolve address %s", localAddr) } } if laddr != nil { out("dialing %s from %s", remoteAddr, laddr) } else { out("dialing %s", remoteAddr) } d := net.Dialer{LocalAddr: laddr} c, err := d.Dial("tcp", remoteAddr) if err != nil { return nil, err } out("connected to %s", c.RemoteAddr()) return c, nil }
// 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 }
// DefaultDial attempts to open a TCP connection to the provided address, explicitly // enabling keep-alives with a one-second interval. func (c *Client) DefaultDial(network, addr string) (net.Conn, error) { dialer := net.Dialer{ Timeout: c.config.DialTimeout, KeepAlive: time.Second, } return dialer.Dial(network, addr) }
func (netDialer) Dial(n, addr string, ctx context.Context) (net.Conn, error) { deadline, _ := ctx.Deadline() d := net.Dialer{ Deadline: deadline, } return d.Dial(n, addr) }
func (*sysView) Dial(network, address string, dialer *net.Dialer) (net.Conn, error) { if dialer != nil { return dialer.Dial(network, address) } else { return net.Dial(network, address) } }