changed hardcoded url to use environment variables

This commit is contained in:
Ulna 2023-09-02 15:26:47 +03:00
parent 84e7a0a230
commit 02f320379e
2 changed files with 54 additions and 8 deletions

View file

@ -3,3 +3,25 @@
PrivTracker allows to share torrent files just with your friends, nobody else. PrivTracker allows to share torrent files just with your friends, nobody else.
Unlike public trackers, it shares peers only within a group which is using the same Announce URL. Unlike public trackers, it shares peers only within a group which is using the same Announce URL.
It really works like a private tracker, but can be generated with one click of a button. It really works like a private tracker, but can be generated with one click of a button.
---
### Build
```bash
# Clone this repository.
$ git clone https://github.com/meehow/privtracker.git
# cd into the directory
$ cd privtracker
# Run go build
$ go build
```
### Usage
```bash
# Runs on port 1337 and redirects to privtracker.com by default.
$ ./privtracker
```
```bash
# Export PORT and DOMAIN variables to use custom values.
$ export PORT=12345 DOMAIN=customprivtracker.com; ./privtracker
```

36
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"log" "log"
"net" "net"
"os" "os"
@ -16,6 +17,8 @@ import (
"golang.org/x/crypto/acme/autocert" "golang.org/x/crypto/acme/autocert"
) )
var envConfig = getEnvConfig()
func main() { func main() {
go Cleanup() go Cleanup()
config := fiber.Config{ config := fiber.Config{
@ -30,6 +33,7 @@ func main() {
config.TrustedProxies = []string{"127.0.0.1"} config.TrustedProxies = []string{"127.0.0.1"}
config.ProxyHeader = fiber.HeaderXForwardedFor config.ProxyHeader = fiber.HeaderXForwardedFor
} }
app := fiber.New(config) app := fiber.New(config)
app.Use(recover.New()) app.Use(recover.New())
// app.Use(pprof.New()) // app.Use(pprof.New())
@ -50,12 +54,32 @@ func main() {
split := strings.Split(domains, ",") split := strings.Split(domains, ",")
log.Fatal(app.Listener(myListener(split...))) log.Fatal(app.Listener(myListener(split...)))
} else { } else {
log.Fatal(app.Listen(":" + envConfig.port))
}
}
type EnvConfig struct {
domain string
port string
}
func getEnvConfig() EnvConfig {
config := EnvConfig{
domain: "privtracker.com",
port: "1337",
}
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { domain := os.Getenv("DOMAIN")
port = "1337" if domain != "" {
config.domain = domain
} }
log.Fatal(app.Listen(":" + port)) if port != "" {
config.port = port
} }
return config
} }
func myListener(domains ...string) net.Listener { func myListener(domains ...string) net.Listener {
@ -98,7 +122,7 @@ func redirect80(config fiber.Config) {
config.DisableStartupMessage = true config.DisableStartupMessage = true
app := fiber.New(config) app := fiber.New(config)
app.Use(func(c *fiber.Ctx) error { app.Use(func(c *fiber.Ctx) error {
return c.Redirect("https://privtracker.com/", fiber.StatusMovedPermanently) return c.Redirect(fmt.Sprintf("https://%s/", envConfig.domain), fiber.StatusMovedPermanently)
}) })
log.Print(app.Listen(":80")) log.Print(app.Listen(":80"))
} }
@ -115,8 +139,8 @@ func hsts(c *fiber.Ctx) error {
} }
func docs(c *fiber.Ctx) error { func docs(c *fiber.Ctx) error {
if c.Hostname() != "privtracker.com" { if c.Hostname() != envConfig.domain {
return c.Redirect("https://privtracker.com/", fiber.StatusMovedPermanently) return c.Redirect(fmt.Sprintf("https://%s/", envConfig.domain), fiber.StatusMovedPermanently)
} }
return c.Next() return c.Next()
} }