Use logrus, log to syslog, implement refresh and set loglevel remotely
This commit is contained in:
parent
351e068a2e
commit
563f90384d
2
go.mod
2
go.mod
@ -5,6 +5,7 @@ go 1.17
|
||||
require (
|
||||
github.com/go-ldap/ldap/v3 v3.4.3
|
||||
github.com/peterbourgon/ff v1.7.1
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/tabalt/pidfile v1.1.0
|
||||
)
|
||||
|
||||
@ -12,4 +13,5 @@ require (
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e // indirect
|
||||
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
|
||||
)
|
||||
|
9
go.sum
9
go.sum
@ -1,6 +1,7 @@
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e h1:ZU22z/2YRFLyf/P4ZwUYSdNCWsMEI0VeyrFoI2rAhJQ=
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A=
|
||||
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
|
||||
@ -10,13 +11,21 @@ github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUb
|
||||
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
|
||||
github.com/peterbourgon/ff v1.7.1 h1:xt1lxTG+Nr2+tFtysY7abFgPoH3Lug8CwYJMOmJRXhk=
|
||||
github.com/peterbourgon/ff v1.7.1/go.mod h1:fYI5YA+3RDqQRExmFbHnBjEeWzh9TrS8rnRpEq7XIg0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/tabalt/pidfile v1.1.0 h1:Q7qQGZ4MoAXE+rvM5tB4/eAIrawewYewByhMiPoDE50=
|
||||
github.com/tabalt/pidfile v1.1.0/go.mod h1:7F1QwNrjfAApsuX4Nyah3RsbHVAdY/D9qZWp0nnJ/Uw=
|
||||
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o=
|
||||
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
|
132
mynettcptable.go
132
mynettcptable.go
@ -10,7 +10,7 @@ import (
|
||||
"os"
|
||||
"fmt"
|
||||
"net"
|
||||
"log"
|
||||
// "log"
|
||||
"flag"
|
||||
"sync"
|
||||
"time"
|
||||
@ -21,17 +21,20 @@ import (
|
||||
"github.com/tabalt/pidfile"
|
||||
"github.com/go-ldap/ldap/v3"
|
||||
"github.com/peterbourgon/ff"
|
||||
"github.com/sirupsen/logrus"
|
||||
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
||||
)
|
||||
|
||||
const (
|
||||
version = "0.0.5"
|
||||
version = "0.0.7"
|
||||
)
|
||||
|
||||
var (
|
||||
logstream *syslog.Writer
|
||||
logstream *logrus.Logger
|
||||
conLdap *ldap.Conn
|
||||
mutex sync.Mutex
|
||||
debug *bool
|
||||
logTo *string
|
||||
logLevel *string
|
||||
listen *string
|
||||
ldapURL *string
|
||||
ldapBaseDN *string
|
||||
@ -71,7 +74,7 @@ func clearNetCache() {
|
||||
}
|
||||
n := netCache[i]
|
||||
if n.Present == false {
|
||||
logstream.Debug(fmt.Sprintf("Delete %s from netCache\n", n.Net.String()))
|
||||
logstream.Debugf("Delete %s from netCache\n", n.Net.String())
|
||||
// Nil the pointer to avoid memory leak
|
||||
n.Net = nil
|
||||
if i+1 < len(netCache) {
|
||||
@ -101,12 +104,10 @@ func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
|
||||
false, filter, []string{attribute}, []ldap.Control{})
|
||||
result, err := searchLdap(searchReq, 0)
|
||||
if err != nil {
|
||||
logstream.Err(fmt.Sprintf("Error searching %s into LDAP: %v\n", filter, err))
|
||||
logstream.Errorf("Error searching %s into LDAP: %v\n", filter, err)
|
||||
return err
|
||||
}
|
||||
if *debug {
|
||||
logstream.Debug(fmt.Sprintf("Received %d results to ipNetwork query\n", len(result.Entries)))
|
||||
}
|
||||
logstream.Debugf("Received %d results to ipNetwork query\n", len(result.Entries))
|
||||
|
||||
// First flag off all elements of netCache
|
||||
unsetNetCachePresentFlag()
|
||||
@ -134,9 +135,9 @@ func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
|
||||
// Finally delete items not previously accessed
|
||||
clearNetCache()
|
||||
|
||||
logstream.Err("Dump netcache:")
|
||||
logstream.Debug("Dump netcache:")
|
||||
for _, n := range netCache {
|
||||
logstream.Err(fmt.Sprintf("%s", n.Net.String()))
|
||||
logstream.Debugf("%s\n", n.Net.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -180,33 +181,49 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
||||
// 10/05/2022 : Drop this conn if client closed connection or timeout occured
|
||||
// Dont notice if client closed connection
|
||||
if err.Error() != "EOF" && !strings.HasSuffix(err.Error(), "i/o timeout") {
|
||||
logstream.Err(fmt.Sprintf("Error reading connection: %v\n", err.Error()))
|
||||
logstream.Errorf("Error reading connection: %v\n", err.Error())
|
||||
}
|
||||
//sendResponse(connClt, err.Error(), 500)
|
||||
//continue
|
||||
return
|
||||
}
|
||||
|
||||
logstream.Debug(fmt.Sprintf("Read: %s\n", string(buf[:readlen-1])))
|
||||
logstream.Debug(fmt.Sprintf("Received: %s\n", string(buf[:readlen-1])))
|
||||
|
||||
if strings.EqualFold(string(buf[:readlen-1]), "quit") {
|
||||
if *debug {
|
||||
logstream.Debug("Received quit instruction, buhbye")
|
||||
}
|
||||
logstream.Infof("Received \"quit\" instruction from %s, closing connection to the client", connClt.RemoteAddr().String())
|
||||
connClt.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// "refresh" sent on listening port will refresh netCache from LDAP
|
||||
if strings.EqualFold(string(buf[:readlen-1]), "refresh") {
|
||||
if *debug {
|
||||
logstream.Debug("Received refresh instruction, refreshing netCache")
|
||||
}
|
||||
// "set refresh" sent on listening port will refresh netCache from LDAP
|
||||
if strings.EqualFold(string(buf[:readlen-1]), "set refresh") {
|
||||
logstream.Infof("Received \"set refresh\" instruction from %s, refreshing netCache", connClt.RemoteAddr().String())
|
||||
buildNetCacheFromIPNetwork(conLdap)
|
||||
sendResponse(connClt, "Refreshing cache", 200)
|
||||
continue
|
||||
}
|
||||
|
||||
// "get loglevel" sent on listening port will return current loglevel
|
||||
if strings.EqualFold(string(buf[:readlen-1]), "get loglevel") {
|
||||
logstream.Infof("Received \"get loglevel\" instruction from %s", connClt.RemoteAddr().String())
|
||||
sendResponse(connClt, logstream.Level.String(), 200)
|
||||
continue
|
||||
}
|
||||
|
||||
// "set loglevel level" sent on listening port will set current loglevel
|
||||
if readlen > 14 && strings.EqualFold(string(buf[:12]), "set loglevel") {
|
||||
logstream.Infof("Received \"%s\" instruction from %s", string(buf[:readlen-1]), connClt.RemoteAddr().String())
|
||||
level, err := logrus.ParseLevel(string(buf[13:readlen-1]))
|
||||
if err != nil {
|
||||
sendResponse(connClt, fmt.Sprintf("Invalid log level: %s", string(buf[13:readlen-1])), 500)
|
||||
} else {
|
||||
logstream.Level = level
|
||||
sendResponse(connClt, "loglevel set", 200)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Get IP sent by postfix
|
||||
// tcp_table request is formated like "get the_ip\n"
|
||||
if false == strings.HasPrefix(string(buf[:readlen-1]), "get ") {
|
||||
@ -218,7 +235,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
||||
// First query netCache built with ipNetworkNumber
|
||||
res, err := isIPContainedInNetCache(ip)
|
||||
if err != nil {
|
||||
logstream.Err(err.Error())
|
||||
logstream.Error(err.Error())
|
||||
sendResponse(connClt, err.Error(), 500)
|
||||
continue
|
||||
}
|
||||
@ -235,7 +252,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
||||
false, filter, []string{"ipHostNumber"}, []ldap.Control{})
|
||||
result, err := searchLdap(searchReq, 0)
|
||||
if err != nil {
|
||||
logstream.Err(fmt.Sprintf("Error searching into LDAP: %v\n", err))
|
||||
logstream.Errorf("Error searching into LDAP: %v\n", err)
|
||||
sendResponse(connClt, err.Error(), 500)
|
||||
continue
|
||||
}
|
||||
@ -245,7 +262,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
||||
continue
|
||||
}
|
||||
if len(result.Entries) > 1 {
|
||||
logstream.Info(fmt.Sprintf("More than one match for IP %s", ip))
|
||||
logstream.Infof("More than one match for IP %s", ip)
|
||||
}
|
||||
|
||||
sendResponse(connClt, ip, 200)
|
||||
@ -268,7 +285,7 @@ func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult,
|
||||
mutex.Unlock()
|
||||
// Let's just manage connection errors here
|
||||
if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") {
|
||||
logstream.Err("LDAP connection closed, retrying")
|
||||
logstream.Error("LDAP connection closed, retrying")
|
||||
mutex.Lock()
|
||||
conLdap.Close()
|
||||
conLdap, err = connectLdap()
|
||||
@ -287,12 +304,12 @@ func connectLdap() (*ldap.Conn, error) {
|
||||
var err error
|
||||
conLdap, err = ldap.DialURL(*ldapURL)
|
||||
if err != nil {
|
||||
logstream.Err(fmt.Sprintf("Error dialing LDAP on %s: %v\n", *ldapURL, err))
|
||||
logstream.Errorf("Error dialing LDAP on %s: %v\n", *ldapURL, err)
|
||||
return conLdap, err
|
||||
}
|
||||
err = conLdap.Bind(*ldapUser, *ldapPass)
|
||||
if err != nil {
|
||||
logstream.Err(fmt.Sprintln("Error binding LDAP: ", err))
|
||||
logstream.Errorf("Error binding LDAP: ", err)
|
||||
return conLdap, err
|
||||
}
|
||||
return conLdap, err
|
||||
@ -309,11 +326,9 @@ func periodicallyUpdateCache(conLdap *ldap.Conn) {
|
||||
}
|
||||
|
||||
func run() {
|
||||
logstream.Info("start")
|
||||
defer logstream.Info("exit")
|
||||
listener, err := net.Listen("tcp", *listen)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Error listening on %s: %v\n", *listen, err))
|
||||
logstream.Fatalf("Error listening on %s: %v\n", *listen, err)
|
||||
}
|
||||
conLdap, err := connectLdap()
|
||||
if err != nil {
|
||||
@ -326,7 +341,7 @@ func run() {
|
||||
for {
|
||||
connClt, err := listener.Accept()
|
||||
if err != nil {
|
||||
logstream.Err(fmt.Sprintln("Error accepting: ", err))
|
||||
logstream.Errorf("Error accepting: ", err)
|
||||
}
|
||||
go handleConnection(connClt, conLdap)
|
||||
}
|
||||
@ -334,11 +349,10 @@ func run() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
var e error
|
||||
|
||||
fs := flag.NewFlagSet("mxrouter", flag.ExitOnError)
|
||||
listen = fs.String("listen-addr", "127.0.0.1:8080", "listen address for server (also via LISTEN env var)")
|
||||
debug = fs.Bool("debug", false, "log debug information (also via DEBUG env var)")
|
||||
logTo = fs.String("logTo", "syslog", "Where to output logs. Valid values are \"stdout\", \"syslog\"")
|
||||
logLevel = fs.String("logLevel", "warning", "Log level. Valid values are \"fatal\", \"error\", \"warn\", \"info\", \"debug\"")
|
||||
ldapURL = fs.String("ldap", "", "LDAP Server URL (also via LDAP 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)")
|
||||
@ -361,24 +375,50 @@ func main() {
|
||||
|
||||
fmt.Printf("MyNetTCPTable v.%s\n", version)
|
||||
|
||||
logstream = logrus.New()
|
||||
level, err := logrus.ParseLevel(*logLevel)
|
||||
if err != nil {
|
||||
fmt.Printf("Invalid log level: %s\n", *logLevel)
|
||||
os.Exit(-1)
|
||||
} else {
|
||||
logstream.Level = level
|
||||
}
|
||||
|
||||
if strings.EqualFold(*logTo, "stdout") {
|
||||
logstream.Out = os.Stdout
|
||||
}
|
||||
if strings.EqualFold(*logTo, "syslog") {
|
||||
// level != priority
|
||||
prio := syslog.LOG_MAIL
|
||||
switch (*logLevel) {
|
||||
case "fatal":
|
||||
prio += syslog.LOG_CRIT
|
||||
case "error":
|
||||
prio += syslog.LOG_ERR
|
||||
case "warn":
|
||||
prio += syslog.LOG_WARNING
|
||||
case "info":
|
||||
prio += syslog.LOG_INFO
|
||||
case "debug":
|
||||
prio += syslog.LOG_DEBUG
|
||||
}
|
||||
hook, err := lSyslog.NewSyslogHook("", "", prio, "mynettcptable")
|
||||
if err != nil {
|
||||
fmt.Printf("Error opening syslog: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logstream.Hooks.Add(hook)
|
||||
}
|
||||
|
||||
if pid, err := pidfile.Create(*pidFilePath); err != nil {
|
||||
log.Fatal(err)
|
||||
logstream.Fatal(err)
|
||||
} else {
|
||||
defer pid.Clear()
|
||||
}
|
||||
|
||||
flags := syslog.LOG_MAIL
|
||||
if *debug {
|
||||
fmt.Printf("Set logging level to DEBUG\n")
|
||||
flags = syslog.LOG_MAIL|syslog.LOG_DEBUG
|
||||
}
|
||||
if logstream, e = syslog.New(flags, "mynettcptable"); e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
//defer logstream.Close()
|
||||
|
||||
defer logstream.Close()
|
||||
|
||||
fmt.Printf("Start listening for incoming connections on %s\n", *listen)
|
||||
logstream.Infof("Start listening for incoming connections on %s\n", *listen)
|
||||
run()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user