github.com/gogf/gf@v1.16.9/net/gtcp/gtcp_func.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gtcp
     8  
     9  import (
    10  	"crypto/rand"
    11  	"crypto/tls"
    12  	"net"
    13  	"time"
    14  
    15  	"github.com/gogf/gf/os/gfile"
    16  )
    17  
    18  const (
    19  	defaultConnTimeout    = 30 * time.Second       // Default connection timeout.
    20  	defaultRetryInternal  = 100 * time.Millisecond // Default retry interval.
    21  	defaultReadBufferSize = 128                    // (Byte) Buffer size for reading.
    22  )
    23  
    24  type Retry struct {
    25  	Count    int           // Retry count.
    26  	Interval time.Duration // Retry interval.
    27  }
    28  
    29  // NewNetConn creates and returns a net.Conn with given address like "127.0.0.1:80".
    30  // The optional parameter <timeout> specifies the timeout for dialing connection.
    31  func NewNetConn(addr string, timeout ...time.Duration) (net.Conn, error) {
    32  	d := defaultConnTimeout
    33  	if len(timeout) > 0 {
    34  		d = timeout[0]
    35  	}
    36  	return net.DialTimeout("tcp", addr, d)
    37  }
    38  
    39  // NewNetConnTLS creates and returns a TLS net.Conn with given address like "127.0.0.1:80".
    40  // The optional parameter <timeout> specifies the timeout for dialing connection.
    41  func NewNetConnTLS(addr string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error) {
    42  	dialer := &net.Dialer{
    43  		Timeout: defaultConnTimeout,
    44  	}
    45  	if len(timeout) > 0 {
    46  		dialer.Timeout = timeout[0]
    47  	}
    48  	return tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)
    49  }
    50  
    51  // NewNetConnKeyCrt creates and returns a TLS net.Conn with given TLS certificate and key files
    52  // and address like "127.0.0.1:80". The optional parameter <timeout> specifies the timeout for
    53  // dialing connection.
    54  func NewNetConnKeyCrt(addr, crtFile, keyFile string, timeout ...time.Duration) (net.Conn, error) {
    55  	tlsConfig, err := LoadKeyCrt(crtFile, keyFile)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return NewNetConnTLS(addr, tlsConfig, timeout...)
    60  }
    61  
    62  // Send creates connection to <address>, writes <data> to the connection and then closes the connection.
    63  // The optional parameter <retry> specifies the retry policy when fails in writing data.
    64  func Send(address string, data []byte, retry ...Retry) error {
    65  	conn, err := NewConn(address)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	defer conn.Close()
    70  	return conn.Send(data, retry...)
    71  }
    72  
    73  // SendRecv creates connection to <address>, writes <data> to the connection, receives response
    74  // and then closes the connection.
    75  //
    76  // The parameter <length> specifies the bytes count waiting to receive. It receives all buffer content
    77  // and returns if <length> is -1.
    78  //
    79  // The optional parameter <retry> specifies the retry policy when fails in writing data.
    80  func SendRecv(address string, data []byte, length int, retry ...Retry) ([]byte, error) {
    81  	conn, err := NewConn(address)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	defer conn.Close()
    86  	return conn.SendRecv(data, length, retry...)
    87  }
    88  
    89  // SendWithTimeout does Send logic with writing timeout limitation.
    90  func SendWithTimeout(address string, data []byte, timeout time.Duration, retry ...Retry) error {
    91  	conn, err := NewConn(address)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	defer conn.Close()
    96  	return conn.SendWithTimeout(data, timeout, retry...)
    97  }
    98  
    99  // SendRecvWithTimeout does SendRecv logic with reading timeout limitation.
   100  func SendRecvWithTimeout(address string, data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error) {
   101  	conn, err := NewConn(address)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	defer conn.Close()
   106  	return conn.SendRecvWithTimeout(data, receive, timeout, retry...)
   107  }
   108  
   109  // isTimeout checks whether given <err> is a timeout error.
   110  func isTimeout(err error) bool {
   111  	if err == nil {
   112  		return false
   113  	}
   114  	if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
   115  		return true
   116  	}
   117  	return false
   118  }
   119  
   120  // LoadKeyCrt creates and returns a TLS configuration object with given certificate and key files.
   121  func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error) {
   122  	crtPath, err := gfile.Search(crtFile)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	keyPath, err := gfile.Search(keyFile)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	crt, err := tls.LoadX509KeyPair(crtPath, keyPath)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	tlsConfig := &tls.Config{}
   135  	tlsConfig.Certificates = []tls.Certificate{crt}
   136  	tlsConfig.Time = time.Now
   137  	tlsConfig.Rand = rand.Reader
   138  	return tlsConfig, nil
   139  }