2018-10-27 03:21:02 +00:00
/ *
*
* Gosora Common Resources
2019-01-21 12:27:59 +00:00
* Copyright Azareal 2018 - 2020
2018-10-27 03:21:02 +00:00
*
* /
package common // import "github.com/Azareal/Gosora/common"
2017-11-10 03:33:11 +00:00
2017-11-11 23:34:27 +00:00
import (
"database/sql"
2019-02-23 06:29:19 +00:00
"io"
2017-11-13 05:22:37 +00:00
"log"
2019-02-23 06:29:19 +00:00
"os"
2018-05-31 06:51:31 +00:00
"sync/atomic"
2018-05-27 09:36:35 +00:00
"time"
2018-12-27 05:42:41 +00:00
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2017-11-11 23:34:27 +00:00
)
2017-11-10 03:33:11 +00:00
2019-04-08 07:50:10 +00:00
var SoftwareVersion = Version { Major : 0 , Minor : 3 , Patch : 0 , Tag : "dev" }
2018-10-02 05:03:20 +00:00
2017-11-11 04:06:16 +00:00
// nolint I don't want to write comments for each of these o.o
2018-05-16 10:46:14 +00:00
const Hour int = 60 * 60
const Day int = Hour * 24
const Week int = Day * 7
const Month int = Day * 30
const Year int = Day * 365
const Kilobyte int = 1024
const Megabyte int = Kilobyte * 1024
const Gigabyte int = Megabyte * 1024
const Terabyte int = Gigabyte * 1024
const Petabyte int = Terabyte * 1024
2017-11-11 04:06:16 +00:00
2018-05-27 09:36:35 +00:00
var StartTime time . Time
2017-11-11 04:06:16 +00:00
var TmplPtrMap = make ( map [ string ] interface { } )
2018-05-31 06:51:31 +00:00
// Anti-spam token with rotated key
2018-06-17 07:28:18 +00:00
var JSTokenBox atomic . Value // TODO: Move this and some of these other globals somewhere else
2018-12-27 05:42:41 +00:00
var SessionSigningKeyBox atomic . Value // For MFA to avoid hitting the database unneccessarily
2018-06-17 07:28:18 +00:00
var OldSessionSigningKeyBox atomic . Value // Just in case we've signed with a key that's about to go stale so we don't annoy the user too much
2018-07-15 10:29:31 +00:00
var IsDBDown int32 = 0 // 0 = false, 1 = true. this is value which should be manipulated with package atomic for representing whether the database is down so we don't spam the log with lots of redundant errors
2018-05-31 06:51:31 +00:00
2017-11-10 03:33:11 +00:00
// ErrNoRows is an alias of sql.ErrNoRows, just in case we end up with non-database/sql datastores
var ErrNoRows = sql . ErrNoRows
2017-11-11 04:06:16 +00:00
// ? - Make this more customisable?
var ExternalSites = map [ string ] string {
"YT" : "https://www.youtube.com/" ,
}
2019-05-18 01:18:19 +00:00
// TODO: Make this more customisable
2019-05-19 03:21:09 +00:00
var SpammyDomainBits = [ ] string { "porn" , "sex" , "lesbian" , "acup" , "nude" , "milf" , "tits" , "vape" , "busty" , "kink" , "lingerie" , "problog" , "fet" , "xblog" , "blogin" , "blognetwork" }
2019-05-18 01:18:19 +00:00
2017-11-11 04:06:16 +00:00
type StringList [ ] string
// ? - Should we allow users to upload .php or .go files? It could cause security issues. We could store them with a mangled extension to render them inert
// TODO: Let admins manage this from the Control Panel
2018-08-07 04:14:28 +00:00
// apng is commented out for now, as we have no way of re-encoding it into a smaller file
2017-11-11 04:06:16 +00:00
var AllowedFileExts = StringList {
2018-07-28 12:52:23 +00:00
"png" , "jpg" , "jpeg" , "svg" , "bmp" , "gif" , "tif" , "webp" , /*"apng",*/ // images
2017-11-11 04:06:16 +00:00
2018-08-07 04:14:28 +00:00
"txt" , "xml" , "json" , "yaml" , "toml" , "ini" , "md" , "html" , "rtf" , "js" , "py" , "rb" , "css" , "scss" , "less" , "eqcss" , "pcss" , "java" , "ts" , "cs" , "c" , "cc" , "cpp" , "cxx" , "C" , "c++" , "h" , "hh" , "hpp" , "hxx" , "h++" , "rs" , "rlib" , "htaccess" , "gitignore" , /*"go","php",*/ // text
2017-11-11 04:06:16 +00:00
"mp3" , "mp4" , "avi" , "wmv" , "webm" , // video
"otf" , "woff2" , "woff" , "ttf" , "eot" , // fonts
}
var ImageFileExts = StringList {
2018-08-07 04:14:28 +00:00
"png" , "jpg" , "jpeg" , "svg" , "bmp" , "gif" , "tif" , "webp" , /* "apng",*/
2017-11-11 04:06:16 +00:00
}
var ArchiveFileExts = StringList {
"bz2" , "zip" , "gz" , "7z" , "tar" , "cab" ,
}
var ExecutableFileExts = StringList {
"exe" , "jar" , "phar" , "shar" , "iso" ,
}
2018-05-31 06:51:31 +00:00
func init ( ) {
JSTokenBox . Store ( "" )
2018-06-17 07:28:18 +00:00
SessionSigningKeyBox . Store ( "" )
OldSessionSigningKeyBox . Store ( "" )
2018-05-31 06:51:31 +00:00
}
2017-11-11 04:06:16 +00:00
// TODO: Write a test for this
func ( slice StringList ) Contains ( needle string ) bool {
for _ , item := range slice {
if item == needle {
return true
}
}
return false
}
2017-11-12 03:29:05 +00:00
type dbInits [ ] func ( acc * qgen . Accumulator ) error
2017-11-11 04:06:16 +00:00
2017-11-11 23:34:27 +00:00
var DbInits dbInits
2017-11-11 04:06:16 +00:00
2017-11-11 23:34:27 +00:00
func ( inits dbInits ) Run ( ) error {
2017-11-11 04:06:16 +00:00
for _ , init := range inits {
2018-08-04 11:46:36 +00:00
err := init ( qgen . NewAcc ( ) )
2017-11-11 04:06:16 +00:00
if err != nil {
return err
}
}
return nil
}
2017-11-12 03:29:05 +00:00
func ( inits dbInits ) Add ( init ... func ( acc * qgen . Accumulator ) error ) {
2017-11-11 23:34:27 +00:00
DbInits = dbInits ( append ( DbInits , init ... ) )
2017-11-11 04:06:16 +00:00
}
2017-11-13 05:22:37 +00:00
2018-08-22 01:32:07 +00:00
// TODO: Add a graceful shutdown function
func StoppedServer ( msg ... interface { } ) {
//log.Print("stopped server")
StopServerChan <- msg
}
var StopServerChan = make ( chan [ ] interface { } )
2019-02-23 06:29:19 +00:00
var LogWriter = io . MultiWriter ( os . Stderr )
2018-02-19 04:26:01 +00:00
func DebugDetail ( args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . SuperDebug {
log . Print ( args ... )
}
}
2018-02-19 04:26:01 +00:00
func DebugDetailf ( str string , args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . SuperDebug {
log . Printf ( str , args ... )
}
}
2018-02-19 04:26:01 +00:00
func DebugLog ( args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . DebugMode {
log . Print ( args ... )
}
}
2018-02-19 04:26:01 +00:00
func DebugLogf ( str string , args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . DebugMode {
log . Printf ( str , args ... )
}
}
2019-04-08 07:44:41 +00:00
func Log ( args ... interface { } ) {
log . Print ( args ... )
}
func Logf ( str string , args ... interface { } ) {
log . Printf ( str , args ... )
}