|
| 1 | +package ring |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/AlexxIT/go2rtc/internal/api" |
| 9 | + "github.com/AlexxIT/go2rtc/internal/streams" |
| 10 | + "github.com/AlexxIT/go2rtc/pkg/core" |
| 11 | + "github.com/AlexxIT/go2rtc/pkg/ring" |
| 12 | +) |
| 13 | + |
| 14 | +func Init() { |
| 15 | + streams.HandleFunc("ring", func(source string) (core.Producer, error) { |
| 16 | + return ring.Dial(source) |
| 17 | + }) |
| 18 | + |
| 19 | + api.HandleFunc("api/ring", apiRing) |
| 20 | +} |
| 21 | + |
| 22 | +func apiRing(w http.ResponseWriter, r *http.Request) { |
| 23 | + query := r.URL.Query() |
| 24 | + var ringAPI *ring.RingRestClient |
| 25 | + var err error |
| 26 | + |
| 27 | + // Check auth method |
| 28 | + if email := query.Get("email"); email != "" { |
| 29 | + // Email/Password Flow |
| 30 | + password := query.Get("password") |
| 31 | + code := query.Get("code") |
| 32 | + |
| 33 | + ringAPI, err = ring.NewRingRestClient(ring.EmailAuth{ |
| 34 | + Email: email, |
| 35 | + Password: password, |
| 36 | + }, nil) |
| 37 | + |
| 38 | + if err != nil { |
| 39 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + // Try authentication (this will trigger 2FA if needed) |
| 44 | + if _, err = ringAPI.GetAuth(code); err != nil { |
| 45 | + if ringAPI.Using2FA { |
| 46 | + // Return 2FA prompt |
| 47 | + json.NewEncoder(w).Encode(map[string]interface{}{ |
| 48 | + "needs_2fa": true, |
| 49 | + "prompt": ringAPI.PromptFor2FA, |
| 50 | + }) |
| 51 | + return |
| 52 | + } |
| 53 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 54 | + return |
| 55 | + } |
| 56 | + } else { |
| 57 | + // Refresh Token Flow |
| 58 | + refreshToken := query.Get("refresh_token") |
| 59 | + if refreshToken == "" { |
| 60 | + http.Error(w, "either email/password or refresh_token is required", http.StatusBadRequest) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + ringAPI, err = ring.NewRingRestClient(ring.RefreshTokenAuth{ |
| 65 | + RefreshToken: refreshToken, |
| 66 | + }, nil) |
| 67 | + if err != nil { |
| 68 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 69 | + return |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Fetch devices |
| 74 | + devices, err := ringAPI.FetchRingDevices() |
| 75 | + if err != nil { |
| 76 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // Create clean query with only required parameters |
| 81 | + cleanQuery := url.Values{} |
| 82 | + cleanQuery.Set("refresh_token", ringAPI.RefreshToken) |
| 83 | + |
| 84 | + var items []*api.Source |
| 85 | + for _, camera := range devices.AllCameras { |
| 86 | + cleanQuery.Set("device_id", camera.DeviceID) |
| 87 | + |
| 88 | + // Stream source |
| 89 | + items = append(items, &api.Source{ |
| 90 | + Name: camera.Description, |
| 91 | + URL: "ring:?" + cleanQuery.Encode(), |
| 92 | + }) |
| 93 | + |
| 94 | + // Snapshot source |
| 95 | + items = append(items, &api.Source{ |
| 96 | + Name: camera.Description + " Snapshot", |
| 97 | + URL: "ring:?" + cleanQuery.Encode() + "&snapshot", |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + api.ResponseSources(w, items) |
| 102 | +} |
0 commit comments