Skip to content

Commit

Permalink
Merge pull request #1 from achetronic/integrate-with-autoheater
Browse files Browse the repository at this point in the history
Integrate with autoheater
  • Loading branch information
achetronic authored Dec 8, 2023
2 parents 051d80f + d2ce40b commit 52bd3d5
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions pkg/tapogo/tapogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
Expand All @@ -26,7 +25,7 @@ type Tapo struct {
password string

// Fields related to the device
client *http.Client
httpClient *http.Client
handshakeData *HandshakeData
}

Expand All @@ -49,13 +48,16 @@ func NewTapo(ip, email, password string) (*Tapo, error) {
email: email,
password: password,

client: http.DefaultClient,
httpClient: &http.Client{
Timeout: 2 * time.Second,
},
}

// start session
if err := d.Handshake(); err != nil {
return d, err
}

return d, nil
}

Expand Down Expand Up @@ -159,7 +161,7 @@ func (d *Tapo) Handshake1() (handshakeData HandshakeData, err error) {

//log.Printf("REQUEST: %v", request) TODO: show this in debug mode only

response, err := http.DefaultClient.Do(request)
response, err := d.httpClient.Do(request)
if err != nil {
return handshakeData, fmt.Errorf("error making HTTP request: %s", err)
}
Expand Down Expand Up @@ -217,7 +219,7 @@ func (d *Tapo) Handshake2(handshakeData *HandshakeData) error {
}

// Perform the HTTP request
response, err := http.DefaultClient.Do(request)
response, err := d.httpClient.Do(request)
if err != nil {
return fmt.Errorf("error making HTTP request: %s", err)
}
Expand All @@ -240,6 +242,7 @@ func (d *Tapo) Handshake2(handshakeData *HandshakeData) error {

// Handshake TODO
func (d *Tapo) Handshake() error {

// Perform first stage of handshake phase
// The mission here is to get a remote seed and cookies
handshakeData, err := d.Handshake1()
Expand All @@ -248,7 +251,7 @@ func (d *Tapo) Handshake() error {
}

// Not waiting ends in failures WTF?!
time.Sleep(time.Second * 1)
time.Sleep(time.Millisecond * 250)

// Perform second stage of handshake phase
// The mission here is to get a KLAP encryption session
Expand All @@ -258,7 +261,7 @@ func (d *Tapo) Handshake() error {
}

// Not waiting ends in failures WTF?!
time.Sleep(time.Second * 1)
time.Sleep(time.Millisecond * 500)

d.handshakeData = &handshakeData
return nil
Expand All @@ -272,8 +275,7 @@ func (d *Tapo) PerformRequest(request *types.RequestSpec) (response *types.Respo
return response, err
}

// TODO: Decide whether to show the request on each one
log.Printf("Request: %s", string(jsonBytes))
//log.Printf("Request: %s", string(jsonBytes)) TODO: Show in debug mode only

// 'KLAP' forces to encrypt the entire message, not only some parts inside as done in the past by 'securePassthrough'
encryptedPayload, seq := d.handshakeData.Session.encrypt(string(jsonBytes))
Expand All @@ -296,7 +298,7 @@ func (d *Tapo) PerformRequest(request *types.RequestSpec) (response *types.Respo
httpRequest.AddCookie(cookie)
}

httpResponse, err := http.DefaultClient.Do(httpRequest)
httpResponse, err := d.httpClient.Do(httpRequest)
if err != nil {
return response, err
}
Expand All @@ -308,15 +310,14 @@ func (d *Tapo) PerformRequest(request *types.RequestSpec) (response *types.Respo
}

// Check request status
// TODO: Check if handshake is expired to do it again
if httpResponse.StatusCode != 200 {
return response, errors.New(fmt.Sprintf("request exited with failed status: %d", httpResponse.StatusCode))
}

// Decrypt the payload to process it
httpResponseBodyString := d.handshakeData.Session.decrypt(httpResponseBody)

log.Printf("Response (from device): %s", httpResponseBodyString)
//log.Printf("Response (from device): %s", httpResponseBodyString) TODO: Show in debug mode only
err = json.Unmarshal([]byte(httpResponseBodyString), &response)

return response, err
Expand Down

0 comments on commit 52bd3d5

Please sign in to comment.