common

package
v0.0.0-...-e675dcb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 30 Imported by: 15

Documentation

Index

Constants

View Source
const AuthenticationSignatureKeySettingKey = "authentication_signature_key"

AuthenticationSignatureKeySettingKey setting key for authentication_signature_key

View Source
const FeatureDefault = "default"

FeatureDefault feature is opt-out

View Source
const FeatureDisabled = "disabled"

FeatureDisabled feature is always off

View Source
const FeatureEnabled = "enabled"

FeatureEnabled feature is opt-in

View Source
const FeatureForced = "forced"

FeatureForced feature is always on

View Source
const FileDeleted = "deleted"

FileDeleted when a file has been deleted from the data backend

View Source
const FileMissing = "missing"

FileMissing when a file is waiting to be uploaded

View Source
const FileRemoved = "removed"

FileRemoved when a file has been removed and can't be downloaded anymore but has not yet been deleted

View Source
const FileUploaded = "uploaded"

FileUploaded when a file has been uploaded and is ready to be downloaded

View Source
const FileUploading = "uploading"

FileUploading when a file is being uploaded

View Source
const ProviderGoogle = "google"

ProviderGoogle for authentication

View Source
const ProviderLocal = "local"

ProviderLocal for authentication

View Source
const ProviderOVH = "ovh"

ProviderOVH for authentication

View Source
const SessionCookieName = "plik-session"

SessionCookieName Cookie key

View Source
const XSRFCookieName = "plik-xsrf"

XSRFCookieName Cookie key

Variables

View Source
var APIMockServerDefaultPort = 44142

APIMockServerDefaultPort is the default port to use for testing HTTP server

View Source
var DummyHandler = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {})

DummyHandler is a dummy http.Handler

Functions

func AskConfirmation

func AskConfirmation(defaultValue bool) (bool, error)

AskConfirmation from process input

func CheckHTTPServer

func CheckHTTPServer(port int) (err error)

CheckHTTPServer for HTTP Server to be UP and running HTTP Server should must respond 404 to URL/not_found to be considered ok

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

CheckPasswordHash check password against bcrypt password hash

func EncodeAuthBasicHeader

func EncodeAuthBasicHeader(login string, password string) (value string)

EncodeAuthBasicHeader return the base64 version of "login:password"

func GenerateRandomID

func GenerateRandomID(length int) string

GenerateRandomID generates a random string with specified length. Used to generate upload id, tokens, ...

func GetUserID

func GetUserID(provider string, providerID string) string

GetUserID return user ID from provider and login

func HashPassword

func HashPassword(password string) (string, error)

HashPassword return bcrypt password hash ( with salt )

func HumanDuration

func HumanDuration(d time.Duration) string

HumanDuration displays duration with days and years

func IsFeatureAvailable

func IsFeatureAvailable(value string) bool

IsFeatureAvailable return true is the feature is available

func IsFeatureDefault

func IsFeatureDefault(value string) bool

IsFeatureDefault return true is the feature is enabled by default

func IsValidProvider

func IsValidProvider(provider string) bool

IsValidProvider return true if the provider string is valid

func Logout

func Logout(resp http.ResponseWriter, sa *SessionAuthenticator)

Logout delete session cookies

func MarshalUpload

func MarshalUpload(upload *Upload, version int) (bytes []byte, err error)

MarshalUpload unmarshal upload if version is (1) marshal using UploadV1 format

func ParseTTL

func ParseTTL(TTL string) (int, error)

ParseTTL string into a number of seconds

func RequireError

func RequireError(t *testing.T, err error, message string)

RequireError is a helper to test the error and it's message

func StartAPIMockServer

func StartAPIMockServer(next http.Handler) (shutdown func(), err error)

StartAPIMockServer starts a new temporary API Server to be used in tests

func StartAPIMockServerCustomPort

func StartAPIMockServerCustomPort(port int, next http.Handler) (shutdown func(), err error)

StartAPIMockServerCustomPort starts a new temporary API Server using a custom port Adds a middleware that handle the /not_found path called by the CheckHTTPServer function

func StripPrefix

