Clarify logs

This commit is contained in:
Elara 2020-12-03 08:58:10 -08:00
parent 561bf06826
commit c8dcda11c6
2 changed files with 28 additions and 8 deletions

View File

@ -101,7 +101,7 @@ func SendFiles(dir string) {
http.HandleFunc("/stop", func(res http.ResponseWriter, req *http.Request) {
// Inform user a client has requested server shutdown
log.Info().Msg("GET Stop")
log.Info().Msg("Shutdown signal received")
log.Info().Msg("Stop signal received")
// Shutdown server and send to empty context
err := srv.Shutdown(context.TODO())
if err != nil { log.Fatal().Err(err).Msg("Error stopping server") }

34
main.go
View File

@ -21,13 +21,6 @@ func main() {
// Use ConsoleWriter logger
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
// Create 32 byte buffer
sharedKeyBytes := make([]byte, 32)
// Read random bytes into buffer
_, err := io.ReadFull(rand.Reader, sharedKeyBytes)
if err != nil { log.Fatal().Err(err).Msg("Error generating random bytes") }
// Encode random bytes to hexadecimal
sharedKey := hex.EncodeToString(sharedKeyBytes)
// Get user's home directory
homeDir, err := os.UserHomeDir()
if err != nil { log.Fatal().Err(err).Msg("Error getting home directory") }
@ -67,6 +60,17 @@ func main() {
_ = os.Mkdir(opensendDir, 0755)
// If -s given
if *sendFlag {
// Create 32 byte buffer
sharedKeyBytes := make([]byte, 32)
// Read random bytes into buffer
_, err := io.ReadFull(rand.Reader, sharedKeyBytes)
if err != nil { log.Fatal().Err(err).Msg("Error generating random bytes") }
// Encode random bytes to hexadecimal
sharedKey := hex.EncodeToString(sharedKeyBytes)
// Notify user a key has been created
log.Info().Msg("Generated random shared key")
// 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
@ -86,8 +90,12 @@ func main() {
choiceIndex := choiceInt - 1
// Get IP of chosen receiver
choiceIP := discoveredIPs[choiceIndex]
// Notify user of key exchange
log.Info().Msg("Performing key exchange")
// Exchange RSA keys with receiver
rawKey := SenderKeyExchange(choiceIP)
// Inform user receiver key has been received
log.Info().Msg("Receiver key received")
// Encrypt shared key using RSA public key
key := EncryptKey(sharedKey, rawKey)
// Save encrypted key in opensend directory as savedKey.aesKey
@ -98,8 +106,12 @@ func main() {
config.CollectFiles(opensendDir)
// Create config file in opensend directory
config.CreateFile(opensendDir)
// Notify user file encryption is beginning
log.Info().Msg("Encrypting files")
// Encrypt all files in opensend directory using shared key
EncryptFiles(opensendDir, sharedKey)
// Notify user server has started
log.Info().Msg("Server started on port 9898")
// Send all files in opensend directory using an HTTP server on port 9898
SendFiles(opensendDir)
// If -r given
@ -108,12 +120,16 @@ func main() {
zeroconfShutdown := RegisterService()
// Shutdown zeroconf server at the end of main()
defer zeroconfShutdown()
// Notify user opensend is waiting for key exchange
log.Info().Msg("Waiting for sender key exchange")
// Generate keypair
privateKey, publicKey := GenerateRSAKeypair()
// Exchange keys with sender
senderIP := ReceiverKeyExchange(publicKey)
// Sleep 300ms to allow sender time to start HTTP server
time.Sleep(300*time.Millisecond)
// Notify user files are being received
log.Info().Msg("Receiving files from server (This may take a while)")
// Get files from sender and place them into the opensend directory
RecvFiles(opensendDir, senderIP)
// Get encrypted shared key from sender
@ -122,12 +138,16 @@ func main() {
SendSrvStopSignal(senderIP)
// Decrypt shared key
sharedKey := DecryptKey(encryptedKey, privateKey)
// Notify user file decryption is beginning
log.Info().Msg("Decrypting files")
// Decrypt all files in opensend directory using shared key
DecryptFiles(opensendDir, sharedKey)
// Instantiate Config
config := &Config{}
// Read config file in opensend directory
config.ReadFile(opensendDir + "/config.json")
// Notify user that action is being executed
log.Info().Msg("Executing JSON action")
// Execute JSON action using files within opensend directory
config.ExecuteAction(opensendDir)
}