2014-10-16 20:01:43 +02:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2014 DutchCoders [https://github.com/dutchcoders/]
|
|
|
|
|
|
|
|
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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
// _ "transfer.sh/app/handlers"
|
|
|
|
// _ "transfer.sh/app/utils"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/PuerkitoBio/ghost/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
2014-11-10 17:18:55 +01:00
|
|
|
"mime"
|
2014-10-16 20:01:43 +02:00
|
|
|
"net/http"
|
2014-10-26 21:30:02 +01:00
|
|
|
"net/url"
|
2014-10-16 20:01:43 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const SERVER_INFO = "transfer.sh"
|
|
|
|
|
|
|
|
// parse request with maximum memory of _24Kilobits
|
|
|
|
const _24K = (1 << 20) * 24
|
|
|
|
|
|
|
|
var config struct {
|
2014-11-11 15:33:00 +01:00
|
|
|
AWS_ACCESS_KEY string
|
|
|
|
AWS_SECRET_KEY string
|
|
|
|
BUCKET string
|
|
|
|
VIRUSTOTAL_KEY string
|
|
|
|
CLAMAV_DAEMON_HOST string "/tmp/clamd.socket"
|
|
|
|
Temp string
|
2014-10-16 20:01:43 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 13:38:40 +02:00
|
|
|
var storage Storage
|
|
|
|
|
2014-10-16 20:01:43 +02:00
|
|
|
func init() {
|
2014-11-11 15:33:00 +01:00
|
|
|
config.AWS_ACCESS_KEY = os.Getenv("AWS_ACCESS_KEY_ID")
|
2014-10-16 20:01:43 +02:00
|
|
|
config.AWS_SECRET_KEY = os.Getenv("AWS_SECRET_KEY")
|
|
|
|
config.BUCKET = os.Getenv("BUCKET")
|
2014-11-11 15:33:00 +01:00
|
|
|
|
2014-10-16 20:01:43 +02:00
|
|
|
config.VIRUSTOTAL_KEY = os.Getenv("VIRUSTOTAL_KEY")
|
2014-11-11 15:33:00 +01:00
|
|
|
|
|
|
|
if os.Getenv("CLAMAV_DAEMON_HOST") != "" {
|
|
|
|
config.CLAMAV_DAEMON_HOST = os.Getenv("CLAMAV_DAEMON_HOST")
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Temp = os.TempDir()
|
2014-10-16 20:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
r.PathPrefix("/scripts/").Handler(http.FileServer(http.Dir("./static/")))
|
|
|
|
r.PathPrefix("/styles/").Handler(http.FileServer(http.Dir("./static/")))
|
|
|
|
r.PathPrefix("/images/").Handler(http.FileServer(http.Dir("./static/")))
|
|
|
|
r.PathPrefix("/fonts/").Handler(http.FileServer(http.Dir("./static/")))
|
|
|
|
r.PathPrefix("/ico/").Handler(http.FileServer(http.Dir("./static/")))
|
2014-10-16 22:10:17 +02:00
|
|
|
r.PathPrefix("/favicon.ico").Handler(http.FileServer(http.Dir("./static/")))
|
2014-10-16 20:01:43 +02:00
|
|
|
r.PathPrefix("/robots.txt").Handler(http.FileServer(http.Dir("./static/")))
|
|
|
|
|
|
|
|
r.HandleFunc("/({files:.*}).zip", zipHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/({files:.*}).tar", tarHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/({files:.*}).tar.gz", tarGzHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/download/{token}/{filename}", getHandler).Methods("GET")
|
|
|
|
|
2014-10-26 21:30:02 +01:00
|
|
|
r.HandleFunc("/{token}/{filename}", previewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
|
2014-11-11 17:31:52 +01:00
|
|
|
// The file will show a preview page when opening the link in browser directly or
|
|
|
|
// from external link. Otherwise it will download the file immediatly.
|
2014-10-26 21:30:02 +01:00
|
|
|
if !acceptsHtml(r.Header) {
|
|
|
|
return false
|
|
|
|
}
|
2014-10-16 20:01:43 +02:00
|
|
|
|
2014-10-26 21:30:02 +01:00
|
|
|
match := (r.Referer() == "")
|
2014-10-16 20:01:43 +02:00
|
|
|
|
2014-10-26 21:30:02 +01:00
|
|
|
u, err := url.Parse(r.Referer())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2014-11-11 17:31:52 +01:00
|
|
|
return match
|
2014-10-26 21:30:02 +01:00
|
|
|
}
|
2014-10-16 20:01:43 +02:00
|
|
|
|
2014-11-11 17:31:52 +01:00
|
|
|
match = match || (u.Host == "transfersh.elasticbeanstalk.com")
|
|
|
|
match = match || (u.Host == "jxm5d6emw5rknovg.onion")
|
2014-10-26 21:30:02 +01:00
|
|
|
match = match || (u.Host == "transfer.sh")
|
|
|
|
match = match || (u.Host == "127.0.0.1")
|
|
|
|
|
|
|
|
return match
|
|
|
|
}).Methods("GET")
|
2014-10-16 20:01:43 +02:00
|
|
|
|
|
|
|
r.HandleFunc("/{token}/{filename}", getHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/get/{token}/{filename}", getHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/{filename}/virustotal", virusTotalHandler).Methods("PUT")
|
|
|
|
r.HandleFunc("/{filename}/scan", scanHandler).Methods("PUT")
|
|
|
|
r.HandleFunc("/put/{filename}", putHandler).Methods("PUT")
|
|
|
|
r.HandleFunc("/upload/{filename}", putHandler).Methods("PUT")
|
|
|
|
r.HandleFunc("/{filename}", putHandler).Methods("PUT")
|
|
|
|
r.HandleFunc("/health.html", healthHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/", postHandler).Methods("POST")
|
2014-10-16 22:10:17 +02:00
|
|
|
// r.HandleFunc("/{page}", viewHandler).Methods("GET")
|
2014-10-16 20:01:43 +02:00
|
|
|
r.HandleFunc("/", viewHandler).Methods("GET")
|
|
|
|
|
|
|
|
r.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
|
|
|
|
|
|
|
port := flag.String("port", "8080", "port number, default: 8080")
|
2014-11-11 15:33:00 +01:00
|
|
|
temp := flag.String("temp", config.Temp, "")
|
2014-10-20 13:38:40 +02:00
|
|
|
basedir := flag.String("basedir", "", "")
|
2014-10-16 20:01:43 +02:00
|
|
|
logpath := flag.String("log", "", "")
|
2014-10-20 13:38:40 +02:00
|
|
|
provider := flag.String("provider", "s3", "")
|
2014-10-16 20:01:43 +02:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *logpath != "" {
|
|
|
|
f, err := os.OpenFile(*logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error opening file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
log.SetOutput(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Temp = *temp
|
2014-10-20 14:54:42 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch *provider {
|
|
|
|
case "s3":
|
|
|
|
storage, err = NewS3Storage()
|
|
|
|
case "local":
|
|
|
|
if *basedir == "" {
|
|
|
|
log.Panic("basedir not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage, err = NewLocalStorage(*basedir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("Error while creating storage.")
|
|
|
|
}
|
2014-10-20 13:38:40 +02:00
|
|
|
|
2014-11-10 17:18:55 +01:00
|
|
|
mime.AddExtensionType(".md", "text/x-markdown")
|
|
|
|
|
2014-10-16 20:01:43 +02:00
|
|
|
log.Printf("Transfer.sh server started. :%v using temp folder: %s", *port, config.Temp)
|
|
|
|
log.Printf("---------------------------")
|
|
|
|
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: fmt.Sprintf(":%s", *port),
|
|
|
|
Handler: handlers.PanicHandler(LoveHandler(RedirectHandler(handlers.LogHandler(r, handlers.NewLogOptions(log.Printf, "_default_")))), nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Panic(s.ListenAndServe())
|
|
|
|
log.Printf("Server stopped.")
|
|
|
|
}
|