func StripPrefix(prefix string, handler http.Handler) http.Handler

StripPrefix returns a handler that serves HTTP requests removing the given prefix from the request URL's Path It differs from http.StripPrefix by defaulting to "/" and not ""

func TestTimeout

func TestTimeout(f func(), duration time.Duration) (err error)

TestTimeout execute a function and return an error if the defined timeout happen before

func UnmarshalUpload

func UnmarshalUpload(bytes []byte, upload *Upload) (version int, err error)

UnmarshalUpload unmarshal upload, if that fails try again with UploadV1 format with files in a map instead of an array

func UpdateUser

func UpdateUser(user *User, userParams *User) (err error)

UpdateUser update a user object with the params

  • prevent to update provider, user ID or login
  • only update password if a new one is provided

func ValidateCustomFeatureFlag

func ValidateCustomFeatureFlag(value string, possibleValues []string) (err error)

ValidateCustomFeatureFlag validates a feature flag string value against a list of possible values

func ValidateFeatureFlag

func ValidateFeatureFlag(value string) (err error)

ValidateFeatureFlag validates a feature flag string value

func WriteJSONResponse

func WriteJSONResponse(resp http.ResponseWriter, obj interface{})

WriteJSONResponse serialize the response to json and write it to the HTTP response body

Types

type BuildInfo

type BuildInfo struct {
	Version string `json:"version"`
	Date    int64  `json:"date"`

	User string `json:"user,omitempty"`
	Host string `json:"host,omitempty"`

	GitShortRevision string `json:"gitShortRevision,omitempty"`
	GitFullRevision  string `json:"gitFullRevision,omitempty"`

	IsRelease bool `json:"isRelease"`
	IsMint    bool `json:"isMint"`

	GoVersion string `json:"goVersion,omitempty"`

	Clients  []*Client  `json:"clients"`
	Releases []*Release `json:"releases"`
}

BuildInfo export build related variables

func GetBuildInfo

func GetBuildInfo() (bi *BuildInfo)

GetBuildInfo get build info

func (*BuildInfo) Sanitize

func (bi *BuildInfo) Sanitize()

Sanitize removes sensitive info from BuildInfo

func (*BuildInfo) String

func (bi *BuildInfo) String() string

type CleaningStats

type CleaningStats struct {
	RemovedUploads      int
	DeletedFiles        int
	DeletedUploads      int
	OrphanFilesCleaned  int
	OrphanTokensCleaned int
}

CleaningStats cleaning statistics

type Client

type Client struct {
	Name string `json:"name"`
	Md5  string `json:"md5"`
	Path string `json:"path"`
	OS   string `json:"os"`
	ARCH string `json:"arch"`
}

Client export client build related variables

type Configuration

