mirror of
https://github.com/SinTan1729/privtracker.git
synced 2024-12-25 15:08:35 -06:00
handlers in separate files
This commit is contained in:
parent
d9d8470b3a
commit
11533d617c
3 changed files with 107 additions and 96 deletions
69
annnounce.go
Normal file
69
annnounce.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/jackpal/bencode-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AnnounceRequest struct {
|
||||||
|
InfoHash string `query:"info_hash"`
|
||||||
|
PeerID string `query:"peer_id"`
|
||||||
|
IP string `query:"ip"`
|
||||||
|
Port uint16 `query:"port"`
|
||||||
|
Uploaded uint `query:"uploaded"`
|
||||||
|
Downloaded uint `query:"downloaded"`
|
||||||
|
Left uint `query:"left"`
|
||||||
|
Numwant uint `query:"numwant"`
|
||||||
|
Key string `query:"key"`
|
||||||
|
Compact bool `query:"compact"`
|
||||||
|
SupportCrypto bool `query:"supportcrypto"`
|
||||||
|
Event string `query:"event"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *AnnounceRequest) IsSeeding() bool {
|
||||||
|
return req.Left == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type AnnounceResponse struct {
|
||||||
|
Interval int `bencode:"interval"`
|
||||||
|
Complete int `bencode:"complete"`
|
||||||
|
Incomplete int `bencode:"incomplete"`
|
||||||
|
Peers []byte `bencode:"peers"`
|
||||||
|
PeersIPv6 []byte `bencode:"peers_ipv6"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func announce(c *fiber.Ctx) error {
|
||||||
|
var req AnnounceRequest
|
||||||
|
err := c.QueryParser(&req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.IP = c.IP()
|
||||||
|
if req.Numwant == 0 {
|
||||||
|
req.Numwant = 30
|
||||||
|
}
|
||||||
|
switch req.Event {
|
||||||
|
case "stopped":
|
||||||
|
DeletePeer(c.Params("room"), req.InfoHash, req.IP, req.Port)
|
||||||
|
case "completed":
|
||||||
|
GraduateLeecher(c.Params("room"), req.InfoHash, req.IP, req.Port)
|
||||||
|
default:
|
||||||
|
PutPeer(c.Params("room"), req.InfoHash, req.IP, req.Port, req.IsSeeding())
|
||||||
|
}
|
||||||
|
peersIPv4, peersIPv6, numSeeders, numLeechers := GetPeers(c.Params("room"), req.InfoHash, req.IP, req.Port, req.IsSeeding(), req.Numwant)
|
||||||
|
interval := 120
|
||||||
|
if numSeeders == 0 {
|
||||||
|
interval /= 2
|
||||||
|
} else if numLeechers == 0 {
|
||||||
|
interval *= 2
|
||||||
|
}
|
||||||
|
resp := AnnounceResponse{
|
||||||
|
Interval: interval,
|
||||||
|
Complete: numSeeders,
|
||||||
|
Incomplete: numLeechers,
|
||||||
|
Peers: peersIPv4,
|
||||||
|
PeersIPv6: peersIPv6,
|
||||||
|
}
|
||||||
|
defer c.Response().SetConnectionClose()
|
||||||
|
return bencode.Marshal(c, resp)
|
||||||
|
}
|
96
main.go
96
main.go
|
@ -13,7 +13,6 @@ import (
|
||||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
"github.com/gofiber/fiber/v2/middleware/monitor"
|
"github.com/gofiber/fiber/v2/middleware/monitor"
|
||||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
"github.com/jackpal/bencode-go"
|
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -118,98 +117,3 @@ func docs(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnnounceRequest struct {
|
|
||||||
InfoHash string `query:"info_hash"`
|
|
||||||
PeerID string `query:"peer_id"`
|
|
||||||
IP string `query:"ip"`
|
|
||||||
Port uint16 `query:"port"`
|
|
||||||
Uploaded uint `query:"uploaded"`
|
|
||||||
Downloaded uint `query:"downloaded"`
|
|
||||||
Left uint `query:"left"`
|
|
||||||
Numwant uint `query:"numwant"`
|
|
||||||
Key string `query:"key"`
|
|
||||||
Compact bool `query:"compact"`
|
|
||||||
SupportCrypto bool `query:"supportcrypto"`
|
|
||||||
Event string `query:"event"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (req *AnnounceRequest) IsSeeding() bool {
|
|
||||||
return req.Left == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
type AnnounceResponse struct {
|
|
||||||
Interval int `bencode:"interval"`
|
|
||||||
Complete int `bencode:"complete"`
|
|
||||||
Incomplete int `bencode:"incomplete"`
|
|
||||||
Peers []byte `bencode:"peers"`
|
|
||||||
PeersIPv6 []byte `bencode:"peers_ipv6"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func announce(c *fiber.Ctx) error {
|
|
||||||
var req AnnounceRequest
|
|
||||||
err := c.QueryParser(&req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.IP = c.IP()
|
|
||||||
if req.Numwant == 0 {
|
|
||||||
req.Numwant = 30
|
|
||||||
}
|
|
||||||
switch req.Event {
|
|
||||||
case "stopped":
|
|
||||||
DeletePeer(c.Params("room"), req.InfoHash, req.IP, req.Port)
|
|
||||||
case "completed":
|
|
||||||
GraduateLeecher(c.Params("room"), req.InfoHash, req.IP, req.Port)
|
|
||||||
default:
|
|
||||||
PutPeer(c.Params("room"), req.InfoHash, req.IP, req.Port, req.IsSeeding())
|
|
||||||
}
|
|
||||||
peersIPv4, peersIPv6, numSeeders, numLeechers := GetPeers(c.Params("room"), req.InfoHash, req.IP, req.Port, req.IsSeeding(), req.Numwant)
|
|
||||||
interval := 120
|
|
||||||
if numSeeders == 0 {
|
|
||||||
interval /= 2
|
|
||||||
} else if numLeechers == 0 {
|
|
||||||
interval *= 2
|
|
||||||
}
|
|
||||||
resp := AnnounceResponse{
|
|
||||||
Interval: interval,
|
|
||||||
Complete: numSeeders,
|
|
||||||
Incomplete: numLeechers,
|
|
||||||
Peers: peersIPv4,
|
|
||||||
PeersIPv6: peersIPv6,
|
|
||||||
}
|
|
||||||
defer c.Response().SetConnectionClose()
|
|
||||||
return bencode.Marshal(c, resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ScrapeRequest struct {
|
|
||||||
InfoHash string `query:"info_hash"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ScrapeResponse struct {
|
|
||||||
Files map[string]Stat `bencode:"files"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Stat struct {
|
|
||||||
Complete int `bencode:"complete"`
|
|
||||||
Incomplete int `bencode:"incomplete"`
|
|
||||||
// Downloaded uint `bencode:"downloaded"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func scrape(c *fiber.Ctx) error {
|
|
||||||
var req ScrapeRequest
|
|
||||||
err := c.QueryParser(&req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
numSeeders, numLeechers := GetStats(c.Params("room"), req.InfoHash)
|
|
||||||
resp := ScrapeResponse{
|
|
||||||
Files: map[string]Stat{
|
|
||||||
req.InfoHash: {
|
|
||||||
Complete: numSeeders,
|
|
||||||
Incomplete: numLeechers,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return bencode.Marshal(c, resp)
|
|
||||||
}
|
|
||||||
|
|
38
scrape.go
Normal file
38
scrape.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/jackpal/bencode-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ScrapeRequest struct {
|
||||||
|
InfoHash string `query:"info_hash"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScrapeResponse struct {
|
||||||
|
Files map[string]Stat `bencode:"files"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Stat struct {
|
||||||
|
Complete int `bencode:"complete"`
|
||||||
|
Incomplete int `bencode:"incomplete"`
|
||||||
|
// Downloaded uint `bencode:"downloaded"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func scrape(c *fiber.Ctx) error {
|
||||||
|
var req ScrapeRequest
|
||||||
|
err := c.QueryParser(&req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
numSeeders, numLeechers := GetStats(c.Params("room"), req.InfoHash)
|
||||||
|
resp := ScrapeResponse{
|
||||||
|
Files: map[string]Stat{
|
||||||
|
req.InfoHash: {
|
||||||
|
Complete: numSeeders,
|
||||||
|
Incomplete: numLeechers,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return bencode.Marshal(c, resp)
|
||||||
|
}
|
Loading…
Reference in a new issue