Fixes #65.
"sort"
"time"
+ "github.com/anacrolix/missinggo"
+
"github.com/anacrolix/torrent/bencode"
)
}
}
- 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
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"`
--- /dev/null
+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
+}
"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",
+ })
}