go fmt
This commit is contained in:
parent
3267a19888
commit
b9807f0ba7
@ -7,22 +7,22 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
// "log"
|
"os"
|
||||||
|
// "log"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
|
"log/syslog"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"log/syslog"
|
|
||||||
|
|
||||||
"github.com/tabalt/pidfile"
|
|
||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
"github.com/peterbourgon/ff"
|
"github.com/peterbourgon/ff"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
||||||
|
"github.com/tabalt/pidfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -30,26 +30,26 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logstream *logrus.Logger
|
logstream *logrus.Logger
|
||||||
conLdap *ldap.Conn
|
conLdap *ldap.Conn
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
logTo *string
|
logTo *string
|
||||||
logLevel *string
|
logLevel *string
|
||||||
listen *string
|
listen *string
|
||||||
ldapURL *string
|
ldapURL *string
|
||||||
ldapBaseDN *string
|
ldapBaseDN *string
|
||||||
ldapUser *string
|
ldapUser *string
|
||||||
ldapPass *string
|
ldapPass *string
|
||||||
refreshInterval *int
|
refreshInterval *int
|
||||||
pidFilePath *string
|
pidFilePath *string
|
||||||
timeout *int
|
timeout *int
|
||||||
|
|
||||||
netCache []NoAuthNet
|
netCache []NoAuthNet
|
||||||
)
|
)
|
||||||
|
|
||||||
type NoAuthNet struct {
|
type NoAuthNet struct {
|
||||||
Net *net.IPNet
|
Net *net.IPNet
|
||||||
Present bool
|
Present bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if a net is present in cache, and set "Present" flag to true
|
// Test if a net is present in cache, and set "Present" flag to true
|
||||||
@ -95,7 +95,6 @@ func unsetNetCachePresentFlag() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
|
func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
|
||||||
attribute := "ipNetworkNumber"
|
attribute := "ipNetworkNumber"
|
||||||
|
|
||||||
@ -117,17 +116,13 @@ func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
|
|||||||
logstream.Info(fmt.Sprintf("Error searching into LDAP: Attribute %s not found for entry %s\n", attribute, r))
|
logstream.Info(fmt.Sprintf("Error searching into LDAP: Attribute %s not found for entry %s\n", attribute, r))
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
// Explode the network to individual IPs
|
|
||||||
// 1: Verify format : Either CIDR, or netmask is in ipNetworkMask (do we want to support this?)
|
|
||||||
// 2: n := iplib.NewNet4(net.ParseIP("192.168.0.0"), 16)
|
|
||||||
// n.Enumerate(
|
|
||||||
_, ipnet, err := net.ParseCIDR(r.Attributes[0].Values[0])
|
_, ipnet, err := net.ParseCIDR(r.Attributes[0].Values[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logstream.Info(err.Error())
|
logstream.Info(err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if false == checkNetCacheContainsAndFlag(ipnet) {
|
if false == checkNetCacheContainsAndFlag(ipnet) {
|
||||||
netCache = append(netCache, NoAuthNet{ Net: ipnet, Present: true })
|
netCache = append(netCache, NoAuthNet{Net: ipnet, Present: true})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -143,7 +138,6 @@ func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func isIPContainedInNetCache(string_ip string) (bool, error) {
|
func isIPContainedInNetCache(string_ip string) (bool, error) {
|
||||||
ip := net.ParseIP(string_ip)
|
ip := net.ParseIP(string_ip)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
@ -214,11 +208,11 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
// "set loglevel level" sent on listening port will set current loglevel
|
// "set loglevel level" sent on listening port will set current loglevel
|
||||||
if readlen > 14 && strings.EqualFold(string(buf[:12]), "set 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())
|
logstream.Infof("Received \"%s\" instruction from %s", string(buf[:readlen-1]), connClt.RemoteAddr().String())
|
||||||
level, err := logrus.ParseLevel(string(buf[13:readlen-1]))
|
level, err := logrus.ParseLevel(string(buf[13 : readlen-1]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendResponse(connClt, fmt.Sprintf("Invalid log level: %s", string(buf[13:readlen-1])), 500)
|
sendResponse(connClt, fmt.Sprintf("Invalid log level: %s", string(buf[13:readlen-1])), 500)
|
||||||
} else {
|
} else {
|
||||||
logstream.Level = level
|
logstream.Level = level
|
||||||
sendResponse(connClt, "loglevel set", 200)
|
sendResponse(connClt, "loglevel set", 200)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@ -230,7 +224,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
sendResponse(connClt, fmt.Sprintf("Invalid request: %s", buf[:readlen-1]), 500)
|
sendResponse(connClt, fmt.Sprintf("Invalid request: %s", buf[:readlen-1]), 500)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ip := string(buf[4:readlen-1])
|
ip := string(buf[4 : readlen-1])
|
||||||
|
|
||||||
// First query netCache built with ipNetworkNumber
|
// First query netCache built with ipNetworkNumber
|
||||||
res, err := isIPContainedInNetCache(ip)
|
res, err := isIPContainedInNetCache(ip)
|
||||||
@ -391,17 +385,17 @@ func main() {
|
|||||||
if strings.EqualFold(*logTo, "syslog") {
|
if strings.EqualFold(*logTo, "syslog") {
|
||||||
// level != priority
|
// level != priority
|
||||||
prio := syslog.LOG_MAIL
|
prio := syslog.LOG_MAIL
|
||||||
switch (*logLevel) {
|
switch *logLevel {
|
||||||
case "fatal":
|
case "fatal":
|
||||||
prio += syslog.LOG_CRIT
|
prio += syslog.LOG_CRIT
|
||||||
case "error":
|
case "error":
|
||||||
prio += syslog.LOG_ERR
|
prio += syslog.LOG_ERR
|
||||||
case "warn":
|
case "warn":
|
||||||
prio += syslog.LOG_WARNING
|
prio += syslog.LOG_WARNING
|
||||||
case "info":
|
case "info":
|
||||||
prio += syslog.LOG_INFO
|
prio += syslog.LOG_INFO
|
||||||
case "debug":
|
case "debug":
|
||||||
prio += syslog.LOG_DEBUG
|
prio += syslog.LOG_DEBUG
|
||||||
}
|
}
|
||||||
hook, err := lSyslog.NewSyslogHook("", "", prio, "mynettcptable")
|
hook, err := lSyslog.NewSyslogHook("", "", prio, "mynettcptable")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -422,4 +416,3 @@ func main() {
|
|||||||
logstream.Infof("Start listening for incoming connections on %s\n", *listen)
|
logstream.Infof("Start listening for incoming connections on %s\n", *listen)
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user