api server support sync workspace (#340)

This commit is contained in:
RoyUP9 2021-10-11 13:09:23 +03:00 committed by GitHub
parent ba6b5c868c
commit da846da334
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 14 deletions

View File

@ -10,6 +10,7 @@ import (
"mizuserver/pkg/utils" "mizuserver/pkg/utils"
"mizuserver/pkg/validation" "mizuserver/pkg/validation"
"net/http" "net/http"
"regexp"
"time" "time"
"github.com/google/martian/har" "github.com/google/martian/har"
@ -72,23 +73,46 @@ func SyncEntries(c *gin.Context) {
c.JSON(http.StatusBadRequest, err) c.JSON(http.StatusBadRequest, err)
return return
} }
if err := validation.Validate(syncParams); err != nil { if err := validation.Validate(syncParams); err != nil {
c.JSON(http.StatusBadRequest, err) c.JSON(http.StatusBadRequest, err)
return return
} }
if up9.GetAnalyzeInfo().IsAnalyzing { if up9.GetAnalyzeInfo().IsAnalyzing {
c.String(http.StatusBadRequest, "Cannot analyze, mizu is already analyzing") c.String(http.StatusBadRequest, "Cannot analyze, mizu is already analyzing")
return return
} }
var (
token, model string
guestMode bool
)
if syncParams.Token == "" {
rlog.Infof("Sync entries - creating token. env %s\n", syncParams.Env) rlog.Infof("Sync entries - creating token. env %s\n", syncParams.Env)
token, err := up9.CreateAnonymousToken(syncParams.Env) guestToken, err := up9.CreateAnonymousToken(syncParams.Env)
if err != nil { if err != nil {
c.String(http.StatusServiceUnavailable, "Cannot analyze, mizu is already analyzing") c.String(http.StatusServiceUnavailable, "Failed creating anonymous token")
return return
} }
rlog.Infof("Sync entries - syncing. token: %s model: %s\n", token.Token, token.Model)
go up9.SyncEntriesImpl(token.Token, token.Model, syncParams.Env, syncParams.SleepIntervalSec) token = guestToken.Token
model = guestToken.Model
guestMode = true
} else {
token = fmt.Sprintf("bearer %s", syncParams.Token)
model = syncParams.Workspace
guestMode = false
}
modelRegex, _ := regexp.Compile("[A-Za-z0-9][-A-Za-z0-9_.]*[A-Za-z0-9]+$")
if len(model) > 63 || !modelRegex.MatchString(model) {
c.String(http.StatusBadRequest, "Invalid model name")
return
}
rlog.Infof("Sync entries - syncing. token: %s, model: %s, guest mode: %v\n", token, model, guestMode)
go up9.SyncEntriesImpl(token, model, syncParams.Env, syncParams.UploadIntervalSec, guestMode)
c.String(http.StatusOK, "OK") c.String(http.StatusOK, "OK")
} }

View File

