Merge pull request #9 from ulnasensei/master

Change hardcoded domain to use environment variables.
This commit is contained in:
meehow 2023-09-03 20:50:53 +02:00 committed by GitHub
commit f1d535e863
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 22 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
```

View file

@ -7,22 +7,26 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>PrivTracker - Private BitTorrent tracker for everyone</title>
<script>
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll(".url").forEach(url => {
url.innerText = url.innerText.replace('{{hostname}}', window.location.hostname);
})
})
function makeRoom(length) {
var roomID = '';
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
let roomID = '';
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
roomID += characters.charAt(Math.floor(Math.random() * charactersLength));
}
var urls = document.getElementsByClassName('url');
for (var i = 0; i < urls.length; i++) {
urls[i].innerText = urls[i].innerText.replace('{{room}}', roomID);
}
document.querySelectorAll(".url").forEach(url => {
url.innerText = url.innerText.replace('{{room}}', roomID);
})
document.getElementById('hidden').style.display = 'block';
document.querySelector('#hidden').style.display = 'block';
}
function copyToClipboard() {
navigator.clipboard.writeText(document.getElementsByClassName('url')[0].innerText);
navigator.clipboard.writeText(document.querySelector('.url').innerText);
}
</script>
</head>
@ -44,7 +48,7 @@
<div id="hidden" style="display:none">
<h3>Your announce URL:</h3>
<code class="url">https://privtracker.com/{{room}}/announce</code>
<code class="url">https://{{hostname}}/{{room}}/announce</code>
<button onclick="copyToClipboard()">Copy to clipboard</button>
<p>(You can also use any random string as the Room ID. We don't store it anywhere.)</p>
</div>
@ -56,7 +60,7 @@
<ul>
<li>File <b>&rarr;</b> New&hellip;</li>
<li>Select file to share</li>
<li>In <b>Trackers</b> field enter <code class="url">https://privtracker.com/{{room}}/announce</code></li>
<li>In <b>Trackers</b> field enter <code class="url">https://{{hostname}}/{{room}}/announce</code></li>
<li>Select <b>Private torrent</b></li>
<li>Click <b>New</b></li>
<li>Click <b>Add</b> in next window</li>
@ -68,7 +72,8 @@
<ul>
<li>File <b>&rarr;</b> Create Torrent File&hellip;</li>
<li>Select file to share</li>
<li>Click <b>&plus;</b> under <b>Trackers</b> field and enter <code class="url">https://privtracker.com/{{room}}/announce</code></li>
<li>Click <b>&plus;</b> under <b>Trackers</b> field and enter <code
class="url">https://{{hostname}}/{{room}}/announce</code></li>
<li>Select <b>Private</b>
<li>Select <b>Open when created</b></li>
<li>Click <b>Create</b></li>
@ -80,4 +85,4 @@
</div>
</body>
</html>
</html>

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()
}