From 61027844d2aa26f70a01bde73e28fb7dc5f4c69f Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Tue, 6 Jun 2023 10:47:41 +0200 Subject: [PATCH] [golangci-lint]: Don't use magic numbers. --- const.go | 30 +++++++++++++++++++++++++----- helpers.go | 6 +++--- main.go | 20 ++++++++++---------- ox.go | 10 +++++----- parseconfig.go | 6 +++--- 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/const.go b/const.go index 8bf1bf4..fba97ff 100644 --- a/const.go +++ b/const.go @@ -5,7 +5,26 @@ package main const ( - version = "0.5.7-dev" + version = "0.5.7-dev" + // defaults + defaultBufferSize = 100 + defaultConfigRowSep = 2 + defaultDirRights = 0o700 + defaultFileRights = 0o600 + defaultFileRightsWin = 0o200 + defaultIDBytes = 12 + defaultLenServerConf = 2 + defaultRpadMultiple = 100 + defaultRSABits = 4096 + defaultShortIDBytes = 4 + defaultSleepTime = 100 + defaultTimeout = 10 + defaultTLSMinVersion = 12 + defaultTLS10 = 10 + defaultTLS11 = 11 + defaultTLS12 = 12 + defaultTLS13 = 13 + // namespace nsDiscoInfo = "http://jabber.org/protocol/disco#info" nsDiscoItems = "http://jabber.org/protocol/disco#items" nsEme = "urn:xmpp:eme:0" @@ -20,8 +39,9 @@ const ( nsXMPPStanzas = "urn:ietf:params:xml:ns:xmpp-stanzas" oxAltBody = "This message is encrypted (XEP-0373: OpenPGP for XMPP)." pubsubPubOptions = "http://jabber.org/protocol/pubsub#publish-options" - strChat = "chat" - strError = "error" - strGroupchat = "groupchat" - strResult = "result" + // strings + strChat = "chat" + strError = "error" + strGroupchat = "groupchat" + strResult = "result" ) diff --git a/helpers.go b/helpers.go index 9c0508a..a91a832 100644 --- a/helpers.go +++ b/helpers.go @@ -50,7 +50,7 @@ func readFile(path string) (*bytes.Buffer, error) { func getRpad(messageLength int) string { rpadRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") - length := 100 - messageLength%100 + length := defaultRpadMultiple - messageLength%defaultRpadMultiple max := big.NewInt(int64(len(rpadRunes))) rpad := make([]rune, length) for i := range rpad { @@ -64,7 +64,7 @@ func getRpad(messageLength int) string { } func getID() string { - id := make([]byte, 12) + id := make([]byte, defaultIDBytes) _, err := rand.Read(id) if err != nil { log.Fatal(err) @@ -73,7 +73,7 @@ func getID() string { } func getShortID() string { - id := make([]byte, 4) + id := make([]byte, defaultShortIDBytes) _, err := rand.Read(id) if err != nil { log.Fatal(err) diff --git a/main.go b/main.go index 8b26ef5..066c439 100644 --- a/main.go +++ b/main.go @@ -104,8 +104,8 @@ func main() { "Skip verification of TLS certificates (not recommended).") flagRaw := getopt.BoolLong("raw", 0, "Send raw XML.") flagListen := getopt.BoolLong("listen", 'l', "Listen for messages and print them to stdout.") - flagTimeout := getopt.IntLong("timeout", 0, 10, "Connection timeout in seconds.") - flagTLSMinVersion := getopt.IntLong("tls-version", 0, 12, + flagTimeout := getopt.IntLong("timeout", 0, defaultTimeout, "Connection timeout in seconds.") + flagTLSMinVersion := getopt.IntLong("tls-version", 0, defaultTLSMinVersion, "Minimal TLS version. 10 (TLSv1.0), 11 (TLSv1.1), 12 (TLSv1.2) or 13 (TLSv1.3).") flagVersion := getopt.BoolLong("version", 0, "Show version information.") flagMUCPassword := getopt.StringLong("muc-password", 0, "", "Password for password protected MUCs.") @@ -222,7 +222,7 @@ func main() { } // Timeout - timeout := time.Duration(*flagTimeout * 1000000000) + timeout := time.Duration(*flagTimeout) * time.Second // Use ALPN var tlsConfig tls.Config @@ -230,13 +230,13 @@ func main() { tlsConfig.NextProtos = append(tlsConfig.NextProtos, "xmpp-client") tlsConfig.InsecureSkipVerify = *flagSkipVerify switch *flagTLSMinVersion { - case 10: + case defaultTLS10: tlsConfig.MinVersion = tls.VersionTLS10 - case 11: + case defaultTLS11: tlsConfig.MinVersion = tls.VersionTLS11 - case 12: + case defaultTLS12: tlsConfig.MinVersion = tls.VersionTLS12 - case 13: + case defaultTLS13: tlsConfig.MinVersion = tls.VersionTLS13 default: fmt.Println("Unknown TLS version.") @@ -307,8 +307,8 @@ func main() { log.Fatal(err) } - iqc := make(chan xmpp.IQ, 100) - msgc := make(chan xmpp.Chat, 100) + iqc := make(chan xmpp.IQ, defaultBufferSize) + msgc := make(chan xmpp.Chat, defaultBufferSize) go rcvStanzas(client, iqc, msgc) for _, r := range getopt.Args() { @@ -594,5 +594,5 @@ func main() { // Wait for a short time as some messages are not delievered by the server // if the connection is closed immediately after sending a message. - time.Sleep(100 * time.Millisecond) + time.Sleep(defaultSleepTime * time.Millisecond) } diff --git a/ox.go b/ox.go index 6d8a584..36287ae 100644 --- a/ox.go +++ b/ox.go @@ -347,7 +347,7 @@ func oxGetPrivKeyLoc(jid string) (string, error) { } dataDir += "/go-sendxmpp/oxprivkeys/" if _, err = os.Stat(dataDir); os.IsNotExist(err) { - err = os.MkdirAll(dataDir, 0o700) + err = os.MkdirAll(dataDir, defaultDirRights) if err != nil { return strError, err } @@ -380,7 +380,7 @@ func oxGetPubKeyLoc(fingerprint string) (string, error) { } dataDir += "/go-sendxmpp/oxpubkeys/" if _, err = os.Stat(dataDir); os.IsNotExist(err) { - err = os.MkdirAll(dataDir, 0o700) + err = os.MkdirAll(dataDir, defaultDirRights) if err != nil { return strError, err } @@ -433,9 +433,9 @@ func oxStoreKey(location string, key string) error { return err } if runtime.GOOS != "windows" { - _ = file.Chmod(os.FileMode(0o600)) + _ = file.Chmod(os.FileMode(defaultFileRights)) } else { - _ = file.Chmod(os.FileMode(0o200)) + _ = file.Chmod(os.FileMode(defaultFileRightsWin)) } _, err = file.Write([]byte(key)) if err != nil { @@ -452,7 +452,7 @@ func oxGenPrivKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, passphrase string, keyType string, ) error { xmppURI := "xmpp:" + jid - key, err := crypto.GenerateKey(xmppURI, "", keyType, 4096) + key, err := crypto.GenerateKey(xmppURI, "", keyType, defaultRSABits) if err != nil { return err } diff --git a/parseconfig.go b/parseconfig.go index fa92bd1..589b95e 100644 --- a/parseconfig.go +++ b/parseconfig.go @@ -96,7 +96,7 @@ func parseConfig(configPath string) (configuration, error) { continue } - row := strings.SplitN(scanner.Text(), " ", 2) + row := strings.SplitN(scanner.Text(), " ", defaultConfigRowSep) switch row[0] { case "username:": @@ -126,7 +126,7 @@ func parseConfig(configPath string) (configuration, error) { case "alias:": output.alias = row[1] default: - if len(row) >= 2 { + if len(row) >= defaultConfigRowSep { if strings.Contains(scanner.Text(), ";") { output.username = strings.Split(row[0], ";")[0] output.jserver = strings.Split(row[0], ";")[1] @@ -134,7 +134,7 @@ func parseConfig(configPath string) (configuration, error) { } else { output.username = strings.Split(row[0], ":")[0] jserver := strings.Split(row[0], "@") - if len(jserver) < 2 { + if len(jserver) < defaultLenServerConf { log.Fatal("Couldn't parse config: ", row[0]) } output.jserver = jserver[0]