114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
// "github.com/gorilla/websocket"
|
|
"dev.sum7.eu/genofire/golang-lib/worker"
|
|
"golang.org/x/net/websocket"
|
|
)
|
|
|
|
type Message struct {
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
func fetchRoomURL(bbbURL *url.URL, bbbName string) (string, *url.URL, error) {
|
|
res, err := http.Get(bbbURL.String())
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != 200 {
|
|
return "", nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
|
|
}
|
|
|
|
doc, err := goquery.NewDocumentFromReader(res.Body)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
form := doc.Find("form")
|
|
authToken, ok := form.Find("input[name='authenticity_token']").Attr("value")
|
|
if !ok {
|
|
return "", nil, fmt.Errorf("'authenticity_token' not found")
|
|
}
|
|
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)
|
|
}
|
|
token := res.Request.URL.Query().Get("sessionToken")
|
|
u, err := url.Parse(res.Request.Header.Get("Referer"))
|
|
return token, u, err
|
|
}
|
|
|
|
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")
|
|
}
|
|
/* not needed ... websocket
|
|
doc, err = goquery.NewDocumentFromReader(res.Body)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Println(doc.Html())
|
|
*/
|
|
}
|