diff --git a/sysloger.go b/sysloger.go index 12065d1..b757921 100644 --- a/sysloger.go +++ b/sysloger.go @@ -60,6 +60,8 @@ func NewSyslogWriterWithTimeout(params SyslogParams, formatter syslog.Formatter, var sysLogger *syslog.Writer var err error var dial func(network, addr string) (net.Conn, error) + netDialer := new(net.Dialer) + netDialer.Timeout = timeout if params.NeedTls { if params.TlsConf == nil { @@ -69,12 +71,12 @@ func NewSyslogWriterWithTimeout(params SyslogParams, formatter syslog.Formatter, dial = func(network, addr string) (net.Conn, error) { // cannot use "network" here as it'll simply be "custom" which will fail - return tls.DialWithDialer(&net.Dialer{Timeout: timeout}, params.Protocol, addr, params.TlsConf) + return tls.DialWithDialer(netDialer, params.Protocol, addr, params.TlsConf) } } else { dial = func(network, addr string) (net.Conn, error) { // cannot use "network" here as it'll simply be "custom" which will fail - return tls.DialWithDialer(&net.Dialer{Timeout: timeout}, params.Protocol, addr, nil) + return net.DialTimeout(params.Protocol, addr, timeout) } } diff --git a/sysloger_test.go b/sysloger_test.go index 87e6e1c..aac14ac 100644 --- a/sysloger_test.go +++ b/sysloger_test.go @@ -4,6 +4,7 @@ package sysloger import ( "testing" + "time" ) const CertPath = "" @@ -24,7 +25,7 @@ func TestSendSingleSyslogMsg(t *testing.T) { // RootCAs: CaCertPool, // InsecureSkipVerify: true, //} - + // header := CefHeader{ Version: 0, DeviceVendor: "Test", @@ -63,6 +64,62 @@ func TestSendSingleSyslogMsg(t *testing.T) { } } +///////////////////////////////////////////////// +func TestSendSingleSyslogMsgWithTimeout(t *testing.T) { + //t.Parallel() + + //GlobalCaCert, err := ioutil.ReadFile(CertPath) + //if err != nil { + // t.Fatal("Bad TestMakeSyslogWithFormatter1: ", err) + //} + // + //CaCertPool := x509.NewCertPool() + //CaCertPool.AppendCertsFromPEM(GlobalCaCert) + // + //tl := tls.Config{ + // RootCAs: CaCertPool, + // InsecureSkipVerify: true, + //} + + header := CefHeader{ + Version: 0, + DeviceVendor: "Test", + DeviceProduct: "TestProd", + DeviceVersion: "1.0", + DeviceEventClassId: "testing", + Name: "TEST", + Severity: "Low", + } + + testMap := map[string]interface{}{ + "src": "HOOOST", + "requestClientApplication": "Test-cli", + } + + // for CEFFormatter + testData, err := MakeCefString(header, testMap, false, true, false) + if err != nil { + t.Fatal(err) + } + + err = SendSingleSyslogMsgWithTimeout(SyslogParams{ + Level: 5, + Host: "127.0.0.1", + Port: 555, + Protocol: "tcp", + Priority: 0, + Tag: "test", + //NeedTls: false, + //TlsConf: &tl, + }, + CEFFormatter, + testData, + 2*time.Second) + if err != nil { + t.Fatal("Bad TestSendSingleSyslogMsg: ", err) + } +} + ///////////////////////////////////////////////// func TestSendListToSyslog(t *testing.T) { //t.Parallel() @@ -128,3 +185,70 @@ func TestSendListToSyslog(t *testing.T) { t.Fatal("Bad TestSendListToSyslog: ", err) } } + +///////////////////////////////////////////////// +func TestSendListToSyslogWithTimeout(t *testing.T) { + //t.Parallel() + + //GlobalCaCert, err := ioutil.ReadFile(CertPath) + //if err != nil { + // t.Fatal("Bad TestMakeSyslogWithFormatter1: ", err) + //} + // + //CaCertPool := x509.NewCertPool() + //CaCertPool.AppendCertsFromPEM(GlobalCaCert) + // + //tl := tls.Config{ + // RootCAs: CaCertPool, + // InsecureSkipVerify: true, + //} + + header := CefHeader{ + Version: 0, + DeviceVendor: "Test", + DeviceProduct: "TestProd", + DeviceVersion: "1.0", + DeviceEventClassId: "testing", + Name: "TEST", + Severity: "Low", + } + + testMap := []map[string]interface{}{ + { + "src": "HOOOST", + "requestClientApplication": "Test-cli", + }, + { + "src": "BOOOST", + "requestClientApplication": "Best-cli", + }, + } + + testData := make([]string, 0) + for _, el := range testMap { + // for CEFFormatter + testDataStr, err := MakeCefString(header, el, false, true, false) + if err != nil { + t.Fatal(err) + } + + testData = append(testData, testDataStr) + } + + err := SendListToSyslogWithTimeout(SyslogParams{ + Level: 3, + Host: "127.0.0.1", + Port: 555, + Protocol: "tcp", + Priority: 0, + Tag: "test", + //NeedTls: false, + //TlsConf: &tl, + }, + CEFFormatter, + testData, + 1*time.Minute) + if err != nil { + t.Fatal("Bad TestSendListToSyslog: ", err) + } +}