From 8099077c50a338556c98af05ca106b1caa785a51 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Sat, 19 Jun 2021 00:36:48 -0700 Subject: [PATCH] Use global logger --- config.go | 16 +++++++--------- deviceDiscovery.go | 7 +++---- extra.go | 5 ++--- fileCrypto.go | 5 ----- files.go | 4 ---- keyCrypto.go | 5 ----- keyExchange.go | 7 ++----- logging.go | 29 +++++++---------------------- main.go | 2 -- parameters.go | 5 ----- 10 files changed, 21 insertions(+), 64 deletions(-) diff --git a/config.go b/config.go index 681163c..3ee0d32 100644 --- a/config.go +++ b/config.go @@ -18,25 +18,25 @@ package main import ( "errors" - "github.com/pelletier/go-toml" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "io/ioutil" "os" + + "github.com/pelletier/go-toml" + "github.com/rs/zerolog/log" ) // Struct for unmarshaling of opensend TOML configs type Config struct { Receiver ReceiverConfig - Sender SenderConfig - Targets map[string]Target + Sender SenderConfig + Targets map[string]Target } // Config section for receiver type ReceiverConfig struct { - DestDir string `toml:"destinationDirectory"` + DestDir string `toml:"destinationDirectory"` SkipZeroconf bool - WorkDir string `toml:"workingDirectory"` + WorkDir string `toml:"workingDirectory"` } // Config section for sender @@ -51,7 +51,6 @@ type Target struct { // Attempt to find config path func GetConfigPath() string { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Possible config locations configLocations := []string{"~/.config/opensend.toml", "/etc/opensend.toml"} // For every possible location @@ -73,7 +72,6 @@ func GetConfigPath() string { // Create new config object using values from given path func NewConfig(path string) *Config { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Create new empty config struct newConfig := &Config{} // Set config defaults diff --git a/deviceDiscovery.go b/deviceDiscovery.go index 0053f59..b4f9582 100644 --- a/deviceDiscovery.go +++ b/deviceDiscovery.go @@ -18,17 +18,16 @@ package main import ( "context" - "github.com/grandcat/zeroconf" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "os" "time" + + "github.com/grandcat/zeroconf" + "github.com/rs/zerolog/log" ) // Discover opensend receivers on the network func DiscoverReceivers() ([]string, []string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Create zeroconf resolver resolver, err := zeroconf.NewResolver(nil) if err != nil { diff --git a/extra.go b/extra.go index af45fea..56bb736 100644 --- a/extra.go +++ b/extra.go @@ -17,16 +17,15 @@ package main import ( - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "os" "path/filepath" "strings" + + "github.com/rs/zerolog/log" ) func ExpandPath(s string) string { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Get user's home directory homeDir, err := os.UserHomeDir() if err != nil { diff --git a/fileCrypto.go b/fileCrypto.go index 097d153..1550141 100644 --- a/fileCrypto.go +++ b/fileCrypto.go @@ -29,7 +29,6 @@ import ( "strings" "github.com/klauspost/compress/zstd" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" "golang.org/x/crypto/chacha20poly1305" ) @@ -37,7 +36,6 @@ import ( // Encrypt given file using the shared key func CompressAndEncryptFile(filePath string, newFilePath string, sharedKey string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Read data from file file, err := os.Open(filePath) if err != nil { @@ -100,7 +98,6 @@ func CompressAndEncryptFile(filePath string, newFilePath string, sharedKey strin // Decrypt given file using the shared key func DecryptAndDecompressFile(filePath string, newFilePath string, sharedKey string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Read data from file data, err := ioutil.ReadFile(filePath) if err != nil { @@ -149,7 +146,6 @@ func DecryptAndDecompressFile(filePath string, newFilePath string, sharedKey str // Encrypt files in given directory using shared key func EncryptFiles(dir string, sharedKey string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Walk given directory err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { // If error reading, return err @@ -177,7 +173,6 @@ func EncryptFiles(dir string, sharedKey string) { // Decrypt files in given directory using shared key func DecryptFiles(dir string, sharedKey string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Walk given directory err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { // If error reading, return err diff --git a/files.go b/files.go index 6de03f9..c67a874 100644 --- a/files.go +++ b/files.go @@ -26,14 +26,12 @@ import ( "strconv" "strings" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) // Save encrypted key to file func SaveEncryptedKey(encryptedKey []byte, filePath string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Create file at given file path keyFile, err := os.Create(filePath) if err != nil { @@ -53,7 +51,6 @@ func SaveEncryptedKey(encryptedKey []byte, filePath string) { // Create HTTP server to transmit files func SendFiles(dir string) { // Use ConsoleWriter logger with normal FatalHook - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Create TCP listener on port 9898 listener, err := net.Listen("tcp", ":9898") if err != nil { @@ -138,7 +135,6 @@ func NewSender(senderAddr string) *Sender { // Get files from sender func RecvFiles(sender *Sender) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) indexReader, code, err := sender.Get("/index") if err != nil { log.Fatal().Err(err).Msg("Error getting index") diff --git a/keyCrypto.go b/keyCrypto.go index 951dc59..7b43710 100644 --- a/keyCrypto.go +++ b/keyCrypto.go @@ -22,16 +22,13 @@ import ( "crypto/sha256" "io/ioutil" "net/http" - "os" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) // Generate RSA keypair func GenerateRSAKeypair() (*rsa.PrivateKey, *rsa.PublicKey) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Generate private/public RSA keypair privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { @@ -46,7 +43,6 @@ func GenerateRSAKeypair() (*rsa.PrivateKey, *rsa.PublicKey) { // Get public key from sender func GetKey(sender *Sender) []byte { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Send key request to connection keyReader, code, err := sender.Get("/key") if err != nil { @@ -75,7 +71,6 @@ func GetKey(sender *Sender) []byte { // Encrypt shared key with received public key func EncryptKey(sharedKey string, recvPubKey *rsa.PublicKey) []byte { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Encrypt shared key using RSA encryptedSharedKey, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, recvPubKey, []byte(sharedKey), nil) if err != nil { diff --git a/keyExchange.go b/keyExchange.go index 3350b02..4452f37 100644 --- a/keyExchange.go +++ b/keyExchange.go @@ -19,16 +19,14 @@ package main import ( "crypto/rsa" "encoding/gob" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "net" - "os" + + "github.com/rs/zerolog/log" ) // Exchange keys with sender func ReceiverKeyExchange(key *rsa.PublicKey) string { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Create TCP listener on port 9797 listener, err := net.Listen("tcp", ":9797") if err != nil { @@ -58,7 +56,6 @@ func ReceiverKeyExchange(key *rsa.PublicKey) string { // Exchange keys with receiver func SenderKeyExchange(receiverIP string) *rsa.PublicKey { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Connect to TCP socket on receiver IP port 9797 connection, err := net.Dial("tcp", receiverIP+":9797") if err != nil { diff --git a/logging.go b/logging.go index 021f1e4..c4e6e86 100644 --- a/logging.go +++ b/logging.go @@ -17,12 +17,16 @@ package main import ( - "fmt" - "github.com/rs/zerolog" - "net" "os" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) +func init() { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) +} + // Fatal hook to run in case of Fatal error type FatalHook struct{} @@ -34,22 +38,3 @@ func (hook FatalHook) Run(_ *zerolog.Event, level zerolog.Level, _ string) { _ = os.RemoveAll(*workDir) } } - -// TCP Fatal hook to run in case of Fatal error with open TCP connection -type TCPFatalHook struct { - conn net.Conn -} - -// Run function on trigger -func (hook TCPFatalHook) Run(_ *zerolog.Event, level zerolog.Level, _ string) { - // If log event is fatal - if level == zerolog.FatalLevel { - // Send error to connection - _, _ = fmt.Fprintln(hook.conn, "ERR;") - // Close connection - _ = hook.conn.Close() - // Attempt removal of opensend directory - _ = os.RemoveAll(*workDir) - } - -} diff --git a/main.go b/main.go index 32715d4..9b988da 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,6 @@ import ( "syscall" "time" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" flag "github.com/spf13/pflag" ) @@ -39,7 +38,6 @@ var destDir *string func main() { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Create --send-to flag to send to a specific IP sendTo := flag.String("send-to", "", "Use IP address of receiver instead of mDNS") diff --git a/parameters.go b/parameters.go index 04591de..a022426 100644 --- a/parameters.go +++ b/parameters.go @@ -27,7 +27,6 @@ import ( "github.com/mholt/archiver/v3" "github.com/pkg/browser" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/vmihailenco/msgpack/v5" ) @@ -66,7 +65,6 @@ func (parameters *Parameters) Validate() { // Create config file func (parameters *Parameters) CreateFile(dir string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Create parameters file at given directory configFile, err := os.Create(dir + "/parameters.msgpack") if err != nil { @@ -91,7 +89,6 @@ func (parameters *Parameters) CreateFile(dir string) { // Collect all required files into given directory func (parameters *Parameters) CollectFiles(dir string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // If action type is file if parameters.ActionType == "file" { // Open file path in parameters.ActionData @@ -128,7 +125,6 @@ func (parameters *Parameters) CollectFiles(dir string) { // Read config file at given file path func (parameters *Parameters) ReadFile(filePath string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // Read file at filePath fileData, err := ioutil.ReadFile(filePath) if err != nil { @@ -144,7 +140,6 @@ func (parameters *Parameters) ReadFile(filePath string) { // Execute action specified in config func (parameters *Parameters) ExecuteAction(srcDir string, destDir string) { // Use ConsoleWriter logger - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) // If action is file switch parameters.ActionType { case "file":