Files
maingate/core/api.go

582 lines
14 KiB
Go
Raw Permalink Normal View History

2023-05-24 12:16:03 +09:00
package core
import (
"bytes"
"encoding/binary"
2023-05-24 12:16:03 +09:00
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"strconv"
2023-05-24 12:16:03 +09:00
"strings"
"sync/atomic"
"time"
"unsafe"
"repositories.action2quare.com/ayo/gocommon"
2023-05-24 15:31:01 +09:00
"repositories.action2quare.com/ayo/gocommon/logger"
2023-05-24 12:16:03 +09:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
type FileDocumentDesc struct {
2023-06-07 13:57:37 +09:00
Key string `bson:"key" json:"key"`
Src string `bson:"src" json:"src"`
Link string `bson:"link" json:"link"`
Desc string `bson:"desc" json:"desc"`
Extract bool `bson:"extract" json:"extract"`
Timestamp int64 `bson:"timestamp" json:"timestamp"`
Contents []byte `bson:"contents,omitempty" json:"contents,omitempty"`
}
2023-06-27 19:13:10 +09:00
func (fd *FileDocumentDesc) Save() error {
// 새 파일 올라옴
if len(fd.Contents) == 0 {
return nil
}
var destFile string
if fd.Extract {
os.MkdirAll(fd.Link, os.ModePerm)
destFile = path.Join(fd.Link, fd.Src)
} else {
os.MkdirAll(path.Dir(fd.Link), os.ModePerm)
destFile = fd.Link
}
f, err := os.Create(destFile)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, bytes.NewBuffer(fd.Contents))
if err != nil {
return err
}
if fd.Extract {
switch path.Ext(destFile) {
case ".zip":
err = gocommon.Unzip(destFile)
case ".tar":
err = gocommon.Untar(destFile)
}
}
return err
}
func (caller apiCaller) filesAPI(w http.ResponseWriter, r *http.Request) error {
if r.Method == "GET" {
2023-06-20 11:07:53 +09:00
allfiles, err := caller.mg.mongoClient.All(CollectionFile, options.Find().SetProjection(bson.M{
"contents": 0,
2023-06-20 11:07:53 +09:00
}).SetReturnKey(false))
if err != nil {
return err
}
2023-06-20 11:07:53 +09:00
if len(allfiles) > 0 {
enc := json.NewEncoder(w)
2023-06-20 11:07:53 +09:00
return enc.Encode(allfiles)
}
} else if r.Method == "DELETE" {
key := r.FormValue("key")
2023-06-20 11:07:53 +09:00
if len(key) == 0 {
w.WriteHeader(http.StatusBadRequest)
return nil
}
_, err := caller.mg.mongoClient.Delete(CollectionFile, bson.M{
2023-06-20 11:07:53 +09:00
"key": key,
})
if err != nil {
return err
}
}
return nil
}
var seq = uint32(0)
func (caller apiCaller) uploadAPI(w http.ResponseWriter, r *http.Request) error {
if r.Method == "PUT" {
infile, header, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return err
}
defer infile.Close()
desc := r.FormValue("desc")
contents, _ := io.ReadAll(infile)
extractstr := r.FormValue("extract")
extract, _ := strconv.ParseBool(extractstr)
var b [5]byte
binary.BigEndian.PutUint32(b[0:4], uint32(time.Now().Unix()))
b[4] = byte(atomic.AddUint32(&seq, 1) % 255)
2023-08-22 18:06:14 +09:00
newidobj := primitive.NewObjectID()
2023-08-22 18:06:14 +09:00
copy(newidobj[:], b[1:])
2023-08-22 18:06:14 +09:00
rf := newidobj.Hex()
var link string
if extract {
2023-08-22 18:06:14 +09:00
link = path.Join("static", rf)
} else {
2023-08-22 18:06:14 +09:00
link = path.Join("static", rf, header.Filename)
}
newdoc := FileDocumentDesc{
Contents: contents,
Src: header.Filename,
Timestamp: time.Now().UTC().Unix(),
Extract: extract,
Link: link,
Desc: desc,
Key: rf,
}
_, _, err = caller.mg.mongoClient.UpsertOne(CollectionFile, bson.M{
2023-08-21 11:01:21 +09:00
"_id": newidobj,
"key": rf,
}, newdoc)
if err == nil {
newdoc.Contents = nil
enc := json.NewEncoder(w)
enc.Encode(newdoc)
}
return err
}
return nil
2023-05-24 15:31:01 +09:00
}
2023-08-22 10:16:09 +09:00
func (caller apiCaller) blockAPI(w http.ResponseWriter, r *http.Request) error {
2023-05-24 12:16:03 +09:00
mg := caller.mg
2023-11-29 17:36:25 +09:00
logger.Println("blockAPI :", r.Method)
switch r.Method {
case "GET":
2023-09-25 12:29:26 +09:00
target, ok := gocommon.ReadObjectIDFormValue(r.Form, "accid")
if !ok {
2023-11-29 17:36:25 +09:00
// 페이지네이션 해야할 듯
//json.NewEncoder(w).Encode(mg.bl.all())
2023-09-25 12:29:26 +09:00
} else if !target.IsZero() {
var blocked []blockinfo
if err := caller.mg.mongoClient.FindAllAs(CollectionBlock, bson.M{
"accid": target,
}, &blocked); err == nil {
2023-09-25 12:29:26 +09:00
json.NewEncoder(w).Encode(blocked)
}
}
case "PUT":
2023-11-29 17:36:25 +09:00
var targets struct {
Start primitive.DateTime
End primitive.DateTime
Accounts map[primitive.ObjectID]primitive.M // accid->meta
2023-08-23 17:48:47 +09:00
}
2023-11-29 17:36:25 +09:00
if err := gocommon.MakeDecoder(r).Decode(&targets); err != nil {
2023-08-22 10:16:09 +09:00
return err
}
2023-11-29 17:36:25 +09:00
for accid, meta := range targets.Accounts {
bi := blockinfo{
Start: targets.Start,
End: targets.End,
Meta: meta,
}
2023-05-24 12:16:03 +09:00
_, err := mg.mongoClient.Collection(CollectionBlock).InsertOne(r.Context(), bi)
2023-11-29 17:36:25 +09:00
if err != nil {
logger.Println("account is not blocked. err :", err)
} else {
logger.Println("account is blocked :", meta)
2024-08-22 11:26:13 +09:00
mg.sessionProvider.RevokeAll(accid)
2023-11-29 17:36:25 +09:00
}
2023-06-20 15:49:50 +09:00
}
case "DELETE":
2023-08-22 10:16:09 +09:00
id := r.URL.Query().Get("id")
2023-05-24 12:16:03 +09:00
2023-08-22 10:16:09 +09:00
if len(id) == 0 {
return errors.New("id param is missing")
}
idobj, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
2023-05-24 12:16:03 +09:00
}
2023-08-22 10:16:09 +09:00
mg.mongoClient.Delete(CollectionBlock, bson.M{"_id": idobj})
2023-08-22 10:16:09 +09:00
}
return nil
}
func (caller apiCaller) whitelistAPI(w http.ResponseWriter, r *http.Request) error {
mg := caller.mg
switch r.Method {
case "GET":
var all []whitelistmember
if err := mg.mongoClient.AllAs(CollectionWhitelist, &all); err == nil {
enc := json.NewEncoder(w)
enc.Encode(all)
}
case "PUT":
2023-05-24 12:16:03 +09:00
body, _ := io.ReadAll(r.Body)
var member whitelistmember
if err := json.Unmarshal(body, &member); err != nil {
return err
}
2023-08-22 10:16:09 +09:00
member.ExpiredAt = 0
member.Id = primitive.NewObjectID()
2023-05-24 12:16:03 +09:00
_, _, err := mg.mongoClient.Update(CollectionWhitelist, bson.M{
"_id": member.Id,
2023-05-24 12:16:03 +09:00
}, bson.M{
"$set": &member,
}, options.Update().SetUpsert(true))
if err != nil {
return err
}
case "DELETE":
id := r.URL.Query().Get("id")
2023-05-24 12:16:03 +09:00
if len(id) == 0 {
return errors.New("id param is missing")
}
idobj, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
_, _, err = mg.mongoClient.Update(CollectionWhitelist, bson.M{
"_id": idobj,
}, bson.M{
"$currentDate": bson.M{
"_ts": bson.M{"$type": "date"},
},
}, options.Update().SetUpsert(false))
if err != nil {
return err
}
}
return nil
}
func (caller apiCaller) serviceAPI(w http.ResponseWriter, r *http.Request) error {
mg := caller.mg
if r.Method == "GET" {
2023-08-31 17:24:21 +09:00
logger.Println("serviceAPI :", r.URL.Path)
2023-06-20 11:07:53 +09:00
if mg.service().Id.IsZero() {
2023-08-31 17:24:21 +09:00
logger.Println(" id is zero")
2023-06-20 11:07:53 +09:00
newService := serviceDescription{
2024-03-29 09:06:38 +09:00
Id: primitive.NewObjectID(),
2023-05-24 12:16:03 +09:00
}
2023-06-20 11:07:53 +09:00
if err := newService.prepare(caller.mg); err != nil {
2023-08-31 17:24:21 +09:00
logger.Println(" prepare failed :", err)
2023-06-20 11:07:53 +09:00
return err
}
atomic.StorePointer(&mg.serviceptr, unsafe.Pointer(&newService))
2023-05-24 12:16:03 +09:00
}
2023-06-20 11:07:53 +09:00
2023-08-22 11:10:59 +09:00
w.Write(mg.service().serviceSerialized)
2023-05-24 12:16:03 +09:00
} else if r.Method == "POST" {
body, _ := io.ReadAll(r.Body)
var service serviceDescription
if err := json.Unmarshal(body, &service); err != nil {
return err
}
if len(service.ServerApiTokens) == 0 {
service.ServerApiTokens = []primitive.ObjectID{
primitive.NewObjectIDFromTimestamp(time.Now().Add(-time.Hour * 24 * 30 * 465)),
}
}
2023-05-24 12:16:03 +09:00
filter := bson.M{"_id": service.Id}
success, _, err := mg.mongoClient.Update(CollectionService, filter, bson.M{
"$set": &service,
}, options.Update().SetUpsert(true))
if err != nil {
return err
}
if !success {
logger.Println("serviceAPI failed. not vaild user :", caller.userinfo)
w.WriteHeader(http.StatusBadRequest)
}
}
return nil
}
func (caller apiCaller) maintenanceAPI(w http.ResponseWriter, r *http.Request) error {
mg := caller.mg
if r.Method == "GET" {
2023-08-16 21:44:19 +09:00
w.Write(mg.service().divisionsSerialized)
} else if r.Method == "POST" {
2023-06-05 18:01:09 +09:00
var divs map[string]*Division
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&divs); err != nil {
w.WriteHeader(http.StatusBadRequest)
return err
}
_, _, err := mg.mongoClient.Update(CollectionService, bson.M{
"_id": mg.service().Id,
}, bson.M{
"$set": bson.M{"divisions": divs},
}, options.Update().SetUpsert(false))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
}
}
return nil
}
2023-08-25 12:31:32 +09:00
func (caller apiCaller) couponAPI(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "PUT":
// 쿠폰 생성
logger.Println("begin generateCoupons")
generateCoupons(caller.mg.mongoClient, w, r)
case "POST":
// TODO : 쿠폰 사용
// 쿠폰 사용 표시 해주고 내용을 응답
logger.Println("begin useCoupon")
useCoupon(caller.mg.mongoClient, w, r)
case "GET":
// 쿠폰 조회
if r.Form.Has("code") {
// 쿠폰 코드 조회
logger.Println("begin queryCoupon")
queryCoupon(caller.mg.mongoClient, w, r)
} else if r.Form.Has("name") {
// 쿠폰 코드 다운
logger.Println("begin downloadCoupons")
downloadCoupons(caller.mg.mongoClient, w, r)
} else {
// 쿠폰 이름 목록
logger.Println("begin listAllCouponNames")
listAllCouponNames(caller.mg.mongoClient, w, r)
}
}
return nil
}
type accountlinkinfo struct {
Uid string `json:"uid"`
Platform string `json:"platform"`
}
func (caller apiCaller) userinfoAPI(w http.ResponseWriter, r *http.Request) error {
mg := caller.mg
if r.Method == "GET" {
// 계정 조회
accid, _ := gocommon.ReadObjectIDFormValue(r.Form, "accid")
2024-07-31 16:30:25 +09:00
if len(accid) > 0 {
all, err := mg.mongoClient.FindAll(CollectionAccount, bson.M{
"accid": accid,
}, options.Find().SetProjection(bson.M{"_id": 1, "accid": 1}))
2024-07-31 16:30:25 +09:00
if err != nil {
return err
}
2024-07-31 16:30:25 +09:00
var linkinfos []accountlinkinfo
for _, doc := range all {
id := doc["_id"].(primitive.ObjectID)
2024-07-31 16:30:25 +09:00
link, err := mg.mongoClient.FindOne(CollectionLink, bson.M{
"_id": id,
}, options.FindOne().SetProjection(bson.M{"_id": 1, "platform": 1, "uid": 1}))
2024-07-31 16:30:25 +09:00
if err != nil {
logger.Error("link failed. FindOneAndUpdate link err:", err)
w.WriteHeader(http.StatusInternalServerError)
return err
}
2024-07-31 16:30:25 +09:00
var info accountlinkinfo
info.Platform = link["platform"].(string)
info.Uid = link["uid"].(string)
linkinfos = append(linkinfos, info)
}
2024-07-31 16:30:25 +09:00
enc := json.NewEncoder(w)
enc.Encode(linkinfos)
}
} else if r.Method == "POST" {
r.ParseMultipartForm(32 << 20)
var body struct {
Platform string
Uid []string
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
return err
}
2024-07-31 16:30:25 +09:00
if len(body.Platform) > 0 && len(body.Uid) > 0 {
output := make(map[string]any)
for _, uid := range body.Uid {
link, err := mg.mongoClient.FindOne(CollectionLink, bson.M{
"platform": body.Platform,
"uid": uid,
}, options.FindOne().SetProjection(bson.M{"_id": 1}))
if err != nil {
return err
}
output[uid] = link["_id"]
}
json.NewEncoder(w).Encode(output)
}
}
return nil
}
2023-05-24 12:16:03 +09:00
var errApiTokenMissing = errors.New("mg-x-api-token is missing")
func (caller apiCaller) configAPI(w http.ResponseWriter, r *http.Request) error {
mg := caller.mg
2023-06-23 17:58:41 +09:00
if !*devflag {
apitoken := r.Header.Get("MG-X-API-TOKEN")
if len(apitoken) == 0 {
return errApiTokenMissing
}
apitokenObj, _ := primitive.ObjectIDFromHex(apitoken)
if !mg.service().isValidToken(apitokenObj) {
return fmt.Errorf("mg-x-api-token is not valid : %s", apitoken)
}
2023-05-24 12:16:03 +09:00
}
return nil
}
type apiCaller struct {
userinfo map[string]any
globalAdmins map[string]bool
mg *Maingate
apiToken primitive.ObjectID
2023-05-24 12:16:03 +09:00
}
func (mg *Maingate) api(w http.ResponseWriter, r *http.Request) {
defer func() {
s := recover()
if s != nil {
logger.Error(s)
}
}()
defer func() {
io.Copy(io.Discard, r.Body)
r.Body.Close()
}()
r.ParseMultipartForm(32 << 20)
2023-05-24 12:16:03 +09:00
var userinfo map[string]any
2023-06-23 17:58:41 +09:00
if !*devflag {
2023-05-24 12:16:03 +09:00
authheader := r.Header.Get("Authorization")
if len(authheader) == 0 {
logger.Println("Authorization header is not valid :", authheader)
w.WriteHeader(http.StatusBadRequest)
return
}
req, _ := http.NewRequest("GET", "https://graph.microsoft.com/oidc/userinfo", nil)
req.Header.Add("Authorization", authheader)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logger.Println("graph microsoft api call failed :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
if err = json.Unmarshal(raw, &userinfo); err != nil {
return
}
if _, expired := userinfo["error"]; expired {
w.WriteHeader(http.StatusUnauthorized)
return
}
}
ptr := atomic.LoadPointer(&mg.admins)
adminsptr := (*globalAdmins)(ptr)
if adminsptr.modtime != gocommon.ConfigModTime() {
2023-05-24 12:16:03 +09:00
var config globalAdmins
if err := gocommon.LoadConfig(&config); err == nil {
2023-05-24 12:16:03 +09:00
config.parse()
adminsptr = &config
atomic.StorePointer(&mg.admins, unsafe.Pointer(adminsptr))
}
}
var apiTokenObj primitive.ObjectID
2023-06-23 17:58:41 +09:00
if !*devflag {
apiToken := r.Header.Get("MG-X-API-TOKEN")
if len(apiToken) > 0 {
obj, err := primitive.ObjectIDFromHex(apiToken)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
apiTokenObj = obj
}
}
2023-05-24 12:16:03 +09:00
logger.Println("api call :", r.URL.Path, r.Method, r.URL.Query(), userinfo)
caller := apiCaller{
userinfo: userinfo,
globalAdmins: adminsptr.emails,
mg: mg,
apiToken: apiTokenObj,
2023-05-24 12:16:03 +09:00
}
var err error
if strings.HasSuffix(r.URL.Path, "/service") {
err = caller.serviceAPI(w, r)
} else if strings.HasSuffix(r.URL.Path, "/whitelist") {
err = caller.whitelistAPI(w, r)
} else if strings.HasSuffix(r.URL.Path, "/config") {
err = caller.configAPI(w, r)
2023-05-24 15:31:01 +09:00
} else if strings.HasSuffix(r.URL.Path, "/upload") {
err = caller.uploadAPI(w, r)
} else if strings.HasSuffix(r.URL.Path, "/maintenance") {
err = caller.maintenanceAPI(w, r)
} else if strings.HasSuffix(r.URL.Path, "/files") {
err = caller.filesAPI(w, r)
2023-08-22 10:16:09 +09:00
} else if strings.HasSuffix(r.URL.Path, "/block") {
err = caller.blockAPI(w, r)
2023-08-25 12:31:32 +09:00
} else if strings.HasSuffix(r.URL.Path, "/coupon") {
err = caller.couponAPI(w, r)
} else if strings.HasSuffix(r.URL.Path, "/userinfo") {
err = caller.userinfoAPI(w, r)
2023-05-24 12:16:03 +09:00
}
if err != nil {
logger.Error(err)
}
}