diec and refractoring
This commit is contained in:
		
							parent
							
								
									a62157e8e5
								
							
						
					
					
						commit
						0d715ccb37
					
				
					 20 changed files with 355 additions and 200 deletions
				
			
		| 
						 | 
				
			
			@ -35,7 +35,6 @@ func CreateFile(ctx context.Context, name string, fileBytes []byte) (sqlc.File,
 | 
			
		|||
		err = nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	//Using UUIDs instead of the file hash to make switching storage backends easier
 | 
			
		||||
	_, err = store.SaveFile(file.ID.String(), fileBytes)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,7 +11,6 @@ import (
 | 
			
		|||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
var pool *pgxpool.Pool
 | 
			
		||||
 | 
			
		||||
func Connect() (*pgxpool.Pool, error) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,3 +28,20 @@ func InsertFileProperties(properties sqlc.InsertFilePropertiesParams) error  {
 | 
			
		|||
	}
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// InsertJsonResult() into one of the following tables:
 | 
			
		||||
// diec, msoffice_mraptor, msoffice_oleid, msoffice_olevba
 | 
			
		||||
func InsertJsonResult(fileID pgtype.UUID, data []byte, table string) error {
 | 
			
		||||
	query := sqlc.New(pool)
 | 
			
		||||
	var err error
 | 
			
		||||
	switch table {
 | 
			
		||||
	case "diec":
 | 
			
		||||
		err = query.InsertFileDIEC(context.Background(), sqlc.InsertFileDIECParams{FileID: fileID, Data: data})
 | 
			
		||||
	case "msoffice_oleid":
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Unable to insert DIEC results", "file-uuid", fileID.String(), "error", err)
 | 
			
		||||
	}
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,4 +3,27 @@ INSERT INTO file_properties (
 | 
			
		|||
  id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
			
		||||
) VALUES ($1, $2, $3, $4, $5, $6);
 | 
			
		||||
 | 
			
		||||
-- name: GetFileProperties :one
 | 
			
		||||
SELECT * FROM file_properties
 | 
			
		||||
WHERE id = $1;
 | 
			
		||||
 | 
			
		||||
-- name: InsertFileDIEC :exec
 | 
			
		||||
INSERT INTO diec (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2);
 | 
			
		||||
 | 
			
		||||
-- name: InsertFileMsofficeOleid :exec
 | 
			
		||||
INSERT INTO msoffice_oleid (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2);
 | 
			
		||||
 | 
			
		||||
-- name: InsertFileMsofficeOlevba :exec
 | 
			
		||||
INSERT INTO msoffice_olevba (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2);
 | 
			
		||||
 | 
			
		||||
-- name: InsertFileMsofficeMraptor :exec
 | 
			
		||||
INSERT INTO msoffice_mraptor (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,6 +21,7 @@ func BasicProcessing(job sqlc.ProcessingJob) error {
 | 
			
		|||
	md5sum := md5.Sum(fileBytes)
 | 
			
		||||
	fileCmdResult, err := FileCmd(job.FileID.String())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		database.FailProcessingJob(job.ID, err)
 | 
			
		||||
		slog.Error("Error processing file", "file-uuid", job.FileID.String(), "error", err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -32,9 +33,11 @@ func BasicProcessing(job sqlc.ProcessingJob) error {
 | 
			
		|||
	fileProperties.LibmagicMime.String = fileCmdResult.MimeType
 | 
			
		||||
	fileProperties.LibmagicApple.String = fileCmdResult.Apple
 | 
			
		||||
	fileProperties.LibmagicExtension.String = fileCmdResult.Extension
 | 
			
		||||
  database.InsertFileProperties(fileProperties)
 | 
			
		||||
 | 
			
		||||
	err = database.InsertFileProperties(fileProperties)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Error inserting basic file properties into database", "file-uuid", job.FileID.String(), "error", err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										25
									
								
								server/internal/processing/basic/diec.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								server/internal/processing/basic/diec.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
package basic
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"log/slog"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
 | 
			
		||||
	"git.jmbit.de/jmb/scanfile/server/internal/store"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// DiecScan() runs diec -jdu on the file
 | 
			
		||||
func DiecScan(fileName string) ([]byte, error) {
 | 
			
		||||
	var by []byte
 | 
			
		||||
	filepath, err := store.AbsPath(fileName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Error in DiecScan", "file-uuid", fileName, "error", err)
 | 
			
		||||
		return by, err
 | 
			
		||||
	}
 | 
			
		||||
	cmd := exec.Command("/usr/bin/diec", "-jdu", filepath)
 | 
			
		||||
	result, err := cmd.Output()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Error in DiecScan", "file-uuid", fileName, "error", err)
 | 
			
		||||
		return by, err
 | 
			
		||||
	}
 | 
			
		||||
	return result, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -8,7 +8,6 @@ import (
 | 
			
		|||
	"git.jmbit.de/jmb/scanfile/server/internal/store"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type FileCmdResult struct {
 | 
			
		||||
	Type      string
 | 
			
		||||
	MimeType  string
 | 
			
		||||
| 
						 | 
				
			
			@ -21,6 +20,7 @@ func FileCmd(fileName string) (FileCmdResult, error) {
 | 
			
		|||
	var returnStruct FileCmdResult
 | 
			
		||||
	filepath, err := store.AbsPath(fileName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Error in FileCmd", "file-uuid", fileName, "error", err)
 | 
			
		||||
		return returnStruct, err
 | 
			
		||||
	}
 | 
			
		||||
	cmd := exec.Command("/usr/bin/file", "-b", filepath)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,6 +14,7 @@ import (
 | 
			
		|||
 | 
			
		||||
var semaphore chan struct{}
 | 
			
		||||
var swg *sync.WaitGroup
 | 
			
		||||
 | 
			
		||||
// Used to determine if a task was started by a previous instance that stalled out or died
 | 
			
		||||
var startup time.Time
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -34,7 +35,6 @@ func Submit(ctx context.Context, file pgtype.UUID ) error {
 | 
			
		|||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func processJob(job sqlc.ProcessingJob) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,18 +5,23 @@ import (
 | 
			
		|||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const TypeBasic = "Basic"
 | 
			
		||||
 | 
			
		||||
// Microsoft Office Document
 | 
			
		||||
const TypeMSOffice = "MSOffice"
 | 
			
		||||
 | 
			
		||||
// Microsoft Windows Portable Executable
 | 
			
		||||
const TypePE = "PE"
 | 
			
		||||
 | 
			
		||||
// Linux/UNIX Executable Linkable Format
 | 
			
		||||
const TypeELF = "ELF"
 | 
			
		||||
 | 
			
		||||
// Java Archive (JAR)
 | 
			
		||||
const TypeJAR = "JAR"
 | 
			
		||||
 | 
			
		||||
// Archives (compressed etc.)
 | 
			
		||||
const TypeArchive = "Archive"
 | 
			
		||||
 | 
			
		||||
// Anything not implemented (yet)
 | 
			
		||||
const TypeOther = "Other"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,6 +11,89 @@ import (
 | 
			
		|||
	"github.com/jackc/pgx/v5/pgtype"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const getFileProperties = `-- name: GetFileProperties :one
 | 
			
		||||
SELECT id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple FROM file_properties
 | 
			
		||||
WHERE id = $1
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
func (q *Queries) GetFileProperties(ctx context.Context, id pgtype.UUID) (FileProperty, error) {
 | 
			
		||||
	row := q.db.QueryRow(ctx, getFileProperties, id)
 | 
			
		||||
	var i FileProperty
 | 
			
		||||
	err := row.Scan(
 | 
			
		||||
		&i.ID,
 | 
			
		||||
		&i.Sha256,
 | 
			
		||||
		&i.Md5,
 | 
			
		||||
		&i.LibmagicMime,
 | 
			
		||||
		&i.LibmagicExtension,
 | 
			
		||||
		&i.LibmagicApple,
 | 
			
		||||
	)
 | 
			
		||||
	return i, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const insertFileDIEC = `-- name: InsertFileDIEC :exec
 | 
			
		||||
INSERT INTO diec (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2)
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
type InsertFileDIECParams struct {
 | 
			
		||||
	FileID pgtype.UUID
 | 
			
		||||
	Data   []byte
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (q *Queries) InsertFileDIEC(ctx context.Context, arg InsertFileDIECParams) error {
 | 
			
		||||
	_, err := q.db.Exec(ctx, insertFileDIEC, arg.FileID, arg.Data)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const insertFileMsofficeMraptor = `-- name: InsertFileMsofficeMraptor :exec
 | 
			
		||||
INSERT INTO msoffice_mraptor (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2)
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
type InsertFileMsofficeMraptorParams struct {
 | 
			
		||||
	FileID pgtype.UUID
 | 
			
		||||
	Data   []byte
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (q *Queries) InsertFileMsofficeMraptor(ctx context.Context, arg InsertFileMsofficeMraptorParams) error {
 | 
			
		||||
	_, err := q.db.Exec(ctx, insertFileMsofficeMraptor, arg.FileID, arg.Data)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const insertFileMsofficeOleid = `-- name: InsertFileMsofficeOleid :exec
 | 
			
		||||
INSERT INTO msoffice_oleid (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2)
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
type InsertFileMsofficeOleidParams struct {
 | 
			
		||||
	FileID pgtype.UUID
 | 
			
		||||
	Data   []byte
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (q *Queries) InsertFileMsofficeOleid(ctx context.Context, arg InsertFileMsofficeOleidParams) error {
 | 
			
		||||
	_, err := q.db.Exec(ctx, insertFileMsofficeOleid, arg.FileID, arg.Data)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const insertFileMsofficeOlevba = `-- name: InsertFileMsofficeOlevba :exec
 | 
			
		||||
INSERT INTO msoffice_olevba (
 | 
			
		||||
  file_id, data
 | 
			
		||||
) VALUES ($1, $2)
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
type InsertFileMsofficeOlevbaParams struct {
 | 
			
		||||
	FileID pgtype.UUID
 | 
			
		||||
	Data   []byte
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (q *Queries) InsertFileMsofficeOlevba(ctx context.Context, arg InsertFileMsofficeOlevbaParams) error {
 | 
			
		||||
	_, err := q.db.Exec(ctx, insertFileMsofficeOlevba, arg.FileID, arg.Data)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const insertFileProperties = `-- name: InsertFileProperties :exec
 | 
			
		||||
INSERT INTO file_properties (
 | 
			
		||||
  id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
// templui component icon - version: main installed by templui v0.71.0
 | 
			
		||||
package icon
 | 
			
		||||
 | 
			
		||||
// This file is auto generated
 | 
			
		||||
// Using Lucide icons version 0.507.0
 | 
			
		||||
var ALargeSmall = Icon("a-large-small")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue