From 91790330d21c1cea287615fb0896df5614cf179c Mon Sep 17 00:00:00 2001 From: mountain Date: Mon, 19 Jun 2023 14:31:34 +0900 Subject: [PATCH] =?UTF-8?q?=ED=99=94=EC=9D=B4=ED=8A=B8=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=A9=A4=EB=B2=84=EC=97=90=20tag=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/api.go | 10 +++++++- core/service.go | 63 +++++++++++++++---------------------------------- 2 files changed, 28 insertions(+), 45 deletions(-) diff --git a/core/api.go b/core/api.go index 116ae3b..587249d 100644 --- a/core/api.go +++ b/core/api.go @@ -326,7 +326,13 @@ func (caller apiCaller) whitelistAPI(w http.ResponseWriter, r *http.Request) err } if len(all) > 0 { - allraw, _ := json.Marshal(all) + var notexp []primitive.M + for _, v := range all { + if _, exp := v["_ts"]; !exp { + notexp = append(notexp, v) + } + } + allraw, _ := json.Marshal(notexp) w.Write(allraw) } } else { @@ -345,6 +351,8 @@ func (caller apiCaller) whitelistAPI(w http.ResponseWriter, r *http.Request) err } member.Expired = 0 + // 테스트 + member.Tag = whitelistMemberTag_QA _, _, err := mg.mongoClient.Update(CollectionWhitelist, bson.M{ "_id": primitive.NewObjectID(), diff --git a/core/service.go b/core/service.go index 2a1c017..0439264 100644 --- a/core/service.go +++ b/core/service.go @@ -28,25 +28,24 @@ type blockinfo struct { Reason string `bson:"reason" json:"reason"` } -type whitelistAuthType = string +type whitelistMemberTag = string const ( - whitelistAuthType_Default = whitelistAuthType("") - whitelistAuthType_QA = whitelistAuthType("qa") + whitelistMemberTag_Default = whitelistMemberTag("") + whitelistMemberTag_QA = whitelistMemberTag("#qa") ) type whitelistmember struct { - 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"` + Service string `bson:"service" json:"service"` + Email string `bson:"email" json:"email"` + Platform string `bson:"platform" json:"platform"` + Desc string `bson:"desc" json:"desc"` + Tag string `bson:"tag" json:"tag"` + Expired primitive.DateTime `bson:"_ts,omitempty" json:"_ts,omitempty"` } type whitelist struct { emailptr unsafe.Pointer - qaptr unsafe.Pointer working int32 } @@ -61,29 +60,11 @@ type usertokeninfo struct { } func (wl *whitelist) init(total []whitelistmember) { - auths := make(map[string]map[string]*whitelistmember) + all := make(map[string]*whitelistmember) for _, member := range total { - all := auths[""] - if all == nil { - all = make(map[string]*whitelistmember) - auths[""] = all - } all[whitelistKey(member.Email)] = &member - - for _, auth := range member.Auth { - spec := auths[auth] - if spec == nil { - spec = make(map[string]*whitelistmember) - auths[auth] = spec - } - spec[whitelistKey(member.Email)] = &member - } } - all := auths[whitelistAuthType_Default] atomic.StorePointer(&wl.emailptr, unsafe.Pointer(&all)) - - qa := auths[whitelistAuthType_QA] - atomic.StorePointer(&wl.qaptr, unsafe.Pointer(&qa)) } func addToUnsafePointer(to *unsafe.Pointer, m *whitelistmember) { @@ -111,17 +92,13 @@ func removeFromUnsafePointer(from *unsafe.Pointer, email string) { } func (wl *whitelist) add(m *whitelistmember) { + // 테스트 + m.Tag = whitelistMemberTag_QA addToUnsafePointer(&wl.emailptr, m) - for _, auth := range m.Auth { - if auth == whitelistAuthType_QA { - addToUnsafePointer(&wl.qaptr, m) - } - } } func (wl *whitelist) remove(email string) { removeFromUnsafePointer(&wl.emailptr, email) - removeFromUnsafePointer(&wl.qaptr, email) } func (wl *whitelist) isMember(email string, platform string) bool { @@ -138,14 +115,12 @@ func (wl *whitelist) isMember(email string, platform string) bool { return false } -func (wl *whitelist) hasAuth(email string, platform string, auth whitelistAuthType) bool { - if auth == whitelistAuthType_QA { - ptr := atomic.LoadPointer(&wl.qaptr) - src := *(*map[string]*whitelistmember)(ptr) +func (wl *whitelist) hasTag(email string, platform string, tag whitelistMemberTag) bool { + ptr := atomic.LoadPointer(&wl.emailptr) + src := *(*map[string]*whitelistmember)(ptr) - if member, exists := src[whitelistKey(email)]; exists { - return member.Platform == platform - } + if member, exists := src[whitelistKey(email)]; exists { + return strings.Contains(member.Tag, tag) } return false @@ -732,7 +707,7 @@ func (sh *serviceDescription) ServeHTTP(w http.ResponseWriter, r *http.Request) // 세션키가 있는지 확인 if _, ok := sh.auths.IsValid(sk, ""); !ok { logger.Println("sessionkey is not valid :", sk) - w.WriteHeader(http.StatusBadRequest) + w.WriteHeader(http.StatusUnauthorized) return } @@ -754,7 +729,7 @@ func (sh *serviceDescription) ServeHTTP(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusBadRequest) return } - if sh.wl.hasAuth(cell.ToAuthinfo().Email, cell.ToAuthinfo().Platform, whitelistAuthType_QA) { + if sh.wl.hasTag(cell.ToAuthinfo().Email, cell.ToAuthinfo().Platform, whitelistMemberTag_QA) { // qa 권한이면 입장 가능 w.Write([]byte(fmt.Sprintf(`{"service":"%s"}`, div.Url))) } else if div.Maintenance != nil {