diff --git a/README.md b/README.md index 409f7d9..d808194 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,11 @@ squid-auth-healthcheck --proxy-addr 127.0.0.1 --proxy-username test@TEST.LOCAL - ``` Application Options: -u, --url= url to check for availability (required) - --auth-type= type of used proxy authentication mechanism. [ntlm, kerberos] (required) + --auth-type= type of used proxy authentication mechanism. [ntlm, kerberos, no] (required) --proxy-addr= proxy server address (required) --proxy-port= proxy server port (default: 3128) (default: 3128) - --proxy-username= proxy user login (required) - --proxy-password= proxy user password (required) + --proxy-username= proxy user login + --proxy-password= proxy user password --timeout= healthcheck connection timeout in seconds (default: 2) (default: 2) --strict-url the check returns a positive result only if all URLs are available --strict-auth the check returns a positive result only if url are available with all auth method diff --git a/app/checker/noauth.go b/app/checker/noauth.go new file mode 100644 index 0000000..5956936 --- /dev/null +++ b/app/checker/noauth.go @@ -0,0 +1,56 @@ +package checker + +import ( + "sync" + + curl "github.com/andelf/go-curl" +) + +type AuthNo struct { + authType string + ProxyAddr string + ProxyPort int + ConnectionTimeout int +} + +func NewAuthNo(ProxyAddr string, ProxyPort int, ConnectionTimeout int) *AuthNo { + var a AuthNo + a.authType = "no" + a.ProxyAddr = ProxyAddr + a.ProxyPort = ProxyPort + a.ConnectionTimeout = ConnectionTimeout + return &a +} + +func (a *AuthNo) Check(urls []string, ch chan HealthResponse, wg *sync.WaitGroup) { + var innerWg sync.WaitGroup + innerWg.Add(len(urls)) + for _, url := range urls { + go func(u string) { + conn := curl.EasyInit() + conn.Setopt(curl.OPT_VERBOSE, 0) + conn.Setopt(curl.OPT_FOLLOWLOCATION, 1) + conn.Setopt(curl.OPT_PROXYTYPE, curl.PROXY_HTTP) + conn.Setopt(curl.OPT_PROXY, a.ProxyAddr) + conn.Setopt(curl.OPT_PROXYPORT, a.ProxyPort) + conn.Setopt(curl.OPT_TIMEOUT, a.ConnectionTimeout) + conn.Setopt(curl.OPT_WRITEFUNCTION, nullHandler) + conn.Setopt(curl.OPT_URL, u) + if err := conn.Perform(); err != nil { + ch <- HealthResponse{u, a.authType, 0, 0} + } else { + code, _ := conn.Getinfo(curl.INFO_RESPONSE_CODE) + responseTime, _ := conn.Getinfo(curl.INFO_TOTAL_TIME) + if code.(int) == 200 { + ch <- HealthResponse{u, a.authType, 1, responseTime.(float64)} + } else { + ch <- HealthResponse{u, a.authType, 0, responseTime.(float64)} + } + } + conn.Cleanup() + innerWg.Done() + }(url) + } + innerWg.Wait() + wg.Done() +} diff --git a/app/main.go b/app/main.go index d1a1e9d..a20d36b 100644 --- a/app/main.go +++ b/app/main.go @@ -12,22 +12,22 @@ import ( ) const ( - version = "0.0.1" + version = "0.0.2" ) var opts struct { URL []string `short:"u" long:"url" description:"url to check for availability (required)" required:"true"` - AuthType []string `long:"auth-type" description:"type of used proxy authentication mechanism. [ntlm, kerberos] (required)" required:"true"` + AuthType []string `long:"auth-type" description:"type of used proxy authentication mechanism. [ntlm, kerberos, no] (required)" required:"true"` ProxyAddr string `long:"proxy-addr" description:"proxy server address (required)" required:"true"` ProxyPort int `long:"proxy-port" description:"proxy server port (default: 3128)" default:"3128"` - ProxyUsername string `long:"proxy-username" description:"proxy user login (required)" required:"true"` - ProxyPassword string `long:"proxy-password" description:"proxy user password (required)" required:"true"` + ProxyUsername string `long:"proxy-username" description:"proxy user login"` + ProxyPassword string `long:"proxy-password" description:"proxy user password"` ConnectionTimeout int `long:"timeout" description:"healthcheck connection timeout in seconds (default: 2)" default:"2"` StrictURL bool `long:"strict-url" description:"the check returns a positive result only if all URLs are available"` StrictAuth bool `long:"strict-auth" description:"the check returns a positive result only if url are available with all auth method"` Verbose bool `short:"v" long:"verbose" description:"output verbose healthcheck information"` } -var allowAuthType = [2]string{"ntlm", "kerberos"} +var allowAuthType = [3]string{"ntlm", "kerberos", "no"} func exitOK(verbose bool) { if !verbose { @@ -72,20 +72,24 @@ func main() { var wg sync.WaitGroup ch := make(chan checker.HealthResponse, len(opts.AuthType)*len(opts.URL)) - var ntlm checker.Interface = checker.NewAuthNTLM(opts.ProxyAddr, opts.ProxyPort, opts.ProxyUsername, opts.ProxyPassword, opts.ConnectionTimeout) - kerberos, err := checker.NewAuthKerberos(opts.ProxyAddr, opts.ProxyPort, opts.ProxyUsername, opts.ProxyPassword, opts.ConnectionTimeout) - if err != nil { - fmt.Println(err) - os.Exit(1) - } wg.Add(len(opts.AuthType)) if slice.StringInSlice("ntlm", opts.AuthType) { + var ntlm checker.Interface = checker.NewAuthNTLM(opts.ProxyAddr, opts.ProxyPort, opts.ProxyUsername, opts.ProxyPassword, opts.ConnectionTimeout) go ntlm.Check(opts.URL, ch, &wg) } if slice.StringInSlice("kerberos", opts.AuthType) { + kerberos, err := checker.NewAuthKerberos(opts.ProxyAddr, opts.ProxyPort, opts.ProxyUsername, opts.ProxyPassword, opts.ConnectionTimeout) + if err != nil { + fmt.Println(err) + os.Exit(1) + } go kerberos.Check(opts.URL, ch, &wg) } + if slice.StringInSlice("no", opts.AuthType) { + var no checker.Interface = checker.NewAuthNo(opts.ProxyAddr, opts.ProxyPort, opts.ConnectionTimeout) + go no.Check(opts.URL, ch, &wg) + } wg.Wait() close(ch)