Add --send-to flag to skip discovery
This commit is contained in:
parent
73b717f2e3
commit
191d243855
60
main.go
60
main.go
@ -50,6 +50,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Create --send-to flag to send to a specific IP
|
||||||
|
sendTo := flag.String("send-to", "", "Use IP address of receiver instead of mDNS")
|
||||||
// Create -t flag for type
|
// Create -t flag for type
|
||||||
actionType := flag.String("t", "","Type of data being sent")
|
actionType := flag.String("t", "","Type of data being sent")
|
||||||
// Create -d flag for data
|
// Create -d flag for data
|
||||||
@ -65,6 +67,9 @@ func main() {
|
|||||||
_ = os.Mkdir(opensendDir, 0755)
|
_ = os.Mkdir(opensendDir, 0755)
|
||||||
// If -s given
|
// If -s given
|
||||||
if *sendFlag {
|
if *sendFlag {
|
||||||
|
if *actionType == "" || *actionData == "" {
|
||||||
|
log.Fatal().Msg("Valid action type and data is required to send")
|
||||||
|
}
|
||||||
// Create 32 byte buffer
|
// Create 32 byte buffer
|
||||||
sharedKeyBytes := make([]byte, 32)
|
sharedKeyBytes := make([]byte, 32)
|
||||||
// Read random bytes into buffer
|
// Read random bytes into buffer
|
||||||
@ -74,27 +79,40 @@ func main() {
|
|||||||
sharedKey := hex.EncodeToString(sharedKeyBytes)
|
sharedKey := hex.EncodeToString(sharedKeyBytes)
|
||||||
// Notify user a key has been created
|
// Notify user a key has been created
|
||||||
log.Info().Msg("Generated random shared key")
|
log.Info().Msg("Generated random shared key")
|
||||||
// Notify user device discovery is beginning
|
// Create variable to store chosen IP
|
||||||
log.Info().Msg("Discovering opensend receivers")
|
var choiceIP string
|
||||||
// Discover all _opensend._tcp.local. mDNS services
|
// If IP is provided via --send-to
|
||||||
discoveredReceivers, discoveredIPs := DiscoverReceivers()
|
if *sendTo != "" {
|
||||||
// Create reader for STDIN
|
// Notify user that provided IP is being used
|
||||||
reader := bufio.NewReader(os.Stdin)
|
log.Info().Msg("IP provided. Skipping discovery.")
|
||||||
// Print hostnames of each receiver
|
// Set chosen IP to provided
|
||||||
for index, receiver := range discoveredReceivers {
|
choiceIP = *sendTo
|
||||||
// Print hostname and index+1
|
// Otherwise
|
||||||
fmt.Println("[" + strconv.Itoa(index + 1) + "]", receiver)
|
} else {
|
||||||
|
// Notify user device discovery is beginning
|
||||||
|
log.Info().Msg("Discovering opensend receivers")
|
||||||
|
// Discover all _opensend._tcp.local. mDNS services
|
||||||
|
discoveredReceivers, discoveredIPs := DiscoverReceivers()
|
||||||
|
// Create reader for STDIN
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
// Print hostnames of each receiver
|
||||||
|
for index, receiver := range discoveredReceivers {
|
||||||
|
// Print hostname and index+1
|
||||||
|
fmt.Println("["+strconv.Itoa(index+1)+"]", receiver)
|
||||||
|
}
|
||||||
|
// Prompt user for choice
|
||||||
|
fmt.Print("Choose a receiver: ")
|
||||||
|
choiceStr, _ := reader.ReadString('\n')
|
||||||
|
// Convert input to int after trimming spaces
|
||||||
|
choiceInt, err := strconv.Atoi(strings.TrimSpace(choiceStr))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Error converting choice to int")
|
||||||
|
}
|
||||||
|
// Set choiceIndex to choiceInt-1 to allow for indexing
|
||||||
|
choiceIndex := choiceInt - 1
|
||||||
|
// Get IP of chosen receiver
|
||||||
|
choiceIP = discoveredIPs[choiceIndex]
|
||||||
}
|
}
|
||||||
// Prompt user for choice
|
|
||||||
fmt.Print("Choose a receiver: ")
|
|
||||||
choiceStr, _ := reader.ReadString('\n')
|
|
||||||
// Convert input to int after trimming spaces
|
|
||||||
choiceInt, err := strconv.Atoi(strings.TrimSpace(choiceStr))
|
|
||||||
if err != nil { log.Fatal().Err(err).Msg("Error converting choice to int") }
|
|
||||||
// Set choiceIndex to choiceInt-1 to allow for indexing
|
|
||||||
choiceIndex := choiceInt - 1
|
|
||||||
// Get IP of chosen receiver
|
|
||||||
choiceIP := discoveredIPs[choiceIndex]
|
|
||||||
// Notify user of key exchange
|
// Notify user of key exchange
|
||||||
log.Info().Msg("Performing key exchange")
|
log.Info().Msg("Performing key exchange")
|
||||||
// Exchange RSA keys with receiver
|
// Exchange RSA keys with receiver
|
||||||
@ -155,6 +173,8 @@ func main() {
|
|||||||
log.Info().Msg("Executing JSON action")
|
log.Info().Msg("Executing JSON action")
|
||||||
// Execute JSON action using files within opensend directory
|
// Execute JSON action using files within opensend directory
|
||||||
config.ExecuteAction(opensendDir)
|
config.ExecuteAction(opensendDir)
|
||||||
|
} else {
|
||||||
|
log.Fatal().Msg("You must choose sender or receiver mode using -s or -r")
|
||||||
}
|
}
|
||||||
// Remove opensend directory
|
// Remove opensend directory
|
||||||
err = os.RemoveAll(opensendDir)
|
err = os.RemoveAll(opensendDir)
|
||||||
|
Reference in New Issue
Block a user