github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/privval/socket_dialers.go (about) 1 package privval 2 3 import ( 4 "errors" 5 "net" 6 "time" 7 8 "github.com/badrootd/nibiru-cometbft/crypto" 9 cmtnet "github.com/badrootd/nibiru-cometbft/libs/net" 10 p2pconn "github.com/badrootd/nibiru-cometbft/p2p/conn" 11 ) 12 13 // Socket errors. 14 var ( 15 ErrDialRetryMax = errors.New("dialed maximum retries") 16 ) 17 18 // SocketDialer dials a remote address and returns a net.Conn or an error. 19 type SocketDialer func() (net.Conn, error) 20 21 // DialTCPFn dials the given tcp addr, using the given timeoutReadWrite and 22 // privKey for the authenticated encryption handshake. 23 func DialTCPFn(addr string, timeoutReadWrite time.Duration, privKey crypto.PrivKey) SocketDialer { 24 return func() (net.Conn, error) { 25 conn, err := cmtnet.Connect(addr) 26 if err == nil { 27 deadline := time.Now().Add(timeoutReadWrite) 28 err = conn.SetDeadline(deadline) 29 } 30 if err == nil { 31 conn, err = p2pconn.MakeSecretConnection(conn, privKey) 32 } 33 return conn, err 34 } 35 } 36 37 // DialUnixFn dials the given unix socket. 38 func DialUnixFn(addr string) SocketDialer { 39 return func() (net.Conn, error) { 40 unixAddr := &net.UnixAddr{Name: addr, Net: "unix"} 41 return net.DialUnix("unix", nil, unixAddr) 42 } 43 }