[golangci-lint]: Don't use magic numbers.

This commit is contained in:
Martin Dosch 2023-06-06 10:47:41 +02:00
parent b8c7876b77
commit 61027844d2
No known key found for this signature in database
GPG Key ID: 52A57CFCE13D657D
5 changed files with 46 additions and 26 deletions

View File

@ -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"
)

View File

@ -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)

20
main.go
View File

@ -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)
}

10
ox.go
View File

@ -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
}

View File

@ -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]