This commit is contained in:
2019-12-22 01:17:18 +05:00
parent da4bc379d8
commit a52b18ffe4
31 changed files with 202 additions and 82 deletions

View File

@@ -39,7 +39,7 @@ var (
log zerolog.Logger
)
// New initializes captcha package and adds neccessary HTTP and API
// New initializes captcha package and adds necessary HTTP and API
// endpoints.
func New(cc *context.Context) {
c = cc
@@ -53,6 +53,7 @@ func New(cc *context.Context) {
func NewCaptcha() string {
s := captcha.New()
log.Debug().Str("captcha string", s).Msg("Created new captcha string")
return s
}

View File

@@ -24,8 +24,8 @@
package config
// ConfigDatabase describes database configuration.
type ConfigDatabase struct {
// Database describes database configuration.
type Database struct {
Type string `yaml:"type"`
Path string `yaml:"path"`
Address string `yaml:"address"`

View File

@@ -24,8 +24,8 @@
package config
// ConfigHTTP describes HTTP server configuration.
type ConfigHTTP struct {
// HTTP describes HTTP server configuration.
type HTTP struct {
Address string `yaml:"address"`
Port string `yaml:"port"`
AllowInsecure bool `yaml:"allow_insecure"`

View File

@@ -24,8 +24,8 @@
package config
// ConfigLogging describes logger configuration.
type ConfigLogging struct {
// Logging describes logger configuration.
type Logging struct {
LogToFile bool `yaml:"log_to_file"`
FileName string `yaml:"filename"`
LogLevel string `yaml:"loglevel"`

View File

@@ -24,7 +24,7 @@
package config
// ConfigPastes describes pastes subsystem configuration.
type ConfigPastes struct {
// Pastes describes pastes subsystem configuration.
type Pastes struct {
Pagination int `yaml:"pagination"`
}

View File

@@ -24,10 +24,10 @@
package config
// ConfigStruct describes whole configuration.
type ConfigStruct struct {
Database ConfigDatabase `yaml:"database"`
Logging ConfigLogging `yaml:"logging"`
HTTP ConfigHTTP `yaml:"http"`
Pastes ConfigPastes `yaml:"pastes"`
// Struct describes whole configuration.
type Struct struct {
Database Database `yaml:"database"`
Logging Logging `yaml:"logging"`
HTTP HTTP `yaml:"http"`
Pastes Pastes `yaml:"pastes"`
}

View File

@@ -46,7 +46,7 @@ import (
// contains everything every part of application need, like configuration
// access, logger, etc.
type Context struct {
Config *config.ConfigStruct
Config *config.Struct
Database databaseinterface.Interface
Echo *echo.Echo
Flagger *flagger.Flagger
@@ -104,7 +104,7 @@ func (c *Context) LoadConfiguration() {
c.Logger.Debug().Msgf("Configuration file path: %s", configPath)
c.Config = &config.ConfigStruct{}
c.Config = &config.Struct{}
// Read configuration file.
fileData, err2 := ioutil.ReadFile(normalizedConfigPath)

View File

@@ -15,6 +15,7 @@ import (
// Puts memory usage into log lines.
func (c *Context) getMemoryUsage(e *zerolog.Event, level zerolog.Level, message string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
e.Str("memalloc", fmt.Sprintf("%dMB", m.Alloc/1024/1024))
@@ -28,6 +29,7 @@ func (c *Context) initializeLogger() {
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
var v string
if ii, ok := i.(string); ok {
ii = strings.ToUpper(ii)
switch ii {
@@ -47,6 +49,7 @@ func (c *Context) initializeLogger() {
v = ii
}
}
return fmt.Sprintf("| %s |", v)
}
@@ -59,6 +62,7 @@ func (c *Context) initializeLogger() {
func (c *Context) initializeLoggerPost() {
// Set log level.
c.Logger.Info().Msgf("Setting logger level: %s", c.Config.Logging.LogLevel)
switch c.Config.Logging.LogLevel {
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)

View File

@@ -30,7 +30,7 @@ import (
// local
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/mysql"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/postgresql"
"go.dev.pztrn.name/fastpastebin/internal/structs"

View File

@@ -27,7 +27,7 @@ package flatfiles
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
)
var (
@@ -38,5 +38,6 @@ var (
func New(cc *context.Context) {
c = cc
f = &FlatFiles{}
c.Database.RegisterDialect(dialectinterface.Interface(Handler{}))
}

View File

@@ -54,15 +54,18 @@ func (ff *FlatFiles) GetPaste(pasteID int) (*structs.Paste, error) {
ff.writeMutex.Lock()
pastePath := filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json")
c.Logger.Debug().Msgf("Trying to load paste data from '%s'...", pastePath)
pasteInBytes, err := ioutil.ReadFile(pastePath)
if err != nil {
c.Logger.Debug().Msgf("Failed to read paste from storage: %s", err.Error())
return nil, err
}
c.Logger.Debug().Msgf("Loaded %d bytes: %s", len(pasteInBytes), string(pasteInBytes))
ff.writeMutex.Unlock()
paste := &structs.Paste{}
err = json.Unmarshal(pasteInBytes, paste)
if err != nil {
c.Logger.Error().Msgf("Failed to parse paste: %s", err.Error())
@@ -83,6 +86,7 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
// Iteration one - get only public pastes.
var publicPastes []*Index
for _, paste := range ff.pastesIndex {
if !paste.Private {
publicPastes = append(publicPastes, paste)
@@ -92,7 +96,9 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
c.Logger.Debug().Msgf("%+v", publicPastes)
// Iteration two - get paginated pastes.
// nolint
var pastesData []structs.Paste
for idx, paste := range publicPastes {
if len(pastesData) == c.Config.Pastes.Pagination {
break
@@ -107,10 +113,12 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
c.Logger.Debug().Msgf("Paste with index %d isn't in pagination query: too high index", idx)
break
}
c.Logger.Debug().Msgf("Getting paste data (ID: %d, index: %d)", paste.ID, idx)
// Get paste data.
pasteData := &structs.Paste{}
pasteRawData, err := ioutil.ReadFile(filepath.Join(ff.path, "pastes", strconv.Itoa(paste.ID)+".json"))
if err != nil {
c.Logger.Error().Msgf("Failed to read paste data: %s", err.Error())
@@ -160,13 +168,16 @@ func (ff *FlatFiles) Initialize() {
curUser, err := user.Current()
if err != nil {
c.Logger.Error().Msg("Failed to get current user. Will replace '~' for '/' in storage path!")
path = strings.Replace(path, "~", "/", -1)
}
path = strings.Replace(path, "~", curUser.HomeDir, -1)
}
path, _ = filepath.Abs(path)
ff.path = path
c.Logger.Debug().Msgf("Storage path is now: %s", ff.path)
// Create directory if necessary.
@@ -209,29 +220,35 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
// Write paste data on disk.
filesOnDisk, _ := ioutil.ReadDir(filepath.Join(ff.path, "pastes"))
pasteID := len(filesOnDisk) + 1
c.Logger.Debug().Msgf("Writing paste to disk, ID will be " + strconv.Itoa(pasteID))
p.ID = pasteID
c.Logger.Debug().Msgf("Writing paste to disk, ID will be " + strconv.Itoa(pasteID))
data, err := json.Marshal(p)
if err != nil {
ff.writeMutex.Unlock()
return 0, err
}
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"), data, 0644)
if err != nil {
ff.writeMutex.Unlock()
return 0, err
}
// Add it to cache.
indexData := &Index{}
indexData.ID = pasteID
indexData.Private = p.Private
ff.pastesIndex = append(ff.pastesIndex, indexData)
ff.writeMutex.Unlock()
return int64(pasteID), nil
}
func (ff *FlatFiles) Shutdown() {
c.Logger.Info().Msg("Saving indexes...")
indexData, err := json.Marshal(ff.pastesIndex)
if err != nil {
c.Logger.Error().Msgf("Failed to encode index data into JSON: %s", err.Error())

View File

@@ -27,7 +27,7 @@ package mysql
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
)
var (
@@ -38,5 +38,6 @@ var (
func New(cc *context.Context) {
c = cc
d = &Database{}
c.Database.RegisterDialect(dialectinterface.Interface(Handler{}))
}

View File

@@ -30,6 +30,7 @@ import (
)
func InitialUp(tx *sql.Tx) error {
// nolint
_, err := tx.Exec("CREATE TABLE `pastes` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Paste ID', `title` text NOT NULL COMMENT 'Paste title', `data` longtext NOT NULL COMMENT 'Paste data', `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Paste creation timestamp', `keep_for` int(4) NOT NULL DEFAULT 1 COMMENT 'Keep for integer. 0 - forever.', `keep_for_unit_type` int(1) NOT NULL DEFAULT 1 COMMENT 'Keep for unit type. 1 - minutes, 2 - hours, 3 - days, 4 - months.', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Pastes';")
if err != nil {
return err

View File

@@ -69,7 +69,9 @@ func (db *Database) GetDatabaseConnection() *sql.DB {
// GetPaste returns a single paste by ID.
func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
db.check()
p := &structs.Paste{}
err := db.db.Get(p, db.db.Rebind("SELECT * FROM `pastes` WHERE id=?"), pasteID)
if err != nil {
return nil, err
@@ -80,8 +82,11 @@ func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
db.check()
var pastesRaw []structs.Paste
var pastes []structs.Paste
var (
pastesRaw []structs.Paste
pastes []structs.Paste
)
// Pagination.
var startPagination = 0
@@ -105,8 +110,12 @@ func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
func (db *Database) GetPastesPages() int {
db.check()
var pastesRaw []structs.Paste
var pastes []structs.Paste
var (
pastesRaw []structs.Paste
pastes []structs.Paste
)
err := db.db.Get(&pastesRaw, "SELECT * FROM `pastes` WHERE private != true")
if err != nil {
return 1
@@ -155,6 +164,7 @@ func (db *Database) Initialize() {
_ = dbConn.MustExec("SET @@session.time_zone='+00:00';")
c.Logger.Info().Msg("Database connection established")
db.db = dbConn
// Perform migrations.
@@ -164,6 +174,7 @@ func (db *Database) Initialize() {
func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
db.check()
result, err := db.db.NamedExec("INSERT INTO `pastes` (title, data, created_at, keep_for, keep_for_unit_type, language, private, password, password_salt) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language, :private, :password, :password_salt)", p)
if err != nil {
return 0, err

View File

@@ -27,7 +27,7 @@ package postgresql
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
)
var (
@@ -38,5 +38,6 @@ var (
func New(cc *context.Context) {
c = cc
d = &Database{}
c.Database.RegisterDialect(dialectinterface.Interface(Handler{}))
}

View File

@@ -36,6 +36,7 @@ import (
// other
"github.com/jmoiron/sqlx"
// postgresql adapter
_ "github.com/lib/pq"
)
@@ -70,7 +71,9 @@ func (db *Database) GetDatabaseConnection() *sql.DB {
// GetPaste returns a single paste by ID.
func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
db.check()
p := &structs.Paste{}
err := db.db.Get(p, db.db.Rebind("SELECT * FROM pastes WHERE id=$1"), pasteID)
if err != nil {
return nil, err
@@ -88,8 +91,11 @@ func (db *Database) GetPaste(pasteID int) (*structs.Paste, error) {
func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
db.check()
var pastesRaw []structs.Paste
var pastes []structs.Paste
var (
pastesRaw []structs.Paste
pastes []structs.Paste
)
// Pagination.
var startPagination = 0
@@ -119,8 +125,12 @@ func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
func (db *Database) GetPastesPages() int {
db.check()
var pastesRaw []structs.Paste
var pastes []structs.Paste
var (
pastesRaw []structs.Paste
pastes []structs.Paste
)
err := db.db.Get(&pastesRaw, "SELECT * FROM pastes WHERE private != true")
if err != nil {
return 1
@@ -164,6 +174,7 @@ func (db *Database) Initialize() {
}
c.Logger.Info().Msg("Database connection established")
db.db = dbConn
// Perform migrations.
@@ -173,12 +184,14 @@ func (db *Database) Initialize() {
func (db *Database) SavePaste(p *structs.Paste) (int64, error) {
db.check()
stmt, err := db.db.PrepareNamed("INSERT INTO pastes (title, data, created_at, keep_for, keep_for_unit_type, language, private, password, password_salt) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language, :private, :password, :password_salt) RETURNING id")
if err != nil {
return 0, err
}
var id int64
err = stmt.Get(&id, p)
if err != nil {
return 0, err

View File

@@ -27,7 +27,7 @@ package database
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
"go.dev.pztrn.name/fastpastebin/internal/database/interface"
databaseinterface "go.dev.pztrn.name/fastpastebin/internal/database/interface"
)
var (
@@ -39,5 +39,6 @@ var (
func New(cc *context.Context) {
c = cc
d = &Database{}
c.RegisterDatabaseInterface(databaseinterface.Interface(Handler{}))
}

View File

@@ -29,7 +29,8 @@ import (
"database/sql"
// local
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@@ -29,7 +29,7 @@ import (
"database/sql"
// local
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@@ -10,6 +10,7 @@ import (
)
// CreateHTML creates pagination HTML based on passed parameters.
// nolint
func CreateHTML(currentPage int, pages int, linksBase string) string {
// Load templates.
paginationHTMLRaw, err := static.ReadFile("pagination.html")
@@ -38,12 +39,15 @@ func CreateHTML(currentPage int, pages int, linksBase string) string {
paginationString = strings.Replace(string(paginationLinkCurrentRaw), "{pageNum}", strconv.Itoa(currentPage), -1)
} else {
paginationString = strings.Replace(string(paginationLinkRaw), "{pageNum}", "1", -1)
paginationString = strings.Replace(string(paginationString), "{paginationLink}", linksBase+"1", -1)
paginationString = strings.Replace(paginationString, "{paginationLink}", linksBase+"1", -1)
}
var ellipsisStartAdded = false
var ellipsisEndAdded = false
i := 2
var (
ellipsisStartAdded = false
ellipsisEndAdded = false
i = 2
)
for i <= pages {
if pages > 5 {
if currentPage-3 < i && currentPage+3 > i || i == pages {

View File

@@ -36,22 +36,22 @@ import (
)
const (
PASTE_KEEP_FOREVER = 0
PASTE_KEEP_FOR_MINUTES = 1
PASTE_KEEP_FOR_HOURS = 2
PASTE_KEEP_FOR_DAYS = 3
PASTE_KEEP_FOR_MONTHS = 4
PasteKeepForever = 0
PasteKeepForMinutes = 1
PasteKeepForHours = 2
PasteKeepForDays = 3
PasteKeepForMonths = 4
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
var (
PASTE_KEEPS_CORELLATION = map[string]int{
"M": PASTE_KEEP_FOR_MINUTES,
"h": PASTE_KEEP_FOR_HOURS,
"d": PASTE_KEEP_FOR_DAYS,
"m": PASTE_KEEP_FOR_MONTHS,
"forever": PASTE_KEEP_FOREVER,
PasteKeepsCorellation = map[string]int{
"M": PasteKeepForMinutes,
"h": PasteKeepForHours,
"d": PasteKeepForDays,
"m": PasteKeepForMonths,
"forever": PasteKeepForever,
}
)
@@ -74,6 +74,7 @@ func (p *Paste) CreatePassword(password string) error {
// Create salt - random string.
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
saltBytes := make([]byte, 64)
for i := range saltBytes {
saltBytes[i] = charset[seededRand.Intn(len(charset))]
}
@@ -86,6 +87,7 @@ func (p *Paste) CreatePassword(password string) error {
if err != nil {
return err
}
passwordHashBytes := sha256.Sum256(passwordCrypted)
p.Password = fmt.Sprintf("%x", passwordHashBytes)
@@ -100,16 +102,17 @@ func (p *Paste) GenerateCryptedCookieValue() string {
func (p *Paste) GetExpirationTime() time.Time {
var expirationTime time.Time
switch p.KeepForUnitType {
case PASTE_KEEP_FOREVER:
case PasteKeepForever:
expirationTime = time.Now().UTC().Add(time.Hour * 1)
case PASTE_KEEP_FOR_MINUTES:
case PasteKeepForMinutes:
expirationTime = p.CreatedAt.Add(time.Minute * time.Duration(p.KeepFor))
case PASTE_KEEP_FOR_HOURS:
case PasteKeepForHours:
expirationTime = p.CreatedAt.Add(time.Hour * time.Duration(p.KeepFor))
case PASTE_KEEP_FOR_DAYS:
case PasteKeepForDays:
expirationTime = p.CreatedAt.Add(time.Hour * 24 * time.Duration(p.KeepFor))
case PASTE_KEEP_FOR_MONTHS:
case PasteKeepForMonths:
expirationTime = p.CreatedAt.Add(time.Hour * 24 * 30 * time.Duration(p.KeepFor))
}
@@ -121,11 +124,7 @@ func (p *Paste) IsExpired() bool {
curTime := time.Now().UTC()
expirationTime := p.GetExpirationTime()
if curTime.Sub(expirationTime).Seconds() > 0 {
return true
}
return false
return curTime.Sub(expirationTime).Seconds() > 0
}
// VerifyPassword verifies that provided password is valid.
@@ -135,12 +134,9 @@ func (p *Paste) VerifyPassword(password string) bool {
if err != nil {
return false
}
passwordHashBytes := sha256.Sum256(passwordCrypted)
providedPassword := fmt.Sprintf("%x", passwordHashBytes)
if providedPassword == p.Password {
return true
}
return false
return providedPassword == p.Password
}