Пример #1
0
func (krpc *KRPC) DecodePackage(data string, addr *net.UDPAddr) error {
	val := make(map[string]interface{})
	if err := bencode.DecodeString(data, &val); err != nil {
		return err
	} else {
		var ok bool
		msg := new(KRPCMSG)
		msg.T, ok = val["t"].(string)
		if !ok {
			err = errors.New("Do not have transaction ID ")
			return err
		}
		msg.Y, ok = val["y"].(string)
		if !ok {
			err = errors.New("Do know message type ")
			return err
		}
		msg.addr = addr
		switch msg.Y {
		case "q":
			query := new(Query)
			query.Q = val["q"].(string)
			query.A = val["a"].(map[string]interface{})
			msg.Args = query
			krpc.Query(msg)
		case "r":
			res := new(Response)
			res.R = val["r"].(map[string]interface{})
			msg.Args = res
			krpc.Response(msg)
		}
		return nil
	}
}
Пример #2
0
func (krpc *KRPC) Decode(data string, raddr *net.UDPAddr) error {
	val := make(map[string]interface{})

	if err := bencode.DecodeString(data, &val); err != nil {
		return err
	} else {
		var ok bool
		message := new(KRPCMessage)

		message.T, ok = val["t"].(string) //请求tid
		if !ok {
			return nil
		}

		message.Y, ok = val["y"].(string) //请求类型
		if !ok {
			return nil
		}

		message.Addr = raddr

		switch message.Y {
		case "q":
			query := new(Query)
			query.Y = val["q"].(string)
			query.A = val["a"].(map[string]interface{})
			message.Addion = query
			break
		case "r":
			res := new(Response)
			res.R = val["r"].(map[string]interface{})
			message.Addion = res
			break
		default:
			krpc.Dht.log.Println("invalid message")
			break
		}

		switch message.Y {
		case "q":
			krpc.Query(message)
			break
		case "r":
			krpc.Response(message)
			break
		}

	}
	return nil
}
Пример #3
0
func NewMetaInfoFromContent(content []byte) (m *MetaInfo, err error) {

	var m1 MetaInfo
	err1 := bencode.DecodeString(string(content), &m1)
	if err1 != nil {
		err = errors.New("Couldn't parse torrent file: " + err1.Error())
		return
	}

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

	m1.InfoHash = string(hash.Sum(nil))

	return &m1, nil
}