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"
2021-04-07 14:23:11 +00:00
"net"
"net/http"
2019-02-23 06:29:19 +00:00
"os"
2021-02-19 03:28:20 +00:00
"strconv"
2021-03-24 12:07:46 +00:00
"strings"
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
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
meta "github.com/Azareal/Gosora/common/meta"
2020-03-07 02:59:06 +00:00
qgen "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
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
var Meta meta . MetaStore
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
2020-04-30 21:32:34 +00:00
const Day = Hour * 24
const Week = Day * 7
const Month = Day * 30
const Year = Day * 365
2018-05-16 10:46:14 +00:00
const Kilobyte int = 1024
2020-04-30 21:32:34 +00:00
const Megabyte = Kilobyte * 1024
const Gigabyte = Megabyte * 1024
const Terabyte = Gigabyte * 1024
const Petabyte = Terabyte * 1024
2017-11-11 04:06:16 +00:00
2018-05-27 09:36:35 +00:00
var StartTime time . Time
2020-03-07 02:59:06 +00:00
var GzipStartEtag string
var StartEtag string
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
2020-07-31 05:58:17 +00:00
//var StrSlicePool sync.Pool
2017-11-11 04:06:16 +00:00
// ? - Make this more customisable?
2020-03-08 04:15:27 +00:00
/ * var ExternalSites = map [ string ] string {
2017-11-11 04:06:16 +00:00
"YT" : "https://www.youtube.com/" ,
2020-03-08 04:15:27 +00:00
} * /
2017-11-11 04:06:16 +00:00
2019-05-18 01:18:19 +00:00
// TODO: Make this more customisable
2020-10-20 21:31:51 +00:00
var SpammyDomainBits = [ ] string { "porn" , "sex" , "acup" , "nude" , "milf" , "tits" , "vape" , "busty" , "kink" , "lingerie" , "strapon" , "problog" , "fet" , "xblog" , "blogin" , "blognetwork" , "relayblog" }
2020-03-08 04:15:27 +00:00
2020-03-22 04:37:45 +00:00
var Chrome , Firefox int // ! Temporary Hack for http push
var SimpleBots [ ] int // ! Temporary hack to stop semrush, ahrefs, python bots and other from wasting resources
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 {
2021-02-19 03:28:20 +00:00
"png" , "jpg" , "jpe" , "jpeg" , "jif" , "jfi" , "jfif" , "svg" , "bmp" , "gif" , "tiff" , "tif" , "webp" , "apng" , "avif" , "flif" , "heif" , "heic" , "bpg" , // images (encodable) + apng (browser support) + bpg + avif + flif + heif / heic
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
2021-02-19 03:28:20 +00:00
"wav" , "mp3" , "oga" , "m4a" , "flac" , "ac3" , "aac" , "opus" , // audio
2020-04-30 21:32:34 +00:00
add avif, flac, ac3, aac, f4v, xvid, mov, movie, qt, xpi, docx and pdf file extensions
2020-10-21 04:31:33 +00:00
"mp4" , "avi" , "ogg" , "ogv" , "ogx" , "wmv" , "webm" , "flv" , "f4v" , "xvid" , "mov" , "movie" , "qt" , // video
2017-11-11 04:06:16 +00:00
"otf" , "woff2" , "woff" , "ttf" , "eot" , // fonts
2019-10-31 23:17:26 +00:00
add avif, flac, ac3, aac, f4v, xvid, mov, movie, qt, xpi, docx and pdf file extensions
2020-10-21 04:31:33 +00:00
"bz2" , "zip" , "zipx" , "gz" , "7z" , "tar" , "cab" , "rar" , "kgb" , "pea" , "xz" , "zz" , "tgz" , "xpi" , // archives
2020-04-30 21:32:34 +00:00
add avif, flac, ac3, aac, f4v, xvid, mov, movie, qt, xpi, docx and pdf file extensions
2020-10-21 04:31:33 +00:00
"docx" , "pdf" , // documents
2017-11-11 04:06:16 +00:00
}
var ImageFileExts = StringList {
add avif, flac, ac3, aac, f4v, xvid, mov, movie, qt, xpi, docx and pdf file extensions
2020-10-21 04:31:33 +00:00
"png" , "jpg" , "jpe" , "jpeg" , "jif" , "jfi" , "jfif" , "svg" , "bmp" , "gif" , "tiff" , "tif" , "webp" , /* "apng", "bpg", "avif", */
2020-04-30 21:32:34 +00:00
}
2020-06-08 07:04:47 +00:00
var TextFileExts = StringList {
"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",*/
}
2020-04-30 21:32:34 +00:00
var VideoFileExts = StringList {
add avif, flac, ac3, aac, f4v, xvid, mov, movie, qt, xpi, docx and pdf file extensions
2020-10-21 04:31:33 +00:00
"mp4" , "avi" , "ogg" , "ogv" , "ogx" , "wmv" , "webm" , "flv" , "f4v" , "xvid" , "mov" , "movie" , "qt" ,
2020-04-30 21:32:34 +00:00
}
var WebVideoFileExts = StringList {
2020-05-05 08:10:19 +00:00
"mp4" , "avi" , "ogg" , "ogv" , "webm" ,
}
var WebAudioFileExts = StringList {
2021-02-19 03:28:20 +00:00
"wav" , "mp3" , "oga" , "m4a" , "flac" ,
2017-11-11 04:06:16 +00:00
}
var ArchiveFileExts = StringList {
add avif, flac, ac3, aac, f4v, xvid, mov, movie, qt, xpi, docx and pdf file extensions
2020-10-21 04:31:33 +00:00
"bz2" , "zip" , "zipx" , "gz" , "7z" , "tar" , "cab" , "rar" , "kgb" , "pea" , "xz" , "zz" , "tgz" , "xpi" ,
2017-11-11 04:06:16 +00:00
}
var ExecutableFileExts = StringList {
2019-10-31 23:34:50 +00:00
"exe" , "jar" , "phar" , "shar" , "iso" , "apk" , "deb" ,
2017-11-11 04:06:16 +00:00
}
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
2020-04-30 21:32:34 +00:00
func ( sl StringList ) Contains ( needle string ) bool {
2020-08-12 08:24:41 +00:00
for _ , it := range sl {
if it == needle {
2017-11-11 04:06:16 +00:00
return true
}
}
return false
}
2020-04-30 21:32:34 +00:00
/ * var DbTables [ ] string
var TableToID = make ( map [ string ] int )
var IDToTable = make ( map [ int ] string )
func InitTables ( acc * qgen . Accumulator ) error {
stmt := acc . Select ( "tables" ) . Columns ( "id,name" ) . Prepare ( )
2020-08-12 08:24:41 +00:00
if e := acc . FirstError ( ) ; e != nil {
return e
2020-04-30 21:32:34 +00:00
}
return eachall ( stmt , func ( r * sql . Rows ) error {
var id int
var name string
2020-08-12 08:24:41 +00:00
if e := r . Scan ( & id , & name ) ; e != nil {
return e
2020-04-30 21:32:34 +00:00
}
TableToID [ name ] = id
IDToTable [ id ] = name
return nil
} )
} * /
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 {
2020-04-30 21:32:34 +00:00
for _ , i := range inits {
2020-10-20 21:31:51 +00:00
if e := i ( qgen . NewAcc ( ) ) ; e != nil {
return e
2017-11-11 04:06:16 +00:00
}
}
return nil
}
2020-08-12 08:24:41 +00:00
func ( inits dbInits ) Add ( i ... func ( acc * qgen . Accumulator ) error ) {
DbInits = dbInits ( append ( DbInits , i ... ) )
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 { } )
2021-04-07 02:10:05 +00:00
var LogWriter = io . MultiWriter ( os . Stdout )
var ErrLogWriter = io . MultiWriter ( os . Stderr )
var ErrLogger = log . New ( os . Stderr , "" , log . LstdFlags )
2019-02-23 06:29:19 +00:00
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 ... )
}
2021-04-07 02:10:05 +00:00
func Err ( args ... interface { } ) {
ErrLogger . Print ( args ... )
}
2019-04-08 07:44:41 +00:00
func Logf ( str string , args ... interface { } ) {
log . Printf ( str , args ... )
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
func Countf ( stmt * sql . Stmt , args ... interface { } ) ( count int ) {
err := stmt . QueryRow ( args ... ) . Scan ( & count )
if err != nil {
LogError ( err )
}
return count
}
func eachall ( stmt * sql . Stmt , f func ( r * sql . Rows ) error ) error {
2020-08-12 08:24:41 +00:00
rows , e := stmt . Query ( )
if e != nil {
return e
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
}
defer rows . Close ( )
for rows . Next ( ) {
2020-08-12 08:24:41 +00:00
if e := f ( rows ) ; e != nil {
return e
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
}
}
return rows . Err ( )
2020-03-07 02:59:06 +00:00
}
2021-02-19 03:28:20 +00:00
2021-04-07 02:10:05 +00:00
var qcache = [ ] string { 0 : "?" , 1 : "?,?" , 2 : "?,?,?" , 3 : "?,?,?,?" , 4 : "?,?,?,?,?" , 5 : "?,?,?,?,?,?" , 6 : "?,?,?,?,?,?,?" , 7 : "?,?,?,?,?,?,?,?" , 8 : "?,?,?,?,?,?,?,?,?" }
2021-02-19 03:28:20 +00:00
func inqbuild ( ids [ ] int ) ( [ ] interface { } , string ) {
2021-04-07 02:10:05 +00:00
if len ( ids ) < 8 {
2021-02-19 03:28:20 +00:00
idList := make ( [ ] interface { } , len ( ids ) )
for i , id := range ids {
idList [ i ] = strconv . Itoa ( id )
}
return idList , qcache [ len ( ids ) - 1 ]
}
2021-03-24 12:07:46 +00:00
var sb strings . Builder
sb . Grow ( ( len ( ids ) * 2 ) - 1 )
2021-02-19 03:28:20 +00:00
idList := make ( [ ] interface { } , len ( ids ) )
for i , id := range ids {
idList [ i ] = strconv . Itoa ( id )
if i == 0 {
2021-03-24 12:07:46 +00:00
sb . WriteRune ( '?' )
2021-02-19 03:28:20 +00:00
} else {
2021-03-24 12:07:46 +00:00
sb . WriteString ( ",?" )
2021-02-19 03:28:20 +00:00
}
}
2021-03-24 12:07:46 +00:00
return idList , sb . String ( )
}
func inqbuild2 ( count int ) string {
2021-04-07 02:10:05 +00:00
if count <= 8 {
2021-03-24 12:07:46 +00:00
return qcache [ count - 1 ]
}
var sb strings . Builder
sb . Grow ( ( count * 2 ) - 1 )
for i := 0 ; i <= count ; i ++ {
if i == 0 {
sb . WriteRune ( '?' )
} else {
sb . WriteString ( ",?" )
}
}
return sb . String ( )
2021-02-19 03:28:20 +00:00
}
func inqbuildstr ( strs [ ] string ) ( [ ] interface { } , string ) {
2021-04-07 02:10:05 +00:00
if len ( strs ) < 8 {
2021-02-19 03:28:20 +00:00
idList := make ( [ ] interface { } , len ( strs ) )
for i , id := range strs {
idList [ i ] = id
}
return idList , qcache [ len ( strs ) - 1 ]
}
2021-03-24 12:07:46 +00:00
var sb strings . Builder
sb . Grow ( ( len ( strs ) * 2 ) - 1 )
2021-02-19 03:28:20 +00:00
idList := make ( [ ] interface { } , len ( strs ) )
for i , id := range strs {
idList [ i ] = id
if i == 0 {
2021-03-24 12:07:46 +00:00
sb . WriteRune ( '?' )
2021-02-19 03:28:20 +00:00
} else {
2021-03-24 12:07:46 +00:00
sb . WriteString ( ",?" )
2021-02-19 03:28:20 +00:00
}
}
2021-03-24 12:07:46 +00:00
return idList , sb . String ( )
2021-02-19 03:28:20 +00:00
}
2021-04-07 14:23:11 +00:00
var ConnWatch = & ConnWatcher { }
type ConnWatcher struct {
n int64
}
func ( cw * ConnWatcher ) StateChange ( conn net . Conn , state http . ConnState ) {
switch state {
case http . StateNew :
atomic . AddInt64 ( & cw . n , 1 )
case http . StateHijacked , http . StateClosed :
atomic . AddInt64 ( & cw . n , - 1 )
}
}
func ( cw * ConnWatcher ) Count ( ) int {
return int ( atomic . LoadInt64 ( & cw . n ) )
}