Clean up service scanner module

This commit is contained in:
Locria Cyber 2023-09-30 16:56:37 +00:00
parent f9f729180c
commit 4adf19a16b
Signed by: iacore
GPG key ID: ED0D424AE4406330
7 changed files with 94 additions and 65 deletions

15
main.go
View file

@ -1,11 +1,11 @@
package main
import (
"log"
"fmt"
"log"
"net/http"
"git.exozy.me/exozyme/status/proto"
"git.exozy.me/exozyme/status/scanner"
"github.com/cbroglie/mustache"
)
@ -25,10 +25,17 @@ func parseAndRenderTemplate(templateLocation string, data interface{}) (string,
return res, nil
}
var config scanner.Config = scanner.LoadConfig()
func rootPage(w http.ResponseWriter, r *http.Request) {
// get service status'
data := proto.TestServices()
var data []scanner.ServiceStatus
for _, v := range config.Service {
data=append(data, scanner.TestService(v))
}
log.Printf("%v", data)
parsedTemplate, err := parseAndRenderTemplate("templates/index.html.mustache", data)
if err != nil {
http.Error(w, err.Error(), 500)

View file

@ -1,58 +0,0 @@
package proto
import (
"net/url"
)
func TestServices() []ServiceStatus {
services := []Service{
{
Name: "exozy.me",
Type: "http",
Url: url.URL{Scheme: "https", Host: "exozy.me"},
},
{
Name: "drgns.space",
Type: "http",
Url: url.URL{Scheme: "https", Host: "drgns.space"},
},
{
Name: "port 1234",
Type: "tcp",
Url: url.URL{Scheme: "tcp://", Host: "localhost:1234"},
},
}
results := []ServiceStatus{}
for _, service := range services {
var serviceResult bool
// Default N/A
var serviceStatus string = "N/A"
switch service.Type {
case "http":
{
serviceResult, serviceStatus = Request(service.Url.String())
break
}
case "tcp":
{
// go includes port in host
serviceResult, serviceStatus = CheckTCPOpen(service.Url.Host)
break
}
}
result := ServiceStatus{
Name: service.Name,
Ok: serviceResult,
Status: serviceStatus,
}
results = append(results, result)
}
return results
}

View file

@ -1,4 +1,4 @@
package proto
package scanner
import "net/url"

49
scanner/config.go Normal file
View file

@ -0,0 +1,49 @@
package scanner
import "net/url"
type Config struct {
Service []ServiceConfig
}
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
}
func LoadConfig() Config {
// todo: make this load from toml
services := []ServiceConfig{
{
description: "exozy.me",
Url: "https://exozy.me",
},
{
description: "drgns.space",
Url: "https://drgns.space",
},
{
description: "invalid.example.com",
Url: "https://invalid.example.com",
},
{
description: "port 1234",
Url: "tcp://localhost:1234",
},
}
return Config {Service: services}
}
type ServiceStatus struct {
Name string
Ok bool
// Status string
}

View file

@ -1,4 +1,4 @@
package proto
package scanner
import "net/http"

31
scanner/index.go Normal file
View file

@ -0,0 +1,31 @@
package scanner
import "log"
func TestService(service ServiceConfig) ServiceStatus {
var serviceResult bool
url := service.URL()
switch url.Scheme {
case "http", "https":
{
serviceResult = Request(url.String())
break
}
case "tcp":
{
// go includes port in host
serviceResult = CheckTCPOpen(url.Host)
break
}
default:
log.Panicf("I can't handle service with scheme: %#v", url.Scheme)
break
}
return ServiceStatus{
Name: service.description,
Ok: serviceResult,
}
}

View file

@ -1,4 +1,4 @@
package proto
package scanner
import (
"net"