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.
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.
---
### 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
```

40
main.go
View file

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