From 2baa15923a663e8091aa171be925e2c379c94301 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 27 Jun 2020 16:59:56 -0400 Subject: [PATCH] Add auth support --- client_slobs.go | 18 +++++++++++++++--- go.mod | 2 -- go.sum | 4 ---- plugin/manifest.json | 2 +- slobs/client.go | 36 +++++++++++++++++++++++++++++++++--- slobs/slobs_test.go | 21 +++++++++++++++------ 6 files changed, 64 insertions(+), 19 deletions(-) diff --git a/client_slobs.go b/client_slobs.go index f9c9409..8297cf8 100644 --- a/client_slobs.go +++ b/client_slobs.go @@ -8,13 +8,14 @@ import ( type SlobsClient struct { client *slobs.Client key string + password string recording bool } func NewSlobsClient(key, host string, port int, password string) *SlobsClient { slobsc := slobs.NewClient(host + ":" + strconv.Itoa(port)) - return &SlobsClient{client: slobsc, key: key} + return &SlobsClient{client: slobsc, key: key, password: password} } const ( @@ -31,6 +32,19 @@ func (c *SlobsClient) Connect() error { return err } + c.client.Auth(c.password, func(err error) { + if err != nil { + // TODO alert that it failed to startup? + return + } + + c.requestInitialData() + }) + + return nil +} + +func (c *SlobsClient) requestInitialData() { c.client.SendRPC("StreamingService", "getModel", func(e *slobs.RPCResponse) { state := &slobs.IStreamingState{} @@ -63,8 +77,6 @@ func (c *SlobsClient) Connect() error { c.recording = false } }) - - return nil } func (c *SlobsClient) Disconnect() error { diff --git a/go.mod b/go.mod index 5b13ee5..df8c3cf 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,9 @@ module meow.tf/streamdeck/obs-replay go 1.13 require ( - github.com/cenkalti/backoff v2.2.1+incompatible // indirect github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 github.com/gorilla/websocket v1.4.1 github.com/mitchellh/mapstructure v1.1.2 - github.com/mordillo123/sockjs-go-client v0.0.0-20161009150606-c1486c966a40 github.com/valyala/fastjson v1.4.1 meow.tf/streamdeck/sdk v0.0.0-20190519021527-54a933f8777d ) diff --git a/go.sum b/go.sum index 0720036..a57dca9 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,9 @@ -github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= -github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 h1:74lLNRzvsdIlkTgfDSMuaPjBr4cf6k7pwQQANm/yLKU= github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mordillo123/sockjs-go-client v0.0.0-20161009150606-c1486c966a40 h1:d/FazCBU+C1trw+F+Gdah5Q3QX5kNsOigUCes3z0gVc= -github.com/mordillo123/sockjs-go-client v0.0.0-20161009150606-c1486c966a40/go.mod h1:b+6Qgn9fCwiTtpRr6lHe51DY2g69kcMnWc53J4EV4Jc= github.com/valyala/fastjson v1.4.1 h1:hrltpHpIpkaxll8QltMU8c3QZ5+qIiCL8yKqPFJI/yE= github.com/valyala/fastjson v1.4.1/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o= meow.tf/streamdeck/sdk v0.0.0-20190519021527-54a933f8777d h1:PPZHRoZFy9p4GjXssLvTneJfX6cS0bEm51md5TqXFgU= diff --git a/plugin/manifest.json b/plugin/manifest.json index 974f846..164cb75 100644 --- a/plugin/manifest.json +++ b/plugin/manifest.json @@ -42,7 +42,7 @@ "Icon": "images/pluginIcon", "CategoryIcon": "images/pluginIcon", "URL": "https://streamdeck.meow.tf/obsreplay", - "Version": "1.1.1", + "Version": "1.1.2", "SDKVersion": 2, "OS": [ { diff --git a/slobs/client.go b/slobs/client.go index 53b5b31..e9c251a 100644 --- a/slobs/client.go +++ b/slobs/client.go @@ -85,12 +85,38 @@ func (c *Client) Connect() error { return nil } +func (c *Client) Auth(key string, callback func(error)) { + c.SendRPC("TcpServerService", "auth", func(response *RPCResponse) { + if response.Error != nil { + callback(errors.New(response.Error.Message)) + return + } + + var result bool + + json.Unmarshal(*response.Result, &result) + + if !result { + return + } + + callback(nil) + }, key) +} + func (c *Client) Disconnect() error { return c.conn.Close() } -func (c *Client) Subscribe(resource, method string, handler SubscriptionHandler) { +func (c *Client) Subscribe(resource, method string, handler SubscriptionHandler) error { + responseCh := make(chan error, 1) + c.SendRPC(resource, method, func(response *RPCResponse) { + if response.Error != nil { + responseCh <- errors.New(response.Error.Message) + return + } + res := &ResourceEvent{} json.Unmarshal(*response.Result, &res) @@ -98,14 +124,18 @@ func (c *Client) Subscribe(resource, method string, handler SubscriptionHandler) c.subscriptionLock.Lock() c.subscriptions[res.ResourceId] = handler c.subscriptionLock.Unlock() + + close(responseCh) }) + + return <-responseCh } -func (c *Client) SendRPC(resource, method string, handler ResponseHandler) error { +func (c *Client) SendRPC(resource, method string, handler ResponseHandler, args ...string) error { m := make(map[string]interface{}) m["resource"] = resource - m["args"] = []string{} + m["args"] = args atomic.AddInt32(&c.requestId, 1) diff --git a/slobs/slobs_test.go b/slobs/slobs_test.go index 27ff69b..2ea2be8 100644 --- a/slobs/slobs_test.go +++ b/slobs/slobs_test.go @@ -14,12 +14,21 @@ func Test_StreamlabsOBS(t *testing.T) { t.Fatal(err) } - c.Subscribe("StreamingService", "replayBufferStatusChange", func(event *ResourceEvent) { - var status string - event.DecodeTo(&status) - log.Println("Event received:", status) + closeCh := make(chan struct{}, 1) + + c.Auth("a", func(err error) { + if err != nil { + t.Fatal(err) + closeCh <- struct{}{} + return + } + + c.Subscribe("StreamingService", "replayBufferStatusChange", func(event *ResourceEvent) { + var status string + event.DecodeTo(&status) + log.Println("Event received:", status) + }) }) - ch := make(chan struct{}, 1) - <-ch + <-closeCh }