forked from die-net/dhtproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (61 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"flag"
"log"
"net/http"
_ "net/http/pprof" //nolint:gosec // TODO: Expose this on a different port.
"time"
"github.com/die-net/dhtproxy/peercache"
)
var (
listenAddr = flag.String("listen", ":6969", "The [IP]:port to listen for incoming HTTP requests.")
debugAddr = flag.String("debugListen", "", "The [IP]:port to listen for pprof HTTP requests. (\"\" = disable)")
dhtPortUDP = flag.Int("dhtPortUDP", 0, "The UDP port number to use for DHT requests")
dhtResetInterval = flag.Duration("dhtResetInterval", time.Hour, "How often to reset the DHT client (0 = disable)")
targetNumPeers = flag.Int("targetNumPeers", 8, "The number of DHT peers to try to find for a given node")
peerCacheSize = flag.Int("peerCacheSize", 16384, "The max number of infohashes to keep a list of peers for.")
maxWant = flag.Int("maxWant", 200, "The largest number of peers to return in one request.")
peerCache *peercache.Cache
dhtNode *DhtNode
)
func main() {
flag.Parse()
setRlimitFromFlags()
var err error
peerCache, err = peercache.New(*peerCacheSize, *maxWant)
if err != nil {
log.Fatal(err)
}
dhtNode, err = NewDhtNode(*dhtPortUDP, *targetNumPeers, *dhtResetInterval, peerCache)
if err != nil {
log.Fatal(err)
}
if *debugAddr != "" {
// Serve /debug/pprof/* on default mux
go func() {
srv := &http.Server{
Addr: *debugAddr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 240 * time.Second,
Handler: http.DefaultServeMux,
}
log.Fatal(srv.ListenAndServe())
}()
}
mux := http.NewServeMux()
mux.HandleFunc("/robots.txt", robotsDisallowHandler)
mux.HandleFunc("/announce", trackerHandler)
srv := &http.Server{
Addr: *listenAddr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 240 * time.Second,
Handler: mux,
}
log.Fatal(srv.ListenAndServe())
}
func robotsDisallowHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte("User-agent: *\nDisallow: /\n"))
}