diff --git a/core/config.go b/core/config.go index 14f1a81..8eefea0 100644 --- a/core/config.go +++ b/core/config.go @@ -14,6 +14,8 @@ type Config struct { IconSrc string Database string UnixSocket string + BasicAuthUsername string + BasicAuthPassword string Service []scanner.ServiceConfig Matrix MatrixConfig } diff --git a/main.go b/main.go index 8a946e1..065dfe0 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func index(w http.ResponseWriter, r *http.Request) { timeout := 3 * time.Second ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() - data := scanner.CheckServiceBatch(ctx, config.Service) + data := scanner.CheckServiceBatch(ctx, config.Service, config.BasicAuthUsername, config.BasicAuthPassword) non_duplicates, err := dbm.StoreServiceStatusBatch(data, time.Now()) if err != nil { http.Error(w, err.Error(), 500) @@ -232,7 +232,7 @@ func routineCheck(timeout time.Duration) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() - data := scanner.CheckServiceBatch(ctx, config.Service) + data := scanner.CheckServiceBatch(ctx, config.Service, config.BasicAuthUsername, config.BasicAuthPassword) non_duplicates, err := dbm.StoreServiceStatusBatch(data, time.Now()) if err != nil { return err diff --git a/proto/config.toml.in b/proto/config.toml.in index 362c305..b4b2ae5 100644 --- a/proto/config.toml.in +++ b/proto/config.toml.in @@ -1,6 +1,8 @@ siteName = "Status Page" iconSrc = "https://exozy.me/img/logo.svg" # unixSocket = "/srv/http/status" +# basicAuthUsername = "user" +# basicAuthPassword = "pass" # database = "/opt/status-config/db" database = "/tmp/exozyme-status-test-db" @@ -9,7 +11,7 @@ service = [ { description = "drgns.space", url = "https://drgns.space" }, { description = "invalid.example.com", url = "https://invalid.example.com" }, { description = "port 1234", url = "tcp://localhost:1234" }, - { description = "basic auth", url = "https://some-service.exozy.me/", user = "user", pass = "pass" }, + { description = "basic auth", url = "https://some-service.exozy.me/", user = "overrideUser", pass = "overridePass" }, ] [matrix] diff --git a/scanner/httpSuccess.go b/scanner/httpSuccess.go index aabdbeb..e433d33 100644 --- a/scanner/httpSuccess.go +++ b/scanner/httpSuccess.go @@ -6,14 +6,12 @@ import ( "net/http" ) -func CheckHTTP200(ctx Context, url string, basicAuthUsername string, basicAuthPassword string) (bool, string) { +func CheckHTTP200(ctx Context, url, basicAuthUsername string, basicAuthPassword string) (bool, string) { req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return false, err.Error() } - if basicAuthUsername != "" || basicAuthPassword != "" { - req.SetBasicAuth(basicAuthUsername, basicAuthPassword) - } + req.SetBasicAuth(basicAuthUsername, basicAuthPassword) resp, err := http.DefaultClient.Do(req) if err != nil { var tlsCertErr x509.CertificateInvalidError diff --git a/scanner/index.go b/scanner/index.go index f6a525f..80127af 100644 --- a/scanner/index.go +++ b/scanner/index.go @@ -32,14 +32,18 @@ type ServiceStatus struct { Status string `json:"status"` } -func CheckService(service ServiceConfig, ctx context.Context) ServiceStatus { +func CheckService(service ServiceConfig, ctx context.Context, basicAuthUsername, basicAuthPassword string) ServiceStatus { var serviceResult bool var status string url := service.URL() switch url.Scheme { case "http", "https": - serviceResult, status = CheckHTTP200(ctx, url.String(), service.User, service.Pass) + if service.User != "" && service.Pass != "" { + serviceResult, status = CheckHTTP200(ctx, url.String(), service.User, service.Pass) + } else { + serviceResult, status = CheckHTTP200(ctx, url.String(), basicAuthUsername, basicAuthPassword) + } case "tcp": // go includes port in host (url.Host is actually "authority") serviceResult, status = CheckTCPOpen(ctx, url.Host) @@ -61,7 +65,7 @@ func CheckService(service ServiceConfig, ctx context.Context) ServiceStatus { type ServiceStatusBatch []ServiceStatus -func CheckServiceBatch(ctx Context, services []ServiceConfig) ServiceStatusBatch { +func CheckServiceBatch(ctx Context, services []ServiceConfig, basicAuthUsername, basicAuthPassword string) ServiceStatusBatch { var wg sync.WaitGroup var data []ServiceStatus = make([]ServiceStatus, len(services)) @@ -70,7 +74,7 @@ func CheckServiceBatch(ctx Context, services []ServiceConfig) ServiceStatusBatch wg.Add(1) go func(i int, config ServiceConfig) { defer wg.Done() - data[i] = CheckService(config, ctx) + data[i] = CheckService(config, ctx, basicAuthUsername, basicAuthPassword) }(i, v) } wg.Wait()