From: Matt Joiner Date: Tue, 23 Feb 2016 11:28:23 +0000 (+1100) Subject: metainfo: Support nodes as []string or [](string, int64) X-Git-Tag: v1.0.0~851 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=752e884155503766dd1b253adee816eb2551709a;p=btrtrc.git metainfo: Support nodes as []string or [](string, int64) Fixes #65. --- diff --git a/metainfo/builder.go b/metainfo/builder.go index b600f188..5fcaaa82 100644 --- a/metainfo/builder.go +++ b/metainfo/builder.go @@ -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 diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index dd8fc6d8..908f19b5 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -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 index 00000000..76b53722 --- /dev/null +++ b/metainfo/nodes.go @@ -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 +} diff --git a/metainfo/nodes_test.go b/metainfo/nodes_test.go index c9438b7e..9a76e137 100644 --- a/metainfo/nodes_test.go +++ b/metainfo/nodes_test.go @@ -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 index 00000000..8fc13ecd 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 index 00000000..47b4619e Binary files /dev/null and b/metainfo/testdata/issue_65b.torrent differ