parent
e438abdba1
commit
cda459ae9c
@ -0,0 +1,53 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gowebsocket/common"
|
||||
"gowebsocket/controllers"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/qiniu/api.v7/v7/auth"
|
||||
)
|
||||
|
||||
var manager *Manager
|
||||
|
||||
func init() {
|
||||
accessKey := "dzumJl3gfsMSR3fvfABL4e0kDpo6FJmrlcuTu8TF"
|
||||
secretKey := "aqWegU2o8tTCe0JtIVfMDdOjC3-jvuv2eWFvKQOm"
|
||||
fmt.Println("accessKey: secretKey: ", accessKey, secretKey)
|
||||
mac := auth.New(accessKey, secretKey)
|
||||
manager = NewManager(mac)
|
||||
}
|
||||
|
||||
// TODO: 获取直播流地址
|
||||
func GetRoomToken(c *gin.Context) {
|
||||
if o := c.Request.Header.Get("Origin"); o != "" {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET,POST,HEAD,PUT,DELETE,OPTIONS")
|
||||
c.Writer.Header().Set("Access-Control-Expose-Headers", "Server,Range,Content-Length,Content-Range")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin,Range,Accept-Encoding,Referer,Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type")
|
||||
}
|
||||
|
||||
q := c.Request.URL.Query()
|
||||
appID, roomName, userID, Permission := q.Get("app_id"), q.Get("room_name"), q.Get("user_id"), q.Get("permission")
|
||||
|
||||
roomAccess := RoomAccess{
|
||||
AppID: appID,
|
||||
RoomName: roomName,
|
||||
UserID: userID,
|
||||
ExpireAt: time.Now().Unix() + 3600,
|
||||
Permission: Permission,
|
||||
}
|
||||
|
||||
token, _ := manager.GetRoomToken(roomAccess)
|
||||
url := fmt.Sprintf("https://rtc.qiniuapi.com/v3/apps/%v/rooms/%v/auth?user=%v&token=%v", roomAccess.AppID, roomAccess.RoomName, roomAccess.UserID, token)
|
||||
fmt.Println("url: ", url)
|
||||
|
||||
data := gin.H{
|
||||
"token": token,
|
||||
}
|
||||
controllers.Response(c, common.OK, "", data)
|
||||
// url := fmt.Sprintf("https://rtc.qiniuapi.com/v3/apps/%v/rooms/%v/auth?user=%v&token=%v", roomAccess.AppID, roomAccess.RoomName, roomAccess.UserID, token)
|
||||
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/qiniu/api.v7/v7/auth"
|
||||
)
|
||||
|
||||
// resInfo is httpresponse infomation
|
||||
type resInfo struct {
|
||||
Code int
|
||||
Err error
|
||||
}
|
||||
|
||||
func newResInfo() resInfo {
|
||||
info := resInfo{}
|
||||
return info
|
||||
}
|
||||
|
||||
func getReqid(src *http.Header) string {
|
||||
for k, v := range *src {
|
||||
K := strings.Title(k)
|
||||
if strings.Contains(K, "Reqid") {
|
||||
return strings.Join(v, ", ")
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func buildURL(path string) string {
|
||||
if strings.Index(path, "/") != 0 {
|
||||
path = "/" + path
|
||||
}
|
||||
return "https://" + RtcHost + path
|
||||
}
|
||||
|
||||
func postReq(httpClient *http.Client, mac *auth.Credentials, url string,
|
||||
reqParam interface{}, ret interface{}) *resInfo {
|
||||
info := newResInfo()
|
||||
var reqData []byte
|
||||
var err error
|
||||
|
||||
switch v := reqParam.(type) {
|
||||
case *string:
|
||||
reqData = []byte(*v)
|
||||
case string:
|
||||
reqData = []byte(v)
|
||||
case *[]byte:
|
||||
reqData = *v
|
||||
case []byte:
|
||||
reqData = v
|
||||
default:
|
||||
reqData, err = json.Marshal(reqParam)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
info.Err = err
|
||||
return &info
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewReader(reqData))
|
||||
if err != nil {
|
||||
info.Err = err
|
||||
return &info
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
return callReq(httpClient, req, mac, &info, ret)
|
||||
}
|
||||
|
||||
func getReq(httpClient *http.Client, mac *auth.Credentials, url string, ret interface{}) *resInfo {
|
||||
info := newResInfo()
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
info.Err = err
|
||||
return &info
|
||||
}
|
||||
return callReq(httpClient, req, mac, &info, ret)
|
||||
}
|
||||
|
||||
func delReq(httpClient *http.Client, mac *auth.Credentials, url string, ret interface{}) *resInfo {
|
||||
info := newResInfo()
|
||||
req, err := http.NewRequest("DELETE", url, nil)
|
||||
if err != nil {
|
||||
info.Err = err
|
||||
return &info
|
||||
}
|
||||
return callReq(httpClient, req, mac, &info, ret)
|
||||
}
|
||||
|
||||
func callReq(httpClient *http.Client, req *http.Request, mac *auth.Credentials,
|
||||
info *resInfo, ret interface{}) (oinfo *resInfo) {
|
||||
oinfo = info
|
||||
accessToken, err := mac.SignRequestV2(req)
|
||||
if err != nil {
|
||||
info.Err = err
|
||||
return
|
||||
}
|
||||
req.Header.Add("Authorization", "Qiniu "+accessToken)
|
||||
client := httpClient
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
info.Err = err
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
info.Code = resp.StatusCode
|
||||
reqid := getReqid(&resp.Header)
|
||||
rebuildErr := func(msg string) error {
|
||||
return fmt.Errorf("Code: %v, Reqid: %v, %v", info.Code, reqid, msg)
|
||||
}
|
||||
|
||||
if resp.ContentLength > 2*1024*1024 {
|
||||
err = rebuildErr(fmt.Sprintf("response is too long. Content-Length: %v", resp.ContentLength))
|
||||
info.Err = err
|
||||
return
|
||||
}
|
||||
resData, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
info.Err = rebuildErr(err.Error())
|
||||
return
|
||||
}
|
||||
if info.Code != 200 {
|
||||
info.Err = rebuildErr(string(resData))
|
||||
return
|
||||
}
|
||||
if ret != nil {
|
||||
err = json.Unmarshal(resData, ret)
|
||||
if err != nil {
|
||||
info.Err = rebuildErr(fmt.Sprintf("err: %v, res: %v", err, resData))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in new issue