]> Sergey Matveev's repositories - btrtrc.git/commitdiff
metainfo: Support nodes as []string or [](string, int64)
authorMatt Joiner <anacrolix@gmail.com>
Tue, 23 Feb 2016 11:28:23 +0000 (22:28 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 23 Feb 2016 11:28:23 +0000 (22:28 +1100)
Fixes #65.

metainfo/builder.go
metainfo/metainfo.go
metainfo/nodes.go [new file with mode: 0644]
metainfo/nodes_test.go
metainfo/testdata/issue_65a.torrent [new file with mode: 0644]
metainfo/testdata/issue_65b.torrent [new file with mode: 0644]

index b600f18880c85cfcd84c40153fe2ba2dcfc94c8c..5fcaaa82f902046b7526ac64e09a681b1a765e39 100644 (file)
@@ -10,6 +10,8 @@ import (
        "sort"
        "time"
 
+       "github.com/anacrolix/missinggo"
+
        "github.com/anacrolix/torrent/bencode"
 )
 
@@ -381,10 +383,7 @@ func (b *Batch) write_torrent(w io.Writer) error {
                }
        }
 
-       if len(b.node_list) != 0 {
-               td.Nodes = b.node_list
-       }
-
+       missinggo.CastSlice(td.Nodes, b.node_list)
        td.CreationDate = b.creation_date.Unix()
        td.Comment = b.comment
        td.CreatedBy = b.created_by
index dd8fc6d8db965e358cb2c77022bc70c610f307e9..908f19b5bce6bb1c8ed76f59d64c2db2d7f54f68 100644 (file)
@@ -233,7 +233,7 @@ type MetaInfo struct {
        Info         InfoEx      `bencode:"info"`
        Announce     string      `bencode:"announce,omitempty"`
        AnnounceList [][]string  `bencode:"announce-list,omitempty"`
-       Nodes        []string    `bencode:"nodes,omitempty"`
+       Nodes        []Node      `bencode:"nodes,omitempty"`
        CreationDate int64       `bencode:"creation date,omitempty"`
        Comment      string      `bencode:"comment,omitempty"`
        CreatedBy    string      `bencode:"created by,omitempty"`
diff --git a/metainfo/nodes.go b/metainfo/nodes.go
new file mode 100644 (file)
index 0000000..76b5372
--- /dev/null
@@ -0,0 +1,40 @@
+package metainfo
+
+import (
+       "fmt"
+       "net"
+       "strconv"
+
+       "github.com/anacrolix/torrent/bencode"
+)
+
+type Node string
+
+var (
+       _ bencode.Unmarshaler = new(Node)
+)
+
+func (me *Node) UnmarshalBencode(b []byte) (err error) {
+       var iface interface{}
+       err = bencode.Unmarshal(b, &iface)
+       if err != nil {
+               return
+       }
+       switch v := iface.(type) {
+       case string:
+               *me = Node(v)
+       case []interface{}:
+               func() {
+                       defer func() {
+                               r := recover()
+                               if r != nil {
+                                       err = r.(error)
+                               }
+                       }()
+                       *me = Node(net.JoinHostPort(v[0].(string), strconv.FormatInt(v[1].(int64), 10)))
+               }()
+       default:
+               err = fmt.Errorf("unsupported type: %T", iface)
+       }
+       return
+}
index c9438b7e88570e393fe8587fadc39b68dae6b5c1..9a76e1377dc687a2a4c867166eb5860cc500e471 100644 (file)
@@ -7,14 +7,34 @@ import (
        "github.com/stretchr/testify/require"
 )
 
-func TestNodesListStrings(t *testing.T) {
-       mi, err := LoadFromFile("testdata/trackerless.torrent")
+func testFileNodesMatch(t *testing.T, file string, nodes []Node) {
+       mi, err := LoadFromFile(file)
        require.NoError(t, err)
-       assert.EqualValues(t, []string{
+       assert.EqualValues(t, nodes, mi.Nodes)
+}
+
+func TestNodesListStrings(t *testing.T) {
+       testFileNodesMatch(t, "testdata/trackerless.torrent", []Node{
                "udp://tracker.openbittorrent.com:80",
                "udp://tracker.openbittorrent.com:80",
-       }, mi.Nodes)
+       })
 }
 
 func TestNodesListPairsBEP5(t *testing.T) {
+       testFileNodesMatch(t, "testdata/issue_65a.torrent", []Node{
+               "185.34.3.132:5680",
+               "185.34.3.103:12340",
+               "94.209.253.165:47232",
+               "78.46.103.11:34319",
+               "195.154.162.70:55011",
+               "185.34.3.137:3732",
+       })
+       testFileNodesMatch(t, "testdata/issue_65b.torrent", []Node{
+               "95.211.203.130:6881",
+               "84.72.116.169:6889",
+               "204.83.98.77:7000",
+               "101.187.175.163:19665",
+               "37.187.118.32:6881",
+               "83.128.223.71:23865",
+       })
 }
diff --git a/metainfo/testdata/issue_65a.torrent b/metainfo/testdata/issue_65a.torrent
new file mode 100644 (file)
index 0000000..8fc13ec
Binary files /dev/null and b/metainfo/testdata/issue_65a.torrent differ
diff --git a/metainfo/testdata/issue_65b.torrent b/metainfo/testdata/issue_65b.torrent
new file mode 100644 (file)
index 0000000..47b4619
Binary files /dev/null and b/metainfo/testdata/issue_65b.torrent differ