mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2024-11-23 04:30:19 +01:00
issue 380 and simplier token generation
This commit is contained in:
parent
d9198e78db
commit
69d979ca48
5 changed files with 26 additions and 41 deletions
|
@ -274,7 +274,7 @@ var globalFlags = []cli.Flag{
|
||||||
Value: "",
|
Value: "",
|
||||||
EnvVar: "CORS_DOMAINS",
|
EnvVar: "CORS_DOMAINS",
|
||||||
},
|
},
|
||||||
cli.Int64Flag{
|
cli.IntFlag{
|
||||||
Name: "random-token-length",
|
Name: "random-token-length",
|
||||||
Usage: "",
|
Usage: "",
|
||||||
Value: 6,
|
Value: 6,
|
||||||
|
@ -383,7 +383,7 @@ func New() *Cmd {
|
||||||
options = append(options, server.RateLimit(v))
|
options = append(options, server.RateLimit(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
v := c.Int64("random-token-length")
|
v := c.Int("random-token-length")
|
||||||
options = append(options, server.RandomTokenLength(v))
|
options = append(options, server.RandomTokenLength(v))
|
||||||
|
|
||||||
purgeDays := c.Int("purge-days")
|
purgeDays := c.Int("purge-days")
|
||||||
|
|
|
@ -36,28 +36,14 @@ const (
|
||||||
|
|
||||||
// someone set us up the bomb !!
|
// someone set us up the bomb !!
|
||||||
BASE = float64(len(SYMBOLS))
|
BASE = float64(len(SYMBOLS))
|
||||||
|
|
||||||
// init seed encode number
|
|
||||||
INIT_SEED = float64(-1)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// encodes a number into our *base* representation
|
// generate a token
|
||||||
// TODO can this be made better with some bitshifting?
|
func Token(length int) string {
|
||||||
func Encode(number float64, length int64) string {
|
result := ""
|
||||||
if number == INIT_SEED {
|
for i := 0; i < length; i++ {
|
||||||
seed := math.Pow(float64(BASE), float64(length))
|
x := rand.Intn(len(SYMBOLS))
|
||||||
number = seed + (rand.Float64() * seed) // start with seed to enforce desired length
|
result = string(SYMBOLS[x]) + result
|
||||||
}
|
|
||||||
|
|
||||||
rest := int64(math.Mod(number, BASE))
|
|
||||||
// strings are a bit weird in go...
|
|
||||||
result := string(SYMBOLS[rest])
|
|
||||||
if rest > 0 && number-float64(rest) != 0 {
|
|
||||||
newnumber := (number - float64(rest)) / BASE
|
|
||||||
result = Encode(newnumber, length) + result
|
|
||||||
} else {
|
|
||||||
// it would always be 1 because of starting with seed and we want to skip
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -4,12 +4,12 @@ import "testing"
|
||||||
|
|
||||||
func BenchmarkEncodeConcat(b *testing.B) {
|
func BenchmarkEncodeConcat(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_ = Encode(INIT_SEED, 5) + Encode(INIT_SEED, 5)
|
_ = Token(5) + Token(5)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkEncodeLonger(b *testing.B) {
|
func BenchmarkEncodeLonger(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_ = Encode(INIT_SEED, 10)
|
_ = Token(10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,7 +276,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := Encode(INIT_SEED, s.randomTokenLength)
|
token := Token(s.randomTokenLength)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
|
||||||
|
@ -398,13 +398,13 @@ type Metadata struct {
|
||||||
DeletionToken string
|
DeletionToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
func MetadataForRequest(contentType string, randomTokenLength int64, r *http.Request) Metadata {
|
func MetadataForRequest(contentType string, randomTokenLength int, r *http.Request) Metadata {
|
||||||
metadata := Metadata{
|
metadata := Metadata{
|
||||||
ContentType: strings.ToLower(contentType),
|
ContentType: strings.ToLower(contentType),
|
||||||
MaxDate: time.Time{},
|
MaxDate: time.Time{},
|
||||||
Downloads: 0,
|
Downloads: 0,
|
||||||
MaxDownloads: -1,
|
MaxDownloads: -1,
|
||||||
DeletionToken: Encode(INIT_SEED, randomTokenLength) + Encode(INIT_SEED, randomTokenLength),
|
DeletionToken: Token(randomTokenLength) + Token(randomTokenLength),
|
||||||
}
|
}
|
||||||
|
|
||||||
if v := r.Header.Get("Max-Downloads"); v == "" {
|
if v := r.Header.Get("Max-Downloads"); v == "" {
|
||||||
|
@ -492,7 +492,7 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
contentType := mime.TypeByExtension(filepath.Ext(vars["filename"]))
|
contentType := mime.TypeByExtension(filepath.Ext(vars["filename"]))
|
||||||
|
|
||||||
token := Encode(INIT_SEED, s.randomTokenLength)
|
token := Token(s.randomTokenLength)
|
||||||
|
|
||||||
metadata := MetadataForRequest(contentType, s.randomTokenLength, r)
|
metadata := MetadataForRequest(contentType, s.randomTokenLength, r)
|
||||||
|
|
||||||
|
@ -636,23 +636,22 @@ func (metadata Metadata) remainingLimitHeaderValues() (remainingDownloads, remai
|
||||||
return remainingDownloads, remainingDays
|
return remainingDownloads, remainingDays
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Lock(token, filename string) error {
|
func (s *Server) Lock(token, filename string) {
|
||||||
key := path.Join(token, filename)
|
key := path.Join(token, filename)
|
||||||
|
|
||||||
if _, ok := s.locks[key]; !ok {
|
lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
|
||||||
s.locks[key] = &sync.Mutex{}
|
|
||||||
|
lock.(*sync.Mutex).Lock()
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.locks[key].Lock()
|
func (s *Server) Unlock(token, filename string) {
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) Unlock(token, filename string) error {
|
|
||||||
key := path.Join(token, filename)
|
key := path.Join(token, filename)
|
||||||
s.locks[key].Unlock()
|
|
||||||
|
|
||||||
return nil
|
lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
|
||||||
|
|
||||||
|
lock.(*sync.Mutex).Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (Metadata, error) {
|
func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (Metadata, error) {
|
||||||
|
|
|
@ -187,7 +187,7 @@ func RateLimit(requests int) OptionFn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandomTokenLength(length int64) OptionFn {
|
func RandomTokenLength(length int) OptionFn {
|
||||||
return func(srvr *Server) {
|
return func(srvr *Server) {
|
||||||
srvr.randomTokenLength = length
|
srvr.randomTokenLength = length
|
||||||
}
|
}
|
||||||
|
@ -288,7 +288,7 @@ type Server struct {
|
||||||
|
|
||||||
profilerEnabled bool
|
profilerEnabled bool
|
||||||
|
|
||||||
locks map[string]*sync.Mutex
|
locks sync.Map
|
||||||
|
|
||||||
maxUploadSize int64
|
maxUploadSize int64
|
||||||
rateLimitRequests int
|
rateLimitRequests int
|
||||||
|
@ -300,7 +300,7 @@ type Server struct {
|
||||||
|
|
||||||
forceHTTPs bool
|
forceHTTPs bool
|
||||||
|
|
||||||
randomTokenLength int64
|
randomTokenLength int
|
||||||
|
|
||||||
ipFilterOptions *IPFilterOptions
|
ipFilterOptions *IPFilterOptions
|
||||||
|
|
||||||
|
@ -329,7 +329,7 @@ type Server struct {
|
||||||
|
|
||||||
func New(options ...OptionFn) (*Server, error) {
|
func New(options ...OptionFn) (*Server, error) {
|
||||||
s := &Server{
|
s := &Server{
|
||||||
locks: map[string]*sync.Mutex{},
|
locks: sync.Map{},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, optionFn := range options {
|
for _, optionFn := range options {
|
||||||
|
|
Loading…
Reference in a new issue