Browse Source

ISSUE-296 add CORS

tags/v1.1.7
Andrea Spacca 4 years ago
parent
commit
ef28bcb28f
5 changed files with 36 additions and 1 deletions
  1. +1
    -0
      README.md
  2. +9
    -0
      cmd/cmd.go
  3. +1
    -0
      go.mod
  4. +2
    -0
      go.sum
  5. +23
    -1
      server/server.go

+ 1
- 0
README.md View File

@@ -106,6 +106,7 @@ gdrive-local-config-path | path to store local transfer.sh config cache for gdri
gdrive-chunk-size | chunk size for gdrive upload in megabytes, must be lower than available memory (8 MB) | |
lets-encrypt-hosts | hosts to use for lets encrypt certificates (comma seperated) | |
log | path to log file| |
cors-domains | comma separated list of domains for CORS, setting it enable CORS | |

If you want to use TLS using lets encrypt certificates, set lets-encrypt-hosts to your domain, set tls-listener to :443 and enable force-https.



+ 9
- 0
cmd/cmd.go View File

@@ -207,6 +207,11 @@ var globalFlags = []cli.Flag{
Usage: "comma separated list of ips not allowed to connect to the service",
Value: "",
},
cli.StringFlag{
Name: "cors-domains",
Usage: "comma separated list of domains allowed for CORS requests",
Value: "",
},
}

type Cmd struct {
@@ -245,6 +250,10 @@ func New() *Cmd {
options = append(options, server.Listener(v))
}

if v := c.String("cors-domains"); v != "" {
options = append(options, server.CorsDomains(v))
}

if v := c.String("tls-listener"); v == "" {
} else if c.Bool("tls-listener-only") {
options = append(options, server.TLSListener(v, true))


+ 1
- 0
go.mod View File

@@ -16,6 +16,7 @@ require (
github.com/garyburd/redigo v1.6.0 // indirect
github.com/golang/gddo v0.0.0-20200310004957-95ce5a452273
github.com/golang/protobuf v1.3.5 // indirect
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect


+ 2
- 0
go.sum View File

@@ -127,6 +127,8 @@ github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=


+ 23
- 1
server/server.go View File

@@ -26,6 +26,7 @@ package server

import (
"errors"
gorillaHandlers "github.com/gorilla/handlers"
"log"
"math/rand"
"mime"
@@ -85,6 +86,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
@@ -275,6 +283,7 @@ type Server struct {

TLSListenerOnly bool

CorsDomains string
ListenerString string
TLSListenerString string
ProfileListenerString string
@@ -413,11 +422,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,


Loading…
Cancel
Save