diff --git a/.gitignore b/.gitignore index 7dbc17c..909ceb6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules main package-lock.json *_templ.go -fly.toml \ No newline at end of file +fly.toml +*.fiber.gz \ No newline at end of file diff --git a/assets/index.html b/assets/index.html index 942d27d..5f41545 100644 --- a/assets/index.html +++ b/assets/index.html @@ -6,6 +6,10 @@ soundcloak - +
+ + + +
\ No newline at end of file diff --git a/assets/robots.txt b/assets/robots.txt new file mode 100644 index 0000000..77470cb --- /dev/null +++ b/assets/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: / \ No newline at end of file diff --git a/lib/cfg/init.go b/lib/cfg/init.go index 321a7b2..bb6541a 100644 --- a/lib/cfg/init.go +++ b/lib/cfg/init.go @@ -19,4 +19,7 @@ const TrackTTL = 5 * time.Minute // default fasthttp one was causing connections to be stuck? todo make it cycle browser useragents or just choose random at startup const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0" +// time-to-live for dns cache +const DNSCacheTTL = 10 * time.Minute + var JSON = jsoniter.ConfigFastest diff --git a/lib/sc/init.go b/lib/sc/init.go index bc5779e..c720738 100644 --- a/lib/sc/init.go +++ b/lib/sc/init.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/url" + "os" "regexp" "strings" "time" @@ -25,6 +26,8 @@ type cached[T any] struct { Expires time.Time } +var httpc = fasthttp.Client{Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial} + var usersCache = map[string]cached[User]{} var tracksCache = map[string]cached[Track]{} @@ -114,6 +117,21 @@ func GetClientID() (string, error) { return "", ErrIDNotFound } +func DoWithRetry(req *fasthttp.Request, resp *fasthttp.Response) (err error) { + for i := 0; i < 5; i++ { + err = httpc.DoTimeout(req, resp, time.Second) + if err == nil { + return nil + } + + if !os.IsTimeout(err) && err != fasthttp.ErrTimeout { + return + } + } + + return +} + func Resolve(path string, out any) error { cid, err := GetClientID() if err != nil { @@ -130,7 +148,7 @@ func Resolve(path string, out any) error { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err = fasthttp.Do(req, resp) + err = DoWithRetry(req, resp) if err != nil { return err } @@ -207,7 +225,7 @@ func (p *Paginated[T]) Proceed() error { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err = fasthttp.Do(req, resp) + err = DoWithRetry(req, resp) if err != nil { return err } @@ -258,7 +276,7 @@ func (t Track) GetStream() (string, error) { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err = fasthttp.Do(req, resp) + err = DoWithRetry(req, resp) if err != nil { return "", err }