Merge branch 'AlexxIT:master' into tuya-new

This commit is contained in:
seydx
2025-10-26 16:57:54 +01:00
committed by GitHub
3 changed files with 36 additions and 64 deletions
+14 -61
View File
@@ -3,10 +3,9 @@ package onvif
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"html" "html"
"io" "io"
"net" "net/http"
"net/url" "net/url"
"regexp" "regexp"
"strings" "strings"
@@ -32,26 +31,18 @@ func NewClient(rawURL string) (*Client, error) {
baseURL := "http://" + u.Host baseURL := "http://" + u.Host
client := &Client{url: u} client := &Client{url: u}
if u.Path == "" { client.deviceURL = baseURL + GetPath(u.Path, PathDevice)
client.deviceURL = baseURL + PathDevice
} else {
client.deviceURL = baseURL + u.Path
}
b, err := client.DeviceRequest(DeviceGetCapabilities) b, err := client.DeviceRequest(DeviceGetCapabilities)
if err != nil { if err != nil {
return nil, err return nil, err
} }
client.mediaURL = FindTagValue(b, "Media.+?XAddr") s := FindTagValue(b, "Media.+?XAddr")
if client.mediaURL == "" { client.mediaURL = baseURL + GetPath(s, "/onvif/media_service")
client.mediaURL = baseURL + "/onvif/media_service"
}
client.imaginURL = FindTagValue(b, "Imaging.+?XAddr") s = FindTagValue(b, "Imaging.+?XAddr")
if client.imaginURL == "" { client.imaginURL = baseURL + GetPath(s, "/onvif/imaging_service")
client.imaginURL = baseURL + "/onvif/imaging_service"
}
return client, nil return client, nil
} }
@@ -183,62 +174,24 @@ func (c *Client) MediaRequest(operation string) ([]byte, error) {
return c.Request(c.mediaURL, operation) return c.Request(c.mediaURL, operation)
} }
func (c *Client) Request(rawUrl, body string) ([]byte, error) { func (c *Client) Request(url, body string) ([]byte, error) {
if rawUrl == "" { if url == "" {
return nil, errors.New("onvif: unsupported service") return nil, errors.New("onvif: unsupported service")
} }
e := NewEnvelopeWithUser(c.url.User) e := NewEnvelopeWithUser(c.url.User)
e.Append(body) e.Append(body)
u, err := url.Parse(rawUrl) client := &http.Client{Timeout: time.Second * 5000}
res, err := client.Post(url, `application/soap+xml;charset=utf-8`, bytes.NewReader(e.Bytes()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer res.Body.Close()
host := u.Host if res.StatusCode != http.StatusOK {
if u.Port() == "" { return nil, errors.New("onvif: wrong response " + res.Status)
host += ":80"
} }
conn, err := net.DialTimeout("tcp", host, 5*time.Second) return io.ReadAll(res.Body)
if err != nil {
return nil, err
}
defer conn.Close()
reqBody := e.Bytes()
rawReq := fmt.Appendf(nil, "POST %s HTTP/1.1\r\n"+
"Host: %s\r\n"+
"Content-Type: application/soap+xml;charset=utf-8\r\n"+
"Content-Length: %d\r\n"+
"Connection: close\r\n"+
"\r\n", u.Path, u.Host, len(reqBody))
rawReq = append(rawReq, reqBody...)
if _, err = conn.Write(rawReq); err != nil {
return nil, err
}
rawRes, err := io.ReadAll(conn)
if err != nil {
return nil, err
}
// Look for XML in complete response
if i := bytes.Index(rawRes, []byte("<?xml")); i > 0 {
return rawRes[i:], nil
}
// No XML found - might be an error response
if i := bytes.Index(rawRes, []byte("\r\n\r\n")); i > 0 {
if bytes.Contains(rawRes[:i], []byte("chunked")) {
return nil, errors.New("onvif: TODO: support chunked encoding")
}
// Return body after headers
return rawRes[i+4:], nil
}
return rawRes, nil
} }
+12
View File
@@ -3,6 +3,7 @@ package onvif
import ( import (
"fmt" "fmt"
"net" "net"
"net/url"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -129,3 +130,14 @@ func GetPosixTZ(current time.Time) string {
return prefix + fmt.Sprintf("%02d:%02d", offset/60, offset%60) return prefix + fmt.Sprintf("%02d:%02d", offset/60, offset%60)
} }
func GetPath(urlOrPath, defPath string) string {
if urlOrPath == "" || urlOrPath[0] == '/' {
return defPath
}
u, err := url.Parse(urlOrPath)
if err != nil {
return defPath
}
return GetPath(u.Path, defPath)
}
+10 -3
View File
@@ -140,6 +140,12 @@ func (c *Client) newDectypter(res *http.Response, brand, username, password stri
username = "admin" username = "admin"
} }
if strings.Contains(exchange, `username="none"`) {
// https://nvd.nist.gov/vuln/detail/CVE-2022-37255
username = "none"
password = "TPL075526460603"
}
key := md5.Sum([]byte(nonce + ":" + password)) key := md5.Sum([]byte(nonce + ":" + password))
iv := md5.Sum([]byte(username + ":" + nonce)) iv := md5.Sum([]byte(username + ":" + nonce))
@@ -158,8 +164,9 @@ func (c *Client) newDectypter(res *http.Response, brand, username, password stri
cbc.CryptBlocks(b, b) cbc.CryptBlocks(b, b)
// unpad // unpad
padSize := int(b[len(b)-1]) n := len(b)
return b[:len(b)-padSize] padSize := int(b[n-1])
return b[:n-padSize]
} }
} }
@@ -297,7 +304,7 @@ func dial(req *http.Request, brand, username, password string) (net.Conn, *http.
auth := res.Header.Get("WWW-Authenticate") auth := res.Header.Get("WWW-Authenticate")
if res.StatusCode != http.StatusUnauthorized || !strings.HasPrefix(auth, "Digest") { if res.StatusCode != http.StatusUnauthorized || !strings.HasPrefix(auth, "Digest") {
return nil, nil, fmt.Errorf("Expected StatusCode to be %d, received %d", http.StatusUnauthorized, res.StatusCode) return nil, nil, errors.New("tapo: wrond status: " + res.Status)
} }
if brand == "tapo" && password == "" { if brand == "tapo" && password == "" {