Example #1
0
func (dhtNode *DhtNode) FindNode(node *KNode) {
	var id Id
	if node.Id != nil {
		id = node.Id.Neighbor()
	} else {
		id = dhtNode.node.Id.Neighbor()
	}
	tid := dhtNode.krpc.GenTID()
	v := make(map[string]interface{})
	v["t"] = fmt.Sprintf("%d", tid)
	v["y"] = "q"
	v["q"] = "find_node"
	args := make(map[string]string)
	args["id"] = string(id)
	args["target"] = string(GenerateID())
	v["a"] = args
	data, err := bencode.EncodeString(v)
	if err != nil {
		dhtNode.log.Fatalln(err)
	}

	raddr := new(net.UDPAddr)
	raddr.IP = node.Ip
	raddr.Port = node.Port

	err = dhtNode.network.Send([]byte(data), raddr)
	if err != nil {
		dhtNode.log.Println(err)
	}
}
Example #2
0
// Wraps the command and arguments in a map[string]interface{}, then
// uses the given Conn to encode them directly to the wire. It sends
// authorization if it is supplied in the given CJDNS.
func (cjdns *CJDNS) Send(command string, args map[string]interface{}) (response map[string]interface{}) {
	// Exit if the command is not given.
	if command == "" {
		return
	}
	// Otherwise, create the map which will be used to encode the
	// message.
	message := make(map[string]interface{})

	if cjdns.cookie != "" && cjdns.password != "" {
		// If there is authentication involved, then use
		// "aq". Otherwise, "q".

		hash := sha256.New()
		hash.Write([]byte(cjdns.password + cjdns.cookie))

		message["aq"] = command
		message["args"] = args
		message["cookie"] = cjdns.cookie
		message["hash"] = hex.EncodeToString(hash.Sum(nil)) // as specified
		message["q"] = CommandAuth

		// Prepare the hash
		m, err := bencode.EncodeString(message)
		if err != nil {
			return
		}

		hash = sha256.New()
		hash.Write([]byte(m))
		message["hash"] = hex.EncodeToString(hash.Sum(nil))
	} else {
		message["q"] = command
	}

	m, err := bencode.EncodeString(message)
	if err == nil {
		io.WriteString(cjdns.Conn, m)
		bencode.NewDecoder(cjdns.Conn).Decode(&response)
	}
	return
}
Example #3
0
func (krpc *KRPC) EncodingNodeResult(tid string, token string, nodes []byte) (string, error) {
	v := make(map[string]interface{})
	v["t"] = tid
	v["y"] = "r"
	args := make(map[string]string)
	args["id"] = krpc.dhtNode.node.Id.String()
	if token != "" {
		args["token"] = token
	}
	args["nodes"] = bytes.NewBuffer(nodes).String()
	v["r"] = args
	s, err := bencode.EncodeString(v)
	return s, err
}
Example #4
0
func init() {
	var err error

	internalError, err = bencode.EncodeString(&errorResponse{
		MinimumInterval: 60,
		Interval:        300,
		Reason:          "Internal error. Please try again later.",
	})
	if err != nil {
		panic("Error setting up internalError: " + err.Error())
	}

	app.Get("/announce", app.CreateHandler(run))
}
Example #5
0
func (r Response) String() string {
	//TODO: make this a whole lot better
	if r.FailureReason != "" {
		var failure struct {
			FailureReason string `bencode:"failure reason"`
		}

		failure.FailureReason = r.FailureReason

		data, err := bencode.EncodeString(failure)
		if err != nil {
			return "failure encoding response"
		}

		return data
	}

	var response struct {
		Interval   int             `bencode:"interval"`
		Complete   int             `bencode:"complete"`
		Incomplete int             `bencode:"incomplete"`
		Peers      []ConnectedPeer `bencode:"peers"`
	}

	response.Interval = int(r.Interval)
	response.Complete = int(r.Complete)
	response.Incomplete = int(r.Incomplete)
	response.Peers = r.Peers

	data, err := bencode.EncodeString(response)
	if err != nil {
		return "failure encoding response"
	}

	return data
}
Example #6
0
func (krpc *KRPC) EncodingFindNode(target Id) (string, error) {
	tid := krpc.GenTID()
	v := make(map[string]interface{})
	v["t"] = fmt.Sprintf("%d", tid)
	v["y"] = "q"
	v["q"] = "find_node"
	args := make(map[string]string)
	args["id"] = string(krpc.dhtNode.node.Id)
	args["target"] = string(target) //查找自己,找到离自己较近的节点
	v["a"] = args
	s, err := bencode.EncodeString(v)
	if err != nil {
		krpc.dhtNode.log.Fatalln(err)
	}
	return s, err

}