Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

status merged

Cette révision appartient à :
ayame-git 2017-05-03 10:33:39 +03:00
Parent bd1609a3a5
révision 017ff90fa0
1 fichiers modifiés avec 27 ajouts et 25 suppressions

52
main.go
Voir le fichier

@ -10,7 +10,6 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -19,6 +18,7 @@ import (
var dbHandle *sql.DB var dbHandle *sql.DB
var templates = template.Must(template.ParseFiles("index.html")) var templates = template.Must(template.ParseFiles("index.html"))
var debugLogger *log.Logger var debugLogger *log.Logger
var trackers = "&tr=udp://zer0day.to:1337/announce&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://explodie.org:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.coppersurfer.tk:6969"
type Record struct { type Record struct {
Category string `json: "category"` Category string `json: "category"`
@ -28,11 +28,11 @@ type Record struct {
} }
type Records struct { type Records struct {
Id string `json: "id"` Id string `json: "id"`
Name string `json: "name"` Name string `json: "name"`
Status int `json: "status"` Status int `json: "status"`
Hash string `json: "hash"` Hash string `json: "hash"`
Magnet string `json: "magnet"` Magnet template.URL `json: "magnet"`
} }
func getDBHandle() *sql.DB { func getDBHandle() *sql.DB {
@ -44,7 +44,6 @@ func getDBHandle() *sql.DB {
func checkErr(err error) { func checkErr(err error) {
if err != nil { if err != nil {
debugLogger.Println(" " + err.Error()) debugLogger.Println(" " + err.Error())
os.Exit(1)
} }
} }
@ -58,14 +57,14 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
for rows.Next() { for rows.Next() {
var id, name, hash, magnet string var id, name, hash, magnet string
var status int var status int
rows.Scan(&id, &name, &hash) rows.Scan(&id, &name, &status, &hash)
magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + "&tr=udp://tracker.openbittorrent.com" magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + trackers
res := Records{ res := Records{
Id: id, Id: id,
Name: name, Name: name,
Status: status, Status: status,
Hash: hash, Hash: hash,
Magnet: magnet} Magnet: safe(magnet)}
b.Records = append(b.Records, res) b.Records = append(b.Records, res)
@ -90,14 +89,14 @@ func singleapiHandler(w http.ResponseWriter, r *http.Request) {
for rows.Next() { for rows.Next() {
var id, name, hash, magnet string var id, name, hash, magnet string
var status int var status int
rows.Scan(&id, &name, &hash) rows.Scan(&id, &name, &status, &hash)
magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + "&tr=udp://tracker.openbittorrent.com" magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + trackers
res := Records{ res := Records{
Id: id, Id: id,
Name: name, Name: name,
Status: status, Status: status,
Hash: hash, Hash: hash,
Magnet: magnet} Magnet: safe(magnet)}
b.Records = append(b.Records, res) b.Records = append(b.Records, res)
@ -129,15 +128,15 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
"%"+html.EscapeString(param1)+"%", html.EscapeString(param2)+"%", html.EscapeString(param3)+"%", 50*pagenum-1) "%"+html.EscapeString(param1)+"%", html.EscapeString(param2)+"%", html.EscapeString(param3)+"%", 50*pagenum-1)
for rows.Next() { for rows.Next() {
var id, name, hash, magnet string var id, name, hash, magnet string
var status int var status int
rows.Scan(&id, &name, &hash) rows.Scan(&id, &name, &status, &hash)
magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + "&tr=udp://tracker.openbittorrent.com" magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + trackers
res := Records{ res := Records{
Id: id, Id: id,
Name: name, Name: name,
Status: status, Status: status,
Hash: hash, Hash: hash,
Magnet: magnet} Magnet: safe(magnet)}
b.Records = append(b.Records, res) b.Records = append(b.Records, res)
@ -149,6 +148,9 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func safe(s string) template.URL {
return template.URL(s)
}
func rootHandler(w http.ResponseWriter, r *http.Request) { func rootHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
@ -158,15 +160,15 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
rows, err := dbHandle.Query("select torrent_id, torrent_name, status_id, torrent_hash from torrents ORDER BY torrent_id DESC LIMIT 50 offset ?", 50*pagenum-1) rows, err := dbHandle.Query("select torrent_id, torrent_name, status_id, torrent_hash from torrents ORDER BY torrent_id DESC LIMIT 50 offset ?", 50*pagenum-1)
for rows.Next() { for rows.Next() {
var id, name, hash, magnet string var id, name, hash, magnet string
var status int var status int
rows.Scan(&id, &name, &hash) rows.Scan(&id, &name, &status, &hash)
magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + "&tr=udp://tracker.openbittorrent.com" magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + url.QueryEscape(name) + trackers
res := Records{ res := Records{
Id: id, Id: id,
Name: name, Name: name,
Status: status, Status: status,
Hash: hash, Hash: hash,
Magnet: magnet} Magnet: safe(magnet)}
b.Records = append(b.Records, res) b.Records = append(b.Records, res)