2022-07-10 18:30:32 +00:00
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/apex/log"
|
2022-07-24 15:43:48 +00:00
|
|
|
"github.com/pterodactyl/wings/internal/database"
|
|
|
|
"github.com/pterodactyl/wings/internal/models"
|
2022-07-10 18:30:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-10 20:51:11 +00:00
|
|
|
// Log parses a SFTP specific file activity event and then passes it off to be stored
|
|
|
|
// in the normal activity database.
|
2022-07-24 15:43:48 +00:00
|
|
|
func (eh *eventHandler) Log(e models.Event, fa FileAction) error {
|
2022-07-10 20:51:11 +00:00
|
|
|
metadata := map[string]interface{}{
|
|
|
|
"files": []string{fa.Entity},
|
|
|
|
}
|
|
|
|
if fa.Target != "" {
|
|
|
|
metadata["files"] = []map[string]string{
|
|
|
|
{"from": fa.Entity, "to": fa.Target},
|
|
|
|
}
|
|
|
|
}
|
2022-07-10 18:30:32 +00:00
|
|
|
|
2022-07-24 15:43:48 +00:00
|
|
|
a := models.Activity{
|
|
|
|
Server: eh.server,
|
|
|
|
Event: e,
|
|
|
|
Metadata: metadata,
|
|
|
|
IP: eh.ip,
|
2022-07-10 18:30:32 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 15:43:48 +00:00
|
|
|
if tx := database.Instance().Create(a.SetUser(eh.user)); tx.Error != nil {
|
|
|
|
return errors.Wrap(tx.Error, "sftp: failed to save event to database")
|
|
|
|
}
|
|
|
|
return nil
|
2022-07-10 18:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-07-24 15:43:48 +00:00
|
|
|
func (eh *eventHandler) MustLog(e models.Event, fa FileAction) {
|
2022-07-10 18:30:32 +00:00
|
|
|
if err := eh.Log(e, fa); err != nil {
|
|
|
|
log.WithField("error", err).Fatal("sftp: failed to log event")
|
|
|
|
}
|
|
|
|
}
|