2022-07-09 21:51:19 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-07-24 20:27:25 +00:00
|
|
|
"context"
|
2022-07-09 21:51:19 +00:00
|
|
|
"emperror.dev/errors"
|
2022-07-24 15:43:48 +00:00
|
|
|
"github.com/pterodactyl/wings/internal/database"
|
|
|
|
"github.com/pterodactyl/wings/internal/models"
|
2022-07-24 20:27:25 +00:00
|
|
|
"time"
|
2022-07-09 21:51:19 +00:00
|
|
|
)
|
|
|
|
|
2022-07-09 21:52:59 +00:00
|
|
|
const ActivityPowerPrefix = "server:power."
|
2022-07-09 21:51:19 +00:00
|
|
|
|
|
|
|
const (
|
2022-07-24 15:43:48 +00:00
|
|
|
ActivityConsoleCommand = models.Event("server:console.command")
|
|
|
|
ActivitySftpWrite = models.Event("server:sftp.write")
|
|
|
|
ActivitySftpCreate = models.Event("server:sftp.create")
|
|
|
|
ActivitySftpCreateDirectory = models.Event("server:sftp.create-directory")
|
|
|
|
ActivitySftpRename = models.Event("server:sftp.rename")
|
|
|
|
ActivitySftpDelete = models.Event("server:sftp.delete")
|
2022-07-24 21:12:47 +00:00
|
|
|
ActivityFileUploaded = models.Event("server:file.uploaded")
|
2022-07-09 21:51:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RequestActivity is a wrapper around a LoggedEvent that is able to track additional request
|
|
|
|
// specific metadata including the specific user and IP address associated with all subsequent
|
|
|
|
// events. The internal logged event structure can be extracted by calling RequestEvent.Event().
|
|
|
|
type RequestActivity struct {
|
|
|
|
server string
|
|
|
|
user string
|
|
|
|
ip string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event returns the underlying logged event from the RequestEvent instance and sets the
|
|
|
|
// specific event and metadata on it.
|
2022-07-24 15:43:48 +00:00
|
|
|
func (ra RequestActivity) Event(event models.Event, metadata models.ActivityMeta) *models.Activity {
|
|
|
|
a := models.Activity{Server: ra.server, IP: ra.ip, Event: event, Metadata: metadata}
|
|
|
|
|
|
|
|
return a.SetUser(ra.user)
|
2022-07-09 21:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetUser clones the RequestActivity struct and sets a new user value on the copy
|
|
|
|
// before returning it.
|
|
|
|
func (ra RequestActivity) SetUser(u string) RequestActivity {
|
|
|
|
c := ra
|
|
|
|
c.user = u
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) NewRequestActivity(user string, ip string) RequestActivity {
|
|
|
|
return RequestActivity{server: s.ID(), user: user, ip: ip}
|
|
|
|
}
|
2022-07-24 20:27:25 +00:00
|
|
|
|
|
|
|
// SaveActivity saves an activity entry to the database in a background routine. If an error is
|
|
|
|
// encountered it is logged but not returned to the caller.
|
|
|
|
func (s *Server) SaveActivity(a RequestActivity, event models.Event, metadata models.ActivityMeta) {
|
|
|
|
ctx, cancel := context.WithTimeout(s.Context(), time.Second*3)
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
|
|
|
if tx := database.Instance().WithContext(ctx).Create(a.Event(event, metadata)); tx.Error != nil {
|
|
|
|
s.Log().WithField("error", errors.WithStack(tx.Error)).
|
|
|
|
WithField("event", event).
|
|
|
|
Error("activity: failed to save event")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|