malscan/rest.go

66 lines
1.4 KiB
Go
Raw Normal View History

2023-04-06 09:35:59 +00:00
package main
2023-04-07 11:57:58 +00:00
2023-04-07 12:22:47 +00:00
import (
"net/http"
"github.com/gin-gonic/gin"
)
2023-04-07 11:57:58 +00:00
type status_response struct {
2023-04-07 12:22:47 +00:00
Scanning_engine string `json:"scanning_engine"`
Signature_count int `json:"signature_count"`
Sanity_check bool `json:"sanity_check"`
2023-04-07 11:57:58 +00:00
}
type scan_response struct {
2023-04-07 12:22:47 +00:00
Malware_detected bool `json:"malware_detected"`
Malware_name string `json:"malware_name"`
Engine status_response `json:"engine"`
2023-04-07 11:57:58 +00:00
}
func scan_api(c *gin.Context) {
data, err := c.GetRawData()
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
malware_type, err, sigCount := scan_data(data)
malware_detected := true
if malware_type == "" {
malware_detected = false
}
response := scan_response{
Malware_detected: malware_detected,
Malware_name: malware_type,
Engine: status_response{
Signature_count: sigCount,
Sanity_check: true,
Scanning_engine: "clamav",
},
}
2023-04-07 11:57:58 +00:00
c.IndentedJSON(http.StatusOK, response)
2023-04-07 11:57:58 +00:00
}
func status_api(c *gin.Context) {
2023-04-07 12:22:47 +00:00
scannerIsSane, sigCount := sanity_check()
responseData := status_response{
Sanity_check: scannerIsSane,
Signature_count: sigCount,
Scanning_engine: "clamav",
}
c.IndentedJSON(http.StatusOK, responseData)
2023-04-07 11:57:58 +00:00
}
func start_api() {
2023-04-07 12:22:47 +00:00
gin.SetMode(gin.ReleaseMode)
2023-04-07 11:57:58 +00:00
router := gin.Default()
router.POST("/scan", scan_api)
2023-04-07 11:57:58 +00:00
router.GET("/status", status_api)
router.Run(":8080")
}