@ -23,8 +23,10 @@ type EntriesFilter struct {
} }
type SyncEntriesRequestQuery struct { type SyncEntriesRequestQuery struct {
Token string `form:"token"`
Env string `form:"env"` Env string `form:"env"`
SleepIntervalSec int `form:"interval"` Workspace string `form:"workspace"`
UploadIntervalSec int `form:"interval"`
} }
type HarFetchRequestQuery struct { type HarFetchRequestQuery struct {

View File

@ -59,14 +59,16 @@ func GetRemoteUrl(analyzeDestination string, analyzeToken string) string {
return fmt.Sprintf("https://%s/share/%s", analyzeDestination, analyzeToken) return fmt.Sprintf("https://%s/share/%s", analyzeDestination, analyzeToken)
} }
func CheckIfModelReady(analyzeDestination string, analyzeModel string, analyzeToken string) bool { func CheckIfModelReady(analyzeDestination string, analyzeModel string, analyzeToken string, guestMode bool) bool {
statusUrl, _ := url.Parse(fmt.Sprintf("https://trcc.%s/models/%s/status", analyzeDestination, analyzeModel)) statusUrl, _ := url.Parse(fmt.Sprintf("https://trcc.%s/models/%s/status", analyzeDestination, analyzeModel))
authHeader := getAuthHeader(guestMode)
req := &http.Request{ req := &http.Request{
Method: http.MethodGet, Method: http.MethodGet,
URL: statusUrl, URL: statusUrl,
Header: map[string][]string{ Header: map[string][]string{
"Content-Type": {"application/json"}, "Content-Type": {"application/json"},
"Guest-Auth": {analyzeToken}, authHeader: {analyzeToken},
}, },
} }
statusResp, err := http.DefaultClient.Do(req) statusResp, err := http.DefaultClient.Do(req)
@ -81,6 +83,14 @@ func CheckIfModelReady(analyzeDestination string, analyzeModel string, analyzeTo
return target.LastMajorGeneration > 0 return target.LastMajorGeneration > 0
} }
func getAuthHeader(guestMode bool) string {
if guestMode {
return "Guest-Auth"
}
return "Authorization"
}
func GetTrafficDumpUrl(analyzeDestination string, analyzeModel string) *url.URL { func GetTrafficDumpUrl(analyzeDestination string, analyzeModel string) *url.URL {
strUrl := fmt.Sprintf("https://traffic.%s/dumpTrafficBulk/%s", analyzeDestination, analyzeModel) strUrl := fmt.Sprintf("https://traffic.%s/dumpTrafficBulk/%s", analyzeDestination, analyzeModel)
if strings.HasPrefix(analyzeDestination, "http") { if strings.HasPrefix(analyzeDestination, "http") {
@ -92,6 +102,7 @@ func GetTrafficDumpUrl(analyzeDestination string, analyzeModel string) *url.URL
type AnalyzeInformation struct { type AnalyzeInformation struct {
IsAnalyzing bool IsAnalyzing bool
GuestMode bool
SentCount int SentCount int
AnalyzedModel string AnalyzedModel string
AnalyzeToken string AnalyzeToken string
@ -100,6 +111,7 @@ type AnalyzeInformation struct {
func (info *AnalyzeInformation) Reset() { func (info *AnalyzeInformation) Reset() {
info.IsAnalyzing = false info.IsAnalyzing = false
info.GuestMode = true
info.AnalyzedModel = "" info.AnalyzedModel = ""
info.AnalyzeToken = "" info.AnalyzeToken = ""
info.AnalyzeDestination = "" info.AnalyzeDestination = ""
@ -112,19 +124,20 @@ func GetAnalyzeInfo() *shared.AnalyzeStatus {
return &shared.AnalyzeStatus{ return &shared.AnalyzeStatus{
IsAnalyzing: analyzeInformation.IsAnalyzing, IsAnalyzing: analyzeInformation.IsAnalyzing,
RemoteUrl: GetRemoteUrl(analyzeInformation.AnalyzeDestination, analyzeInformation.AnalyzeToken), RemoteUrl: GetRemoteUrl(analyzeInformation.AnalyzeDestination, analyzeInformation.AnalyzeToken),
IsRemoteReady: CheckIfModelReady(analyzeInformation.AnalyzeDestination, analyzeInformation.AnalyzedModel, analyzeInformation.AnalyzeToken), IsRemoteReady: CheckIfModelReady(analyzeInformation.AnalyzeDestination, analyzeInformation.AnalyzedModel, analyzeInformation.AnalyzeToken, analyzeInformation.GuestMode),
SentCount: analyzeInformation.SentCount, SentCount: analyzeInformation.SentCount,
} }
} }
func SyncEntriesImpl(token string, model string, envPrefix string, sleepIntervalSec int) { func SyncEntriesImpl(token string, model string, envPrefix string, uploadIntervalSec int, guestMode bool) {
analyzeInformation.IsAnalyzing = true analyzeInformation.IsAnalyzing = true
analyzeInformation.GuestMode = guestMode
analyzeInformation.AnalyzedModel = model analyzeInformation.AnalyzedModel = model
analyzeInformation.AnalyzeToken = token analyzeInformation.AnalyzeToken = token
analyzeInformation.AnalyzeDestination = envPrefix analyzeInformation.AnalyzeDestination = envPrefix
analyzeInformation.SentCount = 0 analyzeInformation.SentCount = 0
sleepTime := time.Second * time.Duration(sleepIntervalSec) sleepTime := time.Second * time.Duration(uploadIntervalSec)
var timestampFrom int64 = 0 var timestampFrom int64 = 0
@ -170,13 +183,14 @@ func SyncEntriesImpl(token string, model string, envPrefix string, sleepInterval
_ = w.Close() _ = w.Close()
reqBody := ioutil.NopCloser(bytes.NewReader(in.Bytes())) reqBody := ioutil.NopCloser(bytes.NewReader(in.Bytes()))
authHeader := getAuthHeader(guestMode)
req := &http.Request{ req := &http.Request{
Method: http.MethodPost, Method: http.MethodPost,
URL: GetTrafficDumpUrl(envPrefix, model), URL: GetTrafficDumpUrl(envPrefix, model),
Header: map[string][]string{ Header: map[string][]string{
"Content-Encoding": {"deflate"}, "Content-Encoding": {"deflate"},
"Content-Type": {"application/octet-stream"}, "Content-Type": {"application/octet-stream"},
"Guest-Auth": {token}, authHeader: {token},
}, },
Body: reqBody, Body: reqBody,
} }