first commit

master
daiao 3 years ago
parent 4c40755d3b
commit e01f369d0e

@ -6,10 +6,14 @@ app:
httpUrl: 127.0.0.1:8080
webSocketUrl: 127.0.0.1:8089
redis:
addr: "localhost:6379"
password: ""
DB: 0
poolSize: 30
minIdleConns: 30
minIdleConns: 30
rtc:
appID: "d5fpitp5"
appKey: "73f28f78574d78f6259bb43fc700aa59"
gslb: "https://rgslb.rtc.aliyuncs.com"

@ -0,0 +1,50 @@
package docker
import (
"context"
"gowebsocket/common"
"gowebsocket/controllers"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/gin-gonic/gin"
)
type Image struct {
Name string `form:"name" binding:"required"`
}
func Run(c *gin.Context) {
var image Image
err := c.Bind(&image)
if err != nil {
c.JSON(common.ParameterIllegal, gin.H{"error": err.Error()})
return
}
q := c.Request.URL.Query()
// 获取镜像名称
imageName := q.Get("name")
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
c.JSON(common.ServerError, gin.H{"error": err.Error()})
return
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageName,
}, nil, nil, nil, "")
if err != nil {
c.JSON(common.ServerError, gin.H{"error": err.Error()})
return
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
data := make(map[string]interface{})
data["image_id"] = resp.ID
controllers.Response(c, common.OK, "", data)
}

@ -0,0 +1,164 @@
package rtc
import (
"bytes"
cr "crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"gowebsocket/common"
"gowebsocket/controllers"
// "flag"
"fmt"
"os"
"time"
"github.com/gin-gonic/gin"
oh "github.com/ossrs/go-oryx-lib/http"
ol "github.com/ossrs/go-oryx-lib/logger"
"github.com/spf13/viper"
"github.com/twinj/uuid"
)
func CreateUserID(channelID, user string) string {
var b bytes.Buffer
b.WriteString(channelID)
b.WriteString("/")
b.WriteString(user)
h := sha256.New()
if _, err := h.Write([]byte(b.String())); err != nil {
return BuildRandom(16)
}
s := h.Sum(nil)
uid := hex.EncodeToString(s)
return uid[:16]
}
func CreateToken(
appID, appKey, channelID, userID, nonce string, timestamp int64,
) (token string, err error) {
var b bytes.Buffer
b.WriteString(appID)
b.WriteString(appKey)
b.WriteString(channelID)
b.WriteString(userID)
b.WriteString(nonce)
b.WriteString(fmt.Sprint(timestamp))
h := sha256.New()
if _, err = h.Write([]byte(b.String())); err != nil {
return "", err
}
s := h.Sum(nil)
token = hex.EncodeToString(s)
return
}
// generate a random string
func BuildRandom(length int) string {
if length <= 0 {
return ""
}
b := make([]byte, length/2+1)
_, _ = cr.Read(b)
s := hex.EncodeToString(b)
return s[:length]
}
// 获取rtc token用于rtc推流与拉流服务
func GetToken(c *gin.Context) {
appID := viper.GetString("rtc.appID")
appKey := viper.GetString("rtc.appKey")
gslb := viper.GetString("rtc.gslb")
r := c.Request
w := c.Writer
if appID == "" || appKey == "" || gslb == "" {
os.Exit(-1)
}
ol.Tf(nil, "appid=%v, appkey=%v, gslb=%v", appID, appKey, gslb)
if o := r.Header.Get("Origin"); o != "" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,HEAD,PUT,DELETE,OPTIONS")
w.Header().Set("Access-Control-Expose-Headers", "Server,Range,Content-Length,Content-Range")
w.Header().Set("Access-Control-Allow-Headers", "Origin,Range,Accept-Encoding,Referer,Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type")
}
// For matched OPTIONS, should directly return without response.
if r.Method == "OPTIONS" {
return
}
q := r.URL.Query()
channelID, user := q.Get("room"), q.Get("user")
ol.Tf(nil, "Request channelId=%v, user=%v, appid=%v", channelID, user, appID)
if channelID == "" || user == "" {
oh.WriteError(nil, w, r, errors.New("invalid parameter"))
return
}
userID := CreateUserID(channelID, user)
// Warning: nonce support the AppKey generated token.
// the Nonce should be prefix with 'AK-' otherwise the joining verification will failed.
// eg. nonce: "AK-0464002093ce3dd010cb05356c8b1d0f".
uuid := uuid.NewV4()
// if err != nil {
// oh.WriteError(nil, w, r, err)
// return
// }
nonce := fmt.Sprintf("AK-%v", uuid)
// Warning: timestamp is the token expiration time.
// User can custom defined the expire time of token.
// eg, Expires in two days. timestamp: 1559890860.
timestamp := time.Now().Add(48 * time.Hour).Unix()
token, err := CreateToken(appID, appKey, channelID, userID, nonce, timestamp)
if err != nil {
oh.WriteError(nil, w, r, err)
return
}
username := fmt.Sprintf("%s?appid=%s&channel=%s&nonce=%s&timestamp=%d",
userID, appID, channelID, nonce, timestamp)
ol.Tf(nil, "Login: appID=%v, appKey=%v, channelID=%v, userID=%v, nonce=%v, "+
"timestamp=%v, user=%v, userName=%v, token=%v",
appID, appKey, channelID, userID, nonce, timestamp, user, username, token)
type TURN struct {
Username string `json:"username"`
Password string `json:"password"`
}
type Response struct {
AppId string `json:"appid"`
UserId string `json:"userid"`
GSLB []string `json:"gslb"`
Token string `json:"token"`
Nonce string `json:"nonce"`
Timestamp int64 `json:"timestamp"`
TURN *TURN `json:"turn"`
}
oh.WriteData(nil, w, r, &Response{
appID, userID, []string{gslb}, token,
nonce, timestamp,
&TURN{username, token},
})
data := gin.H{
"appID": appID,
"userID": userID,
"gslb": []string{gslb},
"token": token,
"nonce": timestamp,
"TURN": &TURN{username, token},
}
controllers.Response(c, common.OK, "", data)
}

