Move the command types into a separate package

This commit is contained in:
Kovid Goyal 2022-08-17 07:13:05 +05:30
parent 45540561cc
commit 4432c1a2ea
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 21 additions and 19 deletions

View File

@ -10,20 +10,12 @@ import (
"fmt" "fmt"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
"kitty/tools/base85" "kitty/tools/base85"
"kitty/tools/utils"
"os" "os"
"strings" "strings"
"time" "time"
) )
type Cmd struct {
Cmd string `json:"cmd"`
Version [3]int `json:"version"`
NoResponse bool `json:"no_response,omitifempty"`
Payload map[string]interface{} `json:"payload,omitifempty"`
Timestamp int64 `json:"timestamp,omitifempty"`
Password string `json:"password,omitifempty"`
}
func curve25519_key_pair() (private_key []byte, public_key []byte, err error) { func curve25519_key_pair() (private_key []byte, public_key []byte, err error) {
private_key = make([]byte, 32) private_key = make([]byte, 32)
_, err = rand.Read(private_key) _, err = rand.Read(private_key)
@ -78,15 +70,7 @@ func encrypt(plaintext []byte, alice_public_key []byte) (iv []byte, tag []byte,
return return
} }
type EncryptedCmd struct { func Encrypt_cmd(cmd *utils.RemoteControlCmd, password string, other_pubkey []byte) (encrypted_cmd utils.EncryptedRemoteControlCmd, err error) {
Version [3]int `json:"version"`
IV string `json:"iv"`
Tag string `json:"tag"`
Pubkey string `json:"pubkey"`
Encrypted string `json:"encrypted"`
}
func Encrypt_cmd(cmd *Cmd, password string, other_pubkey []byte) (encrypted_cmd EncryptedCmd, err error) {
if len(other_pubkey) == 0 { if len(other_pubkey) == 0 {
raw := os.Getenv("KITTY_PUBLIC_KEY") raw := os.Getenv("KITTY_PUBLIC_KEY")
if len(raw) == 0 { if len(raw) == 0 {
@ -109,7 +93,7 @@ func Encrypt_cmd(cmd *Cmd, password string, other_pubkey []byte) (encrypted_cmd
return return
} }
iv, tag, ciphertext, pubkey, err := encrypt(plaintext, other_pubkey) iv, tag, ciphertext, pubkey, err := encrypt(plaintext, other_pubkey)
encrypted_cmd = EncryptedCmd{ encrypted_cmd = utils.EncryptedRemoteControlCmd{
Version: cmd.Version, IV: b85_encode(iv), Tag: b85_encode(tag), Pubkey: b85_encode(pubkey), Encrypted: b85_encode(ciphertext)} Version: cmd.Version, IV: b85_encode(iv), Tag: b85_encode(tag), Pubkey: b85_encode(pubkey), Encrypted: b85_encode(ciphertext)}
return return
} }

18
tools/utils/types.go Normal file
View File

@ -0,0 +1,18 @@
package utils
type RemoteControlCmd struct {
Cmd string `json:"cmd"`
Version [3]int `json:"version"`
NoResponse bool `json:"no_response,omitifempty"`
Payload map[string]interface{} `json:"payload,omitifempty"`
Timestamp int64 `json:"timestamp,omitifempty"`
Password string `json:"password,omitifempty"`
}
type EncryptedRemoteControlCmd struct {
Version [3]int `json:"version"`
IV string `json:"iv"`
Tag string `json:"tag"`
Pubkey string `json:"pubkey"`
Encrypted string `json:"encrypted"`
}