Skip to content

Commit

Permalink
Merge pull request #9 from ulnasensei/master
Browse files Browse the repository at this point in the history
Change hardcoded domain to use environment variables.
  • Loading branch information
meehow committed Sep 3, 2023
2 parents 2cbe796 + 02f3203 commit f1d535e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
33 changes: 19 additions & 14 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -44,7 +48,7 @@ <h2>Private BitTorrent tracker for everyone</h2>

<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>
Expand All @@ -56,7 +60,7 @@ <h3>Using <a href="https://transmissionbt.com/" target="_blank">transmission</a>
<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>
Expand All @@ -68,7 +72,8 @@ <h3>Using <a href="https://transmissionbt.com/" target="_blank">transmission</a>
<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>
Expand All @@ -80,4 +85,4 @@ <h3>Using <a href="https://transmissionbt.com/" target="_blank">transmission</a>
</div>
</body>

</html>
</html>
40 changes: 32 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/tls"
"fmt"
"log"
"net"
"os"
Expand All @@ -16,6 +17,8 @@ import (
"golang.org/x/crypto/acme/autocert"
)

var envConfig = getEnvConfig()

func main() {
go Cleanup()
config := fiber.Config{
Expand All @@ -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())
Expand All @@ -50,12 +54,32 @@ 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 {
Expand Down Expand Up @@ -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"))
}
Expand All @@ -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()
}

0 comments on commit f1d535e

Please sign in to comment.