2023-09-06 13:12:35 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-06 20:49:07 -06:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
2023-09-06 13:12:35 -06:00
|
|
|
"encoding/json"
|
2023-09-06 20:49:07 -06:00
|
|
|
"encoding/pem"
|
2023-09-06 13:12:35 -06:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2023-09-06 20:49:07 -06:00
|
|
|
"time"
|
|
|
|
_ "embed"
|
|
|
|
"RidgedChat/ui"
|
2023-09-06 13:12:35 -06:00
|
|
|
)
|
|
|
|
|
2023-09-06 20:49:07 -06:00
|
|
|
//go:embed "default_config.json"
|
|
|
|
var defaultConfig []byte
|
|
|
|
|
2023-09-06 13:12:35 -06:00
|
|
|
// Config holds the client configuration
|
|
|
|
type Config struct {
|
2023-09-06 20:49:07 -06:00
|
|
|
ServerAddress string `json:"server_address"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
OfflineMode bool `json:"offline_mode"`
|
|
|
|
HeaderVisible bool `json:"header_visible"`
|
|
|
|
SidebarVisible bool `json:"sidebar_visible"`
|
|
|
|
MainChatVisible bool `json:"mainchat_visible"`
|
|
|
|
UserTextEntryVisible bool `json:"usertextentry_visible"`
|
|
|
|
SendButtonVisible bool `json:"sendbutton_visible"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyManager struct {
|
|
|
|
CurrentPrivateKey *rsa.PrivateKey
|
|
|
|
NextPrivateKey *rsa.PrivateKey
|
|
|
|
CurrentPublicKey []byte
|
|
|
|
NextPublicKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (km *KeyManager) GenerateKeyPair() {
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error generating key pair:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
publicKeyDer, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error marshaling public key:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
publicKeyBlock := pem.Block{
|
|
|
|
Type: "PUBLIC KEY",
|
|
|
|
Bytes: publicKeyDer,
|
|
|
|
}
|
|
|
|
publicKeyPem := pem.EncodeToMemory(&publicKeyBlock)
|
|
|
|
|
|
|
|
km.NextPrivateKey = privateKey
|
|
|
|
km.NextPublicKey = publicKeyPem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (km *KeyManager) RotateKeys() {
|
|
|
|
for {
|
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
km.CurrentPrivateKey = km.NextPrivateKey
|
|
|
|
km.CurrentPublicKey = km.NextPublicKey
|
|
|
|
km.GenerateKeyPair()
|
|
|
|
// notifyClients() // Implement this function
|
|
|
|
}
|
2023-09-06 13:12:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig(filePath string) (*Config, error) {
|
|
|
|
file, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var config Config
|
|
|
|
if err := json.Unmarshal(file, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2023-09-06 20:49:07 -06:00
|
|
|
var keyManager = &KeyManager{}
|
|
|
|
|
2023-09-06 13:12:35 -06:00
|
|
|
func main() {
|
2023-09-06 20:49:07 -06:00
|
|
|
configPath := "./client_config.json"
|
|
|
|
|
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
ioutil.WriteFile(configPath, defaultConfig, 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := ReadConfig(configPath)
|
2023-09-06 13:12:35 -06:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading config:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := NewClient(config.ServerAddress, config.Username)
|
|
|
|
|
2023-09-06 14:00:14 -06:00
|
|
|
if !config.OfflineMode {
|
|
|
|
if err := client.Connect(); err != nil {
|
|
|
|
fmt.Println("Error connecting to server:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-09-06 20:49:07 -06:00
|
|
|
go client.ListenForMessages()
|
|
|
|
|
|
|
|
// Initialize and start key rotation
|
|
|
|
keyManager.GenerateKeyPair()
|
|
|
|
keyManager.CurrentPrivateKey = keyManager.NextPrivateKey
|
|
|
|
keyManager.CurrentPublicKey = keyManager.NextPublicKey
|
|
|
|
keyManager.GenerateKeyPair()
|
|
|
|
go keyManager.RotateKeys()
|
2023-09-06 14:00:14 -06:00
|
|
|
} else {
|
2023-09-06 20:49:07 -06:00
|
|
|
fmt.Println("Running in offline mode. No server connection or key rotation.")
|
2023-09-06 13:12:35 -06:00
|
|
|
}
|
|
|
|
|
2023-09-06 20:49:07 -06:00
|
|
|
ui.StartGUI(ui.UIConfig{
|
|
|
|
OfflineMode: config.OfflineMode,
|
|
|
|
HeaderVisible: config.HeaderVisible,
|
|
|
|
SidebarVisible: config.SidebarVisible,
|
|
|
|
MainChatVisible: config.MainChatVisible,
|
|
|
|
UserTextEntryVisible: config.UserTextEntryVisible,
|
|
|
|
SendButtonVisible: config.SendButtonVisible,
|
|
|
|
})
|
2023-09-06 13:12:35 -06:00
|
|
|
}
|