type Configuration struct {
	Debug         bool   `json:"-"`
	DebugRequests bool   `json:"-"`
	LogLevel      string `json:"-"`

	ListenAddress  string `json:"-"`
	ListenPort     int    `json:"-"`
	MetricsAddress string `json:"-"`
	MetricsPort    int    `json:"-"`
	Path           string `json:"-"`

	MaxFileSizeStr   string `json:"-"`
	MaxFileSize      int64  `json:"maxFileSize"`
	MaxUserSizeStr   string `json:"-"`
	MaxUserSize      int64  `json:"maxUserSize"`
	MaxFilePerUpload int    `json:"maxFilePerUpload"`

	DefaultTTLStr string `json:"-"`
	DefaultTTL    int    `json:"defaultTTL"`
	MaxTTLStr     string `json:"-"`
	MaxTTL        int    `json:"maxTTL"`

	SslEnabled bool   `json:"-"`
	SslCert    string `json:"-"`
	SslKey     string `json:"-"`
	TlsVersion string `json:"-"`

	NoWebInterface      bool     `json:"-"`
	DownloadDomain      string   `json:"downloadDomain"`
	DownloadDomainAlias []string `json:"downloadDomainAlias"`
	EnhancedWebSecurity bool     `json:"-"`
	SessionTimeout      string   `json:"-"`
	AbuseContact        string   `json:"abuseContact"`
	WebappDirectory     string   `json:"-"`
	ClientsDirectory    string   `json:"-"`
	ChangelogDirectory  string   `json:"-"`

	SourceIPHeader  string   `json:"-"`
	UploadWhitelist []string `json:"-"`

	// Feature Flags
	FeatureAuthentication string `json:"feature_authentication"`
	FeatureOneShot        string `json:"feature_one_shot"`
	FeatureRemovable      string `json:"feature_removable"`
	FeatureStream         string `json:"feature_stream"`
	FeaturePassword       string `json:"feature_password"`
	FeatureComments       string `json:"feature_comments"`
	FeatureSetTTL         string `json:"feature_set_ttl"`
	FeatureExtendTTL      string `json:"feature_extend_ttl"`
	FeatureClients        string `json:"feature_clients"`
	FeatureGithub         string `json:"feature_github"`
	FeatureText           string `json:"feature_text"`

	// Deprecated Feature Flags
	Authentication      bool `json:"authentication"`      // Deprecated: >1.3.6
	NoAnonymousUploads  bool `json:"noAnonymousUploads"`  // Deprecated: >1.3.6
	OneShot             bool `json:"oneShot"`             // Deprecated: >1.3.6
	Removable           bool `json:"removable"`           // Deprecated: >1.3.6
	Stream              bool `json:"stream"`              // Deprecated: >1.3.6
	ProtectedByPassword bool `json:"protectedByPassword"` // Deprecated: >1.3.6

	GoogleAuthentication bool     `json:"googleAuthentication"`
	GoogleAPISecret      string   `json:"-"`
	GoogleAPIClientID    string   `json:"-"`
	GoogleValidDomains   []string `json:"-"`
	OvhAuthentication    bool     `json:"ovhAuthentication"`
	OvhAPIEndpoint       string   `json:"ovhApiEndpoint"`
	OvhAPIKey            string   `json:"-"`
	OvhAPISecret         string   `json:"-"`

	MetadataBackendConfig map[string]interface{} `json:"-"`

	DataBackend       string                 `json:"-"`
	DataBackendConfig map[string]interface{} `json:"-"`
	// contains filtered or unexported fields
}

Configuration object

func LoadConfiguration

func LoadConfiguration(path string) (config *Configuration, err error)

LoadConfiguration creates a new empty configuration and try to load specified file with toml library to override default params

func NewConfiguration

func NewConfiguration() (config *Configuration)

NewConfiguration creates a new configuration object with default values

func (*Configuration) AutoClean

func (config *Configuration) AutoClean(value bool)

AutoClean enable or disables the periodical upload cleaning goroutine. This needs to be called before Plik server starts to have effect

func (*Configuration) EnvironmentOverride

func (config *Configuration) EnvironmentOverride() (err error)

EnvironmentOverride override config from environment variables Environment variables must match config params in screaming snake case ( DebugRequests -> PLIKD_DEBUG_REQUESTS )

func (*Configuration) GetDownloadDomain

func (config *Configuration) GetDownloadDomain() *url.URL

GetDownloadDomain return the parsed download domain URL

func (*Configuration) GetPath

func (config *Configuration) GetPath() string

GetPath return the web API/UI root path

func (*Configuration) GetServerURL

func (config *Configuration) GetServerURL() *url.URL

GetServerURL is a helper to get the server HTTP URL

func (*Configuration) GetSessionTimeout

func (config *Configuration) GetSessionTimeout() int

GetSessionTimeout return parsed session timeout

func (*Configuration) GetTlsVersion

func (config *Configuration) GetTlsVersion() uint16

GetTlsVersion is a helper to get the TLS version

func (*Configuration) GetUploadWhitelist

func (config *Configuration) GetUploadWhitelist() []*net.IPNet

GetUploadWhitelist return the parsed IP upload whitelist

func (*Configuration) Initialize

func (config *Configuration) Initialize() (err error)

Initialize config internal parameters

func (*Configuration) IsAutoClean

func (config *Configuration) IsAutoClean() bool

IsAutoClean return weather or not to start the cleaning goroutine

func (*Configuration) IsValidDownloadDomain

func (config *Configuration) IsValidDownloadDomain(host string) bool

