From b8f5d71be1577e91bb17d4b3ed3646321b18bbcc Mon Sep 17 00:00:00 2001 From: mountain Date: Wed, 7 Jun 2023 11:56:52 +0900 Subject: [PATCH] =?UTF-8?q?bson,=20json=20=EC=86=8C=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/api.go | 5 ++++ core/service.go | 76 ++++++++++++++++++++++++++++--------------------- core/watch.go | 1 + 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/core/api.go b/core/api.go index 3694615..ff10666 100644 --- a/core/api.go +++ b/core/api.go @@ -465,6 +465,11 @@ func (caller apiCaller) maintenanceAPI(w http.ResponseWriter, r *http.Request) e } } else if r.Method == "POST" { servicename := queryvals.Get("name") + if valid, _ := caller.isValidUser(servicename, "service"); !valid { + logger.Println("maintenanceAPI failed. not vaild user :", r.Method, caller.userinfo) + w.WriteHeader(http.StatusBadRequest) + return nil + } var divs map[string]*Division dec := json.NewDecoder(r.Body) diff --git a/core/service.go b/core/service.go index dc83c0e..a1e2ae4 100644 --- a/core/service.go +++ b/core/service.go @@ -1,6 +1,7 @@ package core import ( + "crypto/md5" "encoding/hex" "encoding/json" "errors" @@ -22,9 +23,9 @@ import ( ) type blockinfo struct { - Start primitive.DateTime + Start primitive.DateTime `bson:"start" json:"start"` End primitive.DateTime `bson:"_ts"` - Reason string + Reason string `bson:"reason" json:"reason"` } type whitelistAuthType = string @@ -35,12 +36,12 @@ const ( ) type whitelistmember struct { - Service string - Email string - Platform string - Desc string - Auth []whitelistAuthType - Expired primitive.DateTime `bson:"_ts,omitempty" json:"_ts,omitempty"` + Service string `bson:"service" json:"service"` + Email string `bson:"email" json:"email"` + Platform string `bson:"platform" json:"platform"` + Desc string `bson:"desc" json:"desc"` + Auth []whitelistAuthType `bson:"auth" json:"auth"` + Expired primitive.DateTime `bson:"_ts,omitempty" json:"_ts,omitempty"` } type whitelist struct { @@ -165,26 +166,30 @@ type Maintenance struct { link string } +type DivisionForUser struct { + Priority int `bson:"priority" json:"priority"` + State DivisionStateName `bson:"state" json:"state"` + Maintenance *Maintenance `bson:",omitempty" json:",omitempty"` +} + type Division struct { - Url string // 요것은 클라이언트 빌드하고 나서 json:"-"으로 변경하자. 클라이언트에 직접 내려보내지 않음 - Priority int - State DivisionStateName - Maintenance *Maintenance `bson:",omitempty" json:",omitempty"` + DivisionForUser `bson:",inline" json:",inline"` + Url string `bson:"url" json:"url"` } type ServiceDescriptionSummary struct { - Id primitive.ObjectID `bson:"_id"` - ServiceName string `bson:"service"` - ServiceCode string `bson:"code"` - UseWhitelist bool `bson:"use_whitelist"` - Closed bool `bson:"closed"` + Id primitive.ObjectID `bson:"_id" json:"_id"` + ServiceName string `bson:"service" json:"service"` + ServiceCode string `bson:"code" json:"code"` + UseWhitelist bool `bson:"use_whitelist" json:"use_whitelist"` + Closed bool `bson:"closed" json:"closed"` } type serviceDescription struct { - ServiceDescriptionSummary `bson:",inline"` - Divisions map[string]*Division `bson:"divisions"` - ServerApiTokens []primitive.ObjectID `bson:"api_tokens"` - ApiUsers map[string][]string `bson:"api_users"` + ServiceDescriptionSummary `bson:",inline" json:",inline"` + Divisions map[string]*Division `bson:"divisions" json:"divisions"` + ServerApiTokens []primitive.ObjectID `bson:"api_tokens" json:"api_tokens"` + ApiUsers map[string][]string `bson:"api_users" json:"api_users"` auths *common.AuthCollection wl whitelist @@ -197,10 +202,11 @@ type serviceDescription struct { updateUserinfo func(info usertokeninfo) (bool, string, string) getProviderInfo func(platform string, uid string) (string, string, error) - apiUsers unsafe.Pointer - divisionsSerialized unsafe.Pointer - serviceSerialized unsafe.Pointer - serviceSummarySerialized unsafe.Pointer + apiUsers unsafe.Pointer + divisionsForUsersSerialized unsafe.Pointer + divisionsSerialized unsafe.Pointer + serviceSerialized unsafe.Pointer + serviceSummarySerialized unsafe.Pointer } func (sh *serviceDescription) readProfile(authtype string, id string, binfo string) (email string, err error) { @@ -240,13 +246,13 @@ func (sh *serviceDescription) prepare(mg *Maingate) error { sh.ServiceCode = hex.EncodeToString(sh.Id[6:]) } - var closed []string + divsForUsers := make(map[string]*DivisionForUser) for dn, div := range divs { if div.State == DivisionState_Closed { - closed = append(closed, dn) continue } + divsForUsers[dn] = &div.DivisionForUser if len(div.State) == 0 { div.State = DivisionState_FullOpen } @@ -262,7 +268,11 @@ func (sh *serviceDescription) prepare(mg *Maingate) error { } else if strings.HasPrefix(div.Maintenance.Notice, "http") { div.Maintenance.link = div.Maintenance.Notice } else { - div.Maintenance.link = path.Join("static", sh.ServiceCode, div.Maintenance.Notice) + hasher := md5.New() + hasher.Write([]byte(sh.ServiceName)) + subfolder := hex.EncodeToString(hasher.Sum(nil))[:8] + + div.Maintenance.link = path.Join("static", subfolder, div.Maintenance.Notice) } } } else { @@ -270,14 +280,14 @@ func (sh *serviceDescription) prepare(mg *Maingate) error { } } - for _, dn := range closed { - delete(divs, dn) - } - divmarshaled, _ := json.Marshal(divs) devstr := string(divmarshaled) sh.divisionsSerialized = unsafe.Pointer(&devstr) + divmarshaled2, _ := json.Marshal(divsForUsers) + devstr2 := string(divmarshaled2) + sh.divisionsForUsersSerialized = unsafe.Pointer(&devstr2) + sh.mongoClient = mg.mongoClient sh.auths = mg.auths sh.sessionTTL = time.Duration(mg.SessionTTL * int64(time.Second)) @@ -764,7 +774,7 @@ func (sh *serviceDescription) ServeHTTP(w http.ResponseWriter, r *http.Request) } } } else { - divstrptr := atomic.LoadPointer(&sh.divisionsSerialized) + divstrptr := atomic.LoadPointer(&sh.divisionsForUsersSerialized) divstr := *(*string)(divstrptr) w.Write([]byte(divstr)) } diff --git a/core/watch.go b/core/watch.go index 99c9c83..b27ac07 100644 --- a/core/watch.go +++ b/core/watch.go @@ -283,6 +283,7 @@ func (mg *Maingate) watchServiceCollection(parentctx context.Context, serveMux * case "update": data.Service.prepare(mg) if old := mg.services.get(data.Service.ServiceName); old != nil { + atomic.SwapPointer(&old.divisionsForUsersSerialized, data.Service.divisionsForUsersSerialized) atomic.SwapPointer(&old.divisionsSerialized, data.Service.divisionsSerialized) atomic.SwapPointer(&old.apiUsers, data.Service.apiUsers) atomic.SwapPointer(&old.serviceSerialized, data.Service.serviceSerialized)