@@ -8,6 +8,7 @@ import ( | |||
"github.com/dutchcoders/transfer.sh/server" | |||
"github.com/dutchcoders/transfer.sh/server/storage" | |||
"github.com/dutchcoders/transfer.sh/server/utils" | |||
"github.com/fatih/color" | |||
"github.com/urfave/cli" | |||
"google.golang.org/api/googleapi" | |||
@@ -327,7 +328,7 @@ func New() *Cmd { | |||
} | |||
applyIPFilter := false | |||
ipFilterOptions := server.IPFilterOptions{} | |||
ipFilterOptions := utils.IPFilterOptions{} | |||
if ipWhitelist := c.String("ip-whitelist"); ipWhitelist != "" { | |||
applyIPFilter = true | |||
ipFilterOptions.AllowedIPs = strings.Split(ipWhitelist, ",") | |||
@@ -1,74 +0,0 @@ | |||
/* | |||
The MIT License (MIT) | |||
Copyright (c) 2014-2017 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 server | |||
import ( | |||
// _ "transfer.sh/app/handlers" | |||
// _ "transfer.sh/app/utils" | |||
"fmt" | |||
"io" | |||
"log" | |||
"net/http" | |||
"time" | |||
"github.com/dutchcoders/go-clamd" | |||
"github.com/dutchcoders/transfer.sh/server/utils" | |||
"github.com/gorilla/mux" | |||
) | |||
func (s *Server) scanHandler(w http.ResponseWriter, r *http.Request) { | |||
vars := mux.Vars(r) | |||
filename := utils.Sanitize(vars["filename"]) | |||
contentLength := r.ContentLength | |||
contentType := r.Header.Get("Content-Type") | |||
s.logger.Printf("Scanning %s %d %s", filename, contentLength, contentType) | |||
var reader io.Reader | |||
reader = r.Body | |||
c := clamd.NewClamd(s.ClamAVDaemonHost) | |||
abort := make(chan bool) | |||
response, err := c.ScanStream(reader, abort) | |||
if err != nil { | |||
log.Printf("%s", err.Error()) | |||
http.Error(w, err.Error(), 500) | |||
return | |||
} | |||
select { | |||
case s := <-response: | |||
_, _ = w.Write([]byte(fmt.Sprintf("%v\n", s.Status))) | |||
case <-time.After(time.Second * 60): | |||
abort <- true | |||
} | |||
close(abort) | |||
} |
@@ -50,7 +50,8 @@ import ( | |||
textTemplate "text/template" | |||
"time" | |||
web "github.com/dutchcoders/transfer.sh-web" | |||
"github.com/dutchcoders/go-clamd" | |||
"github.com/dutchcoders/go-virustotal" | |||
"github.com/dutchcoders/transfer.sh/server/storage" | |||
"github.com/dutchcoders/transfer.sh/server/utils" | |||
"github.com/gorilla/mux" | |||
@@ -64,10 +65,6 @@ var ( | |||
textTemplates = initTextTemplates() | |||
) | |||
func stripPrefix(path string) string { | |||
return strings.Replace(path, web.Prefix+"/", "", -1) | |||
} | |||
func initTextTemplates() *textTemplate.Template { | |||
templateMap := textTemplate.FuncMap{"format": utils.FormatNumber} | |||
@@ -86,7 +83,7 @@ func initHTMLTemplates() *htmlTemplate.Template { | |||
} | |||
// Create a log handler for every request it receives. | |||
func LoveHandler(h http.Handler) http.HandlerFunc { | |||
func (s *Server) LoveHandler(h http.Handler) http.HandlerFunc { | |||
return func(w http.ResponseWriter, r *http.Request) { | |||
w.Header().Set("x-made-with", "<3 by DutchCoders") | |||
w.Header().Set("x-served-by", "Proudly served by DutchCoders") | |||
@@ -95,17 +92,6 @@ func LoveHandler(h http.Handler) http.HandlerFunc { | |||
} | |||
} | |||
func IPFilterHandler(h http.Handler, ipFilterOptions *IPFilterOptions) http.HandlerFunc { | |||
return func(w http.ResponseWriter, r *http.Request) { | |||
if ipFilterOptions == nil { | |||
h.ServeHTTP(w, r) | |||
} else { | |||
WrapIPFilter(h, *ipFilterOptions).ServeHTTP(w, r) | |||
} | |||
return | |||
} | |||
} | |||
func (s *Server) RedirectHandler(h http.Handler) http.HandlerFunc { | |||
return func(w http.ResponseWriter, r *http.Request) { | |||
if !s.forceHTTPs { | |||
@@ -801,6 +787,70 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) { | |||
http.ServeContent(w, r, filename, time.Now(), file) | |||
} | |||
func (s *Server) scanHandler(w http.ResponseWriter, r *http.Request) { | |||
vars := mux.Vars(r) | |||
filename := utils.Sanitize(vars["filename"]) | |||
contentLength := r.ContentLength | |||
contentType := r.Header.Get("Content-Type") | |||
s.logger.Printf("Scanning %s %d %s", filename, contentLength, contentType) | |||
var reader io.Reader | |||
reader = r.Body | |||
c := clamd.NewClamd(s.ClamAVDaemonHost) | |||
abort := make(chan bool) | |||
response, err := c.ScanStream(reader, abort) | |||
if err != nil { | |||
log.Printf("%s", err.Error()) | |||
http.Error(w, err.Error(), 500) | |||
return | |||
} | |||
select { | |||
case s := <-response: | |||
_, _ = w.Write([]byte(fmt.Sprintf("%v\n", s.Status))) | |||
case <-time.After(time.Second * 60): | |||
abort <- true | |||
} | |||
close(abort) | |||
} | |||
func (s *Server) virusTotalHandler(w http.ResponseWriter, r *http.Request) { | |||
vars := mux.Vars(r) | |||
filename := utils.Sanitize(vars["filename"]) | |||
contentLength := r.ContentLength | |||
contentType := r.Header.Get("Content-Type") | |||
s.logger.Printf("Submitting to VirusTotal: %s %d %s", filename, contentLength, contentType) | |||
vt, err := virustotal.NewVirusTotal(s.VirusTotalKey) | |||
if err != nil { | |||
http.Error(w, err.Error(), 500) | |||
return | |||
} | |||
var reader io.Reader | |||
reader = r.Body | |||
result, err := vt.Scan(filename, reader) | |||
if err != nil { | |||
http.Error(w, err.Error(), 500) | |||
return | |||
} | |||
s.logger.Println(result) | |||
_, _ = w.Write([]byte(fmt.Sprintf("%v\n", result.Permalink))) | |||
} | |||
func (s *Server) metadataForRequest(contentType string, contentLength int64, r *http.Request) storage.Metadata { | |||
metadata := storage.Metadata{ | |||
ContentType: contentType, | |||
@@ -230,7 +230,7 @@ func HttpAuthCredentials(user string, pass string) OptionFn { | |||
} | |||
} | |||
func FilterOptions(options IPFilterOptions) OptionFn { | |||
func FilterOptions(options utils.IPFilterOptions) OptionFn { | |||
for i, allowedIP := range options.AllowedIPs { | |||
options.AllowedIPs[i] = strings.TrimSpace(allowedIP) | |||
} | |||
@@ -264,7 +264,7 @@ type Server struct { | |||
forceHTTPs bool | |||
ipFilterOptions *IPFilterOptions | |||
ipFilterOptions *utils.IPFilterOptions | |||
VirusTotalKey string | |||
ClamAVDaemonHost string | |||
@@ -417,9 +417,9 @@ func (s *Server) Run() { | |||
s.logger.Printf("Transfer.sh server started.\nusing temp folder: %s\nusing storage provider: %s", s.tempPath, s.storage.Type()) | |||
h := handlers.PanicHandler( | |||
IPFilterHandler( | |||
utils.IPFilterHandler( | |||
handlers.LogHandler( | |||
LoveHandler( | |||
s.LoveHandler( | |||
s.RedirectHandler(r)), | |||
handlers.NewLogOptions(s.logger.Printf, "_default_"), | |||
), | |||
@@ -473,3 +473,7 @@ func (s *Server) Run() { | |||
s.logger.Printf("Server stopped.") | |||
} | |||
func stripPrefix(path string) string { | |||
return strings.Replace(path, web.Prefix+"/", "", -1) | |||
} |
@@ -94,7 +94,7 @@ func (s *LocalStorage) Put(token string, filename string, reader io.Reader, meta | |||
func (s *LocalStorage) Delete(token string, filename string) (err error) { | |||
dir := filepath.Join(s.basedir, token) | |||
log.Printf("deleting file %s/%s/%s", dir, filename) | |||
log.Printf("deleting file %s/%s/%s", dir, token, filename) | |||
// ensure we do not accidentally delete more than the specified file in the folder | |||
if files, err := ioutil.ReadDir(dir); len(files) > 2 || err != nil { | |||
// ignore if we cannot delete the metadata file | |||
@@ -9,7 +9,7 @@ The above copyright notice and this permission notice shall be included in all c | |||
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 | |||
package utils | |||
import ( | |||
"log" | |||
@@ -59,6 +59,17 @@ type subnet struct { | |||
allowed bool | |||
} | |||
func IPFilterHandler(h http.Handler, ipFilterOptions *IPFilterOptions) http.HandlerFunc { | |||
return func(w http.ResponseWriter, r *http.Request) { | |||
if ipFilterOptions == nil { | |||
h.ServeHTTP(w, r) | |||
} else { | |||
WrapIPFilter(h, *ipFilterOptions).ServeHTTP(w, r) | |||
} | |||
return | |||
} | |||
} | |||
//New constructs IPFilter instance. | |||
func NewIPFilter(opts IPFilterOptions) *IPFilter { | |||
if opts.Logger == nil { |
@@ -1,65 +0,0 @@ | |||
/* | |||
The MIT License (MIT) | |||
Copyright (c) 2014-2017 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 server | |||
import ( | |||
"fmt" | |||
"io" | |||
"net/http" | |||
"github.com/dutchcoders/go-virustotal" | |||
"github.com/dutchcoders/transfer.sh/server/utils" | |||
"github.com/gorilla/mux" | |||
) | |||
func (s *Server) virusTotalHandler(w http.ResponseWriter, r *http.Request) { | |||
vars := mux.Vars(r) | |||
filename := utils.Sanitize(vars["filename"]) | |||
contentLength := r.ContentLength | |||
contentType := r.Header.Get("Content-Type") | |||
s.logger.Printf("Submitting to VirusTotal: %s %d %s", filename, contentLength, contentType) | |||
vt, err := virustotal.NewVirusTotal(s.VirusTotalKey) | |||
if err != nil { | |||
http.Error(w, err.Error(), 500) | |||
return | |||
} | |||
var reader io.Reader | |||
reader = r.Body | |||
result, err := vt.Scan(filename, reader) | |||
if err != nil { | |||
http.Error(w, err.Error(), 500) | |||
return | |||
} | |||
s.logger.Println(result) | |||
_, _ = w.Write([]byte(fmt.Sprintf("%v\n", result.Permalink))) | |||
} |