stuff
Cette révision appartient à :
Parent
df2f0d9839
révision
cc5baed9c1
4 fichiers modifiés avec 125 ajouts et 61 suppressions
|
@ -6,6 +6,7 @@ import (
|
||||||
"html"
|
"html"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
|
@ -100,22 +101,22 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
token := r.Header.Get("Authorization")
|
||||||
|
user := model.User{}
|
||||||
|
db.ORM.Where("api_token = ?", token).First(&user) //i don't like this
|
||||||
|
if user.ID == 0 {
|
||||||
|
http.Error(w, apiService.ErrApiKey.Error(), http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
upload := apiService.TorrentRequest{}
|
||||||
|
var filesize int64
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
if contentType == "application/json" {
|
if contentType == "application/json" {
|
||||||
token := r.Header.Get("Authorization")
|
|
||||||
user := model.User{}
|
|
||||||
db.ORM.Where("api_token = ?", token).First(&user) //i don't like this
|
|
||||||
if user.ID == 0 {
|
|
||||||
http.Error(w, apiService.ErrApiKey.Error(), http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
//verify token
|
|
||||||
//token := r.Header.Get("Authorization")
|
|
||||||
|
|
||||||
upload := apiService.TorrentRequest{}
|
|
||||||
d := json.NewDecoder(r.Body)
|
d := json.NewDecoder(r.Body)
|
||||||
if err := d.Decode(&upload); err != nil {
|
if err := d.Decode(&upload); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -126,27 +127,41 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, err.Error(), code)
|
http.Error(w, err.Error(), code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else if strings.HasPrefix(contentType, "multipart/form-data") {
|
||||||
|
|
||||||
torrent := model.Torrent{
|
upload.Name = r.FormValue("name")
|
||||||
Name: upload.Name,
|
upload.Category, _ = strconv.Atoi(r.FormValue("category"))
|
||||||
Category: upload.Category,
|
upload.SubCategory, _ = strconv.Atoi(r.FormValue("sub_category"))
|
||||||
SubCategory: upload.SubCategory,
|
upload.Description = r.FormValue("description")
|
||||||
Status: 1,
|
|
||||||
Hash: upload.Hash,
|
|
||||||
Date: time.Now(),
|
|
||||||
Filesize: 0, //?
|
|
||||||
Description: upload.Description,
|
|
||||||
UploaderID: user.ID,
|
|
||||||
Uploader: &user,
|
|
||||||
}
|
|
||||||
|
|
||||||
db.ORM.Create(&torrent)
|
var err error
|
||||||
|
var code int
|
||||||
|
|
||||||
|
filesize, err, code = upload.ValidateMultipartUpload(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.SendError(w, err, 500)
|
http.Error(w, err.Error(), code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("%+v\n", torrent)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
torrent := model.Torrent{
|
||||||
|
Name: upload.Name,
|
||||||
|
Category: upload.Category,
|
||||||
|
SubCategory: upload.SubCategory,
|
||||||
|
Status: 1,
|
||||||
|
Hash: upload.Hash,
|
||||||
|
Date: time.Now(),
|
||||||
|
Filesize: filesize,
|
||||||
|
Description: upload.Description,
|
||||||
|
UploaderID: user.ID,
|
||||||
|
Uploader: &user,
|
||||||
|
}
|
||||||
|
db.ORM.Create(&torrent)
|
||||||
|
/*if err != nil {
|
||||||
|
util.SendError(w, err, 500)
|
||||||
|
return
|
||||||
|
}*/
|
||||||
|
fmt.Printf("%+v\n", torrent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
func ApiUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"github.com/ewhal/nyaa/cache"
|
"github.com/ewhal/nyaa/cache"
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
"github.com/ewhal/nyaa/service/captcha"
|
"github.com/ewhal/nyaa/service/captcha"
|
||||||
|
"github.com/ewhal/nyaa/service/upload"
|
||||||
"github.com/ewhal/nyaa/util"
|
"github.com/ewhal/nyaa/util"
|
||||||
"github.com/ewhal/nyaa/util/metainfo"
|
"github.com/ewhal/nyaa/util/metainfo"
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
|
@ -30,7 +31,7 @@ type UploadForm struct {
|
||||||
Category string
|
Category string
|
||||||
Remake bool
|
Remake bool
|
||||||
Description string
|
Description string
|
||||||
Status int
|
Status int
|
||||||
captcha.Captcha
|
captcha.Captcha
|
||||||
|
|
||||||
Infohash string
|
Infohash string
|
||||||
|
@ -133,7 +134,7 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
|
||||||
return ErrPrivateTorrent
|
return ErrPrivateTorrent
|
||||||
}
|
}
|
||||||
trackers := torrent.GetAllAnnounceURLS()
|
trackers := torrent.GetAllAnnounceURLS()
|
||||||
if !CheckTrackers(trackers) {
|
if !uploadService.CheckTrackers(trackers) {
|
||||||
return ErrTrackerProblem
|
return ErrTrackerProblem
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,35 +237,6 @@ func WriteTorrentToDisk(file multipart.File, name string, fullpath *string) erro
|
||||||
return ioutil.WriteFile(*fullpath, b, 0644)
|
return ioutil.WriteFile(*fullpath, b, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckTrackers(trackers []string) bool {
|
|
||||||
// TODO: move to runtime configuration
|
|
||||||
var deadTrackers = []string{ // substring matches!
|
|
||||||
"://open.nyaatorrents.info:6544",
|
|
||||||
"://tracker.openbittorrent.com:80",
|
|
||||||
"://tracker.publicbt.com:80",
|
|
||||||
"://stats.anisource.net:2710",
|
|
||||||
"://exodus.desync.com",
|
|
||||||
"://open.demonii.com:1337",
|
|
||||||
"://tracker.istole.it:80",
|
|
||||||
"://tracker.ccc.de:80",
|
|
||||||
"://bt2.careland.com.cn:6969",
|
|
||||||
"://announce.torrentsmd.com:8080"}
|
|
||||||
|
|
||||||
var numGood int
|
|
||||||
for _, t := range trackers {
|
|
||||||
good := true
|
|
||||||
for _, check := range deadTrackers {
|
|
||||||
if strings.Contains(t, check) {
|
|
||||||
good = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if good {
|
|
||||||
numGood++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return numGood > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewUploadForm creates a new upload form given parameters as list
|
// NewUploadForm creates a new upload form given parameters as list
|
||||||
func NewUploadForm(params ...string) (uploadForm UploadForm) {
|
func NewUploadForm(params ...string) (uploadForm UploadForm) {
|
||||||
if len(params) > 1 {
|
if len(params) > 1 {
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package apiService
|
package apiService
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -9,6 +12,9 @@ import (
|
||||||
|
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
"github.com/ewhal/nyaa/service"
|
"github.com/ewhal/nyaa/service"
|
||||||
|
"github.com/ewhal/nyaa/service/upload"
|
||||||
|
"github.com/ewhal/nyaa/util/metainfo"
|
||||||
|
"github.com/zeebo/bencode"
|
||||||
)
|
)
|
||||||
|
|
||||||
type torrentsQuery struct {
|
type torrentsQuery struct {
|
||||||
|
@ -60,9 +66,9 @@ func (r *TorrentsRequest) ToParams() serviceBase.WhereParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateName(r *TorrentRequest) (error, int) {
|
func validateName(r *TorrentRequest) (error, int) {
|
||||||
if len(r.Name) < 100 { //isn't this too much?
|
/*if len(r.Name) < 100 { //isn't this too much?
|
||||||
return ErrShortName, http.StatusNotAcceptable
|
return ErrShortName, http.StatusNotAcceptable
|
||||||
}
|
}*/
|
||||||
return nil, http.StatusOK
|
return nil, http.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,8 +111,6 @@ func validateHash(r *TorrentRequest) (error, int) {
|
||||||
return nil, http.StatusOK
|
return nil, http.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
//rewrite validators!!!
|
|
||||||
|
|
||||||
func (r *TorrentRequest) ValidateUpload() (err error, code int) {
|
func (r *TorrentRequest) ValidateUpload() (err error, code int) {
|
||||||
validators := []func(r *TorrentRequest) (error, int){
|
validators := []func(r *TorrentRequest) (error, int){
|
||||||
validateName,
|
validateName,
|
||||||
|
@ -128,6 +132,45 @@ func (r *TorrentRequest) ValidateUpload() (err error, code int) {
|
||||||
return err, code
|
return err, code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *TorrentRequest) ValidateMultipartUpload(req *http.Request) (int64, error, int) {
|
||||||
|
tfile, _, err := req.FormFile("torrent")
|
||||||
|
if err == nil {
|
||||||
|
var torrent metainfo.TorrentFile
|
||||||
|
|
||||||
|
// decode torrent
|
||||||
|
if _, err = tfile.Seek(0, io.SeekStart); err != nil {
|
||||||
|
return 0, err, http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
if err = bencode.NewDecoder(tfile).Decode(&torrent); err != nil {
|
||||||
|
return 0, err, http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
// check a few things
|
||||||
|
if torrent.IsPrivate() {
|
||||||
|
return 0, errors.New("private torrents not allowed"), http.StatusBadRequest
|
||||||
|
}
|
||||||
|
trackers := torrent.GetAllAnnounceURLS()
|
||||||
|
if !uploadService.CheckTrackers(trackers) {
|
||||||
|
return 0, errors.New("tracker(s) not allowed"), http.StatusBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Name == "" {
|
||||||
|
r.Name = torrent.TorrentName()
|
||||||
|
}
|
||||||
|
|
||||||
|
binInfohash, err := torrent.Infohash()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err, http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
r.Hash = strings.ToUpper(hex.EncodeToString(binInfohash[:]))
|
||||||
|
|
||||||
|
// extract filesize
|
||||||
|
filesize := int64(torrent.TotalSize())
|
||||||
|
err, code := r.ValidateUpload()
|
||||||
|
return filesize, err, code
|
||||||
|
}
|
||||||
|
return 0, err, http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
func (r *TorrentRequest) ValidateUpdate() (err error, code int) {
|
func (r *TorrentRequest) ValidateUpdate() (err error, code int) {
|
||||||
validators := []func(r *TorrentRequest) (error, int){
|
validators := []func(r *TorrentRequest) (error, int){
|
||||||
validateName,
|
validateName,
|
||||||
|
|
34
service/upload/upload.go
Fichier normal
34
service/upload/upload.go
Fichier normal
|
@ -0,0 +1,34 @@
|
||||||
|
package uploadService
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CheckTrackers(trackers []string) bool {
|
||||||
|
// TODO: move to runtime configuration
|
||||||
|
var deadTrackers = []string{ // substring matches!
|
||||||
|
"://open.nyaatorrents.info:6544",
|
||||||
|
"://tracker.openbittorrent.com:80",
|
||||||
|
"://tracker.publicbt.com:80",
|
||||||
|
"://stats.anisource.net:2710",
|
||||||
|
"://exodus.desync.com",
|
||||||
|
"://open.demonii.com:1337",
|
||||||
|
"://tracker.istole.it:80",
|
||||||
|
"://tracker.ccc.de:80",
|
||||||
|
"://bt2.careland.com.cn:6969",
|
||||||
|
"://announce.torrentsmd.com:8080"}
|
||||||
|
|
||||||
|
var numGood int
|
||||||
|
for _, t := range trackers {
|
||||||
|
good := true
|
||||||
|
for _, check := range deadTrackers {
|
||||||
|
if strings.Contains(t, check) {
|
||||||
|
good = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if good {
|
||||||
|
numGood++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return numGood > 0
|
||||||
|
}
|
Référencer dans un nouveau ticket