示例#1
0
func createTorrent(fileName string, f io.ReadCloser, r *http.Request) ([]byte, error) {
	chunk := make([]byte, TORRENT_PIECE_LENGTH)

	torrent := Torrent{
		Encoding: "UTF-8",
		Info: TorrentInfo{
			PieceLength: TORRENT_PIECE_LENGTH,
			Name:        fileName,
		},
		UrlList: []string{fmt.Sprintf("%sselif/%s", getSiteURL(r), fileName)},
	}

	for {
		n, err := f.Read(chunk)
		if err == io.EOF {
			break
		} else if err != nil {
			return []byte{}, err
		}

		torrent.Info.Length += n
		torrent.Info.Pieces += string(hashPiece(chunk[:n]))
	}

	data, err := bencode.EncodeBytes(&torrent)
	if err != nil {
		return []byte{}, err
	}

	return data, nil
}
示例#2
0
// Encode KRPC response package
func (k *KRPC) EncodeResponsePackage(tid string, response map[string]string) ([]byte, error) {
	dat := make(map[string]interface{})
	dat["t"] = tid
	dat["y"] = "r"
	dat["r"] = response

	return bencode.EncodeBytes(dat)
}
示例#3
0
// Encode KRPC Query package
func (k *KRPC) EncodeQueryPackage(methodName string, args map[string]interface{}) ([]byte, error) {
	dat := make(map[string]interface{})
	dat["t"] = fmt.Sprintf("%d", k.GenerateTID())
	dat["y"] = "q"
	dat["q"] = methodName
	dat["a"] = args

	return bencode.EncodeBytes(dat)
}
示例#4
0
文件: krpc.go 项目: 4396/dht
func encodeMessage(msg interface{}) ([]byte, error) {
	return bencode.EncodeBytes(msg)
}