wings/sftp/event.go
Ethan Alicea 4390bad36b
Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit.

 Author:    Ethan Alicea <64653625+Tech-Gamer@users.noreply.github.com>

 On branch develop
 Your branch is up to date with 'origin/develop'.

 Changes to be committed:
	modified:   .github/workflows/push.yaml
	modified:   .github/workflows/release.yaml
	modified:   CHANGELOG.md
	modified:   Dockerfile
	modified:   Makefile
	modified:   README.md
	modified:   cmd/configure.go
	modified:   cmd/diagnostics.go
	modified:   cmd/root.go
	modified:   config/config.go
	modified:   environment/allocations.go
	modified:   environment/docker.go
	modified:   environment/docker/api.go
	modified:   environment/docker/container.go
	modified:   environment/docker/environment.go
	modified:   environment/docker/power.go
	modified:   environment/docker/stats.go
	modified:   environment/environment.go
	modified:   environment/settings.go
	modified:   events/events.go
	modified:   go.mod
	modified:   internal/cron/activity_cron.go
	modified:   internal/cron/cron.go
	modified:   internal/cron/sftp_cron.go
	modified:   internal/database/database.go
	modified:   internal/progress/progress.go
	modified:   internal/progress/progress_test.go
	modified:   loggers/cli/cli.go
	new file:   oryxBuildBinary
	modified:   parser/parser.go
	modified:   remote/http.go
	modified:   remote/servers.go
	modified:   remote/types.go
	modified:   router/downloader/downloader.go
	modified:   router/middleware.go
	modified:   router/middleware/middleware.go
	modified:   router/middleware/request_error.go
	modified:   router/router.go
	modified:   router/router_download.go
	modified:   router/router_server.go
	modified:   router/router_server_backup.go
	modified:   router/router_server_files.go
	modified:   router/router_server_transfer.go
	modified:   router/router_server_ws.go
	modified:   router/router_system.go
	modified:   router/router_transfer.go
	modified:   router/tokens/parser.go
	modified:   router/websocket/listeners.go
	modified:   router/websocket/websocket.go
	modified:   server/activity.go
	modified:   server/backup.go
	modified:   server/backup/backup.go
	modified:   server/backup/backup_local.go
	modified:   server/backup/backup_s3.go
	modified:   server/configuration.go
	modified:   server/console.go
	modified:   server/crash.go
	modified:   server/events.go
	modified:   server/filesystem/archive.go
	modified:   server/filesystem/filesystem.go
	modified:   server/filesystem/filesystem_test.go
	modified:   server/install.go
	modified:   server/installer/installer.go
	modified:   server/listeners.go
	modified:   server/manager.go
	modified:   server/mounts.go
	modified:   server/power.go
	modified:   server/power_test.go
	modified:   server/resources.go
	modified:   server/server.go
	modified:   server/transfer/archive.go
	modified:   server/transfer/source.go
	modified:   server/transfer/transfer.go
	modified:   server/update.go
	modified:   sftp/event.go
	modified:   sftp/handler.go
	modified:   sftp/server.go
	modified:   wings.go
2023-09-11 17:22:09 +00:00

60 lines
1.7 KiB
Go

package sftp
import (
"emperror.dev/errors"
"github.com/apex/log"
"github.com/Tech-Gamer/nwy-wings/internal/database"
"github.com/Tech-Gamer/nwy-wings/internal/models"
)
type eventHandler struct {
ip string
user string
server string
}
type FileAction struct {
// Entity is the targeted file or directory (depending on the event) that the action
// is being performed _against_, such as "/foo/test.txt". This will always be the full
// path to the element.
Entity string
// Target is an optional (often blank) field that only has a value in it when the event
// is specifically modifying the entity, such as a rename or move event. In that case
// the Target field will be the final value, such as "/bar/new.txt"
Target string
}
// Log parses a SFTP specific file activity event and then passes it off to be stored
// in the normal activity database.
func (eh *eventHandler) Log(e models.Event, fa FileAction) error {
metadata := map[string]interface{}{
"files": []string{fa.Entity},
}
if fa.Target != "" {
metadata["files"] = []map[string]string{
{"from": fa.Entity, "to": fa.Target},
}
}
a := models.Activity{
Server: eh.server,
Event: e,
Metadata: metadata,
IP: eh.ip,
}
if tx := database.Instance().Create(a.SetUser(eh.user)); tx.Error != nil {
return errors.WithStack(tx.Error)
}
return nil
}
// MustLog is a wrapper around log that will trigger a fatal error and exit the application
// if an error is encountered during the logging of the event.
func (eh *eventHandler) MustLog(e models.Event, fa FileAction) {
if err := eh.Log(e, fa); err != nil {
log.WithField("error", errors.WithStack(err)).WithField("event", e).Error("sftp: failed to log event")
}
}