Skip to content

Commit

Permalink
fixed breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
s0up4200 committed Apr 7, 2023
1 parent 074b02f commit 450ee63
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ To use RedactedHook, send POST requests to the following endpoints:

```json
{
"user_id": "USER_ID",
"user_id": USER_ID,
"apikey": "API_KEY",
"minratio": "MINIMUM_RATIO"
"minratio": MINIMUM_RATIO
}
```

Expand All @@ -82,7 +82,7 @@ To use RedactedHook, send POST requests to the following endpoints:
```json

{
"id": "{{.TorrentID}}",
"torrent_id": {{.TorrentID}},
"apikey": "API_KEY",
"uploaders": "BLACKLISTED_USER1,BLACKLISTED_USER2,BLACKLISTED_USER3"
}
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module RedactedHook

go 1.20

require github.com/rs/zerolog v1.29.0

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
golang.org/x/sys v0.7.0 // indirect
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63 changes: 36 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package main
import (
"encoding/json"
"fmt"
"io"
"log"
"io/ioutil"
"net/http"
"strconv"
"os"
"strings"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

type RatioRequestData struct {
ID string `json:"user_id"`
APIKey string `json:"apikey"`
MinRatio string `json:"minratio"`
ID int `json:"user_id"`
APIKey string `json:"apikey"`
MinRatio float64 `json:"minratio"`
}

type UploaderRequestData struct {
ID string `json:"id"`
ID int `json:"torrent_id"`
APIKey string `json:"apikey"`
Usernames string `json:"uploaders"`
}
Expand All @@ -41,9 +43,17 @@ type UploaderResponseData struct {
}

func main() {
// Configure zerolog to use colored output
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "2006-01-02 15:04:05", NoColor: false})

http.HandleFunc("/redacted/ratio", checkRatio)
http.HandleFunc("/redacted/uploader", checkUploader)
log.Fatal(http.ListenAndServe("127.0.0.1:42135", nil))
log.Info().Msg("Starting server on 127.0.0.1:42135")
err := http.ListenAndServe("127.0.0.1:42135", nil)
if err != nil {
log.Fatal().Err(err).Msg("Failed to start server")
}
}

func checkRatio(w http.ResponseWriter, r *http.Request) {
Expand All @@ -53,10 +63,10 @@ func checkRatio(w http.ResponseWriter, r *http.Request) {
}

// Log request received
log.Printf("Received request from %s", r.RemoteAddr)
log.Debug().Msgf("Received request from %s", r.RemoteAddr)

// Read JSON payload from the request body
body, err := io.ReadAll(r.Body)
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -66,11 +76,12 @@ func checkRatio(w http.ResponseWriter, r *http.Request) {
var requestData RatioRequestData
err = json.Unmarshal(body, &requestData)
if err != nil {
log.Debug().Msgf("Failed to unmarshal JSON payload: %s", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

endpoint := fmt.Sprintf("https://redacted.ch/ajax.php?action=user&id=%s", requestData.ID)
endpoint := fmt.Sprintf("https://redacted.ch/ajax.php?action=user&id=%d", requestData.ID)

client := &http.Client{}
req, err := http.NewRequest("GET", endpoint, nil)
Expand All @@ -87,7 +98,7 @@ func checkRatio(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -101,31 +112,28 @@ func checkRatio(w http.ResponseWriter, r *http.Request) {
}

ratio := responseData.Response.Stats.Ratio
minRatio, err := strconv.ParseFloat(requestData.MinRatio, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
minRatio := requestData.MinRatio

if ratio < minRatio {
w.WriteHeader(http.StatusIMUsed) // HTTP status code 226
log.Printf("Returned ratio (%f) is below minratio (%f), responding with status 226\n", ratio, minRatio)
log.Debug().Msgf("Returned ratio (%f) is below minratio (%f), responding with status 226", ratio, minRatio)
} else {
w.WriteHeader(http.StatusOK) // HTTP status code 200
log.Printf("Returned ratio (%f) is equal to or above minratio (%f), responding with status 200\n", ratio, minRatio)
log.Debug().Msgf("Returned ratio (%f) is equal to or above minratio (%f), responding with status 200", ratio, minRatio)
}
}

func checkUploader(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Debug().Msg("Non-POST method received")
http.Error(w, "Only POST method is supported", http.StatusBadRequest)
return
}

// Log request received
log.Printf("Received request from %s", r.RemoteAddr)
log.Debug().Msgf("Received request from %s", r.RemoteAddr)

body, err := io.ReadAll(r.Body)
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -135,11 +143,12 @@ func checkUploader(w http.ResponseWriter, r *http.Request) {
var requestData UploaderRequestData
err = json.Unmarshal(body, &requestData)
if err != nil {
log.Debug().Msgf("Failed to unmarshal JSON payload: %s", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

endpoint := fmt.Sprintf("https://redacted.ch/ajax.php?action=torrent&id=%s", requestData.ID)
endpoint := fmt.Sprintf("https://redacted.ch/ajax.php?action=torrent&id=%d", requestData.ID)

client := &http.Client{}
req, err := http.NewRequest("GET", endpoint, nil)
Expand All @@ -156,7 +165,7 @@ func checkUploader(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -171,24 +180,24 @@ func checkUploader(w http.ResponseWriter, r *http.Request) {

// Check for a "failure" status in the JSON response
if responseData.Status == "failure" {
log.Printf("JSON response indicates a failure: %s\n", responseData.Error)
log.Error().Str("JSON response indicates a failure: %s\n", responseData.Error)
http.Error(w, responseData.Error, http.StatusBadRequest)
return
}

username := responseData.Response.Torrent.Username
usernames := strings.Split(requestData.Usernames, ",")

log.Printf("Found uploader: %s\n", username) // Print the uploader's username
log.Debug().Msgf("Found uploader: %s", username) // Print the uploader's username

for _, uname := range usernames {
if uname == username {
w.WriteHeader(http.StatusIMUsed + 1) // HTTP status code 226
log.Printf("Uploader (%s) is blacklisted, responding with status 226\n", username)
log.Debug().Msgf("Uploader (%s) is blacklisted, responding with status 226", username)
return
}
}

w.WriteHeader(http.StatusOK) // HTTP status code 200
log.Printf("Uploader not in blacklist, responding with status 200\n")
log.Debug().Msg("Uploader not in blacklist, responding with status 200")
}

0 comments on commit 450ee63

Please sign in to comment.