Compare commits

...

6 Commits

View File

@ -26,7 +26,7 @@ import (
) )
const ( const (
version = "1.0.0-rc" version = "1.0.2"
) )
var ( var (
@ -177,8 +177,6 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
if err.Error() != "EOF" && !strings.HasSuffix(err.Error(), "i/o timeout") { if err.Error() != "EOF" && !strings.HasSuffix(err.Error(), "i/o timeout") {
logstream.Errorf("Error reading connection: %v\n", err.Error()) logstream.Errorf("Error reading connection: %v\n", err.Error())
} }
//sendResponse(connClt, err.Error(), 500)
//continue
return return
} }
@ -229,7 +227,12 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
// First query netCache built with ipNetworkNumber // First query netCache built with ipNetworkNumber
res, err := isIPContainedInNetCache(ip) res, err := isIPContainedInNetCache(ip)
if err != nil { if err != nil {
if strings.EqualFold(err.Error(), fmt.Sprintf("Invalid IP: %s", ip)) {
// We don't want those msg to pollute logs
logstream.Info(err.Error())
} else {
logstream.Error(err.Error()) logstream.Error(err.Error())
}
sendResponse(connClt, err.Error(), 500) sendResponse(connClt, err.Error(), 500)
continue continue
} }
@ -278,10 +281,14 @@ func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult,
result, err := conLdap.Search(searchReq) result, err := conLdap.Search(searchReq)
mutex.Unlock() mutex.Unlock()
// Let's just manage connection errors here // Let's just manage connection errors here
if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") { if (err != nil && (strings.HasSuffix(err.Error(), "ldap: connection closed")|| strings.HasSuffix(err.Error(), "ldap: conn is nil, expected net.Conn"))) {
logstream.Error("LDAP connection closed, retrying") logstream.Error("LDAP connection closed, retrying")
mutex.Lock() mutex.Lock()
// 16/01/2023: panic: runtime error: invalid memory address or nil pointer dereference
// probably bc connection is already closed
if conLdap != nil {
conLdap.Close() conLdap.Close()
}
conLdap, err = connectLdap() conLdap, err = connectLdap()
mutex.Unlock() mutex.Unlock()
if err != nil { if err != nil {
@ -299,12 +306,12 @@ func connectLdap() (*ldap.Conn, error) {
conLdap, err = ldap.DialURL(*ldapURL) conLdap, err = ldap.DialURL(*ldapURL)
if err != nil { if err != nil {
logstream.Errorf("Error dialing LDAP on %s: %v\n", *ldapURL, err) logstream.Errorf("Error dialing LDAP on %s: %v\n", *ldapURL, err)
return conLdap, err return nil, err
} }
err = conLdap.Bind(*ldapUser, *ldapPass) err = conLdap.Bind(*ldapUser, *ldapPass)
if err != nil { if err != nil {
logstream.Errorf("Error binding LDAP: ", err) logstream.Errorf("Error binding LDAP: %s", err)
return conLdap, err return nil, err
} }
return conLdap, err return conLdap, err
} }
@ -351,7 +358,7 @@ func main() {
ldapBaseDN = fs.String("ldapDN", "", "LDAP base DN (also via LDAPDN env var)") ldapBaseDN = fs.String("ldapDN", "", "LDAP base DN (also via LDAPDN env var)")
ldapUser = fs.String("ldapUser", "", "LDAP user DN (also via LDAPUSER env var)") ldapUser = fs.String("ldapUser", "", "LDAP user DN (also via LDAPUSER env var)")
ldapPass = fs.String("ldapPass", "", "LDAP user password (also via LDAPPASS env var)") ldapPass = fs.String("ldapPass", "", "LDAP user password (also via LDAPPASS env var)")
pidFilePath = fs.String("pidfile", "/var/run/mynettcptable/mynettcptable.pid", "PID File (also via PIDFILE env var)") pidFilePath = fs.String("pidfile", "", "PID File (also via PIDFILE env var). Creates pidfile only if defined")
refreshInterval = fs.Int("refresh", 300, "Net cache update interval in seconds") refreshInterval = fs.Int("refresh", 300, "Net cache update interval in seconds")
timeout = fs.Int("timeout", 5, "timeout in seconds") timeout = fs.Int("timeout", 5, "timeout in seconds")
_ = fs.String("config", "", "config file (optional)") _ = fs.String("config", "", "config file (optional)")
@ -368,7 +375,7 @@ func main() {
return return
} }
fmt.Printf("MyNetTCPTable v.%s\n", version) fmt.Printf("%s: MyNetTCPTable v.%s starting\n", time.Now().Format(time.RFC3339), version)
logstream = logrus.New() logstream = logrus.New()
level, err := logrus.ParseLevel(*logLevel) level, err := logrus.ParseLevel(*logLevel)
@ -405,13 +412,13 @@ func main() {
logstream.Hooks.Add(hook) logstream.Hooks.Add(hook)
} }
if len(*pidFilePath) > 0 {
if pid, err := pidfile.Create(*pidFilePath); err != nil { if pid, err := pidfile.Create(*pidFilePath); err != nil {
logstream.Fatal(err) logstream.Fatal(err)
} else { } else {
defer pid.Clear() defer pid.Clear()
} }
}
//defer logstream.Close()
logstream.Infof("Start listening for incoming connections on %s\n", *listen) logstream.Infof("Start listening for incoming connections on %s\n", *listen)
run() run()