mirror of
https://github.com/AlistGo/alist.git
synced 2025-11-25 03:15:10 +08:00
Introduces the ability to sign generated URLs for enhanced security and access control. This feature is activated by configuring a `PrivateKey`, `UID`, and `ValidDuration` in the driver settings. If a private key is provided, the driver will sign the output URLs, making them time-limited based on the `ValidDuration`. The `ValidDuration` defaults to 30 minutes if not specified. The core signing logic is encapsulated in the new `sign.go` file. The `driver.go` file integrates this signing process before returning the final URL.
28 lines
652 B
Go
28 lines
652 B
Go
package _123Open
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"fmt"
|
|
"math/rand"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
func SignURL(originURL, privateKey string, uid uint64, validDuration time.Duration) (string, error) {
|
|
if privateKey == "" {
|
|
return originURL, nil
|
|
}
|
|
parsed, err := url.Parse(originURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
ts := time.Now().Add(validDuration).Unix()
|
|
randInt := rand.Int()
|
|
signature := fmt.Sprintf("%d-%d-%d-%x", ts, randInt, uid, md5.Sum([]byte(fmt.Sprintf("%s-%d-%d-%d-%s",
|
|
parsed.Path, ts, randInt, uid, privateKey))))
|
|
query := parsed.Query()
|
|
query.Add("auth_key", signature)
|
|
parsed.RawQuery = query.Encode()
|
|
return parsed.String(), nil
|
|
}
|