Merge remote-tracking branch 'dutchcoders/master'
# Conflicts: # README.md # go.sum
This commit is contained in:
@ -158,9 +158,9 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
relativeURL, _ := url.Parse(path.Join(s.proxyPath, token, filename))
|
||||
resolvedURL := resolveURL(r, relativeURL)
|
||||
resolvedURL := resolveURL(r, relativeURL, s.proxyPort)
|
||||
relativeURLGet, _ := url.Parse(path.Join(s.proxyPath, getPathPart, token, filename))
|
||||
resolvedURLGet := resolveURL(r, relativeURLGet)
|
||||
resolvedURLGet := resolveURL(r, relativeURLGet, s.proxyPort)
|
||||
var png []byte
|
||||
png, err = qrcode.Encode(resolvedURL, qrcode.High, 150)
|
||||
if err != nil {
|
||||
@ -170,8 +170,8 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
qrCode := base64.StdEncoding.EncodeToString(png)
|
||||
|
||||
hostname := getURL(r).Host
|
||||
webAddress := resolveWebAddress(r, s.proxyPath)
|
||||
hostname := getURL(r, s.proxyPort).Host
|
||||
webAddress := resolveWebAddress(r, s.proxyPath, s.proxyPort)
|
||||
|
||||
data := struct {
|
||||
ContentType string
|
||||
@ -212,8 +212,8 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) viewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// vars := mux.Vars(r)
|
||||
|
||||
hostname := getURL(r).Host
|
||||
webAddress := resolveWebAddress(r, s.proxyPath)
|
||||
hostname := getURL(r, s.proxyPort).Host
|
||||
webAddress := resolveWebAddress(r, s.proxyPath, s.proxyPort)
|
||||
|
||||
data := struct {
|
||||
Hostname string
|
||||
@ -339,7 +339,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
filename = url.PathEscape(filename)
|
||||
relativeURL, _ := url.Parse(path.Join(s.proxyPath, token, filename))
|
||||
fmt.Fprintln(w, getURL(r).ResolveReference(relativeURL).String())
|
||||
fmt.Fprintln(w, getURL(r, s.proxyPort).ResolveReference(relativeURL).String())
|
||||
|
||||
cleanTmpFile(file)
|
||||
}
|
||||
@ -500,15 +500,15 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
|
||||
relativeURL, _ := url.Parse(path.Join(s.proxyPath, token, filename))
|
||||
deleteURL, _ := url.Parse(path.Join(s.proxyPath, token, filename, metadata.DeletionToken))
|
||||
|
||||
w.Header().Set("X-Url-Delete", resolveURL(r, deleteURL))
|
||||
w.Header().Set("X-Url-Delete", resolveURL(r, deleteURL, s.proxyPort))
|
||||
|
||||
fmt.Fprint(w, resolveURL(r, relativeURL))
|
||||
fmt.Fprint(w, resolveURL(r, relativeURL, s.proxyPort))
|
||||
}
|
||||
|
||||
func resolveURL(r *http.Request, u *url.URL) string {
|
||||
func resolveURL(r *http.Request, u *url.URL, proxyPort string) string {
|
||||
r.URL.Path = ""
|
||||
|
||||
return getURL(r).ResolveReference(u).String()
|
||||
return getURL(r, proxyPort).ResolveReference(u).String()
|
||||
}
|
||||
|
||||
func resolveKey(key, proxyPath string) string {
|
||||
@ -525,8 +525,8 @@ func resolveKey(key, proxyPath string) string {
|
||||
return key
|
||||
}
|
||||
|
||||
func resolveWebAddress(r *http.Request, proxyPath string) string {
|
||||
url := getURL(r)
|
||||
func resolveWebAddress(r *http.Request, proxyPath string, proxyPort string) string {
|
||||
url := getURL(r, proxyPort)
|
||||
|
||||
var webAddress string
|
||||
|
||||
@ -544,7 +544,7 @@ func resolveWebAddress(r *http.Request, proxyPath string) string {
|
||||
return webAddress
|
||||
}
|
||||
|
||||
func getURL(r *http.Request) *url.URL {
|
||||
func getURL(r *http.Request, proxyPort string) *url.URL {
|
||||
u, _ := url.Parse(r.URL.String())
|
||||
|
||||
if r.TLS != nil {
|
||||
@ -555,16 +555,25 @@ func getURL(r *http.Request) *url.URL {
|
||||
u.Scheme = "http"
|
||||
}
|
||||
|
||||
if u.Host != "" {
|
||||
} else if host, port, err := net.SplitHostPort(r.Host); err != nil {
|
||||
u.Host = r.Host
|
||||
} else {
|
||||
if port == "80" && u.Scheme == "http" {
|
||||
u.Host = host
|
||||
} else if port == "443" && u.Scheme == "https" {
|
||||
if u.Host == "" {
|
||||
host, port, err := net.SplitHostPort(r.Host)
|
||||
if err != nil {
|
||||
host = r.Host
|
||||
port = ""
|
||||
}
|
||||
if len(proxyPort) != 0 {
|
||||
port = proxyPort
|
||||
}
|
||||
if len(port) == 0 {
|
||||
u.Host = host
|
||||
} else {
|
||||
u.Host = net.JoinHostPort(host, port)
|
||||
if port == "80" && u.Scheme == "http" {
|
||||
u.Host = host
|
||||
} else if port == "443" && u.Scheme == "https" {
|
||||
u.Host = host
|
||||
} else {
|
||||
u.Host = net.JoinHostPort(host, port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -614,9 +623,7 @@ func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (M
|
||||
var metadata Metadata
|
||||
|
||||
r, _, err := s.storage.Get(token, fmt.Sprintf("%s.metadata", filename))
|
||||
if s.storage.IsNotExist(err) {
|
||||
return metadata, nil
|
||||
} else if err != nil {
|
||||
if err != nil {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
@ -1011,7 +1018,7 @@ func (s *Server) RedirectHandler(h http.Handler) http.HandlerFunc {
|
||||
} else if r.Header.Get("X-Forwarded-Proto") == "https" {
|
||||
} else if r.URL.Scheme == "https" {
|
||||
} else {
|
||||
u := getURL(r)
|
||||
u := getURL(r, s.proxyPort)
|
||||
u.Scheme = "https"
|
||||
|
||||
http.Redirect(w, r, u.String(), http.StatusPermanentRedirect)
|
||||
|
@ -26,7 +26,10 @@ package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
gorillaHandlers "github.com/gorilla/handlers"
|
||||
"log"
|
||||
crypto_rand "crypto/rand"
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
"mime"
|
||||
"net/http"
|
||||
@ -85,6 +88,13 @@ func Listener(s string) OptionFn {
|
||||
|
||||
}
|
||||
|
||||
func CorsDomains(s string) OptionFn {
|
||||
return func(srvr *Server) {
|
||||
srvr.CorsDomains = s
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func GoogleAnalytics(gaKey string) OptionFn {
|
||||
return func(srvr *Server) {
|
||||
srvr.gaKey = gaKey
|
||||
@ -131,6 +141,12 @@ func ProxyPath(s string) OptionFn {
|
||||
}
|
||||
}
|
||||
|
||||
func ProxyPort(s string) OptionFn {
|
||||
return func(srvr *Server) {
|
||||
srvr.proxyPort = s
|
||||
}
|
||||
}
|
||||
|
||||
func TempPath(s string) OptionFn {
|
||||
return func(srvr *Server) {
|
||||
if s[len(s)-1:] != "/" {
|
||||
@ -270,11 +286,13 @@ type Server struct {
|
||||
|
||||
webPath string
|
||||
proxyPath string
|
||||
proxyPort string
|
||||
gaKey string
|
||||
userVoiceKey string
|
||||
|
||||
TLSListenerOnly bool
|
||||
|
||||
CorsDomains string
|
||||
ListenerString string
|
||||
TLSListenerString string
|
||||
ProfileListenerString string
|
||||
@ -297,7 +315,11 @@ func New(options ...OptionFn) (*Server, error) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
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[:])))
|
||||
}
|
||||
|
||||
func (s *Server) Run() {
|
||||
@ -413,11 +435,24 @@ func (s *Server) Run() {
|
||||
|
||||
s.logger.Printf("Transfer.sh server started.\nusing temp folder: %s\nusing storage provider: %s", s.tempPath, s.storage.Type())
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
h := handlers.PanicHandler(
|
||||
IPFilterHandler(
|
||||
handlers.LogHandler(
|
||||
LoveHandler(
|
||||
s.RedirectHandler(r)),
|
||||
s.RedirectHandler(cors(r))),
|
||||
handlers.NewLogOptions(s.logger.Printf, "_default_"),
|
||||
),
|
||||
s.ipFilterOptions,
|
||||
|
Reference in New Issue
Block a user