diff --git a/cmd/root.go b/cmd/root.go index 95dc23912205d8217ba97fb45a2465743f7ed28c..c3f915d186e3a6884bcc2263cf08f78b2051a670 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -52,6 +52,24 @@ var rootCmd = &cobra.Command{ jww.INFO.Printf("User: %s", user.ReceptionID) writeContact(user.GetContact()) + // Get Recipient and/or set it to myself + isPrecanPartner := false + recipientContact := readContact() + recipientID := recipientContact.ID + + // Try to get recipientID from destid + if recipientID == nil { + recipientID, isPrecanPartner = parseRecipient( + viper.GetString("destid")) + } + + // Set it to myself + if recipientID == nil { + jww.INFO.Printf("sending message to self") + recipientID = user.ReceptionID + recipientContact = user.GetContact() + } + // Set up reception handler swboard := client.GetSwitchboard() recvCh := make(chan message.Receive, 10000) @@ -65,12 +83,12 @@ var rootCmd = &cobra.Command{ authMgr.AddGeneralRequestCallback(printChanRequest) // If unsafe channels, add auto-acceptor - num_channels_confirmed := 0 + authConfirmed := false authMgr.AddGeneralConfirmCallback(func( partner contact.Contact) { jww.INFO.Printf("Channel Confirmed: %s", partner.ID) - num_channels_confirmed++ + authConfirmed = recipientID.Cmp(partner.ID) }) if viper.GetBool("unsafe-channel-creation") { authMgr.AddGeneralRequestCallback(func( @@ -82,7 +100,8 @@ var rootCmd = &cobra.Command{ if err != nil { jww.FATAL.Panicf("%+v", err) } - num_channels_confirmed++ + authConfirmed = recipientID.Cmp( + requestor.ID) }) } @@ -113,39 +132,53 @@ var rootCmd = &cobra.Command{ // Send Messages msgBody := viper.GetString("message") - isPrecanPartner := false - recipientContact := readContact() - recipientID := recipientContact.ID - - // Try to get recipientID from destid - if recipientID == nil { - recipientID, isPrecanPartner = parseRecipient( - viper.GetString("destid")) - } - - // Set it to myself - if recipientID == nil { - jww.INFO.Printf("sending message to self") - recipientID = user.ReceptionID - recipientContact = user.GetContact() - } - time.Sleep(10 * time.Second) // Accept auth request for this recipient if viper.GetBool("accept-channel") { acceptChannel(client, recipientID) + // Do not wait for channel confirmations if we + // accepted one + authConfirmed = true + } + + if client.HasAuthenticatedChannel(recipientID) { + jww.INFO.Printf("Authenticated channel already in "+ + "place for %s", recipientID) + authConfirmed = true } // Send unsafe messages or not? unsafe := viper.GetBool("unsafe") - assumeAuth := viper.GetBool("assume-auth-channel") - if !unsafe && !assumeAuth { + + sendAuthReq := viper.GetBool("send-auth-request") + if !unsafe && !authConfirmed && !isPrecanPartner && + sendAuthReq { addAuthenticatedChannel(client, recipientID, - recipientContact, isPrecanPartner) - // Do not wait for channel confirmations if we - // tried to add a channel - num_channels_confirmed++ + recipientContact) + } else if !unsafe && !authConfirmed && isPrecanPartner { + addPrecanAuthenticatedChannel(client, + recipientID, recipientContact) + authConfirmed = true + } + + if !unsafe && !authConfirmed { + jww.INFO.Printf("Waiting for authentication channel "+ + " confirmation with partner %s", recipientID) + scnt := uint(0) + waitSecs := viper.GetUint("auth-timeout") + for !authConfirmed && scnt < waitSecs { + time.Sleep(1 * time.Second) + scnt++ + } + if scnt == waitSecs { + jww.FATAL.Panicf("Could not confirm "+ + "authentication channel for %s, "+ + "waited %d seconds.", recipientID, + waitSecs) + } + jww.INFO.Printf("Authentication channel confirmation"+ + " took %d seconds", waitSecs) } msg := message.Send{ @@ -216,13 +249,6 @@ var rootCmd = &cobra.Command{ } } fmt.Printf("Received %d\n", receiveCnt) - if receiveCnt == 0 && sendCnt == 0 { - scnt := uint(0) - for num_channels_confirmed == 0 && scnt < waitSecs { - time.Sleep(1 * time.Second) - scnt++ - } - } err = client.StopNetworkFollower(5 * time.Second) if err != nil { jww.WARN.Printf( @@ -390,14 +416,27 @@ func printChanRequest(requestor contact.Contact, message string) { //fmt.Printf(msg) } -func addAuthenticatedChannel(client *api.Client, recipientID *id.ID, - recipient contact.Contact, isPrecanPartner bool) { - if client.HasAuthenticatedChannel(recipientID) { - jww.INFO.Printf("Authenticated channel already in place for %s", - recipientID) - return +func addPrecanAuthenticatedChannel(client *api.Client, recipientID *id.ID, + recipient contact.Contact) { + jww.WARN.Printf("Precanned user id detected: %s", recipientID) + preUsr, err := client.MakePrecannedAuthenticatedChannel( + getPrecanID(recipientID)) + if err != nil { + jww.FATAL.Panicf("%+v", err) } + // Sanity check, make sure user id's haven't changed + preBytes := preUsr.ID.Bytes() + idBytes := recipientID.Bytes() + for i := 0; i < len(preBytes); i++ { + if idBytes[i] != preBytes[i] { + jww.FATAL.Panicf("no id match: %v %v", + preBytes, idBytes) + } + } +} +func addAuthenticatedChannel(client *api.Client, recipientID *id.ID, + recipient contact.Contact) { var allowed bool if viper.GetBool("unsafe-channel-creation") { msg := "unsafe channel creation enabled\n" @@ -418,24 +457,7 @@ func addAuthenticatedChannel(client *api.Client, recipientID *id.ID, recipientContact := recipient - if isPrecanPartner { - jww.WARN.Printf("Precanned user id detected: %s", - recipientID) - preUsr, err := client.MakePrecannedAuthenticatedChannel( - getPrecanID(recipientID)) - if err != nil { - jww.FATAL.Panicf("%+v", err) - } - // Sanity check, make sure user id's haven't changed - preBytes := preUsr.ID.Bytes() - idBytes := recipientID.Bytes() - for i := 0; i < len(preBytes); i++ { - if idBytes[i] != preBytes[i] { - jww.FATAL.Panicf("no id match: %v %v", - preBytes, idBytes) - } - } - } else if recipientContact.ID != nil && recipientContact.DhPubKey != nil { + if recipientContact.ID != nil && recipientContact.DhPubKey != nil { me := client.GetUser().GetContact() jww.INFO.Printf("Requesting auth channel from: %s", recipientID) @@ -707,16 +729,22 @@ func init() { viper.BindPFlag("unsafe-channel-creation", rootCmd.Flags().Lookup("unsafe-channel-creation")) - rootCmd.Flags().BoolP("assume-auth-channel", "", false, - "Do not check for an authentication channel for this user") - viper.BindPFlag("assume-auth-channel", - rootCmd.Flags().Lookup("assume-auth-channel")) - rootCmd.Flags().BoolP("accept-channel", "", false, "Accept the channel request for the corresponding recipient ID") viper.BindPFlag("accept-channel", rootCmd.Flags().Lookup("accept-channel")) + rootCmd.Flags().BoolP("send-auth-request", "", false, + "Send an auth request to the specified destination and wait"+ + "for confirmation") + viper.BindPFlag("send-auth-request", + rootCmd.Flags().Lookup("send-auth-request")) + rootCmd.Flags().UintP("auth-timeout", "", 120, + "The number of seconds to wait for an authentication channel"+ + "to confirm") + viper.BindPFlag("auth-timeout", + rootCmd.Flags().Lookup("auth-timeout")) + rootCmd.Flags().BoolP("forceHistoricalRounds", "", false, "Force all rounds to be sent to historical round retrieval") viper.BindPFlag("forceHistoricalRounds",