Skip to content

Commit

Permalink
Add checking without authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
verdel committed Jan 11, 2019
1 parent 80bae0d commit 5004e55
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ squid-auth-healthcheck --proxy-addr 127.0.0.1 --proxy-username [email protected] -
```
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
Expand Down
56 changes: 56 additions & 0 deletions app/checker/noauth.go
Original file line number Diff line number Diff line change
@@ -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()
}
26 changes: 15 additions & 11 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5004e55

Please sign in to comment.