|
|
|
@ -6,7 +6,10 @@ package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"crypto/aes"
|
|
|
|
|
"crypto/cipher"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/gob"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
@ -17,7 +20,9 @@ import (
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid" // BSD-3-Clause
|
|
|
|
|
"github.com/google/uuid" // BSD-3-Clause
|
|
|
|
|
"github.com/xmppo/go-xmpp" // BSD-3-Clause
|
|
|
|
|
"golang.org/x/crypto/scrypt" // BSD-3-Clause
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func validUTF8(s string) string {
|
|
|
|
@ -52,6 +57,98 @@ func readFile(path string) (*bytes.Buffer, error) {
|
|
|
|
|
return buffer, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
return xmpp.Fast{}, fmt.Errorf("getFastData: failed to read fast cache file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
fastFileLoc := fastPath + "fast"
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
fastFileLoc := fastPath + "fast"
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getClientID(jid string) (string, error) {
|
|
|
|
|
var clientID string
|
|
|
|
|
folder := strings.Replace(strings.Replace(jid, "@", "_at_", -1), ".", "_", -1)
|
|
|
|
|