Add (dis|re)connect commands and provision api

Also fixed a number of data races.
This commit is contained in:
Gary Kramlich
2022-02-22 07:56:15 -06:00
parent d883befee5
commit 4b87ea1cc7
8 changed files with 287 additions and 110 deletions

View File

@@ -8,11 +8,14 @@ import (
"encoding/base64"
"encoding/json"
"net/http"
"sync"
"github.com/gorilla/websocket"
)
type Client struct {
sync.Mutex
URL string
Origin string
@@ -48,6 +51,9 @@ func New() (*Client, error) {
// Dial will start the QRCode login process. ctx may be used to abandon the
// process.
func (c *Client) Dial(ctx context.Context, qrChan chan string, doneChan chan struct{}) error {
c.Lock()
defer c.Unlock()
header := http.Header{
"Origin": []string{c.Origin},
}
@@ -68,10 +74,16 @@ func (c *Client) Dial(ctx context.Context, qrChan chan string, doneChan chan str
}
func (c *Client) Result() (User, error) {
c.Lock()
defer c.Unlock()
return c.user, c.err
}
func (c *Client) close() error {
c.Lock()
defer c.Unlock()
if c.closed {
return nil
}
@@ -89,6 +101,9 @@ func (c *Client) close() error {
}
func (c *Client) write(p clientPacket) error {
c.Lock()
defer c.Unlock()
payload, err := json.Marshal(p)
if err != nil {
return err

View File

@@ -22,10 +22,15 @@ func (c *Client) processMessages() {
defer c.close()
for {
c.Lock()
_, packet, err := c.conn.ReadMessage()
c.Unlock()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
c.Lock()
c.err = err
c.Unlock()
}
return
@@ -33,7 +38,9 @@ func (c *Client) processMessages() {
raw := rawPacket{}
if err := json.Unmarshal(packet, &raw); err != nil {
c.Lock()
c.err = err
c.Unlock()
return
}
@@ -57,7 +64,9 @@ func (c *Client) processMessages() {
}
if err := json.Unmarshal(packet, dest); err != nil {
c.Lock()
c.err = err
c.Unlock()
return
}
@@ -65,7 +74,9 @@ func (c *Client) processMessages() {
op := dest.(serverPacket)
err = op.process(c)
if err != nil {
c.Lock()
c.err = err
c.Unlock()
return
}
@@ -92,7 +103,10 @@ func (h *serverHello) process(client *Client) error {
case <-ticker.C:
h := clientHeartbeat{}
if err := h.send(client); err != nil {
client.Lock()
client.err = err
client.Unlock()
return
}
}
@@ -104,8 +118,10 @@ func (h *serverHello) process(client *Client) error {
<-time.After(duration)
client.Lock()
client.err = fmt.Errorf("Timed out after %s", duration)
client.close()
client.Unlock()
}()
i := clientInit{}