Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor http service with authentication middleware. #236

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions platform/ai-talk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ func handleAITalkService(ctx context.Context, handler *http.ServeMux) error {
return errors.Wrapf(err, "parse body")
}

// TODO: why do token Authentication only roomToken is empty?
// Authenticate by bearer token if no room token
if roomToken == "" {
apiSecret := envApiSecret()
Expand Down
39 changes: 9 additions & 30 deletions platform/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/google/uuid"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/google/uuid"

// From ossrs.
"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
Expand Down Expand Up @@ -49,23 +50,9 @@ func NewCallbackWorker() *CallbackWorker {
func (v *CallbackWorker) Handle(ctx context.Context, handler *http.ServeMux) error {
ep := "/terraform/v1/mgmt/hooks/query"
logger.Tf(ctx, "Handle %v", ep)
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
if err := func() error {
var token string
if err := ParseBody(ctx, r.Body, &struct {
Token *string `json:"token"`
All *bool `json:"all"`
}{
Token: &token,
}); err != nil {
return errors.Wrapf(err, "parse body")
}

apiSecret := envApiSecret()
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
return errors.Wrapf(err, "authenticate")
}

handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := func() error {
var config CallbackConfig
if err := config.Load(ctx); err != nil {
return errors.Wrapf(err, "load")
Expand All @@ -91,34 +78,26 @@ func (v *CallbackWorker) Handle(ctx context.Context, handler *http.ServeMux) err
Response: res,
CallbackConfig: &config,
})
logger.Tf(ctx, "hooks apply ok, %v, token=%vB", config.String(), len(token))
logger.Tf(ctx, "hooks apply ok, %v", config.String())
return nil
}(); err != nil {
ohttp.WriteError(ctx, w, r, err)
}
})
})))

ep = "/terraform/v1/mgmt/hooks/apply"
logger.Tf(ctx, "Handle %v", ep)
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := func() error {
var token string
var config CallbackConfig
if err := ParseBody(ctx, r.Body, &struct {
Token *string `json:"token"`
*CallbackConfig
}{
Token: &token,
CallbackConfig: &config,
}); err != nil {
return errors.Wrapf(err, "parse body")
}

apiSecret := envApiSecret()
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
return errors.Wrapf(err, "authenticate")
}

if err := rdb.HSet(ctx, SRS_HOOKS, "target", config.Target).Err(); err != nil && err != redis.Nil {
return errors.Wrapf(err, "hset %v target %v", SRS_HOOKS, config.Target)
}
Expand Down Expand Up @@ -148,12 +127,12 @@ func (v *CallbackWorker) Handle(ctx context.Context, handler *http.ServeMux) err
}

ohttp.WriteData(ctx, w, r, nil)
logger.Tf(ctx, "hooks apply ok, %v, token=%vB", config.String(), len(token))
logger.Tf(ctx, "hooks apply ok, %v", config.String())
return nil
}(); err != nil {
ohttp.WriteError(ctx, w, r, err)
}
})
})))

ep = "/terraform/v1/mgmt/hooks/example"
logger.Tf(ctx, "Handle %v", ep)
Expand Down
68 changes: 18 additions & 50 deletions platform/camera-live-stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
Expand Down Expand Up @@ -49,25 +50,19 @@ func (v *CameraWorker) GetTask(platform string) *CameraTask {
func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error {
ep := "/terraform/v1/ffmpeg/camera/secret"
logger.Tf(ctx, "Handle %v", ep)
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := func() error {
var token, action string
var action string
var userConf CameraConfigure
if err := ParseBody(ctx, r.Body, &struct {
Token *string `json:"token"`
Action *string `json:"action"`
*CameraConfigure
}{
Token: &token, Action: &action, CameraConfigure: &userConf,
Action: &action, CameraConfigure: &userConf,
}); err != nil {
return errors.Wrapf(err, "parse body")
}

apiSecret := envApiSecret()
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
return errors.Wrapf(err, "authenticate")
}

allowedActions := []string{"update"}
allowedPlatforms := []string{"wx", "bilibili", "kuaishou"}
if action != "" {
Expand Down Expand Up @@ -122,7 +117,7 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
}

ohttp.WriteData(ctx, w, r, nil)
logger.Tf(ctx, "Camera: update secret ok, token=%vB", len(token))
logger.Tf(ctx, "Camera: update secret ok")
return nil
} else {
confObjs := make(map[string]*CameraConfigure)
Expand All @@ -139,32 +134,18 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
}

ohttp.WriteData(ctx, w, r, confObjs)
logger.Tf(ctx, "Camera: query configures ok, token=%vB", len(token))
logger.Tf(ctx, "Camera: query configures ok")
return nil
}
}(); err != nil {
ohttp.WriteError(ctx, w, r, err)
}
})
})))

