diff --git a/platform/ai-talk.go b/platform/ai-talk.go index deb62dcb..29b1a456 100644 --- a/platform/ai-talk.go +++ b/platform/ai-talk.go @@ -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() diff --git a/platform/callback.go b/platform/callback.go index 7758aaaf..17862c82 100644 --- a/platform/callback.go +++ b/platform/callback.go @@ -9,7 +9,6 @@ import ( "crypto/tls" "encoding/json" "fmt" - "github.com/google/uuid" "io/ioutil" "net/http" "strconv" @@ -17,6 +16,8 @@ import ( "sync" "time" + "github.com/google/uuid" + // From ossrs. "github.com/ossrs/go-oryx-lib/errors" ohttp "github.com/ossrs/go-oryx-lib/http" @@ -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") @@ -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) } @@ -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) diff --git a/platform/camera-live-stream.go b/platform/camera-live-stream.go index 90c63712..e3d73c30 100644 --- a/platform/camera-live-stream.go +++ b/platform/camera-live-stream.go @@ -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" @@ -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 != "" { @@ -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) @@ -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) @@ -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 { @@ -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. @@ -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") } @@ -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 } diff --git a/platform/dubbing.go b/platform/dubbing.go index e452e769..a876cb39 100644 --- a/platform/dubbing.go +++ b/platform/dubbing.go @@ -47,27 +47,21 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { ep := "/terraform/v1/dubbing/create" 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, title string + var title string var files []*FFprobeSource if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` // Project title. Title *string `json:"title"` // File type and path. Files *[]*FFprobeSource `json:"files"` }{ - Token: &token, Title: &title, Files: &files, + Title: &title, Files: &files, }); 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(files) != 1 { return errors.Errorf("invalid files %v", len(files)) } @@ -137,26 +131,12 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/list" 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") - } - var projects []*SrsDubbingProject if configs, err := rdb.HGetAll(ctx, SRS_DUBBING_PROJECTS).Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hgetall %v", SRS_DUBBING_PROJECTS) @@ -180,27 +160,21 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/remove" 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, dubbingUUID string + var dubbingUUID string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` DubbingUUID *string `json:"uuid"` }{ - Token: &token, DubbingUUID: &dubbingUUID, + DubbingUUID: &dubbingUUID, }); 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") - } - dubbing := &SrsDubbingProject{UUID: dubbingUUID} if err := dubbing.Load(ctx); err != nil { return errors.Wrapf(err, "load dubbing project %v", dubbingUUID) @@ -226,27 +200,21 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/query" 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, dubbingUUID string + var dubbingUUID string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` DubbingUUID *string `json:"uuid"` }{ - Token: &token, DubbingUUID: &dubbingUUID, + DubbingUUID: &dubbingUUID, }); 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") - } - dubbing := &SrsDubbingProject{UUID: dubbingUUID} if err := dubbing.Load(ctx); err != nil { return errors.Wrapf(err, "load dubbing project %v", dubbingUUID) @@ -264,28 +232,21 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/update" 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 dubbing SrsDubbingProject if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` *SrsDubbingProject }{ - Token: &token, SrsDubbingProject: &dubbing, + SrsDubbingProject: &dubbing, }); 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") - } - // TODO: FIXME: Should load dubbing from redis and merge the fields. if b, err := json.Marshal(dubbing); err != nil { return errors.Wrapf(err, "marshal dubbing") @@ -311,31 +272,19 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/play" logger.Tf(ctx, "Handle %v", ep) - handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) { + handler.Handle(ep, middlewareAuthTokenInURL(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if err := func() error { q := r.URL.Query() - token := q.Get("token") - if token == "" { - return errors.Errorf("empty token") - } dubbingUUID := q.Get("uuid") if dubbingUUID == "" { return errors.Errorf("empty uuid") } - // Convert the token in query to header Bearer token. - r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) - - apiSecret := envApiSecret() - if err := Authenticate(ctx, apiSecret, "", r.Header); err != nil { - return errors.Wrapf(err, "authenticate") - } - dubbing := &SrsDubbingProject{UUID: dubbingUUID} if err := dubbing.Load(ctx); err != nil { return errors.Wrapf(err, "load dubbing project %v", dubbingUUID) @@ -363,29 +312,22 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/export" 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 dubbingUUID, taskUUID string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` UUID *string `json:"uuid"` TaskUUID *string `json:"task"` }{ - Token: &token, UUID: &dubbingUUID, TaskUUID: &taskUUID, + UUID: &dubbingUUID, TaskUUID: &taskUUID, }); 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") - } - dubbing := &SrsDubbingProject{UUID: dubbingUUID} if err := dubbing.Load(ctx); err != nil { return errors.Wrapf(err, "load dubbing project %v", dubbingUUID) @@ -561,28 +503,21 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/task-start" 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 dubbingUUID string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - UUID *string `json:"uuid"` + UUID *string `json:"uuid"` }{ - Token: &token, UUID: &dubbingUUID, + UUID: &dubbingUUID, }); 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") - } - dubbing := &SrsDubbingProject{UUID: dubbingUUID} if err := dubbing.Load(ctx); err != nil { return errors.Wrapf(err, "load dubbing project %v", dubbingUUID) @@ -649,31 +584,24 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/task-rephrase" 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 dubbingUUID, taskUUID, groupUUID string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` UUID *string `json:"uuid"` TaskUUID *string `json:"task"` GroupUUID *string `json:"group"` }{ - Token: &token, UUID: &dubbingUUID, TaskUUID: &taskUUID, + UUID: &dubbingUUID, TaskUUID: &taskUUID, GroupUUID: &groupUUID, }); 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") - } - dubbing := NewSrsDubbingProject(func(dubbing *SrsDubbingProject) { dubbing.UUID = dubbingUUID }) @@ -765,32 +693,25 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/task-merge" 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 dubbingUUID, taskUUID, groupUUID, direction string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` UUID *string `json:"uuid"` TaskUUID *string `json:"task"` GroupUUID *string `json:"group"` Direction *string `json:"direction"` }{ - Token: &token, UUID: &dubbingUUID, TaskUUID: &taskUUID, + UUID: &dubbingUUID, TaskUUID: &taskUUID, GroupUUID: &groupUUID, Direction: &direction, }); 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") - } - dubbing := NewSrsDubbingProject(func(dubbing *SrsDubbingProject) { dubbing.UUID = dubbingUUID }) @@ -844,29 +765,22 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/task-query" 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 dubbingUUID, taskUUID string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` UUID *string `json:"uuid"` TaskUUID *string `json:"task"` }{ - Token: &token, UUID: &dubbingUUID, TaskUUID: &taskUUID, + UUID: &dubbingUUID, TaskUUID: &taskUUID, }); 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") - } - dubbing := NewSrsDubbingProject(func(dubbing *SrsDubbingProject) { dubbing.UUID = dubbingUUID }) @@ -896,18 +810,14 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/task-tts" logger.Tf(ctx, "Handle %v", ep) - handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) { + handler.Handle(ep, middlewareAuthTokenInURL(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if err := func() error { q := r.URL.Query() - token := q.Get("token") - if token == "" { - return errors.Errorf("empty token") - } dubbingUUID := q.Get("uuid") if dubbingUUID == "" { return errors.Errorf("empty uuid") @@ -917,14 +827,6 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { return errors.Errorf("empty group") } - // Convert the token in query to header Bearer token. - r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) - - apiSecret := envApiSecret() - if err := Authenticate(ctx, apiSecret, "", r.Header); err != nil { - return errors.Wrapf(err, "authenticate") - } - dubbing := &SrsDubbingProject{UUID: dubbingUUID} if err := dubbing.Load(ctx); err != nil { return errors.Wrapf(err, "load dubbing project %v", dubbingUUID) @@ -961,11 +863,11 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/dubbing/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 DubbingTempFile struct { // The file name. @@ -982,22 +884,15 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { Type FFprobeSourceType `json:"type"` } - var token string var files []*DubbingTempFile if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Files *[]*DubbingTempFile `json:"files"` }{ - Token: &token, Files: &files, + Files: &files, }); 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(files) == 0 { return errors.New("no files") } @@ -1125,12 +1020,12 @@ func handleDubbingService(ctx context.Context, handler *http.ServeMux) error { }{ Files: parsedFiles, }) - logger.Tf(ctx, "Dubbing: Update dubbing ok, token=%vB", len(token)) + logger.Tf(ctx, "Dubbing: Update dubbing ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) return nil } diff --git a/platform/forward.go b/platform/forward.go index 3cb82c4a..42f469c6 100644 --- a/platform/forward.go +++ b/platform/forward.go @@ -49,25 +49,19 @@ func (v *ForwardWorker) GetTask(platform string) *ForwardTask { func (v *ForwardWorker) Handle(ctx context.Context, handler *http.ServeMux) error { ep := "/terraform/v1/ffmpeg/forward/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 ForwardConfigure if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Action *string `json:"action"` *ForwardConfigure }{ - Token: &token, Action: &action, ForwardConfigure: &userConf, + Action: &action, ForwardConfigure: &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 != "" { @@ -119,7 +113,7 @@ func (v *ForwardWorker) Handle(ctx context.Context, handler *http.ServeMux) erro } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "Forward update secret ok, token=%vB", len(token)) + logger.Tf(ctx, "Forward update secret ok") return nil } else { confObjs := make(map[string]*ForwardConfigure) @@ -136,32 +130,18 @@ func (v *ForwardWorker) Handle(ctx context.Context, handler *http.ServeMux) erro } ohttp.WriteData(ctx, w, r, confObjs) - logger.Tf(ctx, "forward query configures ok, token=%vB", len(token)) + logger.Tf(ctx, "forward query configures ok") return nil } }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ffmpeg/forward/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 configItems, err := rdb.HGetAll(ctx, SRS_FORWARD_CONFIG).Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hgetall %v", SRS_FORWARD_CONFIG) @@ -204,12 +184,12 @@ func (v *ForwardWorker) Handle(ctx context.Context, handler *http.ServeMux) erro }) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "Query forward streams ok, token=%vB", len(token)) + logger.Tf(ctx, "Query forward streams ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) return nil } diff --git a/platform/live-room.go b/platform/live-room.go index a743367f..36ce7191 100644 --- a/platform/live-room.go +++ b/platform/live-room.go @@ -12,6 +12,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" ) @@ -19,23 +20,17 @@ import ( func handleLiveRoomService(ctx context.Context, handler *http.ServeMux) error { ep := "/terraform/v1/live/room/create" 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, title string + var title string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Title *string `json:"title"` }{ - Token: &token, Title: &title, + Title: &title, }); 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") - } - room := NewLiveRoom(func(room *SrsLiveRoom) { room.Title = title // By default, we always enable the AI assistant for user. @@ -59,27 +54,21 @@ func handleLiveRoomService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/live/room/query" 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, rid string + var rid string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` RoomUUID *string `json:"uuid"` }{ - Token: &token, RoomUUID: &rid, + RoomUUID: &rid, }); 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") - } - var room SrsLiveRoom if r0, err := rdb.HGet(ctx, SRS_LIVE_ROOM, rid).Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v %v", SRS_LIVE_ROOM, rid) @@ -95,28 +84,21 @@ func handleLiveRoomService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/live/room/update" 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 room SrsLiveRoom if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` *SrsLiveRoom }{ - Token: &token, SrsLiveRoom: &room, + SrsLiveRoom: &room, }); 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") - } - // As room is a template config, to create active stage. So if we update the template, we // need to update the active stage object. if err := room.UpdateStage(ctx); err != nil { @@ -148,26 +130,12 @@ func handleLiveRoomService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/live/room/list" 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") - } - var rooms []*SrsLiveRoom if configs, err := rdb.HGetAll(ctx, SRS_LIVE_ROOM).Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hgetall %v", SRS_LIVE_ROOM) @@ -191,27 +159,21 @@ func handleLiveRoomService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/live/room/remove" 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, roomUUID string + var roomUUID string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` RoomUUID *string `json:"uuid"` }{ - Token: &token, RoomUUID: &roomUUID, + RoomUUID: &roomUUID, }); 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") - } - var room SrsLiveRoom if r0, err := rdb.HGet(ctx, SRS_LIVE_ROOM, roomUUID).Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v %v", SRS_LIVE_ROOM, roomUUID) @@ -237,7 +199,7 @@ func handleLiveRoomService(ctx context.Context, handler *http.ServeMux) error { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) return nil } diff --git a/platform/ocr.go b/platform/ocr.go index 5c739198..d3c9479c 100644 --- a/platform/ocr.go +++ b/platform/ocr.go @@ -21,6 +21,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" @@ -61,22 +62,8 @@ func NewOCRWorker() *OCRWorker { func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { ep := "/terraform/v1/ai/ocr/query" 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") - } - config := NewOCRConfig() if err := config.Load(ctx); err != nil { return errors.Wrapf(err, "load config") @@ -95,37 +82,29 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { resp.Task.UUID = v.task.UUID ohttp.WriteData(ctx, w, r, resp) - logger.Tf(ctx, "ocr query ok, config=<%v>, uuid=%v, token=%vB", - config, v.task.UUID, len(token)) + logger.Tf(ctx, "ocr query ok, config=<%v>, uuid=%v", + config, v.task.UUID) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/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 uuid string var config OCRConfig if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - UUID *string `json:"uuid"` + UUID *string `json:"uuid"` *OCRConfig }{ - Token: &token, - UUID: &uuid, OCRConfig: &config, + UUID: &uuid, OCRConfig: &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") - } - // Not required yet. if uuid != v.task.UUID { logger.Wf(ctx, "ocr ignore uuid mismatch, query=%v, task=%v", uuid, v.task.UUID) @@ -145,35 +124,27 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { ohttp.WriteData(ctx, w, r, &ApplyResponse{ UUID: v.task.UUID, }) - logger.Tf(ctx, "ocr apply ok, config=<%v>, uuid=%v, token=%vB", - config, v.task.UUID, len(token)) + logger.Tf(ctx, "ocr apply ok, config=<%v>, uuid=%v", + config, v.task.UUID) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/check" 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 ocrConfig OCRConfig if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` *OCRConfig }{ - Token: &token, OCRConfig: &ocrConfig, }); 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") - } - // Query whisper-1 model detail. var config openai.ClientConfig config = openai.DefaultConfig(ocrConfig.AISecretKey) @@ -206,35 +177,27 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "ocr check ok, config=<%v>, model=<%v>, msg=<%v>, token=%vB", - ocrConfig, model.ID, resp.Choices[0].Message.Content, len(token)) + logger.Tf(ctx, "ocr check ok, config=<%v>, model=<%v>, msg=<%v>", + ocrConfig, model.ID, resp.Choices[0].Message.Content) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/reset" 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 uuid string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - UUID *string `json:"uuid"` + UUID *string `json:"uuid"` }{ - Token: &token, - UUID: &uuid, + UUID: &uuid, }); 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 uuid != v.task.UUID { return errors.Errorf("invalid uuid %v", uuid) } @@ -249,31 +212,17 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { ohttp.WriteData(ctx, w, r, &ResetResponse{ UUID: v.task.UUID, }) - logger.Tf(ctx, "ocr reset ok, uuid=%v, new=%v, token=%vB", uuid, v.task.UUID, len(token)) + logger.Tf(ctx, "ocr reset ok, uuid=%v, new=%v", uuid, v.task.UUID) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/live-queue" 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") - } - type Segment struct { TsID string `json:"tsid"` SeqNo uint64 `json:"seqno"` @@ -289,7 +238,7 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { segments := v.task.liveSegments() for _, segment := range segments { - res.Segments = append(res.Segments, []*Segment{&Segment{ + res.Segments = append(res.Segments, []*Segment{{ TsID: segment.TsFile.TsID, SeqNo: segment.TsFile.SeqNo, URL: segment.TsFile.URL, @@ -301,31 +250,17 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "ocr query live ok, token=%vB", len(token)) + logger.Tf(ctx, "ocr query live ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/ocr-queue" 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") - } - type Segment struct { TsID string `json:"tsid"` SeqNo uint64 `json:"seqno"` @@ -361,31 +296,17 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "ocr query ocr ok, token=%vB", len(token)) + logger.Tf(ctx, "ocr query ocr ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/callback-queue" 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") - } - type Segment struct { TsID string `json:"tsid"` SeqNo uint64 `json:"seqno"` @@ -409,7 +330,7 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { segments := v.task.callbackSegments() for _, segment := range segments { - res.Segments = append(res.Segments, []*Segment{&Segment{ + res.Segments = append(res.Segments, []*Segment{{ TsID: segment.ImageFile.TsID, SeqNo: segment.ImageFile.SeqNo, URL: segment.ImageFile.File, @@ -429,31 +350,17 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "ocr query callback ok, token=%vB", len(token)) + logger.Tf(ctx, "ocr query callback ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/cleanup-queue" 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") - } - type Segment struct { TsID string `json:"tsid"` SeqNo uint64 `json:"seqno"` @@ -479,7 +386,7 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { segments := v.task.cleanupSegments() for _, segment := range segments { - res.Segments = append(res.Segments, []*Segment{&Segment{ + res.Segments = append(res.Segments, []*Segment{{ TsID: segment.ImageFile.TsID, SeqNo: segment.ImageFile.SeqNo, URL: segment.ImageFile.File, @@ -501,12 +408,12 @@ func (v *OCRWorker) Handle(ctx context.Context, handler *http.ServeMux) error { res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "ocr query cleanup ok, token=%vB", len(token)) + logger.Tf(ctx, "ocr query cleanup ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/ocr/image/" logger.Tf(ctx, "Handle %v", ep) diff --git a/platform/service.go b/platform/service.go index 96b1e6f5..404a2072 100644 --- a/platform/service.go +++ b/platform/service.go @@ -4,11 +4,11 @@ package main import ( + "bytes" "context" "crypto/tls" "encoding/json" "fmt" - "github.com/joho/godotenv" "io" "io/ioutil" "net/http" @@ -19,9 +19,12 @@ import ( "sync" "time" + "github.com/joho/godotenv" + "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" ) @@ -82,6 +85,7 @@ func (v *httpService) Run(ctx context.Context) error { if true { serviceHandler := http.NewServeMux() if err := handleHTTPService(ctx, serviceHandler); err != nil { + cancel() return errors.Wrapf(err, "handle service") } @@ -204,12 +208,74 @@ func (v *httpService) Run(ctx context.Context) error { wg.Wait() for _, r := range []error{r0, r1, r2} { if r != nil { + cancel() return r } } + + cancel() return nil } +func middlewareAuthTokenInURL(ctx context.Context, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := func() error { + q := r.URL.Query() + token := q.Get("token") + if token == "" { + return errors.Errorf("empty token") + } + // Convert the token in query to header Bearer token. + r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) + + apiSecret := envApiSecret() + + if err := Authenticate(ctx, apiSecret, "", r.Header); err != nil { + return errors.Wrapf(err, "authenticate") + } + + return nil + }(); err != nil { + ohttp.WriteError(ctx, w, r, err) + } + + next.ServeHTTP(w, r) + }) +} + +func middlewareAuthTokenInBody(ctx context.Context, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := func() error { + body, err := io.ReadAll(r.Body) + if err != nil { + return errors.Wrapf(err, "read http request body") + } + // set a new io.ReadCloser to r.Body + r.Body = io.NopCloser(bytes.NewReader(body)) + bodyReader := io.NopCloser(bytes.NewReader(body)) + + apiSecret := envApiSecret() + var token string + if err := ParseBody(ctx, bodyReader, &struct { + Token *string `json:"token"` + }{ + Token: &token, + }); err != nil { + return errors.Wrapf(err, "parse body") + } + + if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil { + return errors.Wrapf(err, "authenticate") + } + return nil + }(); err != nil { + ohttp.WriteError(ctx, w, r, err) + } + + next.ServeHTTP(w, r) + }) +} + func handleHTTPService(ctx context.Context, handler *http.ServeMux) error { ohttp.Server = fmt.Sprintf("Oryx/%v", version) @@ -525,7 +591,7 @@ func handleMgmtInit(ctx context.Context, handler *http.ServeMux) { } apiSecret := envApiSecret() - expireAt, createAt, token, err := createToken(ctx, envApiSecret()) + expireAt, createAt, token, err := createToken(apiSecret) if err != nil { return errors.Wrapf(err, "build token") } @@ -688,23 +754,9 @@ func handleMgmtEnvs(ctx context.Context, handler *http.ServeMux) { func handleMgmtToken(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/token" 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") - } - - expireAt, createAt, token, err := createToken(ctx, envApiSecret()) + expireAt, createAt, token, err := createToken(envApiSecret()) if err != nil { return errors.Wrapf(err, "build token") } @@ -721,7 +773,7 @@ func handleMgmtToken(ctx context.Context, handler *http.ServeMux) { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtLogin(ctx context.Context, handler *http.ServeMux) { @@ -770,7 +822,7 @@ func handleMgmtLogin(ctx context.Context, handler *http.ServeMux) { } apiSecret := envApiSecret() - expireAt, createAt, token, err := createToken(ctx, apiSecret) + expireAt, createAt, token, err := createToken(apiSecret) if err != nil { return errors.Wrapf(err, "build token") } @@ -796,22 +848,8 @@ func handleMgmtLogin(ctx context.Context, handler *http.ServeMux) { func handleMgmtStatus(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/status" 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") - } - upgrading, err := rdb.HGet(ctx, SRS_UPGRADING, "upgrading").Result() if err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v upgrading", SRS_UPGRADING) @@ -828,34 +866,28 @@ func handleMgmtStatus(ctx context.Context, handler *http.ServeMux) { Upgrading: upgrading == "1", Strategy: "manual", }) - logger.Tf(ctx, "status ok, versions=%v, upgrading=%v, token=%vB", conf.Versions.String(), upgrading, len(token)) + logger.Tf(ctx, "status ok, versions=%v, upgrading=%v", conf.Versions.String(), upgrading) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtBilibili(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/bilibili" 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, bvid string + var bvid string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - BVID *string `json:"bvid"` + BVID *string `json:"bvid"` }{ - Token: &token, BVID: &bvid, + BVID: &bvid, }); 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 bvid == "" { return errors.New("no bvid") } @@ -921,33 +953,19 @@ func handleMgmtBilibili(ctx context.Context, handler *http.ServeMux) { } ohttp.WriteData(ctx, w, r, bilibiliObj.Res) - logger.Tf(ctx, "bilibili cache bvid=%v, update=%v, token=%vB", bvid, bilibiliObj.Update, len(token)) + logger.Tf(ctx, "bilibili cache bvid=%v, update=%v", bvid, bilibiliObj.Update) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtOpenAIQuery(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/openai/query" 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") - } - aiSecretKey, err := rdb.HGet(ctx, SRS_SYS_OPENAI, "key").Result() if err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v key", SRS_SYS_OPENAI) @@ -979,33 +997,26 @@ func handleMgmtOpenAIQuery(ctx context.Context, handler *http.ServeMux) { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtOpenAIUpdate(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/openai/update" 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 aiSecretKey, aiBaseURL, aiOrganization string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` AISecretKey *string `json:"aiSecretKey"` AIBaseURL *string `json:"aiBaseURL"` AIOrganization *string `json:"aiOrganization"` }{ - Token: &token, AISecretKey: &aiSecretKey, AIBaseURL: &aiBaseURL, + AISecretKey: &aiSecretKey, AIBaseURL: &aiBaseURL, AIOrganization: &aiOrganization, }); 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 aiSecretKey == "" { return errors.New("no aiSecretKey") } @@ -1029,28 +1040,14 @@ func handleMgmtOpenAIUpdate(ctx context.Context, handler *http.ServeMux) { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtLimitsQuery(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/limits/query" 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") - } - vLiveLimits, err := rdb.HGet(ctx, SRS_SYS_LIMITS, "vlive").Int64() if err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v vlive", SRS_SYS_LIMITS) @@ -1079,31 +1076,24 @@ func handleMgmtLimitsQuery(ctx context.Context, handler *http.ServeMux) { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtLimitsUpdate(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/limits/update" 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 vlive, camera int64 if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - VLive *int64 `json:"vlive"` - IPCamera *int64 `json:"camera"` + VLive *int64 `json:"vlive"` + IPCamera *int64 `json:"camera"` }{ - Token: &token, VLive: &vlive, IPCamera: &camera, + VLive: &vlive, IPCamera: &camera, }); 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 vlive <= 0 { return errors.Errorf("invalid vlive %v", vlive) } @@ -1124,7 +1114,7 @@ func handleMgmtLimitsUpdate(ctx context.Context, handler *http.ServeMux) { }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } // Note that this API is not verified by token. @@ -1150,52 +1140,33 @@ func handleMgmtBeianQuery(ctx context.Context, handler *http.ServeMux) { func handleMgmtSecretQuery(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/secret/query" 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") - } - ohttp.WriteData(ctx, w, r, apiSecret) - logger.Tf(ctx, "query apiSecret ok, versions=%v, token=%vB", conf.Versions.String(), len(token)) + logger.Tf(ctx, "query apiSecret ok, versions=%v", conf.Versions.String()) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtBeianUpdate(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/beian/update" 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, beian, text string + var beian, text string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Beian *string `json:"beian"` Text *string `json:"text"` }{ - Token: &token, Beian: &beian, Text: &text, + Beian: &beian, Text: &text, }); 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 beian == "" { return errors.New("no beian") } @@ -1208,35 +1179,28 @@ func handleMgmtBeianUpdate(ctx context.Context, handler *http.ServeMux) { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "beian: update ok, beian=%v, text=%v, token=%vB", beian, text, len(token)) + logger.Tf(ctx, "beian: update ok, beian=%v, text=%v", beian, text) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtNginxHlsUpdate(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/hphls/update" 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 noHlsCtx bool if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - NoHlsCtx *bool `json:"noHlsCtx"` + NoHlsCtx *bool `json:"noHlsCtx"` }{ - Token: &token, NoHlsCtx: &noHlsCtx, + NoHlsCtx: &noHlsCtx, }); 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") - } - noHlsCtxValue := fmt.Sprintf("%v", noHlsCtx) if err := rdb.HSet(ctx, SRS_HP_HLS, "noHlsCtx", noHlsCtxValue).Err(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hset %v noHlsCtx %v", SRS_HP_HLS, noHlsCtxValue) @@ -1247,33 +1211,19 @@ func handleMgmtNginxHlsUpdate(ctx context.Context, handler *http.ServeMux) { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "nginx hls update ok, enabled=%v, token=%vB", noHlsCtx, len(token)) + logger.Tf(ctx, "nginx hls update ok, enabled=%v", noHlsCtx) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtNginxHlsQuery(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/hphls/query" 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") - } - var enabled bool if v, err := rdb.HGet(ctx, SRS_HP_HLS, "noHlsCtx").Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v %v", SRS_HP_HLS, "noHlsCtx") @@ -1286,35 +1236,28 @@ func handleMgmtNginxHlsQuery(ctx context.Context, handler *http.ServeMux) { }{ NoHlsCtx: enabled, }) - logger.Tf(ctx, "nginx hls query ok, enabled=%v, token=%vB", enabled, len(token)) + logger.Tf(ctx, "nginx hls query ok, enabled=%v", enabled) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtHlsLowLatencyUpdate(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/hlsll/update" 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 hlsLowLatency bool if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - HlsLowLatency *bool `json:"hlsLowLatency"` + HlsLowLatency *bool `json:"hlsLowLatency"` }{ - Token: &token, HlsLowLatency: &hlsLowLatency, + HlsLowLatency: &hlsLowLatency, }); 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") - } - hlsLowLatencyValue := fmt.Sprintf("%v", hlsLowLatency) if err := rdb.HSet(ctx, SRS_LL_HLS, "hlsLowLatency", hlsLowLatencyValue).Err(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hset %v hlsLowLatency %v", SRS_LL_HLS, hlsLowLatencyValue) @@ -1325,33 +1268,19 @@ func handleMgmtHlsLowLatencyUpdate(ctx context.Context, handler *http.ServeMux) } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "hls low latency update ok, enabled=%v, token=%vB", hlsLowLatency, len(token)) + logger.Tf(ctx, "hls low latency update ok, enabled=%v", hlsLowLatency) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtHlsLowLatencyQuery(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/hlsll/query" 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") - } - var enabled bool if v, err := rdb.HGet(ctx, SRS_LL_HLS, "hlsLowLatency").Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v %v", SRS_LL_HLS, "hlsLowLatency") @@ -1364,68 +1293,47 @@ func handleMgmtHlsLowLatencyQuery(ctx context.Context, handler *http.ServeMux) { }{ HlsLowLatency: enabled, }) - logger.Tf(ctx, "hls low latency query ok, enabled=%v, token=%vB", enabled, len(token)) + logger.Tf(ctx, "hls low latency query ok, enabled=%v", enabled) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtAutoSelfSignedCertificate(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/auto-self-signed-certificate" 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") - } - if err := certManager.createSelfSignCertificate(ctx); err != nil { return errors.Wrapf(err, "create self sign certificate") } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "create self-signed cert ok, token=%vB", len(token)) + logger.Tf(ctx, "create self-signed cert ok, token=%vB") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtSsl(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/ssl" 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 key, crt string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - Key *string `json:"key"` - Crt *string `json:"crt"` + Key *string `json:"key"` + Crt *string `json:"crt"` }{ - Token: &token, Key: &key, Crt: &crt, + Key: &key, Crt: &crt, }); 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 key = strings.TrimSpace(key); key == "" { return errors.New("empty key") } @@ -1446,35 +1354,28 @@ func handleMgmtSsl(ctx context.Context, handler *http.ServeMux) { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "nginx ssl file ok, key=%vB, crt=%vB, token=%vB", len(key), len(crt), len(token)) + logger.Tf(ctx, "nginx ssl file ok, key=%vB, crt=%vB", len(key), len(crt)) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtLetsEncrypt(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/letsencrypt" 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 domain string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Domain *string `json:"domain"` }{ - Token: &token, Domain: &domain, + Domain: &domain, }); 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 domain = strings.TrimSpace(domain); domain == "" { return errors.New("empty domain") } @@ -1495,33 +1396,19 @@ func handleMgmtLetsEncrypt(ctx context.Context, handler *http.ServeMux) { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "nginx letsencrypt ok, domain=%v, token=%vB", domain, len(token)) + logger.Tf(ctx, "nginx letsencrypt ok, domain=%v", domain) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtCertQuery(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/cert/query" 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") - } - provider, err := rdb.Get(ctx, SRS_HTTPS).Result() if err != nil && err != redis.Nil { return errors.Wrapf(err, "get %v", SRS_HTTPS) @@ -1548,35 +1435,21 @@ func handleMgmtCertQuery(ctx context.Context, handler *http.ServeMux) { }{ Provider: provider, Domain: domain, Key: key, Crt: crt, }) - logger.Tf(ctx, "query cert ok, provider=%v, domain=%v, key=%vB, crt=%vB, token=%vB", - provider, domain, len(key), len(crt), len(token), + logger.Tf(ctx, "query cert ok, provider=%v, domain=%v, key=%vB, crt=%vB", + provider, domain, len(key), len(crt), ) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtStreamsQuery(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/streams/query" 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") - } - streams, err := rdb.HGetAll(ctx, SRS_STREAM_ACTIVE).Result() if err != nil { return errors.Wrapf(err, "hgetall %v", SRS_STREAM_ACTIVE) @@ -1597,12 +1470,12 @@ func handleMgmtStreamsQuery(ctx context.Context, handler *http.ServeMux) { }{ streamObjects, }) - logger.Tf(ctx, "query streams ok, streams=%v, token=%vB", len(streamObjects), len(token)) + logger.Tf(ctx, "query streams ok, streams=%v", len(streamObjects)) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } // See SRS error code ERROR_RTMP_CLIENT_NOT_FOUND @@ -1611,26 +1484,19 @@ const ErrorRtmpClientNotFound = 2049 func handleMgmtStreamsKickoff(ctx context.Context, handler *http.ServeMux) { ep := "/terraform/v1/mgmt/streams/kickoff" 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 vhost, app, stream string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Vhost *string `json:"vhost"` App *string `json:"app"` Stream *string `json:"stream"` }{ - Token: &token, Vhost: &vhost, App: &app, Stream: &stream, + Vhost: &vhost, App: &app, Stream: &stream, }); 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 vhost == "" { return errors.New("no vhost") } @@ -1713,12 +1579,12 @@ func handleMgmtStreamsKickoff(ctx context.Context, handler *http.ServeMux) { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "kickoff stream ok, code=%v, token=%vB", code, len(token)) + logger.Tf(ctx, "kickoff stream ok, code=%v", code) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) } func handleMgmtUI(ctx context.Context, handler *http.ServeMux) { diff --git a/platform/srs-hooks.go b/platform/srs-hooks.go index 6de847cb..5637f057 100644 --- a/platform/srs-hooks.go +++ b/platform/srs-hooks.go @@ -1,8 +1,6 @@ -// // Copyright (c) 2022-2024 Winlin // // SPDX-License-Identifier: MIT -// package main import ( @@ -21,6 +19,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" cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" @@ -200,20 +199,6 @@ func handleHooksService(ctx context.Context, handler *http.ServeMux) error { secretQueryHandler := 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") - } - publish, err := rdb.HGet(ctx, SRS_AUTH_SECRET, "pubSecret").Result() if err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v pubSecret", SRS_AUTH_SECRET) @@ -227,7 +212,7 @@ func handleHooksService(ctx context.Context, handler *http.ServeMux) error { }{ Publish: publish, }) - logger.Tf(ctx, "srs secret ok ok, token=%vB", len(token)) + logger.Tf(ctx, "srs secret ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) @@ -236,31 +221,25 @@ func handleHooksService(ctx context.Context, handler *http.ServeMux) error { ep = "/terraform/v1/hooks/srs/secret" logger.Tf(ctx, "Handle %v", ep) - handler.HandleFunc(ep, secretQueryHandler) + handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(secretQueryHandler))) ep = "/terraform/v1/hooks/srs/secret/query" logger.Tf(ctx, "Handle %v", ep) - handler.HandleFunc(ep, secretQueryHandler) + handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(secretQueryHandler))) ep = "/terraform/v1/hooks/srs/secret/update" 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, secret string + var secret string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Secret *string `json:"secret"` }{ - Token: &token, Secret: &secret, + Secret: &secret, }); 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 secret == "" { return errors.New("no secret") } @@ -273,66 +252,53 @@ func handleHooksService(ctx context.Context, handler *http.ServeMux) error { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "hooks update secret, secret=%vB, token=%vB", len(secret), len(token)) + logger.Tf(ctx, "hooks update secret, secret=%vB", len(secret)) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/hooks/srs/secret/disable" 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 pubNoAuth bool if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - PubNoAuth *bool `json:"pubNoAuth"` + PubNoAuth *bool `json:"pubNoAuth"` }{ - Token: &token, PubNoAuth: &pubNoAuth, + PubNoAuth: &pubNoAuth, }); 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_AUTH_SECRET, "pubNoAuth", fmt.Sprintf("%v", pubNoAuth)).Err(); err != nil { return errors.Wrapf(err, "hset %v pubSecret %v", SRS_AUTH_SECRET, pubNoAuth) } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "hooks disable secret, pubNoAuth=%v, token=%vB", pubNoAuth, len(token)) + logger.Tf(ctx, "hooks disable secret, pubNoAuth=%v", pubNoAuth) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) // See https://console.cloud.tencent.com/cam ep = "/terraform/v1/tencent/cam/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, secretId, secretKey string + var secretId, secretKey string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` SecretID *string `json:"secretId"` SecretKey *string `json:"secretKey"` }{ - Token: &token, SecretID: &secretId, SecretKey: &secretKey, + SecretID: &secretId, SecretKey: &secretKey, }); 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 secretId == "" { return errors.New("no secretId") } @@ -695,12 +661,12 @@ func handleHooksService(ctx context.Context, handler *http.ServeMux) error { } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "CAM: Update ok, %v, token=%vB", sb.String(), len(token)) + logger.Tf(ctx, "CAM: Update ok, %v", sb.String()) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) if err := handleOnHls(ctx, handler); err != nil { return errors.Wrapf(err, "handle hooks") diff --git a/platform/trancode.go b/platform/trancode.go index 2c04877f..ee543ba4 100644 --- a/platform/trancode.go +++ b/platform/trancode.go @@ -1,6 +1,7 @@ // Copyright (c) 2022-2024 Winlin // // SPDX-License-Identifier: MIT +// TODO: rename this file trancode.go -> transcode.go package main import ( @@ -20,6 +21,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" @@ -45,22 +47,8 @@ func NewTranscodeWorker() *TranscodeWorker { func (v *TranscodeWorker) Handle(ctx context.Context, handler *http.ServeMux) error { ep := "/terraform/v1/ffmpeg/transcode/query" 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") - } - var config TranscodeConfig if b, err := rdb.HGet(ctx, SRS_TRANSCODE_CONFIG, "global").Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v global", SRS_TRANSCODE_CONFIG) @@ -71,34 +59,26 @@ func (v *TranscodeWorker) Handle(ctx context.Context, handler *http.ServeMux) er } ohttp.WriteData(ctx, w, r, &config) - logger.Tf(ctx, "transcode query ok, %v, token=%vB", config, len(token)) + logger.Tf(ctx, "transcode query ok, %v", config) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ffmpeg/transcode/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 TranscodeConfig if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` *TranscodeConfig }{ - Token: &token, TranscodeConfig: &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 b, err := json.Marshal(config); err != nil { return errors.Wrapf(err, "marshal conf %v", config) } else if err := rdb.HSet(ctx, SRS_TRANSCODE_CONFIG, "global", string(b)).Err(); err != nil && err != redis.Nil { @@ -110,31 +90,17 @@ func (v *TranscodeWorker) Handle(ctx context.Context, handler *http.ServeMux) er } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "transcode apply ok, %v, token=%vB", config, len(token)) + logger.Tf(ctx, "transcode apply ok, %v", config) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ffmpeg/transcode/task" 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") - } - var config TranscodeConfig if b, err := rdb.HGet(ctx, SRS_TRANSCODE_CONFIG, "global").Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hget %v global", SRS_TRANSCODE_CONFIG) @@ -173,13 +139,13 @@ func (v *TranscodeWorker) Handle(ctx context.Context, handler *http.ServeMux) er } ohttp.WriteData(ctx, w, r, &res) - logger.Tf(ctx, "transcode task ok, %v, pid=%v, input=%v, output=%v, frame=%v, update=%v, token=%vB", - config, pid, input, output, frame, update, len(token)) + logger.Tf(ctx, "transcode task ok, %v, pid=%v, input=%v, output=%v, frame=%v, update=%v", + config, pid, input, output, frame, update) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) return nil } diff --git a/platform/transcript.go b/platform/transcript.go index 51340915..5f233468 100644 --- a/platform/transcript.go +++ b/platform/transcript.go @@ -21,6 +21,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" @@ -61,22 +62,8 @@ func NewTranscriptWorker() *TranscriptWorker { func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) error { ep := "/terraform/v1/ai/transcript/query" 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") - } - config := NewTranscriptConfig() if err := config.Load(ctx); err != nil { return errors.Wrapf(err, "load config") @@ -95,37 +82,29 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e resp.Task.UUID = v.task.UUID ohttp.WriteData(ctx, w, r, resp) - logger.Tf(ctx, "transcript query ok, config=<%v>, uuid=%v, token=%vB", - config, v.task.UUID, len(token)) + logger.Tf(ctx, "transcript query ok, config=<%v>, uuid=%v", + config, v.task.UUID) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/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 uuid string var config TranscriptConfig if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - UUID *string `json:"uuid"` + UUID *string `json:"uuid"` *TranscriptConfig }{ - Token: &token, - UUID: &uuid, TranscriptConfig: &config, + UUID: &uuid, TranscriptConfig: &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") - } - // Not required yet. if uuid != v.task.UUID { logger.Wf(ctx, "transcript ignore uuid mismatch, query=%v, task=%v", uuid, v.task.UUID) @@ -145,35 +124,27 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e ohttp.WriteData(ctx, w, r, &ApplyResponse{ UUID: v.task.UUID, }) - logger.Tf(ctx, "transcript apply ok, config=<%v>, uuid=%v, token=%vB", - config, v.task.UUID, len(token)) + logger.Tf(ctx, "transcript apply ok, config=<%v>, uuid=%v", + config, v.task.UUID) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/check" 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 transcriptConfig TranscriptConfig if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` *TranscriptConfig }{ - Token: &token, TranscriptConfig: &transcriptConfig, }); 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") - } - // Query whisper-1 model detail. var config openai.ClientConfig config = openai.DefaultConfig(transcriptConfig.SecretKey) @@ -206,36 +177,28 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "transcript check ok, config=<%v>, model=<%v>, msg=<%v>, token=%vB", - transcriptConfig, model.ID, resp.Choices[0].Message.Content, len(token)) + logger.Tf(ctx, "transcript check ok, config=<%v>, model=<%v>, msg=<%v>", + transcriptConfig, model.ID, resp.Choices[0].Message.Content) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/clear-subtitle" 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 uuid, tsid string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - UUID *string `json:"uuid"` - TSID *string `json:"tsid"` + UUID *string `json:"uuid"` + TSID *string `json:"tsid"` }{ - Token: &token, - UUID: &uuid, TSID: &tsid, + UUID: &uuid, TSID: &tsid, }); 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 uuid != v.task.UUID { return errors.Errorf("invalid uuid %v", uuid) } @@ -249,34 +212,26 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e } ohttp.WriteData(ctx, w, r, &ClearSubtitleResponse{uuid}) - logger.Tf(ctx, "transcript clear subtitle ok, uuid=%v, token=%vB", uuid, len(token)) + logger.Tf(ctx, "transcript clear subtitle ok, uuid=%v", uuid) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/reset" 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 uuid string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` - UUID *string `json:"uuid"` + UUID *string `json:"uuid"` }{ - Token: &token, - UUID: &uuid, + UUID: &uuid, }); 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 uuid != v.task.UUID { return errors.Errorf("invalid uuid %v", uuid) } @@ -291,31 +246,17 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e ohttp.WriteData(ctx, w, r, &ResetResponse{ UUID: v.task.UUID, }) - logger.Tf(ctx, "transcript reset ok, uuid=%v, new=%v, token=%vB", uuid, v.task.UUID, len(token)) + logger.Tf(ctx, "transcript reset ok, uuid=%v, new=%v", uuid, v.task.UUID) return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/live-queue" 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") - } - type Segment struct { TsID string `json:"tsid"` SeqNo uint64 `json:"seqno"` @@ -343,31 +284,17 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "transcript query live ok, token=%vB", len(token)) + logger.Tf(ctx, "transcript query live ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/asr-queue" 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") - } - type Segment struct { TsID string `json:"tsid"` SeqNo uint64 `json:"seqno"` @@ -403,31 +330,17 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "transcript query asr ok, token=%vB", len(token)) + logger.Tf(ctx, "transcript query asr ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/fix-queue" 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") - } - type AsrSegment struct { Start float64 `json:"start"` End float64 `json:"end"` @@ -470,7 +383,7 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e }) } - res.Segments = append(res.Segments, []*Segment{&Segment{ + res.Segments = append(res.Segments, []*Segment{{ TsID: segment.AudioFile.TsID, SeqNo: segment.AudioFile.SeqNo, URL: segment.AudioFile.File, @@ -494,31 +407,17 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "transcript query fix ok, token=%vB", len(token)) + logger.Tf(ctx, "transcript query fix ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/overlay-queue" 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") - } - type AsrSegment struct { Start float64 `json:"start"` End float64 `json:"end"` @@ -593,12 +492,12 @@ func (v *TranscriptWorker) Handle(ctx context.Context, handler *http.ServeMux) e res.Count = len(res.Segments) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "transcript query overlay ok, token=%vB", len(token)) + logger.Tf(ctx, "transcript query overlay ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ai/transcript/hls/webvtt/" logger.Tf(ctx, "Handle %v", ep) diff --git a/platform/utils.go b/platform/utils.go index c8283a7f..b4e5b617 100644 --- a/platform/utils.go +++ b/platform/utils.go @@ -514,7 +514,7 @@ func InitRdb() error { } // For platform to build token by jwt. -func createToken(ctx context.Context, apiSecret string) (expireAt, createAt time.Time, token string, err error) { +func createToken(apiSecret string) (expireAt, createAt time.Time, token string, err error) { createAt, expireAt = time.Now(), time.Now().Add(365*24*time.Hour) claims := struct { diff --git a/platform/virtual-live-stream.go b/platform/virtual-live-stream.go index f55ad2e3..4f94f945 100644 --- a/platform/virtual-live-stream.go +++ b/platform/virtual-live-stream.go @@ -54,25 +54,19 @@ func (v *VLiveWorker) GetTask(platform string) *VLiveTask { func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error { ep := "/terraform/v1/ffmpeg/vlive/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 VLiveConfigure if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Action *string `json:"action"` *VLiveConfigure }{ - Token: &token, Action: &action, VLiveConfigure: &userConf, + Action: &action, VLiveConfigure: &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 != "" { @@ -127,7 +121,7 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error } ohttp.WriteData(ctx, w, r, nil) - logger.Tf(ctx, "vLive: Update secret ok, token=%vB", len(token)) + logger.Tf(ctx, "vLive: Update secret ok") return nil } else { confObjs := make(map[string]*VLiveConfigure) @@ -144,32 +138,18 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error } ohttp.WriteData(ctx, w, r, confObjs) - logger.Tf(ctx, "vLive: Query configures ok, token=%vB", len(token)) + logger.Tf(ctx, "vLive: Query configures ok") return nil } }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ffmpeg/vlive/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_VLIVE_CONFIG).Result(); err != nil && err != redis.Nil { return errors.Wrapf(err, "hgetall %v", SRS_VLIVE_CONFIG) @@ -213,31 +193,24 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error }) ohttp.WriteData(ctx, w, r, res) - logger.Tf(ctx, "vLive: Query vLive streams ok, token=%vB", len(token)) + logger.Tf(ctx, "vLive: Query vLive streams ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) streamUrlHandler := 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 { @@ -280,32 +253,25 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error ep = "/terraform/v1/ffmpeg/vlive/streamUrl" logger.Tf(ctx, "Handle %v", ep) - handler.HandleFunc(ep, streamUrlHandler) + handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(streamUrlHandler))) ep = "/terraform/v1/ffmpeg/vlive/stream-url" logger.Tf(ctx, "Handle %v", ep) - handler.HandleFunc(ep, streamUrlHandler) + handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(streamUrlHandler))) ep = "/terraform/v1/ffmpeg/vlive/ytdl" 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 qFile string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` YtdlURL *string `json:"url"` }{ - Token: &token, YtdlURL: &qFile, + YtdlURL: &qFile, }); 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 !strings.HasPrefix(qFile, "http") && !strings.HasPrefix(qFile, "https") { return errors.Errorf("invalid url %v", qFile) } @@ -413,28 +379,21 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ffmpeg/vlive/server" 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 qFile string if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` StreamFile *string `json:"file"` }{ - Token: &token, StreamFile: &qFile, + StreamFile: &qFile, }); 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") - } - fileAbsPath, err := filepath.Abs(qFile) if err != nil { return errors.Wrapf(err, "abs %v", qFile) @@ -497,7 +456,7 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) ep = "/terraform/v1/ffmpeg/vlive/upload/" logger.Tf(ctx, "Handle %v", ep) @@ -601,7 +560,7 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error ep = "/terraform/v1/ffmpeg/vlive/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 VLiveTempFile struct { // The file name. @@ -618,23 +577,17 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error Type FFprobeSourceType `json:"type"` } - var token, platform string + var platform string var files []*VLiveTempFile if err := ParseBody(ctx, r.Body, &struct { - Token *string `json:"token"` Platform *string `json:"platform"` Files *[]*VLiveTempFile `json:"files"` }{ - Token: &token, Platform: &platform, Files: &files, + Platform: &platform, Files: &files, }); 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(files) == 0 { return errors.New("no files") } @@ -842,12 +795,12 @@ func (v *VLiveWorker) Handle(ctx context.Context, handler *http.ServeMux) error }{ Platform: platform, Files: parsedFiles, }) - logger.Tf(ctx, "vLive: Update vLive ok, token=%vB", len(token)) + logger.Tf(ctx, "vLive: Update vLive ok") return nil }(); err != nil { ohttp.WriteError(ctx, w, r, err) } - }) + }))) return nil }