// dial opens a websocket connection using the passed connection configuration
// details.
516616
func dial(config *ConnConfig) (*websocket.Conn, error) {
	// Setup TLS if not disabled.
	var tlsConfig *tls.Config
	var scheme = "ws"
	if !config.DisableTLS {
		tlsConfig = &tls.Config{
			MinVersion: tls.VersionTLS12,
		}
		if len(config.Certificates) > 0 {
			pool := x509.NewCertPool()
			pool.AppendCertsFromPEM(config.Certificates)
			tlsConfig.RootCAs = pool
		}
		scheme = "wss"
	}

	// Create a websocket dialer that will be used to make the connection.
	// It is modified by the proxy setting below as needed.
	dialer := websocket.Dialer{TLSClientConfig: tlsConfig}

	// Setup the proxy if one is configured.
	if config.Proxy != "" {
		proxy := &socks.Proxy{
			Addr:     config.Proxy,
			Username: config.ProxyUser,
			Password: config.ProxyPass,
		}
		dialer.NetDial = proxy.Dial
	}

	// The RPC server requires basic authorization, so create a custom
	// request header with the Authorization header set.
	login := config.User + ":" + config.Pass
	auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
	requestHeader := make(http.Header)
	requestHeader.Add("Authorization", auth)

	// Dial the connection.
	url := fmt.Sprintf("%s://%s/%s", scheme, config.Host, config.Endpoint)
	wsConn, resp, err := dialer.Dial(url, requestHeader)
	if err != nil {
		if err != websocket.ErrBadHandshake || resp == nil {
			return nil, err
		}

		// Detect HTTP authentication error status codes.
		if resp.StatusCode == http.StatusUnauthorized ||
			resp.StatusCode == http.StatusForbidden {
			return nil, ErrInvalidAuth
		}

		// The connection was authenticated and the status response was
		// ok, but the websocket handshake still failed, so the endpoint
		// is invalid in some way.
		if resp.StatusCode == http.StatusOK {
			return nil, ErrInvalidEndpoint
		}

		// Return the status text from the server if none of the special
		// cases above apply.
		return nil, errors.New(resp.Status)
	}
	return wsConn, nil
}
1369712
func dial(cid, port uint32) (*Conn, error) {
	cfd, err := newConnFD()
	if err != nil {
		return nil, err
	}

	return dialLinux(cfd, cid, port)
}
683339
func (sc *syncClient) dial() error {
	sc.lock.Lock()
	defer sc.lock.Unlock()
	if sc.client != nil {
		sc.refs++
		return nil
	}

	// TODO(jsing): Make this default to IPv6, if configured.
	peer := &net.TCPAddr{
		IP:   sc.engine.config.Peer.IPv4Addr,
		Port: sc.engine.config.SyncPort,
	}
	self := &net.TCPAddr{
		IP: sc.engine.config.Node.IPv4Addr,
	}
	conn, err := net.DialTCP("tcp", self, peer)
	if err != nil {
		return fmt.Errorf("failed to connect: %v", err)
	}
	sc.client = rpc.NewClient(conn)
	sc.refs = 1
	return nil
}
1229883
func Dial(addr string) (*Client, error) {
	conn, err := net.Dial("udp", addr)
	if err != nil {
		return nil, err
	}
	return newClient(conn, 0), nil
}
267098
func dial(req *http.Request, transport http.RoundTripper) (net.Conn, error) {
	conn, err := DialURL(req.Context(), req.URL, transport)
	if err != nil {
		return nil, fmt.Errorf("error dialing backend: %v", err)
	}

	if err = req.Write(conn); err != nil {
		conn.Close()
		return nil, fmt.Errorf("error sending request: %v", err)
	}

	return conn, err
}
427256
func (dialer Dialer) Dial(ctx context.Context, url string, httpHeader http.Header) (*Global, error) {
	// Set empty http header if omitted
	if httpHeader == nil {
		httpHeader = make(http.Header, 0)
	}

	if dialer.MockMode {
		dialer.CreateSocket = func(ctx context.Context, url string, httpHeader http.Header) (Socket, error) {
			socket, err := NewMockSocket(dialer.TrafficDumpFile)
			return socket, err
		}
	} else {
		// Create default CreateSocket function if omitted
		if dialer.CreateSocket == nil {
			dialer.CreateSocket = func(ctx context.Context, url string, httpHeader http.Header) (Socket, error) {
				gorillaDialer := websocket.Dialer{
					TLSClientConfig: dialer.TLSClientConfig,
					NetDial: func(network, addr string) (net.Conn, error) {
						return (&net.Dialer{}).DialContext(ctx, network, addr)
					},
					Jar: dialer.Jar,
				}

				// Run the actual websocket dialing (including the upgrade) in a goroutine so we can
				// return if the context times out
				chConn := make(chan *websocket.Conn, 1)
				chErr := make(chan error, 1)
				go func() {
					conn, resp, err := gorillaDialer.Dial(url, httpHeader)
					if err == websocket.ErrBadHandshake {
						chErr <- errors.Wrapf(err, "%d from ws server", resp.StatusCode)
					} else if err != nil {
						chErr <- err
					} else {
						select {
						case <-ctx.Done():
							conn.Close()
						default:
							chConn <- conn
						}
					}
				}()
				select {
				case <-ctx.Done():
					return nil, errors.Wrapf(ctx.Err(), "error connecting to ws server %s", url)
				case err := <-chErr:
					return nil, err
				case ws := <-chConn:
					return ws, nil
				}
			}
		}
		if dialer.TrafficDumpFile != "" {
			dialer.TrafficLogger = newFileTrafficLogger(dialer.TrafficDumpFile)
		}
	}

	enigmaSession := newSession(&dialer)
	err := enigmaSession.connect(ctx, url, httpHeader)
	if err != nil {
		return nil, err
	}
	return &Global{RemoteObject: enigmaSession.getRemoteObject(&ObjectInterface{Handle: -1, Type: "Global"})}, nil
}
1274498
func Dial(network string, addr string) (net.Conn, error) {
	return DialTimeout(network, addr, defaultDialTimeout)
}
1361542
func dial() (*websocket.Conn, error) {
	LoadAuth()

	if os.Getenv("TUTUM_STREAM_HOST") != "" {
		u, _ := url.Parse(os.Getenv("TUTUM_STREAM_HOST"))
		_, port, _ := net.SplitHostPort(u.Host)
		if port == "" {
			u.Host = u.Host + ":443"
		}
		StreamUrl = u.Scheme + "://" + u.Host + "/v1/"
	} else if os.Getenv("TUTUM_STREAM_URL") != "" {
		u, _ := url.Parse(os.Getenv("TUTUM_STREAM_URL"))
		_, port, _ := net.SplitHostPort(u.Host)
		if port == "" {
			u.Host = u.Host + ":443"
		}
		StreamUrl = u.Scheme + "://" + u.Host + "/v1/"
	}

	Url := StreamUrl + "events/"

	header := http.Header{}
	header.Add("Authorization", AuthHeader)
	header.Add("User-Agent", customUserAgent)

	var Dialer websocket.Dialer
	ws, _, err := Dialer.Dial(Url, header)
	if err != nil {
		return nil, err
	}

	return ws, nil
}
1142667
func (r *resolver) dial(ctx context.Context, network, _ string) (net.Conn, error) {
	return r.dialer.DialContext(ctx, network, r.address())
}
326926
func Dial(proxyURI string) func(string, string) (net.Conn, error) {
	cfg, err := parse(proxyURI)
	if err != nil {
		return dialError(err)
	}
	return cfg.dialFunc()
}
891762
func Dial(addr string, TLS *tls.Config) (Conn, error) {
	if addr == "" {
		addr = "localhost:2775"
	}
	fd, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}
	if TLS != nil {
		fd = tls.Client(fd, TLS)
	}
	c := &conn{
		rwc: fd,
		r:   bufio.NewReader(fd),
		w:   bufio.NewWriter(fd),
	}
	return c, nil
}
780315
func (c *Client) Dial(addr string, options ...DialOption) (net.Conn, error) {
	return c.Transporter.Dial(addr, options...)
}
296295
func (p *Pool) Dial(address string) error {
	if p.RequestTimeout <= 0 {
		p.RequestTimeout = 10 * time.Second
	}
	if p.InitialConn <= 0 {
		p.InitialConn = Config.PoolInitialSize
	}
	if p.MaximumConn <= 0 {
		p.MaximumConn = Config.PoolMaximumSize
	}
	if p.MaximumConn < p.InitialConn {
		p.MaximumConn = p.InitialConn
	}
	p.l = list.New()
	p.mutex = &sync.Mutex{}
	p.cond = sync.NewCond(p.mutex)
	p.address = address
	return p.connect(0)
}
598247
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
	return d.DialContext(context.Background(), urlStr, requestHeader)
}
119937
func Dial(addr string, options ...DialOption) (*ServerConn, error) {
	do := &dialOptions{}
	for _, option := range options {
		option.setup(do)
	}

	if do.location == nil {
		do.location = time.UTC
	}

	tconn := do.conn
	if tconn == nil {
		var err error

		if do.dialFunc != nil {
			tconn, err = do.dialFunc("tcp", addr)
		} else {
			ctx := do.context

			if ctx == nil {
				ctx = context.Background()
			}

			tconn, err = do.dialer.DialContext(ctx, "tcp", addr)
		}

		if err != nil {
			return nil, err
		}
	}

	// Use the resolved IP address in case addr contains a domain name
	// If we use the domain name, we might not resolve to the same IP.
	remoteAddr := tconn.RemoteAddr().(*net.TCPAddr)

	var sourceConn io.ReadWriteCloser = tconn
	if do.debugOutput != nil {
		sourceConn = newDebugWrapper(tconn, do.debugOutput)
	}

	c := &ServerConn{
		options:  do,
		features: make(map[string]string),
		conn:     textproto.NewConn(sourceConn),
		host:     remoteAddr.IP.String(),
	}

	_, _, err := c.conn.ReadResponse(StatusReady)
	if err != nil {
		c.Quit()
		return nil, err
	}

	err = c.feat()
	if err != nil {
		c.Quit()
		return nil, err
	}

	if _, mlstSupported := c.features["MLST"]; mlstSupported {
		c.mlstSupported = true
	}

	return c, nil
}
349612
func (r *ClearNet) Dial(network, address string) (net.Conn, error) {
	return net.Dial(network, address)
}
1319993
func Dial(address string) (*Conn, error) {
	conn := &Conn{
		RequestTimeout: time.Duration(Config.RequestTimeout) * time.Second,
	}
	conn.mutex.Lock()
	defer conn.mutex.Unlock()
	err := conn.connect(address, 0)
	return conn, err
}