Fix onvif client for some cameras

This commit is contained in:
Alex X
2025-10-23 14:42:38 +03:00
parent be80eb1ac9
commit 6db4dda535
2 changed files with 59 additions and 42 deletions
+41 -36
View File
@@ -1,12 +1,14 @@
package onvif package onvif
import ( import (
"bufio"
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"html" "html"
"io" "io"
"net" "net"
"net/http"
"net/url" "net/url"
"regexp" "regexp"
"strings" "strings"
@@ -32,26 +34,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
} }
@@ -188,9 +182,6 @@ func (c *Client) Request(rawUrl, body string) ([]byte, error) {
return nil, errors.New("onvif: unsupported service") return nil, errors.New("onvif: unsupported service")
} }
e := NewEnvelopeWithUser(c.url.User)
e.Append(body)
u, err := url.Parse(rawUrl) u, err := url.Parse(rawUrl)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -201,44 +192,58 @@ func (c *Client) Request(rawUrl, body string) ([]byte, error) {
host += ":80" host += ":80"
} }
conn, err := net.DialTimeout("tcp", host, 5*time.Second) const timeout = 5 * time.Second
conn, err := net.DialTimeout("tcp", host, timeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer conn.Close() defer conn.Close()
reqBody := e.Bytes() e := NewEnvelopeWithUser(c.url.User)
rawReq := fmt.Appendf(nil, "POST %s HTTP/1.1\r\n"+ e.Append(body)
"Host: %s\r\n"+ buf := e.Bytes()
"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 { req := &http.Request{
Method: "POST",
URL: u,
Proto: "HTTP/1.1",
Header: http.Header{"Content-Type": {"application/soap+xml;charset=utf-8"}},
Body: io.NopCloser(bytes.NewReader(buf)),
ContentLength: int64(len(buf)),
Close: true,
}
_ = conn.SetWriteDeadline(time.Now().Add(timeout))
if err = req.Write(conn); err != nil {
return nil, err return nil, err
} }
rawRes, err := io.ReadAll(conn) rd := bufio.NewReaderSize(conn, 16*1024)
_ = conn.SetReadDeadline(time.Now().Add(timeout))
res, err := http.ReadResponse(rd, req)
if err != nil { if err != nil {
// Try to fix broken response https://github.com/AlexxIT/go2rtc/pull/1589
if buf, err = io.ReadAll(rd); err != nil {
return nil, err return nil, err
} }
// Look for XML in complete response // Look for XML in complete response
if i := bytes.Index(rawRes, []byte("<?xml")); i > 0 { if i := bytes.Index(buf, []byte("<?xml")); i > 0 {
return rawRes[i:], nil return buf[i:], nil
} }
// No XML found - might be an error response return nil, fmt.Errorf("onvif: broken response: %.100s", buf)
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 if res.StatusCode != http.StatusOK {
return rawRes[i+4:], nil return nil, errors.New("onvif: wrong response " + res.Status)
} }
return rawRes, nil if buf, err = io.ReadAll(res.Body); err != nil {
return nil, err
}
return buf, 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)
}