IsValidDownloadDomain return weather or not the host is a valid download domain

func (*Configuration) IsWhitelisted

func (config *Configuration) IsWhitelisted(ip net.IP) bool

IsWhitelisted return weather or not the IP matches of the config upload whitelist

func (*Configuration) NewLogger

func (config *Configuration) NewLogger() (log *logger.Logger)

NewLogger returns a new logger instance

func (*Configuration) String

func (config *Configuration) String() string

type ErrorReader

type ErrorReader struct {
	// contains filtered or unexported fields
}

ErrorReader impement io.Reader and return err for every read call attempted

func NewErrorReader

func NewErrorReader(err error) (reader *ErrorReader)

NewErrorReader return a new ErrorReader

func NewErrorReaderString

func NewErrorReaderString(err string) (reader *ErrorReader)

NewErrorReaderString return a new ErrorReader from the provided string

func (*ErrorReader) Read

func (reader *ErrorReader) Read(p []byte) (n int, err error)

Read method to implement io.Reader

type File

type File struct {
	ID       string `json:"id"`
	UploadID string `json:"-" gorm:"size:256;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT;"`
	Name     string `json:"fileName"`

	Status string `json:"status"`

	Md5       string `json:"fileMd5"`
	Type      string `json:"fileType"`
	Size      int64  `json:"fileSize"`
	Reference string `json:"reference"`

	BackendDetails string `json:"-"`

	CreatedAt time.Time `json:"createdAt"`
}

File object

func NewFile

func NewFile() (file *File)

NewFile instantiate a new object and generate a random id

func (*File) GenerateID

func (file *File) GenerateID()

GenerateID generate a new File ID

func (*File) Sanitize

func (file *File) Sanitize()

Sanitize clear some fields to hide sensible information from the API.

type HTTPError

type HTTPError struct {
	Message    string
	Err        error
	StatusCode int
}

HTTPError allows to return an error and a HTTP status code

func NewHTTPError

func NewHTTPError(message string, err error, code int) HTTPError

NewHTTPError return a new HTTPError

func (HTTPError) Error

func (e HTTPError) Error() string

Error return the error

func (HTTPError) String

func (e HTTPError) String() string

type PagingQuery

type PagingQuery struct {
	Before *string `json:"before"`
	After  *string `json:"after"`
	Limit  *int    `json:"limit"`
	Order  *string `json:"order"`
}

PagingQuery for the paging system

func NewPagingQuery

func NewPagingQuery() (pq *PagingQuery)

NewPagingQuery return a new paging query

func (*PagingQuery) Paginator

func (pq *PagingQuery) Paginator() *paginator.Paginator

Paginator return a new Paginator for the PagingQuery

func (*PagingQuery) WithAfterCursor

func (pq *PagingQuery) WithAfterCursor(cursor string) *PagingQuery

WithAfterCursor set the paging query after cursor and unset the before cursor

func (*PagingQuery) WithBeforeCursor

func (pq *PagingQuery) WithBeforeCursor(cursor string) *PagingQuery

WithBeforeCursor set the paging query before cursor and unset the after cursor

func (*PagingQuery) WithLimit

func (pq *PagingQuery) WithLimit(limit int) *PagingQuery

WithLimit set the paging query limit if limit is a valid positive integer

func (*PagingQuery) WithOrder

func (pq *PagingQuery) WithOrder(order string) *PagingQuery

WithOrder set the paging query order if oder is a valid order string "asc" or "desc"

type PagingResponse

type PagingResponse struct {
	After   *string       `json:"after"`
	Before  *string       `json:"before"`
	Results []interface{} `json:"results"`
}

PagingResponse for the paging system

func NewPagingResponse

func NewPagingResponse(results interface{}, cursor *paginator.Cursor) (pr *PagingResponse)

NewPagingResponse create a new PagingResponse from query results ( results must be a slice )

type PlikMetrics

type PlikMetrics struct {
	// contains filtered or unexported fields
}

PlikMetrics handles Prometheus metrics

func NewPlikMetrics

func NewPlikMetrics() (m *PlikMetrics)

