install go2rtc on bob

This commit is contained in:
2026-04-04 19:36:14 +02:00
parent f0b56e63d1
commit ccf88187b8
537 changed files with 69213 additions and 0 deletions
@@ -0,0 +1,39 @@
# Tuya
[`new in v1.9.13`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.13) by [@seydx](https://github.com/seydx)
[Tuya](https://www.tuya.com/) is a proprietary camera protocol with **two-way audio** support. go2rtc supports `Tuya Smart API` and `Tuya Cloud API`.
**Tuya Smart API (recommended)**:
- **Smart Life accounts are NOT supported**, you need to create a Tuya Smart account. If the cameras are already added to the Smart Life app, you need to remove them and add them again to the [Tuya Smart](https://play.google.com/store/apps/details?id=com.tuya.smart) app.
- Cameras can be discovered through the go2rtc web interface via Tuya Smart account (Add > Tuya > Select region and fill in email and password > Login).
**Tuya Cloud API**:
- Requires setting up a cloud project in the Tuya Developer Platform.
- Obtain `device_id`, `client_id`, `client_secret`, and `uid` from [Tuya IoT Platform](https://iot.tuya.com/). [Here's a guide](https://xzetsubou.github.io/hass-localtuya/cloud_api/).
- Please ensure that you have subscribed to the `IoT Video Live Stream` service (Free Trial) in the Tuya Developer Platform, otherwise the stream will not work (Tuya Developer Platform > Service API > Authorize > IoT Video Live Stream).
## Configuration
Use the `resolution` parameter to select the stream (not all cameras support an `hd` stream through WebRTC even if the camera supports it):
- `hd` - HD stream (default)
- `sd` - SD stream
```yaml
streams:
# Tuya Smart API: WebRTC main stream (use Add > Tuya to discover the URL)
tuya_main:
- tuya://protect-us.ismartlife.me?device_id=XXX&email=XXX&password=XXX
# Tuya Smart API: WebRTC sub stream (use Add > Tuya to discover the URL)
tuya_sub:
- tuya://protect-us.ismartlife.me?device_id=XXX&email=XXX&password=XXX&resolution=sd
# Tuya Cloud API: WebRTC main stream
tuya_webrtc:
- tuya://openapi.tuyaus.com?device_id=XXX&uid=XXX&client_id=XXX&client_secret=XXX
# Tuya Cloud API: WebRTC sub stream
tuya_webrtc_sd:
- tuya://openapi.tuyaus.com?device_id=XXX&uid=XXX&client_id=XXX&client_secret=XXX&resolution=sd
```
@@ -0,0 +1,248 @@
package tuya
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/tuya"
)
func Init() {
streams.HandleFunc("tuya", func(source string) (core.Producer, error) {
return tuya.Dial(source)
})
api.HandleFunc("api/tuya", apiTuya)
}
func apiTuya(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
region := query.Get("region")
email := query.Get("email")
password := query.Get("password")
if email == "" || password == "" || region == "" {
http.Error(w, "email, password and region are required", http.StatusBadRequest)
return
}
var tuyaRegion *tuya.Region
for _, r := range tuya.AvailableRegions {
if r.Host == region {
tuyaRegion = &r
break
}
}
if tuyaRegion == nil {
http.Error(w, fmt.Sprintf("invalid region: %s", region), http.StatusBadRequest)
return
}
httpClient := tuya.CreateHTTPClientWithSession()
_, err := login(httpClient, tuyaRegion.Host, email, password, tuyaRegion.Continent)
if err != nil {
http.Error(w, fmt.Sprintf("login failed: %v", err), http.StatusInternalServerError)
return
}
tuyaAPI, err := tuya.NewTuyaSmartApiClient(
httpClient,
tuyaRegion.Host,
email,
password,
"",
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var devices []tuya.Device
homes, _ := tuyaAPI.GetHomeList()
if homes != nil && len(homes.Result) > 0 {
for _, home := range homes.Result {
roomList, err := tuyaAPI.GetRoomList(strconv.Itoa(home.Gid))
if err != nil {
continue
}
for _, room := range roomList.Result {
for _, device := range room.DeviceList {
if (device.Category == "sp" || device.Category == "dghsxj") && !containsDevice(devices, device.DeviceId) {
devices = append(devices, device)
}
}
}
}
}
sharedHomes, _ := tuyaAPI.GetSharedHomeList()
if sharedHomes != nil && len(sharedHomes.Result.SecurityWebCShareInfoList) > 0 {
for _, sharedHome := range sharedHomes.Result.SecurityWebCShareInfoList {
for _, device := range sharedHome.DeviceInfoList {
if (device.Category == "sp" || device.Category == "dghsxj") && !containsDevice(devices, device.DeviceId) {
devices = append(devices, device)
}
}
}
}
if len(devices) == 0 {
http.Error(w, "no cameras found", http.StatusNotFound)
return
}
var items []*api.Source
for _, device := range devices {
cleanQuery := url.Values{}
cleanQuery.Set("device_id", device.DeviceId)
cleanQuery.Set("email", email)
cleanQuery.Set("password", password)
url := fmt.Sprintf("tuya://%s?%s", tuyaRegion.Host, cleanQuery.Encode())
items = append(items, &api.Source{
Name: device.DeviceName,
URL: url,
})
}
api.ResponseSources(w, items)
}
func login(client *http.Client, serverHost, email, password, countryCode string) (*tuya.LoginResult, error) {
tokenResp, err := getLoginToken(client, serverHost, email, countryCode)
if err != nil {
return nil, err
}
encryptedPassword, err := tuya.EncryptPassword(password, tokenResp.Result.PbKey)
if err != nil {
return nil, fmt.Errorf("failed to encrypt password: %v", err)
}
var loginResp *tuya.PasswordLoginResponse
var url string
loginReq := tuya.PasswordLoginRequest{
CountryCode: countryCode,
Passwd: encryptedPassword,
Token: tokenResp.Result.Token,
IfEncrypt: 1,
Options: `{"group":1}`,
}
if tuya.IsEmailAddress(email) {
url = fmt.Sprintf("https://%s/api/private/email/login", serverHost)
loginReq.Email = email
} else {
url = fmt.Sprintf("https://%s/api/private/phone/login", serverHost)
loginReq.Mobile = email
}
loginResp, err = performLogin(client, url, loginReq, serverHost)
if err != nil {
return nil, err
}
if !loginResp.Success {
return nil, errors.New(loginResp.ErrorMsg)
}
return &loginResp.Result, nil
}
func getLoginToken(client *http.Client, serverHost, username, countryCode string) (*tuya.LoginTokenResponse, error) {
url := fmt.Sprintf("https://%s/api/login/token", serverHost)
tokenReq := tuya.LoginTokenRequest{
CountryCode: countryCode,
Username: username,
IsUid: false,
}
jsonData, err := json.Marshal(tokenReq)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "*/*")
req.Header.Set("Origin", fmt.Sprintf("https://%s", serverHost))
req.Header.Set("Referer", fmt.Sprintf("https://%s/login", serverHost))
req.Header.Set("X-Requested-With", "XMLHttpRequest")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var tokenResp tuya.LoginTokenResponse
if err = json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
return nil, err
}
if !tokenResp.Success {
return nil, errors.New("tuya: " + tokenResp.Msg)
}
return &tokenResp, nil
}
func performLogin(client *http.Client, url string, loginReq tuya.PasswordLoginRequest, serverHost string) (*tuya.PasswordLoginResponse, error) {
jsonData, err := json.Marshal(loginReq)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "*/*")
req.Header.Set("Origin", fmt.Sprintf("https://%s", serverHost))
req.Header.Set("Referer", fmt.Sprintf("https://%s/login", serverHost))
req.Header.Set("X-Requested-With", "XMLHttpRequest")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var loginResp tuya.PasswordLoginResponse
if err := json.NewDecoder(resp.Body).Decode(&loginResp); err != nil {
return nil, err
}
return &loginResp, nil
}
func containsDevice(devices []tuya.Device, deviceID string) bool {
for _, device := range devices {
if device.DeviceId == deviceID {
return true
}
}
return false
}