2017-03-22 18:09:21 +01:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2017-03-23 00:02:36 +01:00
|
|
|
Copyright (c) 2014-2017 DutchCoders [https://github.com/dutchcoders/]
|
2017-03-22 18:09:21 +01:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-12-31 20:08:42 +01:00
|
|
|
crypto_rand "crypto/rand"
|
|
|
|
"encoding/binary"
|
2017-03-22 18:09:21 +01:00
|
|
|
"errors"
|
2020-04-04 14:29:33 +02:00
|
|
|
gorillaHandlers "github.com/gorilla/handlers"
|
2017-03-22 18:09:21 +01:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
2017-03-28 16:12:31 +02:00
|
|
|
"sync"
|
2017-03-22 18:09:21 +01:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
context "golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/ghost/handlers"
|
2017-03-28 17:08:34 +02:00
|
|
|
"github.com/VojtechVitek/ratelimit"
|
|
|
|
"github.com/VojtechVitek/ratelimit/memory"
|
2017-03-22 18:09:21 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
_ "net/http/pprof"
|
|
|
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
|
|
web "github.com/dutchcoders/transfer.sh-web"
|
|
|
|
assetfs "github.com/elazarl/go-bindata-assetfs"
|
|
|
|
|
|
|
|
autocert "golang.org/x/crypto/acme/autocert"
|
2018-07-07 12:05:58 +02:00
|
|
|
"path/filepath"
|
2017-03-22 18:09:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const SERVER_INFO = "transfer.sh"
|
|
|
|
|
|
|
|
// parse request with maximum memory of _24Kilobits
|
2019-04-28 21:36:45 +02:00
|
|
|
const _24K = (1 << 3) * 24
|
|
|
|
|
|
|
|
// parse request with maximum memory of _5Megabytes
|
|
|
|
const _5M = (1 << 20) * 5
|
2017-03-22 18:09:21 +01:00
|
|
|
|
|
|
|
type OptionFn func(*Server)
|
|
|
|
|
2017-03-22 23:13:34 +01:00
|
|
|
func ClamavHost(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.ClamAVDaemonHost = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 23:14:01 +01:00
|
|
|
func VirustotalKey(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.VirusTotalKey = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
func Listener(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.ListenerString = s
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-04 14:29:33 +02:00
|
|
|
func CorsDomains(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.CorsDomains = s
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-26 18:39:56 +02:00
|
|
|
func GoogleAnalytics(gaKey string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.gaKey = gaKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserVoice(userVoiceKey string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.userVoiceKey = userVoiceKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 19:05:16 +02:00
|
|
|
func TLSListener(s string, t bool) OptionFn {
|
2017-03-22 18:09:21 +01:00
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.TLSListenerString = s
|
2018-07-07 19:05:16 +02:00
|
|
|
srvr.TLSListenerOnly = t
|
2017-03-22 18:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProfileListener(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.ProfileListenerString = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WebPath(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
2018-07-09 00:39:57 +02:00
|
|
|
if s[len(s)-1:] != "/" {
|
2018-07-07 12:05:58 +02:00
|
|
|
s = s + string(filepath.Separator)
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
srvr.webPath = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 12:35:57 +01:00
|
|
|
func ProxyPath(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
if s[len(s)-1:] != "/" {
|
|
|
|
s = s + string(filepath.Separator)
|
|
|
|
}
|
|
|
|
|
|
|
|
srvr.proxyPath = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:19:32 +02:00
|
|
|
func ProxyPort(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.proxyPort = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
func TempPath(s string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
2018-07-09 00:39:57 +02:00
|
|
|
if s[len(s)-1:] != "/" {
|
2018-07-07 12:05:58 +02:00
|
|
|
s = s + string(filepath.Separator)
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
srvr.tempPath = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 00:15:55 +02:00
|
|
|
func LogFile(logger *log.Logger, s string) OptionFn {
|
2017-03-22 18:09:21 +01:00
|
|
|
return func(srvr *Server) {
|
|
|
|
f, err := os.OpenFile(s, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error opening file: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-27 00:15:55 +02:00
|
|
|
logger.SetOutput(f)
|
|
|
|
srvr.logger = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Logger(logger *log.Logger) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.logger = logger
|
2017-03-22 18:09:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 20:08:42 +01:00
|
|
|
func MaxUploadSize(kbytes int64) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.maxUploadSize = kbytes * 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-03-28 17:08:34 +02:00
|
|
|
func RateLimit(requests int) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.rateLimitRequests = requests
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 08:26:07 +02:00
|
|
|
func RandomTokenLength(length int64) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.randomTokenLength = length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 17:23:47 +01:00
|
|
|
func Purge(days, interval int) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.purgeDays = time.Duration(days) * time.Hour * 24
|
|
|
|
srvr.purgeInterval = time.Duration(interval) * time.Hour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
func ForceHTTPs() OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.forceHTTPs = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnableProfiler() OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.profilerEnabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UseStorage(s Storage) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.storage = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UseLetsEncrypt(hosts []string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
cacheDir := "./cache/"
|
|
|
|
|
|
|
|
m := autocert.Manager{
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
Cache: autocert.DirCache(cacheDir),
|
|
|
|
HostPolicy: func(_ context.Context, host string) error {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, h := range hosts {
|
|
|
|
found = found || strings.HasSuffix(host, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return errors.New("acme/autocert: host not configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
srvr.tlsConfig = &tls.Config{
|
|
|
|
GetCertificate: m.GetCertificate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TLSConfig(cert, pk string) OptionFn {
|
|
|
|
certificate, err := tls.LoadX509KeyPair(cert, pk)
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.tlsConfig = &tls.Config{
|
|
|
|
GetCertificate: func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
return &certificate, err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 18:46:28 +02:00
|
|
|
func HttpAuthCredentials(user string, pass string) OptionFn {
|
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.AuthUser = user
|
|
|
|
srvr.AuthPass = pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 14:42:59 +02:00
|
|
|
func FilterOptions(options IPFilterOptions) OptionFn {
|
2019-05-12 10:14:27 +02:00
|
|
|
for i, allowedIP := range options.AllowedIPs {
|
|
|
|
options.AllowedIPs[i] = strings.TrimSpace(allowedIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, blockedIP := range options.BlockedIPs {
|
|
|
|
options.BlockedIPs[i] = strings.TrimSpace(blockedIP)
|
|
|
|
}
|
|
|
|
|
2019-05-11 14:42:59 +02:00
|
|
|
return func(srvr *Server) {
|
|
|
|
srvr.ipFilterOptions = &options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
type Server struct {
|
2018-06-23 18:46:28 +02:00
|
|
|
AuthUser string
|
|
|
|
AuthPass string
|
|
|
|
|
2018-10-27 00:15:55 +02:00
|
|
|
logger *log.Logger
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
tlsConfig *tls.Config
|
|
|
|
|
|
|
|
profilerEnabled bool
|
|
|
|
|
2017-03-28 16:12:31 +02:00
|
|
|
locks map[string]*sync.Mutex
|
|
|
|
|
2020-12-31 20:08:42 +01:00
|
|
|
maxUploadSize int64
|
2017-03-28 17:08:34 +02:00
|
|
|
rateLimitRequests int
|
|
|
|
|
2021-01-05 17:24:16 +01:00
|
|
|
purgeDays time.Duration
|
|
|
|
purgeInterval time.Duration
|
2021-01-05 17:23:47 +01:00
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
storage Storage
|
|
|
|
|
|
|
|
forceHTTPs bool
|
|
|
|
|
2021-05-20 08:26:07 +02:00
|
|
|
randomTokenLength int64
|
|
|
|
|
2019-05-11 14:42:59 +02:00
|
|
|
ipFilterOptions *IPFilterOptions
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
VirusTotalKey string
|
|
|
|
ClamAVDaemonHost string
|
|
|
|
|
|
|
|
tempPath string
|
|
|
|
|
2018-06-26 18:39:56 +02:00
|
|
|
webPath string
|
2019-05-18 14:13:23 +02:00
|
|
|
proxyPath string
|
2020-05-03 11:19:32 +02:00
|
|
|
proxyPort string
|
2018-06-26 18:39:56 +02:00
|
|
|
gaKey string
|
|
|
|
userVoiceKey string
|
2017-03-22 18:09:21 +01:00
|
|
|
|
2018-07-07 19:05:16 +02:00
|
|
|
TLSListenerOnly bool
|
|
|
|
|
2020-04-04 14:29:33 +02:00
|
|
|
CorsDomains string
|
2017-03-22 18:09:21 +01:00
|
|
|
ListenerString string
|
|
|
|
TLSListenerString string
|
|
|
|
ProfileListenerString string
|
|
|
|
|
|
|
|
Certificate string
|
|
|
|
|
|
|
|
LetsEncryptCache string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(options ...OptionFn) (*Server, error) {
|
2017-03-28 16:12:31 +02:00
|
|
|
s := &Server{
|
|
|
|
locks: map[string]*sync.Mutex{},
|
|
|
|
}
|
2017-03-22 18:09:21 +01:00
|
|
|
|
|
|
|
for _, optionFn := range options {
|
|
|
|
optionFn(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-05-12 03:57:04 +02:00
|
|
|
var seedBytes [8]byte
|
|
|
|
if _, err := crypto_rand.Read(seedBytes[:]); err != nil {
|
|
|
|
panic("cannot obtain cryptographically secure seed")
|
|
|
|
}
|
|
|
|
rand.Seed(int64(binary.LittleEndian.Uint64(seedBytes[:])))
|
2017-03-22 18:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run() {
|
2018-07-07 19:05:16 +02:00
|
|
|
listening := false
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
if s.profilerEnabled {
|
2018-07-07 19:05:16 +02:00
|
|
|
listening = true
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
go func() {
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Println("Profiled listening at: :6060")
|
2017-03-22 18:09:21 +01:00
|
|
|
|
|
|
|
http.ListenAndServe(":6060", nil)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
var fs http.FileSystem
|
|
|
|
|
|
|
|
if s.webPath != "" {
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Println("Using static file path: ", s.webPath)
|
2017-03-22 18:09:21 +01:00
|
|
|
|
|
|
|
fs = http.Dir(s.webPath)
|
|
|
|
|
2017-03-22 22:25:42 +01:00
|
|
|
htmlTemplates, _ = htmlTemplates.ParseGlob(s.webPath + "*.html")
|
|
|
|
textTemplates, _ = textTemplates.ParseGlob(s.webPath + "*.txt")
|
2017-03-22 18:09:21 +01:00
|
|
|
} else {
|
|
|
|
fs = &assetfs.AssetFS{
|
|
|
|
Asset: web.Asset,
|
|
|
|
AssetDir: web.AssetDir,
|
|
|
|
AssetInfo: func(path string) (os.FileInfo, error) {
|
|
|
|
return os.Stat(path)
|
|
|
|
},
|
|
|
|
Prefix: web.Prefix,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range web.AssetNames() {
|
|
|
|
bytes, err := web.Asset(path)
|
|
|
|
if err != nil {
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Panicf("Unable to parse: path=%s, err=%s", path, err)
|
2017-03-22 18:09:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 22:25:42 +01:00
|
|
|
htmlTemplates.New(stripPrefix(path)).Parse(string(bytes))
|
|
|
|
textTemplates.New(stripPrefix(path)).Parse(string(bytes))
|
2017-03-22 18:09:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
staticHandler := http.FileServer(fs)
|
|
|
|
|
2019-04-07 17:06:27 +02:00
|
|
|
r.PathPrefix("/images/").Handler(staticHandler).Methods("GET")
|
|
|
|
r.PathPrefix("/styles/").Handler(staticHandler).Methods("GET")
|
|
|
|
r.PathPrefix("/scripts/").Handler(staticHandler).Methods("GET")
|
|
|
|
r.PathPrefix("/fonts/").Handler(staticHandler).Methods("GET")
|
|
|
|
r.PathPrefix("/ico/").Handler(staticHandler).Methods("GET")
|
2019-04-07 17:20:46 +02:00
|
|
|
r.HandleFunc("/favicon.ico", staticHandler.ServeHTTP).Methods("GET")
|
|
|
|
r.HandleFunc("/robots.txt", staticHandler.ServeHTTP).Methods("GET")
|
2017-03-22 18:09:21 +01:00
|
|
|
|
2019-04-07 17:41:58 +02:00
|
|
|
r.HandleFunc("/{filename:(?:favicon\\.ico|robots\\.txt|health\\.html)}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
|
2019-04-07 17:12:11 +02:00
|
|
|
|
2017-03-28 17:08:34 +02:00
|
|
|
r.HandleFunc("/health.html", healthHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/", s.viewHandler).Methods("GET")
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
r.HandleFunc("/({files:.*}).zip", s.zipHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/({files:.*}).tar", s.tarHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/({files:.*}).tar.gz", s.tarGzHandler).Methods("GET")
|
|
|
|
|
2018-08-13 08:56:08 +02:00
|
|
|
r.HandleFunc("/{token}/{filename}", s.headHandler).Methods("HEAD")
|
|
|
|
r.HandleFunc("/{action:(?:download|get|inline)}/{token}/{filename}", s.headHandler).Methods("HEAD")
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
r.HandleFunc("/{token}/{filename}", s.previewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) (match bool) {
|
|
|
|
match = false
|
|
|
|
|
|
|
|
// The file will show a preview page when opening the link in browser directly or
|
|
|
|
// from external link. If the referer url path and current path are the same it will be
|
|
|
|
// downloaded.
|
2017-03-22 21:09:40 +01:00
|
|
|
if !acceptsHTML(r.Header) {
|
2017-03-22 18:09:21 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
match = (r.Referer() == "")
|
|
|
|
|
|
|
|
u, err := url.Parse(r.Referer())
|
|
|
|
if err != nil {
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Fatal(err)
|
2017-03-22 18:09:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
match = match || (u.Path != r.URL.Path)
|
|
|
|
return
|
|
|
|
}).Methods("GET")
|
|
|
|
|
2017-03-28 17:08:34 +02:00
|
|
|
getHandlerFn := s.getHandler
|
|
|
|
if s.rateLimitRequests > 0 {
|
|
|
|
getHandlerFn = ratelimit.Request(ratelimit.IP).Rate(s.rateLimitRequests, 60*time.Second).LimitBy(memory.New())(http.HandlerFunc(getHandlerFn)).ServeHTTP
|
|
|
|
}
|
|
|
|
|
|
|
|
r.HandleFunc("/{token}/{filename}", getHandlerFn).Methods("GET")
|
2018-08-13 08:56:08 +02:00
|
|
|
r.HandleFunc("/{action:(?:download|get|inline)}/{token}/{filename}", getHandlerFn).Methods("GET")
|
2017-03-28 17:08:34 +02:00
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
r.HandleFunc("/{filename}/virustotal", s.virusTotalHandler).Methods("PUT")
|
|
|
|
r.HandleFunc("/{filename}/scan", s.scanHandler).Methods("PUT")
|
2018-06-23 18:46:28 +02:00
|
|
|
r.HandleFunc("/put/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
|
|
|
|
r.HandleFunc("/upload/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
|
|
|
|
r.HandleFunc("/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
|
2018-09-02 14:08:26 +02:00
|
|
|
r.HandleFunc("/", s.BasicAuthHandler(http.HandlerFunc(s.postHandler))).Methods("POST")
|
2017-03-22 18:09:21 +01:00
|
|
|
// r.HandleFunc("/{page}", viewHandler).Methods("GET")
|
|
|
|
|
2018-06-24 06:46:57 +02:00
|
|
|
r.HandleFunc("/{token}/{filename}/{deletionToken}", s.deleteHandler).Methods("DELETE")
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
r.NotFoundHandler = http.HandlerFunc(s.notFoundHandler)
|
|
|
|
|
|
|
|
mime.AddExtensionType(".md", "text/x-markdown")
|
|
|
|
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Printf("Transfer.sh server started.\nusing temp folder: %s\nusing storage provider: %s", s.tempPath, s.storage.Type())
|
2017-03-22 18:09:21 +01:00
|
|
|
|
2020-04-04 14:29:33 +02:00
|
|
|
var cors func(http.Handler) http.Handler
|
|
|
|
if len(s.CorsDomains) > 0 {
|
|
|
|
cors = gorillaHandlers.CORS(
|
|
|
|
gorillaHandlers.AllowedHeaders([]string{"*"}),
|
|
|
|
gorillaHandlers.AllowedOrigins(strings.Split(s.CorsDomains, ",")),
|
|
|
|
gorillaHandlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"}),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
cors = func(h http.Handler) http.Handler {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 14:42:59 +02:00
|
|
|
h := handlers.PanicHandler(
|
|
|
|
IPFilterHandler(
|
|
|
|
handlers.LogHandler(
|
|
|
|
LoveHandler(
|
2020-04-04 14:29:33 +02:00
|
|
|
s.RedirectHandler(cors(r))),
|
2019-05-11 14:42:59 +02:00
|
|
|
handlers.NewLogOptions(s.logger.Printf, "_default_"),
|
|
|
|
),
|
|
|
|
s.ipFilterOptions,
|
|
|
|
),
|
|
|
|
nil,
|
|
|
|
)
|
2017-03-22 18:09:21 +01:00
|
|
|
|
2018-07-07 19:05:16 +02:00
|
|
|
if !s.TLSListenerOnly {
|
|
|
|
srvr := &http.Server{
|
|
|
|
Addr: s.ListenerString,
|
|
|
|
Handler: h,
|
|
|
|
}
|
2017-03-22 18:09:21 +01:00
|
|
|
|
2018-07-07 19:05:16 +02:00
|
|
|
listening = true
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Printf("listening on port: %v\n", s.ListenerString)
|
2018-07-07 19:05:16 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
srvr.ListenAndServe()
|
|
|
|
}()
|
|
|
|
}
|
2017-03-22 18:09:21 +01:00
|
|
|
|
|
|
|
if s.TLSListenerString != "" {
|
2018-07-07 19:05:16 +02:00
|
|
|
listening = true
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Printf("listening on port: %v\n", s.TLSListenerString)
|
2018-07-07 19:05:16 +02:00
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
go func() {
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: s.TLSListenerString,
|
|
|
|
Handler: h,
|
|
|
|
TLSConfig: s.tlsConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.ListenAndServeTLS("", ""); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Printf("---------------------------")
|
2017-03-22 18:09:21 +01:00
|
|
|
|
2021-01-05 17:23:47 +01:00
|
|
|
if s.purgeDays > 0 {
|
|
|
|
go s.purgeHandler()
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:09:21 +01:00
|
|
|
term := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(term, os.Interrupt)
|
|
|
|
signal.Notify(term, syscall.SIGTERM)
|
|
|
|
|
2018-07-07 19:05:16 +02:00
|
|
|
if listening {
|
|
|
|
<-term
|
|
|
|
} else {
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Printf("No listener active.")
|
2018-07-07 19:05:16 +02:00
|
|
|
}
|
2017-03-22 18:09:21 +01:00
|
|
|
|
2018-10-27 00:15:55 +02:00
|
|
|
s.logger.Printf("Server stopped.")
|
2017-03-22 18:09:21 +01:00
|
|
|
}
|