NewPlikMetrics initialize Plik metrics

func (*PlikMetrics) GetRegistry

func (m *PlikMetrics) GetRegistry() *prometheus.Registry

GetRegistry returns the dedicated Prometheus Registry

func (*PlikMetrics) Register

func (m *PlikMetrics) Register(collectors ...prometheus.Collector)

Register a set of collectors to the dedicated Prometheus registry This can be used by modules to register dedicated metrics

func (*PlikMetrics) UpdateCleaningStatistics

func (m *PlikMetrics) UpdateCleaningStatistics(stats *CleaningStats, elapsed time.Duration)

UpdateCleaningStatistics update metrics about plik cleaning

func (*PlikMetrics) UpdateHTTPMetrics

func (m *PlikMetrics) UpdateHTTPMetrics(method string, path string, statusCode int, elapsed time.Duration)

UpdateHTTPMetrics update metrics about HTTP requests/responses

func (*PlikMetrics) UpdateServerStatistics

func (m *PlikMetrics) UpdateServerStatistics(stats *ServerStats, elapsed time.Duration)

UpdateServerStatistics update metrics about plik metadata

type Release

type Release struct {
	Name string `json:"name"`
	Date int64  `json:"date"`
}

Release export releases related variables

type ServerStats

type ServerStats struct {
	Users            int   `json:"users"`
	Uploads          int   `json:"uploads"`
	AnonymousUploads int   `json:"anonymousUploads"`
	Files            int   `json:"files"`
	TotalSize        int64 `json:"totalSize"`
	AnonymousSize    int64 `json:"anonymousTotalSize"`
}

ServerStats server statistics

type SessionAuthenticator

type SessionAuthenticator struct {
	SignatureKey   string
	SecureCookies  bool
	SessionTimeout int
	Path           string
}

SessionAuthenticator to generate and authenticate session cookies

func (*SessionAuthenticator) GenAuthCookies

func (sa *SessionAuthenticator) GenAuthCookies(user *User) (sessionCookie *http.Cookie, xsrfCookie *http.Cookie, err error)

GenAuthCookies generate a sign a jwt session cookie to authenticate a user

func (*SessionAuthenticator) Logout

func (sa *SessionAuthenticator) Logout() (sessionCookie *http.Cookie, xsrfCookie *http.Cookie, err error)

Logout generate logout session cookies

func (*SessionAuthenticator) ParseSessionCookie

func (sa *SessionAuthenticator) ParseSessionCookie(value string) (uid string, xsrf string, err error)

ParseSessionCookie parse and validate the session cookie

type Setting

type Setting struct {
	Key   string `gorm:"primary_key"`
	Value string
}

Setting is a config object meant to be shard by all Plik instances using the metadata backend

func GenerateAuthenticationSignatureKey

func GenerateAuthenticationSignatureKey() (s *Setting)

GenerateAuthenticationSignatureKey create a new random key

type Token

type Token struct {
	Token   string `json:"token" gorm:"primary_key"`
	Comment string `json:"comment,omitempty"`

	UserID string `json:"-" gorm:"size:256;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT;"`

	CreatedAt time.Time `json:"createdAt"`
}

Token provide a very basic authentication mechanism

func NewToken

func NewToken() (t *Token)

NewToken create a new Token instance

func (*Token) Initialize

func (t *Token) Initialize()

Initialize generate the token uuid and sets the creation date

type Upload

type Upload struct {
	ID        string `json:"id"`
	TTL       int    `json:"ttl"`
	ExtendTTL bool   `json:"extend_ttl"`

	DownloadDomain string `json:"downloadDomain" gorm:"-"`
	RemoteIP       string `json:"uploadIp,omitempty"`
	Comments       string `json:"comments"`

	Files []*File `json:"files"`

	UploadToken string `json:"uploadToken,omitempty"`
	User        string `json:"user,omitempty" gorm:"index:idx_upload_user"`
	Token       string `json:"token,omitempty" gorm:"index:idx_upload_user_token"`

	IsAdmin bool `json:"admin" gorm:"-"`

	Stream    bool `json:"stream"`
	OneShot   bool `json:"oneShot"`
	Removable bool `json:"removable"`

	ProtectedByPassword bool   `json:"protectedByPassword"`
	Login               string `json:"login,omitempty"`
	Password            string `json:"password,omitempty"`

	CreatedAt time.Time      `json:"createdAt"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index:idx_upload_deleted_at"`
	ExpireAt  *time.Time     `json:"expireAt" gorm:"index:idx_upload_expire_at"`
}