@ -3,26 +3,29 @@ module gowebsocket
go 1.14
require (
github.com/fsnotify/fsnotify v1.4.8-0.20190312181446-1485a34d5d57 // indirect
github.com/containerd/containerd v1.5.8 // indirect
github.com/docker/docker v20.10.11+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/gin-gonic/gin v1.7.2
github.com/go-playground/validator/v10 v10.8.0 // indirect
github.com/go-redis/redis v0.0.0-20190719092155-6bc7daa5b1e8
github.com/golang/protobuf v1.5.2
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2
github.com/hashicorp/hcl v1.0.1-0.20190611123218-cf7d376da96d // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pelletier/go-toml v1.4.1-0.20190725070617-84da2c4a25c5 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/ossrs/go-oryx-lib v0.0.9
github.com/spf13/cast v1.3.1-0.20190531093228-c01685bb8421 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.4-0.20181223182923-24fa6976df40 // indirect
github.com/spf13/viper v1.4.1-0.20190728125013-1b33e8258e07
github.com/subosito/gotenv v1.1.1 // indirect
github.com/twinj/uuid v1.0.0
github.com/ugorji/go v1.2.6 // indirect
google.golang.org/grpc v1.21.0
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
google.golang.org/grpc v1.33.2
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
)

818
go.sum

File diff suppressed because it is too large Load Diff

@ -15,7 +15,7 @@
[GIN-debug] POST /user/sendMessageAll --> gowebsocket/controllers/user.SendMessageAll (3 handlers)
[GIN-debug] GET /system/state --> gowebsocket/controllers/systems.Status (3 handlers)
[GIN-debug] GET /home/index --> gowebsocket/controllers/home.Index (3 handlers)
[GIN] 2021/11/20 - 00:19:19 | 200 | 2.067409ms | 127.0.0.1 | GET "/home/index"
[GIN] 2021/11/20 - 00:19:20 | 200 | 748.062µs | 127.0.0.1 | GET "/user/list?appId=101"
[GIN] 2021/11/20 - 00:19:30 | 200 | 1.828355ms | 127.0.0.1 | GET "/home/index?appId=104"
[GIN] 2021/11/20 - 00:19:31 | 200 | 490.323µs | 127.0.0.1 | GET "/user/list?appId=104"
[GIN-debug] GET /rtc/get_token --> gowebsocket/controllers/rtc.GetToken (3 handlers)
[GIN-debug] GET /docker/run --> gowebsocket/controllers/docker.Run (3 handlers)
[GIN] 2021/11/23 - 12:34:45 | 200 | 5.269909ms | 127.0.0.1 | GET "/home/index"
[GIN] 2021/11/23 - 12:34:48 | 200 | 1.004100998s | 127.0.0.1 | GET "/user/list?appId=101"

@ -45,7 +45,7 @@ func main() {
// grpc
go grpcserver.Init()
go open()
// go open()
httpPort := viper.GetString("app.httpPort")
http.ListenAndServe(":"+httpPort, router)

@ -8,10 +8,13 @@
package routers
import (
"github.com/gin-gonic/gin"
"gowebsocket/controllers/docker"
"gowebsocket/controllers/home"
"gowebsocket/controllers/rtc"
"gowebsocket/controllers/systems"
"gowebsocket/controllers/user"
"github.com/gin-gonic/gin"
)
func Init(router *gin.Engine) {
@ -38,5 +41,17 @@ func Init(router *gin.Engine) {
homeRouter.GET("/index", home.Index)
}
// rtc
rtcRouter := router.Group("/rtc")
{
rtcRouter.GET("/get_token", rtc.GetToken)
}
// docker
dockerRouter := router.Group("/docker")
{
dockerRouter.GET("/run", docker.Run)
}
// router.POST("/user/online", user.Online)
}

Loading…
Cancel
Save