status/scanner/index.go
2023-10-03 10:40:41 +00:00

84 lines
1.3 KiB
Go

package scanner
import (
"log"
"net/url"
"sync"
)
type ServiceConfig struct {
Description string
Url string
}
func (s ServiceConfig) URL() *url.URL {
res, err := url.Parse(s.Url)
if err != nil {
panic(err)
}
return res
}
type ServiceStatus struct {
Name string
Url string
Type string
Ok bool
Status string
}
func TestService(service ServiceConfig) ServiceStatus {
var serviceResult bool
var status string
url := service.URL()
switch url.Scheme {
case "http", "https":
{
serviceResult, status = Request(url.String())
break
}
case "tcp":
{
// go includes port in host
serviceResult, status = CheckTCPOpen(url.Host)
break
}
case "icmp":
{
// go includes port in host
serviceResult, status = CheckICMPUp(url.Host)
break
}
default:
log.Panicf("I can't handle service with scheme: %#v", url.Scheme)
break
}
return ServiceStatus{
Name: service.Description,
Type: url.Scheme,
Url: service.Url,
Ok: serviceResult,
Status: status,
}
}
func TestMultipleServices(services []ServiceConfig) []ServiceStatus {
var wg sync.WaitGroup
var data []ServiceStatus = make([]ServiceStatus, len(services))
for i, v := range services {
wg.Add(1)
go func(i int, config ServiceConfig) {
defer wg.Done()
data[i] = TestService(config)
}(i, v)
}
wg.Wait()
return data
}