Example #1
0
// 读取种子信息
func ReadTorrent(r io.Reader) (meta MetaInfo, err error) {
	// 读取文件信息
	s, err := ioutil.ReadAll(r)
	if err != nil {
		return
	}

	err = bencode.DecodeBytes(s, &meta)
	if err != nil {
		return
	}

	hash := sha1.New()
	err = bencode.NewEncoder(hash).Encode(meta.Info)
	if err != nil {
		return
	}

	meta.InfoHash = fmt.Sprintf("%02X", string(hash.Sum(nil)))

	return
	/*var m interface{}
	m, err = bencode.Decode(r)
	if err != nil {
		return
	}

	topMap, ok := m.(map[string]interface{})
	if !ok {
		return
	}

	infoMap, ok := topMap["info"]
	if !ok {
		return
	}

	var b bytes.Buffer
	if err = bencode.Marshal(&b, infoMap); err != nil {
		return
	}

	hash := sha1.New()
	hash.Write(b.Bytes())

	err = bencode.Unmarshal(&b, &meta.Info)
	if err != nil {
		return
	}

	meta.InfoHash = fmt.Sprintf("%X", string(hash.Sum(nil)))
	meta.Announce = getString(topMap, "announce")
	meta.Comment = getString(topMap, "comment")
	meta.CreatedBy = getString(topMap, "created by")
	meta.CreationDate = getInt64(topMap, "creation date")
	meta.Encoding = getString(topMap, "encoding")

	return*/
}
Example #2
0
File: krpc.go Project: 4396/dht
func decodeMessage(b []byte, val interface{}) (err error) {
	defer func() {
		if x := recover(); x != nil {
			err = fmt.Errorf("")
		}
	}()
	return bencode.DecodeBytes(b, val)
}
Example #3
0
func TestCreateTorrentWithImage(t *testing.T) {
	var decoded Torrent

	encoded, err := createTorrent("test.jpg", "static/images/404.jpg")
	if err != nil {
		t.Fatal(err)
	}

	bencode.DecodeBytes(encoded, &decoded)

	if decoded.Info.Pieces != "r\x01\x80j\x99\x84\n\xd3dZ;1NX\xec;\x9d$+f" {
		t.Fatal("Torrent pieces did not match expected pieces for image")
	}
}
Example #4
0
// Read torrent information
func (b *BitTorrent) ReadTorrentInformation(r io.Reader) (*MetaInfo, error) {
	// read file data.
	dat, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}

	// decode dat to meta_info.
	m := &MetaInfo{}
	if err = bencode.DecodeBytes(dat, m); err != nil {
		return nil, err
	}

	m.InfoHash = b.Info_hash

	return m, nil
}
Example #5
0
// Decode KRPC Package
func (k *KRPC) DecodePackage(b []byte) (interface{}, error) {
	dat := make(map[string]interface{})

	if err := bencode.DecodeBytes(b, &dat); err != nil {
		return nil, err
	}

	message := &Msg{}
	var ok bool
	if message.T, ok = dat["t"].(string); !ok {
		return nil, errors.New("Response package not have transcation ID.")
	}

	if message.Y, ok = dat["y"].(string); !ok {
		return nil, errors.New("Response package message type unknown.")
	}

	// encode message type.
	switch message.Y {
	// query
	case "q":
		query := &QueryMsg{}
		query.tid = message.T
		query.Q = dat["q"].(string)
		query.A = dat["a"].(map[string]interface{})
		return query, nil
	// response
	case "r":
		response := &ResponseMsg{}
		response.tid = message.T
		response.R = dat["r"].(map[string]interface{})
		return response, nil
	// error
	case "e":
		err := &ErrorMsg{}
		err.tid = message.T
		err.Errors = dat["e"].([]interface{})
		return err, nil
	default:
		return nil, errors.New("Can not parse message type.")
	}
}
Example #6
0
func TestCreateTorrent(t *testing.T) {
	fileName := "server.go"
	var decoded Torrent

	encoded, err := createTorrent(fileName, fileName)
	if err != nil {
		t.Fatal(err)
	}

	bencode.DecodeBytes(encoded, &decoded)

	if decoded.Encoding != "UTF-8" {
		t.Fatalf("Encoding was %s, expected UTF-8", decoded.Encoding)
	}

	if decoded.Info.Name != "server.go" {
		t.Fatalf("Name was %s, expected server.go", decoded.Info.Name)
	}

	if decoded.Info.PieceLength <= 0 {
		t.Fatal("Expected a piece length, got none")
	}

	if len(decoded.Info.Pieces) <= 0 {
		t.Fatal("Expected at least one piece, got none")
	}

	if decoded.Info.Length <= 0 {
		t.Fatal("Length was less than or equal to 0, expected more")
	}

	tracker := fmt.Sprintf("%sselif/%s", Config.siteURL, fileName)
	if decoded.UrlList[0] != tracker {
		t.Fatalf("First entry in URL list was %s, expected %s", decoded.UrlList[0], tracker)
	}
}