Compare commits
4 Commits
v1.0.0-rc3
..
v1.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
| f59d548f04 | |||
| 0e73dac143 | |||
| 8ac556ad8b | |||
| 6e9947809c |
+33
-10
@@ -26,7 +26,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "1.0.0-rc3"
|
version = "1.0.3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -176,6 +176,8 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
// Dont notice if client closed connection
|
// Dont notice if client closed connection
|
||||||
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())
|
||||||
|
} else {
|
||||||
|
logstream.Debug("Error reading on connection : %s", err.Error())
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -227,7 +229,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
|
||||||
}
|
}
|
||||||
@@ -271,15 +278,21 @@ func sendResponse(con net.Conn, respMsg string, respCode int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// attempt is useless now. It was introduced to break after N attempts, but this is not implemented. Should it be?
|
||||||
func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
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 {
|
||||||
|
if strings.HasSuffix(err.Error(), "ldap: connection closed") || err == ldap.ErrNilConnection {
|
||||||
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 {
|
||||||
@@ -289,6 +302,7 @@ func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult,
|
|||||||
return searchLdap(searchReq, attempt)
|
return searchLdap(searchReq, attempt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,23 +311,32 @@ 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: %s", err)
|
logstream.Errorf("Error binding LDAP: %s", err)
|
||||||
return conLdap, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return conLdap, err
|
return conLdap, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : buildNetCache should have its own LDAP connection
|
func periodicallyUpdateCache() {
|
||||||
func periodicallyUpdateCache(conLdap *ldap.Conn) {
|
myLdap, err := connectLdap()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
// On initialise immediatement le cache
|
// On initialise immediatement le cache
|
||||||
buildNetCacheFromIPNetwork(conLdap)
|
buildNetCacheFromIPNetwork(myLdap)
|
||||||
|
myLdap.Close()
|
||||||
|
|
||||||
for range time.Tick(time.Second * time.Duration(*refreshInterval)) {
|
for range time.Tick(time.Second * time.Duration(*refreshInterval)) {
|
||||||
buildNetCacheFromIPNetwork(conLdap)
|
myLdap, err := connectLdap()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
buildNetCacheFromIPNetwork(myLdap)
|
||||||
|
myLdap.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,7 +350,7 @@ func run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go periodicallyUpdateCache(conLdap)
|
go periodicallyUpdateCache()
|
||||||
|
|
||||||
// Spawn a go routine for incoming connection
|
// Spawn a go routine for incoming connection
|
||||||
for {
|
for {
|
||||||
@@ -366,7 +389,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("MyNetTCPTable v.%s\n", version)
|
fmt.Printf("%s: MyNetTCPTable v.%s listening on %s\n", time.Now().Format(time.RFC3339), version, *listen)
|
||||||
|
|
||||||
logstream = logrus.New()
|
logstream = logrus.New()
|
||||||
level, err := logrus.ParseLevel(*logLevel)
|
level, err := logrus.ParseLevel(*logLevel)
|
||||||
|
|||||||
Reference in New Issue
Block a user