Working end to end with in-memory data store.
This commit is contained in:
parent
320dc46010
commit
426500df45
7 changed files with 313 additions and 27 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/server"
|
||||
"github.com/AYM1607/ccclip/pkg/api"
|
||||
"github.com/AYM1607/ccclip/pkg/crypto"
|
||||
)
|
||||
|
||||
|
|
@ -62,6 +63,7 @@ func (c *Client) RegisterDevice(email, password string, devicePublicKey []byte)
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hres, err := http.Post(c.url+"/registerDevice", "application/json", bytes.NewReader(reqJson))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -85,7 +87,102 @@ func (c *Client) RegisterDevice(email, password string, devicePublicKey []byte)
|
|||
return &res, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetDevices(deviceId string, pvk *ecdh.PrivateKey) (*server.GetUserDevicesResponse, error) {
|
||||
func (c *Client) SetClipboard(plaintext string, deviceId string, pvk *ecdh.PrivateKey) error {
|
||||
devices, err := c.getDevices(deviceId, pvk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payloads := encryptForAll(plaintext, pvk, devices)
|
||||
|
||||
req := server.SetClipboardRequest{
|
||||
FingerPrint: server.FingerPrint{Timestamp: time.Now().UTC()},
|
||||
Clipboard: &api.Clipboard{
|
||||
SenderPublicKey: pvk.PublicKey().Bytes(),
|
||||
Payloads: payloads,
|
||||
},
|
||||
}
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key := crypto.NewSharedKey(pvk, serverPublicKey, crypto.SendDirection)
|
||||
encryptedReq := crypto.Encrypt(key, reqBytes)
|
||||
|
||||
authReq := &server.AuthenticatedPayload{
|
||||
DeviceID: deviceId,
|
||||
Payload: encryptedReq,
|
||||
}
|
||||
authReqJson, err := json.Marshal(authReq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hres, err := http.Post(c.url+"/setClipboard", "application/json", bytes.NewReader(authReqJson))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hresBody, err := io.ReadAll(hres.Body)
|
||||
defer hres.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println(string(hresBody))
|
||||
|
||||
if hres.StatusCode != http.StatusOK {
|
||||
return errors.New("got unexpected response code from server")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetClipboard(deviceId string, pvk *ecdh.PrivateKey) (string, error) {
|
||||
req := server.GetClipboardRequest{
|
||||
FingerPrint: server.FingerPrint{Timestamp: time.Now().UTC()},
|
||||
}
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
key := crypto.NewSharedKey(pvk, serverPublicKey, crypto.SendDirection)
|
||||
encryptedReq := crypto.Encrypt(key, reqBytes)
|
||||
|
||||
authReq := server.AuthenticatedPayload{
|
||||
DeviceID: deviceId,
|
||||
Payload: encryptedReq,
|
||||
}
|
||||
authReqJson, err := json.Marshal(authReq)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
hres, err := http.Post(c.url+"/clipboard", "application/json", bytes.NewReader(authReqJson))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
hresBody, err := io.ReadAll(hres.Body)
|
||||
defer hres.Body.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Println(string(hresBody))
|
||||
|
||||
if hres.StatusCode != http.StatusOK {
|
||||
return "", errors.New("got unexpected response code from server")
|
||||
}
|
||||
|
||||
var res server.GetClipboardResponse
|
||||
err = json.Unmarshal(hresBody, &res)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
key = crypto.NewSharedKey(pvk, crypto.PublicKeyFromBytes(res.SenderPublicKey), crypto.ReceiveDirection)
|
||||
plain := crypto.Decrypt(key, res.Ciphertext)
|
||||
return string(plain), nil
|
||||
}
|
||||
|
||||
func (c *Client) getDevices(deviceId string, pvk *ecdh.PrivateKey) ([]*api.Device, error) {
|
||||
req := server.GetUserDevicesRequest{
|
||||
FingerPrint: server.FingerPrint{Timestamp: time.Now().UTC()},
|
||||
}
|
||||
|
|
@ -127,5 +224,5 @@ func (c *Client) GetDevices(deviceId string, pvk *ecdh.PrivateKey) (*server.GetU
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &res, nil
|
||||
return res.Devices, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue