Skip to content

Commit

Permalink
Merge pull request #1029 from aprilmaccydee/h200-child-devices
Browse files Browse the repository at this point in the history
feat(tapo): Add support for H200 hub and child devices (for example battery/sub2G powered D230S1)
  • Loading branch information
AlexxIT committed Apr 28, 2024
2 parents 5f0366a + 3bc5274 commit fb756b7
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/tapo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (c *Client) newConn() (net.Conn, error) {
return nil, err
}

query := u.Query()

if deviceId := query.Get("deviceId"); deviceId != "" {
req.URL.RawQuery = "deviceId=" + deviceId
}

req.URL.User = u.User
req.Header.Set("Content-Type", "multipart/mixed; boundary=--client-stream-boundary--")

Expand All @@ -91,7 +97,6 @@ func (c *Client) newConn() (net.Conn, error) {
c.newDectypter(res)
}

query := u.Query()
channel := query.Get("channel")
if channel == "" {
channel = "0"
Expand Down Expand Up @@ -277,6 +282,7 @@ func dial(req *http.Request) (net.Conn, *http.Response, error) {
if err != nil {
return nil, nil, err
}
_ = res.Body.Close() // ignore response body

auth := res.Header.Get("WWW-Authenticate")

Expand All @@ -301,14 +307,19 @@ func dial(req *http.Request) (net.Conn, *http.Response, error) {
ha1 := tcp.HexMD5(username, realm, password)
ha2 := tcp.HexMD5(req.Method, uri)
nc := "00000001"
cnonce := "00000001"
cnonce := core.RandString(32, 64)
response := tcp.HexMD5(ha1, nonce, nc, cnonce, qop, ha2)

// https://datatracker.ietf.org/doc/html/rfc7616
header := fmt.Sprintf(
`Digest username="%s", realm="%s", nonce="%s", uri="%s", qop=%s, nc=%s, cnonce="%s", response="%s"`,
username, realm, nonce, uri, qop, nc, cnonce, response,
)

if opaque := tcp.Between(auth, `opaque="`, `"`); opaque != "" {
header += fmt.Sprintf(`, opaque="%s", algorithm=MD5`, opaque)
}

req.Header.Set("Authorization", header)

if err = req.Write(conn); err != nil {
Expand Down

0 comments on commit fb756b7

Please sign in to comment.