ep = "/terraform/v1/ffmpeg/camera/streams"
logger.Tf(ctx, "Handle %v", ep)
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := func() error {
var token string
if err := ParseBody(ctx, r.Body, &struct {
Token *string `json:"token"`
}{
Token: &token,
}); err != nil {
return errors.Wrapf(err, "parse body")
}

apiSecret := envApiSecret()
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
return errors.Wrapf(err, "authenticate")
}

res := make([]map[string]interface{}, 0)
if configs, err := rdb.HGetAll(ctx, SRS_CAMERA_CONFIG).Result(); err != nil && err != redis.Nil {
return errors.Wrapf(err, "hgetall %v", SRS_CAMERA_CONFIG)
Expand Down Expand Up @@ -209,33 +190,26 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
})

ohttp.WriteData(ctx, w, r, res)
logger.Tf(ctx, "Camera: Query streams ok, token=%vB", len(token))
logger.Tf(ctx, "Camera: Query streams ok")
return nil
}(); err != nil {
ohttp.WriteError(ctx, w, r, err)
}
})
})))

ep = "/terraform/v1/ffmpeg/camera/stream-url"
logger.Tf(ctx, "Handle %v", ep)
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := func() error {
var token string
var qUrl string
if err := ParseBody(ctx, r.Body, &struct {
Token *string `json:"token"`
StreamURL *string `json:"url"`
}{
Token: &token, StreamURL: &qUrl,
StreamURL: &qUrl,
}); err != nil {
return errors.Wrapf(err, "parse body")
}

apiSecret := envApiSecret()
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
return errors.Wrapf(err, "authenticate")
}

// Parse URL to object.
u, err := RebuildStreamURL(qUrl)
if err != nil {
Expand Down Expand Up @@ -274,11 +248,11 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
}(); err != nil {
ohttp.WriteError(ctx, w, r, err)
}
})
})))

ep = "/terraform/v1/ffmpeg/camera/source"
logger.Tf(ctx, "Handle %v", ep)
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := func() error {
type CameraTempFile struct {
// The file name.
Expand All @@ -293,23 +267,17 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
Type FFprobeSourceType `json:"type"`
}

var token, platform string
var platform string
var streams []*CameraTempFile
if err := ParseBody(ctx, r.Body, &struct {
Token *string `json:"token"`
Platform *string `json:"platform"`
Streams *[]*CameraTempFile `json:"files"`
}{
Token: &token, Platform: &platform, Streams: &streams,
Platform: &platform, Streams: &streams,
}); err != nil {
return errors.Wrapf(err, "parse body")
}

apiSecret := envApiSecret()
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
return errors.Wrapf(err, "authenticate")
}

if len(streams) == 0 {
return errors.New("no files")
}
Expand Down Expand Up @@ -469,12 +437,12 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
}{
Platform: platform, Files: parsedStreams,
})
logger.Tf(ctx, "Camera:: Update ok, token=%vB", len(token))
logger.Tf(ctx, "Camera:: Update ok")
return nil
}(); err != nil {
ohttp.WriteError(ctx, w, r, err)
}
})
})))

return nil
}
Expand Down
Loading
Loading