2015-09-24 07:44:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2015-10-01 16:32:59 +02:00
|
|
|
"net/http/fcgi"
|
2015-09-26 04:03:14 +02:00
|
|
|
"os"
|
2015-09-28 04:17:12 +02:00
|
|
|
"regexp"
|
2015-10-01 16:32:59 +02:00
|
|
|
"strconv"
|
2015-09-30 21:54:30 +02:00
|
|
|
"time"
|
2015-09-24 07:44:49 +02:00
|
|
|
|
2015-09-29 02:46:58 +02:00
|
|
|
"github.com/GeertJohan/go.rice"
|
2015-09-25 05:21:37 +02:00
|
|
|
"github.com/flosch/pongo2"
|
2015-09-24 07:44:49 +02:00
|
|
|
"github.com/zenazn/goji"
|
2015-09-25 18:00:14 +02:00
|
|
|
"github.com/zenazn/goji/web/middleware"
|
2015-09-24 07:44:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var Config struct {
|
2015-10-04 23:58:00 +02:00
|
|
|
bind string
|
|
|
|
filesDir string
|
|
|
|
metaDir string
|
|
|
|
noLogs bool
|
|
|
|
allowHotlink bool
|
|
|
|
siteName string
|
|
|
|
siteURL string
|
|
|
|
fastcgi bool
|
|
|
|
remoteUploads bool
|
|
|
|
contentSecurityPolicy string
|
|
|
|
fileContentSecurityPolicy string
|
|
|
|
xFrameOptions string
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 02:46:58 +02:00
|
|
|
var Templates = make(map[string]*pongo2.Template)
|
|
|
|
var TemplateSet *pongo2.TemplateSet
|
2015-09-30 21:54:30 +02:00
|
|
|
var staticBox *rice.Box
|
|
|
|
var timeStarted time.Time
|
2015-10-01 16:32:59 +02:00
|
|
|
var timeStartedStr string
|
2015-09-29 02:46:58 +02:00
|
|
|
|
2015-09-28 18:30:21 +02:00
|
|
|
func setup() {
|
2015-10-04 23:58:00 +02:00
|
|
|
goji.Use(ContentSecurityPolicy(CSPOptions{
|
|
|
|
policy: Config.contentSecurityPolicy,
|
|
|
|
frame: Config.xFrameOptions,
|
|
|
|
}))
|
|
|
|
|
2015-09-25 18:00:14 +02:00
|
|
|
if Config.noLogs {
|
|
|
|
goji.Abandon(middleware.Logger)
|
|
|
|
}
|
2015-09-25 05:21:37 +02:00
|
|
|
|
2015-09-28 04:17:12 +02:00
|
|
|
// make directories if needed
|
2015-09-29 02:46:58 +02:00
|
|
|
err := os.MkdirAll(Config.filesDir, 0755)
|
2015-09-26 04:03:14 +02:00
|
|
|
if err != nil {
|
2015-10-06 08:49:57 +02:00
|
|
|
log.Fatal("Could not create files directory:", err)
|
2015-09-26 04:03:14 +02:00
|
|
|
}
|
2015-09-25 18:47:55 +02:00
|
|
|
|
2015-09-28 04:17:12 +02:00
|
|
|
err = os.MkdirAll(Config.metaDir, 0700)
|
|
|
|
if err != nil {
|
2015-10-06 08:49:57 +02:00
|
|
|
log.Fatal("Could not create metadata directory:", err)
|
2015-09-28 04:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure siteURL ends wth '/'
|
|
|
|
if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
|
|
|
|
Config.siteURL = Config.siteURL + "/"
|
|
|
|
}
|
|
|
|
|
2015-09-29 02:46:58 +02:00
|
|
|
// Template setup
|
2015-09-29 03:58:50 +02:00
|
|
|
p2l, err := NewPongo2TemplatesLoader()
|
2015-09-29 02:46:58 +02:00
|
|
|
if err != nil {
|
2015-10-06 08:49:57 +02:00
|
|
|
log.Fatal("Error: could not load templates", err)
|
2015-09-29 02:46:58 +02:00
|
|
|
}
|
|
|
|
TemplateSet := pongo2.NewSet("templates", p2l)
|
|
|
|
TemplateSet.Globals["sitename"] = Config.siteName
|
|
|
|
err = populateTemplatesMap(TemplateSet, Templates)
|
|
|
|
if err != nil {
|
2015-10-06 08:49:57 +02:00
|
|
|
log.Fatal("Error: could not load templates", err)
|
2015-09-29 02:46:58 +02:00
|
|
|
}
|
2015-09-25 05:21:37 +02:00
|
|
|
|
2015-09-30 21:54:30 +02:00
|
|
|
staticBox = rice.MustFindBox("static")
|
|
|
|
timeStarted = time.Now()
|
2015-10-01 16:32:59 +02:00
|
|
|
timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)
|
2015-09-30 21:54:30 +02:00
|
|
|
|
2015-09-25 05:21:37 +02:00
|
|
|
// Routing setup
|
2015-09-24 07:44:49 +02:00
|
|
|
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
|
|
|
|
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
|
2015-09-29 05:46:43 +02:00
|
|
|
selifIndexRe := regexp.MustCompile(`^/selif/$`)
|
2015-09-29 07:58:14 +02:00
|
|
|
torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)
|
2015-09-24 07:44:49 +02:00
|
|
|
|
|
|
|
goji.Get("/", indexHandler)
|
2015-09-30 06:56:51 +02:00
|
|
|
goji.Get("/paste/", pasteHandler)
|
|
|
|
goji.Get("/paste", http.RedirectHandler("/paste/", 301))
|
2015-09-25 18:00:14 +02:00
|
|
|
|
2015-10-02 02:58:08 +02:00
|
|
|
if Config.remoteUploads {
|
|
|
|
goji.Get("/upload", uploadRemote)
|
|
|
|
goji.Get("/upload/", uploadRemote)
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
goji.Post("/upload", uploadPostHandler)
|
2015-09-28 22:02:03 +02:00
|
|
|
goji.Post("/upload/", uploadPostHandler)
|
2015-09-24 07:44:49 +02:00
|
|
|
goji.Put("/upload", uploadPutHandler)
|
2015-09-25 18:00:14 +02:00
|
|
|
goji.Put("/upload/:name", uploadPutHandler)
|
2015-09-29 05:46:43 +02:00
|
|
|
goji.Delete("/:name", deleteHandler)
|
2015-09-25 18:00:14 +02:00
|
|
|
|
2015-09-30 21:54:30 +02:00
|
|
|
goji.Get("/static/*", staticHandler)
|
2015-10-04 18:58:30 +02:00
|
|
|
goji.Get("/favicon.ico", staticHandler)
|
2015-10-05 03:12:19 +02:00
|
|
|
goji.Get("/robots.txt", staticHandler)
|
2015-09-24 07:44:49 +02:00
|
|
|
goji.Get(nameRe, fileDisplayHandler)
|
2015-09-24 22:04:51 +02:00
|
|
|
goji.Get(selifRe, fileServeHandler)
|
2015-09-29 05:46:43 +02:00
|
|
|
goji.Get(selifIndexRe, unauthorizedHandler)
|
2015-09-29 07:58:14 +02:00
|
|
|
goji.Get(torrentRe, fileTorrentHandler)
|
2015-09-25 18:00:14 +02:00
|
|
|
goji.NotFound(notFoundHandler)
|
2015-09-28 18:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
|
|
|
|
"host to bind to (default: 127.0.0.1:8080)")
|
|
|
|
flag.StringVar(&Config.filesDir, "filespath", "files/",
|
|
|
|
"path to files directory")
|
|
|
|
flag.StringVar(&Config.metaDir, "metapath", "meta/",
|
|
|
|
"path to metadata directory")
|
|
|
|
flag.BoolVar(&Config.noLogs, "nologs", false,
|
|
|
|
"remove stdout output for each request")
|
2015-09-30 01:28:10 +02:00
|
|
|
flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
|
|
|
|
"Allow hotlinking of files")
|
2015-09-28 18:30:21 +02:00
|
|
|
flag.StringVar(&Config.siteName, "sitename", "linx",
|
|
|
|
"name of the site")
|
|
|
|
flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
|
|
|
|
"site base url (including trailing slash)")
|
2015-10-01 16:32:59 +02:00
|
|
|
flag.BoolVar(&Config.fastcgi, "fastcgi", false,
|
|
|
|
"serve through fastcgi")
|
2015-10-02 02:58:08 +02:00
|
|
|
flag.BoolVar(&Config.remoteUploads, "remoteuploads", false,
|
|
|
|
"enable remote uploads")
|
2015-10-05 04:43:42 +02:00
|
|
|
flag.StringVar(&Config.contentSecurityPolicy, "contentsecuritypolicy",
|
2015-10-05 00:11:23 +02:00
|
|
|
"default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; referrer none;",
|
2015-10-04 23:58:00 +02:00
|
|
|
"value of default Content-Security-Policy header")
|
2015-10-05 04:43:42 +02:00
|
|
|
flag.StringVar(&Config.fileContentSecurityPolicy, "filecontentsecuritypolicy",
|
2015-10-04 23:58:00 +02:00
|
|
|
"default-src 'none'; img-src 'self'; object-src 'self'; media-src 'self'; sandbox; referrer none;",
|
|
|
|
"value of Content-Security-Policy header for file access")
|
2015-10-05 04:43:42 +02:00
|
|
|
flag.StringVar(&Config.xFrameOptions, "xframeoptions", "SAMEORIGIN",
|
2015-10-04 23:58:00 +02:00
|
|
|
"value of X-Frame-Options header")
|
2015-09-28 18:30:21 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
setup()
|
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
listener, err := net.Listen("tcp", Config.bind)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not bind: ", err)
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:32:59 +02:00
|
|
|
if Config.fastcgi {
|
|
|
|
fcgi.Serve(listener, goji.DefaultMux)
|
|
|
|
} else {
|
|
|
|
goji.ServeListener(listener)
|
|
|
|
}
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|