Add --send-to flag to skip discovery
This commit is contained in:
parent
73b717f2e3
commit
191d243855
26
main.go
26
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,6 +79,16 @@ 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")
|
||||||
|
// Create variable to store chosen IP
|
||||||
|
var choiceIP string
|
||||||
|
// If IP is provided via --send-to
|
||||||
|
if *sendTo != "" {
|
||||||
|
// Notify user that provided IP is being used
|
||||||
|
log.Info().Msg("IP provided. Skipping discovery.")
|
||||||
|
// Set chosen IP to provided
|
||||||
|
choiceIP = *sendTo
|
||||||
|
// Otherwise
|
||||||
|
} else {
|
||||||
// Notify user device discovery is beginning
|
// Notify user device discovery is beginning
|
||||||
log.Info().Msg("Discovering opensend receivers")
|
log.Info().Msg("Discovering opensend receivers")
|
||||||
// Discover all _opensend._tcp.local. mDNS services
|
// Discover all _opensend._tcp.local. mDNS services
|
||||||
@ -83,18 +98,21 @@ func main() {
|
|||||||
// Print hostnames of each receiver
|
// Print hostnames of each receiver
|
||||||
for index, receiver := range discoveredReceivers {
|
for index, receiver := range discoveredReceivers {
|
||||||
// Print hostname and index+1
|
// Print hostname and index+1
|
||||||
fmt.Println("[" + strconv.Itoa(index + 1) + "]", receiver)
|
fmt.Println("["+strconv.Itoa(index+1)+"]", receiver)
|
||||||
}
|
}
|
||||||
// Prompt user for choice
|
// Prompt user for choice
|
||||||
fmt.Print("Choose a receiver: ")
|
fmt.Print("Choose a receiver: ")
|
||||||
choiceStr, _ := reader.ReadString('\n')
|
choiceStr, _ := reader.ReadString('\n')
|
||||||
// Convert input to int after trimming spaces
|
// Convert input to int after trimming spaces
|
||||||
choiceInt, err := strconv.Atoi(strings.TrimSpace(choiceStr))
|
choiceInt, err := strconv.Atoi(strings.TrimSpace(choiceStr))
|
||||||
if err != nil { log.Fatal().Err(err).Msg("Error converting choice to int") }
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Error converting choice to int")
|
||||||
|
}
|
||||||
// Set choiceIndex to choiceInt-1 to allow for indexing
|
// Set choiceIndex to choiceInt-1 to allow for indexing
|
||||||
choiceIndex := choiceInt - 1
|
choiceIndex := choiceInt - 1
|
||||||
// Get IP of chosen receiver
|
// Get IP of chosen receiver
|
||||||
choiceIP := discoveredIPs[choiceIndex]
|
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