aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2018-02-26 11:27:15 +0000
committerFelix Hanley <felix@userspace.com.au>2018-02-26 11:27:15 +0000
commit19f63bb03bf2a83515fd47e6cf10a4db18a923d7 (patch)
tree2f5b66f49904ed1a5e17bcf566587abb7f894dd3
parent6e449039520843c8df5203d6a16a0c6fadfe312b (diff)
downloaddhtsearch-19f63bb03bf2a83515fd47e6cf10a4db18a923d7.tar.gz
dhtsearch-19f63bb03bf2a83515fd47e6cf10a4db18a923d7.tar.bz2
Moved shared structs to package
-rw-r--r--models/infohash.go (renamed from dht/infohash.go)6
-rw-r--r--models/infohash_test.go (renamed from dht/infohash_test.go)2
-rw-r--r--models/peer.go (renamed from dht/peer.go)3
-rw-r--r--models/storage.go15
-rw-r--r--models/torrent.go23
5 files changed, 29 insertions, 20 deletions
diff --git a/dht/infohash.go b/models/infohash.go
index cb5170e..e8db422 100644
--- a/dht/infohash.go
+++ b/models/infohash.go
@@ -1,4 +1,4 @@
-package dht
+package models
import (
"crypto/sha1"
@@ -9,7 +9,7 @@ import (
"time"
)
-const ihLength = 20
+const InfohashLength = 20
// Infohash is a 160 bit (20 byte) value
type Infohash []byte
@@ -77,7 +77,7 @@ func (ih Infohash) Distance(other Infohash) int {
return 8*i + j
}
-func generateNeighbour(first, second Infohash) Infohash {
+func GenerateNeighbour(first, second Infohash) Infohash {
s := append(second[:10], first[10:]...)
return Infohash(s)
}
diff --git a/dht/infohash_test.go b/models/infohash_test.go
index 6d627fc..e855e35 100644
--- a/dht/infohash_test.go
+++ b/models/infohash_test.go
@@ -1,4 +1,4 @@
-package dht
+package models
import (
"encoding/hex"
diff --git a/dht/peer.go b/models/peer.go
index 42e8438..2feee03 100644
--- a/dht/peer.go
+++ b/models/peer.go
@@ -1,4 +1,4 @@
-package dht
+package models
import (
"fmt"
@@ -8,7 +8,6 @@ import (
// Peer on DHT network
type Peer struct {
Addr net.Addr
- ID Infohash
Infohash Infohash
}
diff --git a/models/storage.go b/models/storage.go
new file mode 100644
index 0000000..84ac6aa
--- /dev/null
+++ b/models/storage.go
@@ -0,0 +1,15 @@
+package models
+
+type torrentSearcher interface {
+ torrentsByHash(hashes Infohash, offset, limit int) (*Torrent, error)
+ torrentsByName(query string, offset, limit int) ([]*Torrent, error)
+ torrentsByTags(tags []string, offset, limit int) ([]*Torrent, error)
+}
+
+type peerStore interface {
+ savePeer(*Peer) error
+}
+
+type torrentStore interface {
+ saveTorrent(*Torrent) error
+}
diff --git a/models/torrent.go b/models/torrent.go
index 960a6de..4ff3143 100644
--- a/models/torrent.go
+++ b/models/torrent.go
@@ -10,14 +10,13 @@ import (
"time"
"github.com/felix/dhtsearch/bencode"
- "github.com/felix/dhtsearch/dht"
"github.com/felix/dhtsearch/krpc"
)
// Data for persistent storage
type Torrent struct {
ID int `json:"-"`
- InfoHash string `json:"infohash"`
+ Infohash string `json:"infohash"`
Name string `json:"name"`
Files []File `json:"files" db:"-"`
Size int `json:"size"`
@@ -32,20 +31,13 @@ type File struct {
TorrentID int `json:"torrent_id" db:"torrent_id"`
}
-type torrentStore interface {
- saveTorrent(*Torrent) error
- torrentsByHash(hashes dht.Infohash, offset, limit int) (*Torrent, error)
- torrentsByName(query string, offset, limit int) ([]*Torrent, error)
- torrentsByTags(tags []string, offset, limit int) ([]*Torrent, error)
-}
-
-func validMetadata(ih dht.Infohash, md []byte) bool {
+func InfohashMatchesMetadata(ih Infohash, md []byte) bool {
info := sha1.Sum(md)
return bytes.Equal([]byte(ih), info[:])
}
-func TorrentFromMetadata(ih dht.Infohash, md []byte) (*Torrent, error) {
- if !validMetadata(ih, md) {
+func TorrentFromMetadata(ih Infohash, md []byte) (*Torrent, error) {
+ if !InfohashMatchesMetadata(ih, md) {
return nil, fmt.Errorf("infohash does not match metadata")
}
info, _, err := bencode.DecodeDict(md, 0)
@@ -60,7 +52,7 @@ func TorrentFromMetadata(ih dht.Infohash, md []byte) (*Torrent, error) {
}
bt := Torrent{
- InfoHash: hex.EncodeToString([]byte(ih)),
+ Infohash: hex.EncodeToString([]byte(ih)),
Name: name,
}
@@ -79,7 +71,10 @@ func TorrentFromMetadata(ih dht.Infohash, md []byte) (*Torrent, error) {
path[j] = p.(string)
}
- fSize := file["length"].(int)
+ fSize, err := krpc.GetInt(file, "length")
+ if err != nil {
+ return nil, err
+ }
bt.Files[i] = File{
// Assume Unix path sep?
Path: strings.Join(path[:], string(os.PathSeparator)),