// Route - Router that does default behavior func (r *DefaultRouter) Route(node api.Node, message []byte) error { // Stuff Everything will need just about every time... // var channelLen uint16 // beginning uint16 of message is channel name length channelName := "" channelLen = (uint16(message[0]) << 8) | uint16(message[1]) if len(message) < int(channelLen)+2+16+16 { // uint16 + nonce + hash //todo return errors.New("Incorrect channel name length") } idx := 2 + channelLen //skip over the channel name nonce := message[idx : idx+16] if r.seenRecently(nonce) { // LOOP PREVENTION before handling or forwarding return nil } cid, err := node.CID() // we need this for cloning if err != nil { return err } // // When the channel tag is set... if channelLen > 0 { // channel message channelName = string(message[2 : 2+channelLen]) consumed := false if r.CheckChannels { chn, err := node.GetChannel(channelName) if err == nil { // this is a channel key we know pubkey := cid.Clone() pubkey.FromB64(chn.Pubkey) consumed, err = r.check(node, pubkey, channelName, idx, nonce, message) if err != nil { return err } } } if (!consumed && r.ForwardUnknownChannels) || (consumed && r.ForwardConsumedChannels) { if err := r.forward(node, channelName, message); err != nil { return err } } } else { // private message (zero length channel) // content key case (to be removed, deprecated) consumed := false if r.CheckContent { consumed, err = r.check(node, cid, channelName, idx, nonce, message) if err != nil { return err } } if (!consumed && r.ForwardUnknownContent) || (consumed && r.ForwardConsumedContent) { if err := r.forward(node, channelName, message); err != nil { return err } } // profile keys case consumed = false if r.CheckProfiles { profiles, err := node.GetProfiles() if err != nil { return err } for _, profile := range profiles { if !profile.Enabled { continue } pubkey := cid.Clone() pubkey.FromB64(profile.Pubkey) consumed, err = r.check(node, pubkey, channelName, idx, nonce, message) if err != nil { return err } if consumed { break } } } if (!consumed && r.ForwardUnknownProfiles) || (consumed && r.ForwardConsumedProfiles) { if err := r.forward(node, channelName, message); err != nil { return err } } } return nil }