Skip to content

Commit

Permalink
Merge pull request #8 from efremovmi/main
Browse files Browse the repository at this point in the history
v1.0.7
  • Loading branch information
NGRsoftlab authored Mar 10, 2023
2 parents 196c3b4 + e5a229c commit c8da12d
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 3 deletions.
6 changes: 4 additions & 2 deletions sysloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down
126 changes: 125 additions & 1 deletion sysloger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sysloger

import (
"testing"
"time"
)

const CertPath = ""
Expand All @@ -24,7 +25,7 @@ func TestSendSingleSyslogMsg(t *testing.T) {
// RootCAs: CaCertPool,
// InsecureSkipVerify: true,
//}

//
header := CefHeader{
Version: 0,
DeviceVendor: "Test",
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}

0 comments on commit c8da12d

Please sign in to comment.