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 10, 2023
1 parent 648761a commit c6ceb97
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
Expand All @@ -12,26 +12,37 @@ import (
"github.com/rs/zerolog/log"
)

type RequestData struct {
ID int `json:"id"`
APIKey string `json:"apikey"`
MinRatio float64 `json:"minratio,omitempty"`
TorrentID int `json:"torrent_id,omitempty"`
Usernames string `json:"uploaders,omitempty"`
type RatioRequestData struct {
ID int `json:"user_id"`
APIKey string `json:"apikey"`
MinRatio float64 `json:"minratio"`
}

type ResponseData struct {
Status string `json:"status"`
Error string `json:"error"`
TorrentUploader struct {
Username string `json:"username"`
} `json:"torrentUploader"`
Stats struct {
Ratio float64 `json:"ratio"`
} `json:"stats"`
type UploaderRequestData struct {
ID int `json:"torrent_id"`
APIKey string `json:"apikey"`
Usernames string `json:"uploaders"`
}

var httpClient = &http.Client{}
type RatioResponseData struct {
Status string `json:"status"`
Error string `json:"error"`
Response struct {
Stats struct {
Ratio float64 `json:"ratio"`
} `json:"stats"`
} `json:"response"`
}

type UploaderResponseData struct {
Status string `json:"status"`
Error string `json:"error"`
Response struct {
Torrent struct {
Username string `json:"username"`
} `json:"torrent"`
} `json:"response"`
}

func main() {
// Configure zerolog to use colored output
Expand All @@ -57,14 +68,14 @@ func checkRatio(w http.ResponseWriter, r *http.Request) {
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)
return
}

var requestData RequestData
var requestData RatioRequestData
err = json.Unmarshal(body, &requestData)
if err != nil {
log.Debug().Msgf("Failed to unmarshal JSON payload: %s", err.Error())
Expand All @@ -74,33 +85,28 @@ func checkRatio(w http.ResponseWriter, r *http.Request) {

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

client := &http.Client{}
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
req.Header.Set("Authorization", requestData.APIKey)

if requestData.APIKey == "" {
log.Error().Msg("API key is empty")
http.Error(w, "API key is empty", http.StatusBadRequest)
return
}

resp, err := httpClient.Do(req)
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
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
}

var responseData ResponseData
var responseData RatioResponseData
err = json.Unmarshal(respBody, &responseData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -114,7 +120,7 @@ func checkRatio(w http.ResponseWriter, r *http.Request) {
return
}

ratio := responseData.Stats.Ratio
ratio := responseData.Response.Stats.Ratio
minRatio := requestData.MinRatio

if ratio < minRatio {
Expand All @@ -136,24 +142,24 @@ func checkUploader(w http.ResponseWriter, r *http.Request) {
// Log request received
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)
return
}

var requestData RequestData
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
}

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

client := &http.Client{}
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -167,22 +173,20 @@ func checkUploader(w http.ResponseWriter, r *http.Request) {
return
}

resp, err := httpClient.Do(req)
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()

// Read the response body
respBody, err := io.ReadAll(resp.Body)
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Unmarshal the response JSON into the appropriate struct
var responseData ResponseData
var responseData UploaderResponseData
err = json.Unmarshal(respBody, &responseData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -196,8 +200,7 @@ func checkUploader(w http.ResponseWriter, r *http.Request) {
return
}

// Check if the uploader is in the blacklist
username := responseData.TorrentUploader.Username
username := responseData.Response.Torrent.Username
usernames := strings.Split(requestData.Usernames, ",")

log.Debug().Msgf("Found uploader: %s", username) // Print the uploader's username
Expand All @@ -210,7 +213,6 @@ func checkUploader(w http.ResponseWriter, r *http.Request) {
}
}

// If uploader is not in the blacklist, respond with HTTP status 200
w.WriteHeader(http.StatusOK) // HTTP status code 200
log.Debug().Msg("Uploader not in blacklist, responding with status 200")
}

0 comments on commit c6ceb97

Please sign in to comment.