Upload object

func NewUpload

func NewUpload() (upload *Upload)

NewUpload creates a new upload object

func (*Upload) ExtendExpirationDate

func (upload *Upload) ExtendExpirationDate()

ExtendExpirationDate extends the upload expiration date by TTL

func (*Upload) GenerateID

func (upload *Upload) GenerateID()

GenerateID generate a new Upload ID and UploadToken

func (*Upload) GenerateUploadToken

func (upload *Upload) GenerateUploadToken()

GenerateUploadToken generate a new UploadToken

func (*Upload) GetFile

func (upload *Upload) GetFile(ID string) (file *File)

GetFile get file with ID from upload files. Return nil if not found

func (*Upload) GetFileByReference

func (upload *Upload) GetFileByReference(ref string) (file *File)

GetFileByReference get file with Reference from upload files. Return nil if not found

func (*Upload) InitializeForTests

func (upload *Upload) InitializeForTests()

InitializeForTests initialize upload for database insert without config checks and override for testing purpose

func (*Upload) IsExpired

func (upload *Upload) IsExpired() bool

IsExpired check if the upload is expired

func (*Upload) NewFile

func (upload *Upload) NewFile() (file *File)

NewFile creates a new file and add it to the current upload

func (*Upload) Sanitize

func (upload *Upload) Sanitize(config *Configuration)

Sanitize clear some fields to hide sensible information from the API.

type UploadV1

type UploadV1 struct {
	ID       string `json:"id"`
	Creation int64  `json:"uploadDate"`
	TTL      int    `json:"ttl"`

	DownloadDomain string `json:"downloadDomain"`
	RemoteIP       string `json:"uploadIp,omitempty"`
	Comments       string `json:"comments"`

	Files map[string]*File `json:"files"`

	UploadToken string `json:"uploadToken,omitempty"`
	User        string `json:"user,omitempty"`
	Token       string `json:"token,omitempty"`
	IsAdmin     bool   `json:"admin"`

	Stream    bool `json:"stream"`
	OneShot   bool `json:"oneShot"`
	Removable bool `json:"removable"`

	ProtectedByPassword bool   `json:"protectedByPassword"`
	Login               string `json:"login,omitempty"`
	Password            string `json:"password,omitempty"`

	ProtectedByYubikey bool   `json:"protectedByYubikey"`
	Yubikey            string `json:"yubikey,omitempty"`
}

UploadV1 upload object compatible with Plik <1.3

type User

type User struct {
	ID       string `json:"id,omitempty"`
	Provider string `json:"provider"`
	Login    string `json:"login,omitempty"`
	Password string `json:"-"`
	Name     string `json:"name,omitempty"`
	Email    string `json:"email,omitempty"`
	IsAdmin  bool   `json:"admin"`

	MaxFileSize int64 `json:"maxFileSize"`
	MaxUserSize int64 `json:"maxUserSize"`
	MaxTTL      int   `json:"maxTTL"`

	Tokens []*Token `json:"tokens,omitempty"`

	CreatedAt time.Time `json:"createdAt"`
}

User is a Plik user

func CreateUserFromParams

func CreateUserFromParams(userParams *User) (user *User, err error)

CreateUserFromParams return a user object ready to be inserted in the metadata backend

func NewUser

func NewUser(provider string, providerID string) (user *User)

NewUser create a new user object

func (*User) NewToken

func (user *User) NewToken() (token *Token)

NewToken add a new token to a user

func (*User) String

func (user *User) String() string

NewToken add a new token to a user

type UserStats

type UserStats struct {
	Uploads   int   `json:"uploads"`
	Files     int   `json:"files"`
	TotalSize int64 `json:"totalSize"`
}

UserStats user statistics

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL