2023-05-11 18:05:31 +00:00
|
|
|
|
// Copyright Martin Dosch.
|
2022-04-09 03:00:38 +00:00
|
|
|
|
// Use of this source code is governed by the BSD-2-clause
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2022-04-18 08:16:00 +00:00
|
|
|
|
"bytes"
|
2024-04-10 13:19:50 +00:00
|
|
|
|
"crypto/aes"
|
|
|
|
|
"crypto/cipher"
|
2023-06-04 14:14:32 +00:00
|
|
|
|
"crypto/rand"
|
2024-04-10 13:19:50 +00:00
|
|
|
|
"encoding/gob"
|
2024-04-09 08:59:00 +00:00
|
|
|
|
"errors"
|
2022-04-09 03:00:38 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2023-06-04 14:14:32 +00:00
|
|
|
|
"math/big"
|
2023-02-18 14:42:04 +00:00
|
|
|
|
"net/url"
|
2022-04-18 08:16:00 +00:00
|
|
|
|
"os"
|
2022-11-05 11:40:40 +00:00
|
|
|
|
"regexp"
|
2024-04-09 12:52:15 +00:00
|
|
|
|
"runtime"
|
2022-11-05 11:40:40 +00:00
|
|
|
|
"strings"
|
2024-04-09 08:59:00 +00:00
|
|
|
|
|
2024-04-10 13:19:50 +00:00
|
|
|
|
"github.com/google/uuid" // BSD-3-Clause
|
|
|
|
|
"github.com/xmppo/go-xmpp" // BSD-3-Clause
|
|
|
|
|
"golang.org/x/crypto/scrypt" // BSD-3-Clause
|
2022-04-09 03:00:38 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-11-05 11:40:40 +00:00
|
|
|
|
func validUTF8(s string) string {
|
|
|
|
|
// Remove invalid code points.
|
2023-08-10 11:42:31 +00:00
|
|
|
|
s = strings.ToValidUTF8(s, "<22>")
|
2022-11-05 11:40:40 +00:00
|
|
|
|
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
|
2023-08-10 11:42:31 +00:00
|
|
|
|
s = reg.ReplaceAllString(s, "<22>")
|
2022-11-05 11:40:40 +00:00
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 15:24:46 +00:00
|
|
|
|
func validURI(s string) (*url.URL, error) {
|
2023-02-18 14:42:04 +00:00
|
|
|
|
// Check if URI is valid
|
2023-02-18 15:24:46 +00:00
|
|
|
|
uri, err := url.ParseRequestURI(s)
|
2023-06-06 20:09:59 +00:00
|
|
|
|
return uri, fmt.Errorf("validURI: %w", err)
|
2023-02-18 14:42:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 08:16:00 +00:00
|
|
|
|
func readFile(path string) (*bytes.Buffer, error) {
|
|
|
|
|
file, err := os.Open(path)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-06 20:09:59 +00:00
|
|
|
|
return nil, fmt.Errorf("readFile: %w", err)
|
2022-04-18 08:16:00 +00:00
|
|
|
|
}
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
_, err = buffer.ReadFrom(file)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-06 20:09:59 +00:00
|
|
|
|
return nil, fmt.Errorf("readFile: %w", err)
|
2022-04-18 08:16:00 +00:00
|
|
|
|
}
|
2023-06-04 13:15:31 +00:00
|
|
|
|
err = file.Close()
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-06 20:09:59 +00:00
|
|
|
|
fmt.Println("error while closing file:", err)
|
2023-06-04 13:15:31 +00:00
|
|
|
|
}
|
2022-04-18 08:16:00 +00:00
|
|
|
|
return buffer, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 13:19:50 +00:00
|
|
|
|
func getFastData(jid string, password string) (xmpp.Fast, error) {
|
|
|
|
|
folder := strings.Replace(strings.Replace(jid, "@", "_at_", -1), ".", "_", -1)
|
|
|
|
|
var fast xmpp.Fast
|
|
|
|
|
fastPath, err := getDataPath(folder)
|
|
|
|
|
if err != nil {
|
2024-04-10 13:50:58 +00:00
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to read fast cache folder: %w", err)
|
2024-04-10 13:19:50 +00:00
|
|
|
|
}
|
2024-04-10 13:50:58 +00:00
|
|
|
|
fastFileLoc := fastPath + "fast.bin"
|
2024-04-10 13:19:50 +00:00
|
|
|
|
buf, err := readFile(fastFileLoc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to read fast cache file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
decBuf := bytes.NewBuffer(buf.Bytes())
|
|
|
|
|
decoder := gob.NewDecoder(decBuf)
|
|
|
|
|
err = decoder.Decode(&fast)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to read fast cache file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
|
key, err := scrypt.Key([]byte(password), salt, 32768, 8, 1, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to create aes key: %w", err)
|
|
|
|
|
}
|
|
|
|
|
c, err := aes.NewCipher([]byte(key))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to read fast cache file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to read fast cache file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
nonceSize := gcm.NonceSize()
|
|
|
|
|
cryptBuf := []byte(fast.Token)
|
|
|
|
|
nonce, cryptBuf := cryptBuf[:nonceSize], cryptBuf[nonceSize:]
|
|
|
|
|
tokenBuf, err := gcm.Open(nil, []byte(nonce), cryptBuf, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to read fast cache file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
fast.Token = string(tokenBuf)
|
|
|
|
|
return fast, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeFastData(jid string, password string, fast xmpp.Fast) error {
|
|
|
|
|
var encBuf bytes.Buffer
|
|
|
|
|
folder := strings.Replace(strings.Replace(jid, "@", "_at_", -1), ".", "_", -1)
|
|
|
|
|
fastPath, err := getDataPath(folder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to write fast cache file: %w", err)
|
|
|
|
|
}
|
2024-04-10 13:50:58 +00:00
|
|
|
|
fastFileLoc := fastPath + "fast.bin"
|
2024-04-10 13:19:50 +00:00
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
|
key, err := scrypt.Key([]byte(password), salt, 32768, 8, 1, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to create aes cipher: %w", err)
|
|
|
|
|
}
|
|
|
|
|
c, err := aes.NewCipher(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to create aes cipher: %w", err)
|
|
|
|
|
}
|
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to create aes cipher: %w", err)
|
|
|
|
|
}
|
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
|
|
|
_, err = rand.Read(nonce)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to create aes cipher: %w", err)
|
|
|
|
|
}
|
|
|
|
|
buf := gcm.Seal(nonce, nonce, []byte(fast.Token), nil)
|
|
|
|
|
fast.Token = string(buf)
|
|
|
|
|
encode := gob.NewEncoder(&encBuf)
|
|
|
|
|
err = encode.Encode(fast)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to create fast token file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
file, err := os.Create(fastFileLoc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to create fast token file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
|
_ = file.Chmod(os.FileMode(defaultFileRights))
|
|
|
|
|
} else {
|
|
|
|
|
_ = file.Chmod(os.FileMode(defaultFileRightsWin))
|
|
|
|
|
}
|
|
|
|
|
_, err = file.Write(encBuf.Bytes())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("writeFastData: failed to write fast token file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 12:52:15 +00:00
|
|
|
|
func getClientID(jid string) (string, error) {
|
|
|
|
|
var clientID string
|
|
|
|
|
folder := strings.Replace(strings.Replace(jid, "@", "_at_", -1), ".", "_", -1)
|
|
|
|
|
clientIDLoc, err := getClientIDLoc(folder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return strError, err
|
|
|
|
|
}
|
|
|
|
|
buf, err := readFile(clientIDLoc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
clientID = uuid.NewString()
|
|
|
|
|
file, err := os.Create(clientIDLoc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return strEmpty, fmt.Errorf("getClientID: failed to create clientid file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
|
_ = file.Chmod(os.FileMode(defaultFileRights))
|
|
|
|
|
} else {
|
|
|
|
|
_ = file.Chmod(os.FileMode(defaultFileRightsWin))
|
|
|
|
|
}
|
|
|
|
|
_, err = file.Write([]byte(clientID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return strEmpty, fmt.Errorf("getClientID: failed to write client id file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
clientID = buf.String()
|
|
|
|
|
}
|
|
|
|
|
return clientID, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 08:59:00 +00:00
|
|
|
|
func getDataPath(folder string) (string, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var homeDir, dataDir string
|
|
|
|
|
switch {
|
|
|
|
|
case os.Getenv("$XDG_DATA_HOME") != "":
|
|
|
|
|
dataDir = os.Getenv("$XDG_DATA_HOME")
|
|
|
|
|
case os.Getenv("$XDG_HOME") != "":
|
|
|
|
|
homeDir = os.Getenv("$XDG_HOME")
|
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
|
case os.Getenv("$HOME") != "":
|
|
|
|
|
homeDir = os.Getenv("$HOME")
|
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
|
default:
|
|
|
|
|
homeDir, err = os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return strError, fmt.Errorf("getDataPath: failed to determine user dir: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if homeDir == "" {
|
|
|
|
|
return strError, errors.New("getDataPath: received empty string for home directory")
|
|
|
|
|
}
|
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
|
}
|
|
|
|
|
if folder != "" && !strings.HasSuffix(folder, "/") {
|
|
|
|
|
folder = fmt.Sprintf("%s/", folder)
|
|
|
|
|
}
|
|
|
|
|
dataDir = fmt.Sprintf("%s/go-sendxmpp/%s", dataDir, folder)
|
|
|
|
|
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
|
|
|
|
|
err = os.MkdirAll(dataDir, defaultDirRights)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return strError, fmt.Errorf("getDataPath: could not create folder: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dataDir, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 12:52:15 +00:00
|
|
|
|
func getClientIDLoc(folder string) (string, error) {
|
|
|
|
|
dataDir, err := getDataPath(folder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return strError, fmt.Errorf("getClientIDLoc: %w", err)
|
|
|
|
|
}
|
|
|
|
|
dataFile := dataDir + "clientid"
|
|
|
|
|
return dataFile, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 09:36:41 +00:00
|
|
|
|
func getRpad(messageLength int) string {
|
2023-05-29 09:40:37 +00:00
|
|
|
|
rpadRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
2023-06-06 08:47:41 +00:00
|
|
|
|
length := defaultRpadMultiple - messageLength%defaultRpadMultiple
|
2023-06-04 14:14:32 +00:00
|
|
|
|
max := big.NewInt(int64(len(rpadRunes)))
|
2023-05-29 09:40:37 +00:00
|
|
|
|
rpad := make([]rune, length)
|
|
|
|
|
for i := range rpad {
|
2023-06-04 14:14:32 +00:00
|
|
|
|
randInt, err := rand.Int(rand.Reader, max)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-04 14:14:32 +00:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
rpad[i] = rpadRunes[randInt.Int64()]
|
2022-04-17 15:16:29 +00:00
|
|
|
|
}
|
2023-05-29 09:40:37 +00:00
|
|
|
|
return string(rpad)
|
2022-04-17 15:16:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-09 03:00:38 +00:00
|
|
|
|
func getID() string {
|
2024-04-09 08:59:00 +00:00
|
|
|
|
return uuid.NewString()
|
2022-04-09 03:00:38 +00:00
|
|
|
|
}
|
2022-04-23 21:40:15 +00:00
|
|
|
|
|
|
|
|
|
func getShortID() string {
|
2023-06-06 08:47:41 +00:00
|
|
|
|
id := make([]byte, defaultShortIDBytes)
|
2022-05-02 14:13:00 +00:00
|
|
|
|
_, err := rand.Read(id)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2022-04-23 21:40:15 +00:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2022-05-02 14:13:00 +00:00
|
|
|
|
return fmt.Sprintf("%x", id[0:4])
|
2022-04-23 21:40:15 +00:00
|
|
|
|
}
|