genofire
/
bbb-fetcher
Archived
1
0
Fork 0
This repository has been archived on 2020-11-24. You can view files and clone it, but cannot push or open issues or pull requests.
bbb-fetcher/main.go

114 lines
2.8 KiB
Go
Raw Normal View History

2020-11-19 17:15:54 +01:00
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"time"
"github.com/bdlm/log"
2020-11-19 17:15:54 +01:00
"github.com/PuerkitoBio/goquery"
// "github.com/gorilla/websocket"
"dev.sum7.eu/genofire/golang-lib/worker"
"golang.org/x/net/websocket"
2020-11-19 17:15:54 +01:00
)
type Message struct {
ID string `json:"id"`
}
2020-11-19 17:15:54 +01:00
func fetchRoomURL(bbbURL *url.URL, bbbName string) (string, *url.URL, error) {
2020-11-19 17:15:54 +01:00
res, err := http.Get(bbbURL.String())
if err != nil {
return "", nil, err
2020-11-19 17:15:54 +01:00
}
defer res.Body.Close()
if res.StatusCode != 200 {
return "", nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
2020-11-19 17:15:54 +01:00
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", nil, err
2020-11-19 17:15:54 +01:00
}
form := doc.Find("form")
authToken, ok := form.Find("input[name='authenticity_token']").Attr("value")
if !ok {
return "", nil, fmt.Errorf("'authenticity_token' not found")
2020-11-19 17:15:54 +01:00
}
res.Body.Close()
// ----
res, err = http.PostForm(bbbURL.String(), url.Values{
"authenticity_token": {authToken},
fmt.Sprintf("%s[join_name]", bbbURL.Path): {bbbName},
})
//defer res.Body.Close()
if res.StatusCode != 200 {
return "", nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
2020-11-19 17:15:54 +01:00
}
token := res.Request.URL.Query().Get("sessionToken")
u, err := url.Parse(res.Request.Header.Get("Referer"))
return token, u, err
}
2020-11-19 17:15:54 +01:00
func main() {
log.SetLevel(log.DebugLevel)
bbbName := "bbbBot"
bbbURL, _ := url.Parse(os.Args[1])
token, u, err := fetchRoomURL(bbbURL, bbbName)
if err != nil {
log.Fatal(err)
}
log.WithField("url", u).Debug("join url")
ws, err := websocket.Dial(fmt.Sprintf("wss://%s/sockjs/...", u.Host), "", u.String())
//ws, err := websocket.Dial(fmt.Sprintf("wss://%s/bbb-webrtc-sfu?sessionToken=%s", u.Host, token), "", u.String())
if err != nil {
log.Fatal(err)
}
log.Info("websocket established")
wPing := worker.NewWorker(time.Duration(15)*time.Second, func() {
if err := websocket.JSON.Send(ws, Message{ID: "ping"}); err != nil {
log.Fatal(err)
}
log.WithField("id", "ping").Debug("sended")
})
wPing.Start()
if err := websocket.JSON.Send(ws, map[string]interface{}{
"id": "start",
"type": "send",
"role": "",
"internalMeetingId": u.Query().Get("meetingID"),
"voiceBridge": "",
"callerName": u.Query().Get("userID"),
"sdpOffer": "",
"vh": "",
"vw": "",
"userName": bbbName,
}); err != nil {
log.Fatal(err)
}
log.WithField("id", "start").Debug("sended")
for {
var msgJSON interface{}
if err = websocket.JSON.Receive(ws, &msgJSON); err != nil {
log.Fatal(err)
}
log.WithField("msg", msgJSON).Debug("received")
}
2020-11-19 17:15:54 +01:00
/* not needed ... websocket
doc, err = goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
log.Println(doc.Html())
*/
}