feat (backend-http): enable ios endpoints
This commit is contained in:
parent
e3d8807cc7
commit
5523df8fb2
3 changed files with 192 additions and 161 deletions
|
|
@ -14,8 +14,8 @@ import (
|
|||
gzip "github.com/klauspost/pgzip"
|
||||
|
||||
"openreplay/backend/pkg/db/postgres"
|
||||
. "openreplay/backend/pkg/messages"
|
||||
"openreplay/backend/pkg/token"
|
||||
. "openreplay/backend/pkg/messages"
|
||||
)
|
||||
|
||||
const JSON_SIZE_LIMIT int64 = 1e3 // 1Kb
|
||||
|
|
|
|||
|
|
@ -1,145 +1,176 @@
|
|||
package main
|
||||
|
||||
// const FILES_SIZE_LIMIT int64 = 1e8 // 100Mb
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"errors"
|
||||
"time"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
|
||||
// func startSessionHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
||||
// type request struct {
|
||||
// // SessionID *string
|
||||
// EncodedProjectID *uint64 `json:"projectID"`
|
||||
// TrackerVersion string `json:"trackerVersion"`
|
||||
// RevID string `json:"revID"`
|
||||
// UserUUID *string `json:"userUUID"`
|
||||
// //UserOS string `json"userOS"` //hardcoded 'MacOS'
|
||||
// UserOSVersion string `json:"userOSVersion"`
|
||||
// UserDevice string `json:"userDevice"`
|
||||
// Timestamp uint64 `json:"timestamp"`
|
||||
// // UserDeviceType uint 0:phone 1:pad 2:tv 3:carPlay 5:mac
|
||||
// // “performances”:{
|
||||
// // “activeProcessorCount”:8,
|
||||
// // “isLowPowerModeEnabled”:0,
|
||||
// // “orientation”:0,
|
||||
// // “systemUptime”:585430,
|
||||
// // “batteryState”:0,
|
||||
// // “thermalState”:0,
|
||||
// // “batteryLevel”:0,
|
||||
// // “processorCount”:8,
|
||||
// // “physicalMemory”:17179869184
|
||||
// // },
|
||||
// }
|
||||
// type response struct {
|
||||
// Token string `json:"token"`
|
||||
// ImagesHashList []string `json:"imagesHashList"`
|
||||
// UserUUID string `json:"userUUID"`
|
||||
// SESSION_ID uint64 `json:"SESSION_ID"` ///TEMP
|
||||
// }
|
||||
// startTime := time.Now()
|
||||
// req := &request{}
|
||||
// body := http.MaxBytesReader(w, r.Body, JSON_SIZE_LIMIT)
|
||||
// //defer body.Close()
|
||||
// if err := json.NewDecoder(body).Decode(req); err != nil {
|
||||
// responseWithError(w, http.StatusBadRequest, err)
|
||||
// return
|
||||
// }
|
||||
"openreplay/backend/pkg/db/postgres"
|
||||
"openreplay/backend/pkg/token"
|
||||
. "openreplay/backend/pkg/messages"
|
||||
)
|
||||
|
||||
// if req.EncodedProjectID == nil {
|
||||
// responseWithError(w, http.StatusForbidden, errors.New("ProjectID value required"))
|
||||
// return
|
||||
// }
|
||||
// projectID := decodeProjectID(*(req.EncodedProjectID))
|
||||
// if projectID == 0 {
|
||||
// responseWithError(w, http.StatusUnprocessableEntity, errors.New("ProjectID value is invalid"))
|
||||
// return
|
||||
// }
|
||||
// p, err := pgconn.GetProject(uint32(projectID))
|
||||
// if err != nil {
|
||||
// if postgres.IsNoRowsErr(err) {
|
||||
// responseWithError(w, http.StatusNotFound, errors.New("Project doesn't exist or is not active"))
|
||||
// } else {
|
||||
// responseWithError(w, http.StatusInternalServerError, err) // TODO: send error here only on staging
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// sessionID, err := flaker.Compose(req.Timestamp)
|
||||
// if err != nil {
|
||||
// responseWithError(w, http.StatusInternalServerError, err)
|
||||
// return
|
||||
// }
|
||||
// userUUID := getUUID(req.UserUUID)
|
||||
// country := geoIP.ExtractISOCodeFromHTTPRequest(r)
|
||||
// expirationTime := startTime.Add(time.Duration(p.MaxSessionDuration) * time.Millisecond)
|
||||
const FILES_SIZE_LIMIT int64 = 1e8 // 100Mb
|
||||
|
||||
// imagesHashList, err := s3.GetFrequentlyUsedKeys(*(req.EncodedProjectID)) // TODO: reuse index: ~ frequency * size
|
||||
// if err != nil {
|
||||
// responseWithError(w, http.StatusInternalServerError, err)
|
||||
// return
|
||||
// }
|
||||
func startSessionHandlerIOS(w http.ResponseWriter, r *http.Request) {
|
||||
type request struct {
|
||||
Token string `json:"token"`
|
||||
ProjectKey *string `json:"projectKey"`
|
||||
TrackerVersion string `json:"trackerVersion"`
|
||||
RevID string `json:"revID"`
|
||||
UserUUID *string `json:"userUUID"`
|
||||
//UserOS string `json"userOS"` //hardcoded 'MacOS'
|
||||
UserOSVersion string `json:"userOSVersion"`
|
||||
UserDevice string `json:"userDevice"`
|
||||
Timestamp uint64 `json:"timestamp"`
|
||||
// UserDeviceType uint 0:phone 1:pad 2:tv 3:carPlay 5:mac
|
||||
// “performances”:{
|
||||
// “activeProcessorCount”:8,
|
||||
// “isLowPowerModeEnabled”:0,
|
||||
// “orientation”:0,
|
||||
// “systemUptime”:585430,
|
||||
// “batteryState”:0,
|
||||
// “thermalState”:0,
|
||||
// “batteryLevel”:0,
|
||||
// “processorCount”:8,
|
||||
// “physicalMemory”:17179869184
|
||||
// },
|
||||
}
|
||||
type response struct {
|
||||
Token string `json:"token"`
|
||||
ImagesHashList []string `json:"imagesHashList"`
|
||||
UserUUID string `json:"userUUID"`
|
||||
BeaconSizeLimit int64 `json:"beaconSizeLimit"`
|
||||
SessionID string `json:"sessionID"`
|
||||
}
|
||||
startTime := time.Now()
|
||||
req := &request{}
|
||||
body := http.MaxBytesReader(w, r.Body, JSON_SIZE_LIMIT)
|
||||
//defer body.Close()
|
||||
if err := json.NewDecoder(body).Decode(req); err != nil {
|
||||
responseWithError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
// responseWithJSON(w, &response{
|
||||
// Token: tokenizer.Compose(sessionID, uint64(expirationTime.UnixNano()/1e6)),
|
||||
// ImagesHashList: imagesHashList,
|
||||
// UserUUID: userUUID,
|
||||
// //TEMP:
|
||||
// SESSION_ID: sessionID,
|
||||
// })
|
||||
// producer.Produce(topicRaw, sessionID, messages.Encode(&messages.IOSSessionStart{
|
||||
// Timestamp: req.Timestamp,
|
||||
// ProjectID: projectID,
|
||||
// TrackerVersion: req.TrackerVersion,
|
||||
// RevID: req.RevID,
|
||||
// UserUUID: userUUID,
|
||||
// UserOS: "MacOS",
|
||||
// UserOSVersion: req.UserOSVersion,
|
||||
// UserDevice: MapIOSDevice(req.UserDevice),
|
||||
// UserDeviceType: GetIOSDeviceType(req.UserDevice), // string `json:"userDeviceType"` // From UserDevice; ENUM ?
|
||||
// UserCountry: country,
|
||||
// }))
|
||||
// }
|
||||
if req.ProjectKey == nil {
|
||||
responseWithError(w, http.StatusForbidden, errors.New("ProjectKey value required"))
|
||||
return
|
||||
}
|
||||
|
||||
p, err := pgconn.GetProjectByKey(*req.ProjectKey)
|
||||
if err != nil {
|
||||
if postgres.IsNoRowsErr(err) {
|
||||
responseWithError(w, http.StatusNotFound, errors.New("Project doesn't exist or is not active"))
|
||||
} else {
|
||||
responseWithError(w, http.StatusInternalServerError, err) // TODO: send error here only on staging
|
||||
}
|
||||
return
|
||||
}
|
||||
userUUID := getUUID(req.UserUUID)
|
||||
tokenData, err := tokenizer.Parse(req.Token)
|
||||
|
||||
if err != nil { // Starting the new one
|
||||
dice := byte(rand.Intn(100)) // [0, 100)
|
||||
if dice >= p.SampleRate {
|
||||
responseWithError(w, http.StatusForbidden, errors.New("cancel"))
|
||||
return
|
||||
}
|
||||
|
||||
ua := uaParser.ParseFromHTTPRequest(r)
|
||||
if ua == nil {
|
||||
responseWithError(w, http.StatusForbidden, errors.New("browser not recognized"))
|
||||
return
|
||||
}
|
||||
sessionID, err := flaker.Compose(uint64(startTime.UnixNano() / 1e6))
|
||||
if err != nil {
|
||||
responseWithError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
// TODO: if EXPIRED => send message for two sessions association
|
||||
expTime := startTime.Add(time.Duration(p.MaxSessionDuration) * time.Millisecond)
|
||||
tokenData = &token.TokenData{sessionID, expTime.UnixNano() / 1e6}
|
||||
|
||||
country := geoIP.ExtractISOCodeFromHTTPRequest(r)
|
||||
|
||||
// The difference with web is mostly here:
|
||||
producer.Produce(TOPIC_RAW, tokenData.ID, Encode(&IOSSessionStart{
|
||||
Timestamp: req.Timestamp,
|
||||
ProjectID: uint64(p.ProjectID),
|
||||
TrackerVersion: req.TrackerVersion,
|
||||
RevID: req.RevID,
|
||||
UserUUID: userUUID,
|
||||
UserOS: "IOS",
|
||||
UserOSVersion: req.UserOSVersion,
|
||||
UserDevice: MapIOSDevice(req.UserDevice),
|
||||
UserDeviceType: GetIOSDeviceType(req.UserDevice),
|
||||
UserCountry: country,
|
||||
}))
|
||||
}
|
||||
|
||||
// imagesHashList, err := s3.GetFrequentlyUsedKeys(*(req.EncodedProjectID)) // TODO: reuse index: ~ frequency * size
|
||||
// if err != nil {
|
||||
// responseWithError(w, http.StatusInternalServerError, err)
|
||||
// return
|
||||
// }
|
||||
|
||||
responseWithJSON(w, &response{
|
||||
// ImagesHashList: imagesHashList,
|
||||
Token: tokenizer.Compose(*tokenData),
|
||||
UserUUID: userUUID,
|
||||
SessionID: strconv.FormatUint(tokenData.ID, 10),
|
||||
BeaconSizeLimit: BEACON_SIZE_LIMIT,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// func pushLateMessagesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// sessionData, err := tokenizer.ParseFromHTTPRequest(r)
|
||||
// if err != nil && err != token.EXPIRED {
|
||||
// responseWithError(w, http.StatusUnauthorized, err)
|
||||
// return
|
||||
// }
|
||||
// // Check timestamps here?
|
||||
// pushMessages(w, r, sessionData.ID)
|
||||
// }
|
||||
func pushLateMessagesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
sessionData, err := tokenizer.ParseFromHTTPRequest(r)
|
||||
if err != nil && err != token.EXPIRED {
|
||||
responseWithError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
// Check timestamps here?
|
||||
pushMessages(w, r, sessionData.ID)
|
||||
}
|
||||
|
||||
|
||||
// func iosImagesUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// r.Body = http.MaxBytesReader(w, r.Body, FILES_SIZE_LIMIT)
|
||||
// // defer r.Body.Close()
|
||||
// err := r.ParseMultipartForm(1e5) // 100Kb
|
||||
// if err == http.ErrNotMultipart || err == http.ErrMissingBoundary {
|
||||
// responseWithError(w, http.StatusUnsupportedMediaType, err)
|
||||
// // } else if err == multipart.ErrMessageTooLarge // if non-files part exceeds 10 MB
|
||||
// } else if err != nil {
|
||||
// responseWithError(w, http.StatusInternalServerError, err) // TODO: send error here only on staging
|
||||
// }
|
||||
func iosImagesUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
sessionData, err := tokenizer.ParseFromHTTPRequest(r)
|
||||
if err != nil { // Should accept expired token?
|
||||
responseWithError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
// if len(r.MultipartForm.Value["projectID"]) == 0 {
|
||||
// responseWithError(w, http.StatusBadRequest, errors.New("projectID parameter required")) // status for missing/wrong parameter?
|
||||
// return
|
||||
// }
|
||||
// // encodedProjectID, err := strconv.ParseUint(r.MultipartForm.Value["projectID"][0], 10, 64)
|
||||
// // projectID := decodeProjectID(encodedProjectID)
|
||||
// // if projectID == 0 || err != nil {
|
||||
// // responseWithError(w, http.StatusUnprocessableEntity, errors.New("projectID value is incorrect"))
|
||||
// // return
|
||||
// // }
|
||||
// prefix := r.MultipartForm.Value["projectID"][0] + "/" //strconv.FormatUint(uint64(projectID), 10) + "/"
|
||||
r.Body = http.MaxBytesReader(w, r.Body, FILES_SIZE_LIMIT)
|
||||
// defer r.Body.Close()
|
||||
err = r.ParseMultipartForm(1e5) // 100Kb
|
||||
if err == http.ErrNotMultipart || err == http.ErrMissingBoundary {
|
||||
responseWithError(w, http.StatusUnsupportedMediaType, err)
|
||||
// } else if err == multipart.ErrMessageTooLarge // if non-files part exceeds 10 MB
|
||||
} else if err != nil {
|
||||
responseWithError(w, http.StatusInternalServerError, err) // TODO: send error here only on staging
|
||||
}
|
||||
|
||||
// for _, fileHeaderList := range r.MultipartForm.File {
|
||||
// for _, fileHeader := range fileHeaderList {
|
||||
// file, err := fileHeader.Open()
|
||||
// if err != nil {
|
||||
// continue // TODO: send server error or accumulate successful files
|
||||
// }
|
||||
// key := prefix + fileHeader.Filename // TODO: Malicious image put: use jwt?
|
||||
// go s3.Upload(file, key, "image/png", false)
|
||||
// }
|
||||
// }
|
||||
if len(r.MultipartForm.Value["projectKey"]) == 0 {
|
||||
responseWithError(w, http.StatusBadRequest, errors.New("projectKey parameter missing")) // status for missing/wrong parameter?
|
||||
return
|
||||
}
|
||||
|
||||
// w.WriteHeader(http.StatusOK)
|
||||
// }
|
||||
prefix := r.MultipartForm.Value["projectKey"][0] + "/" + strconv.FormatUint(sessionData.ID, 10) + "/"
|
||||
|
||||
for _, fileHeaderList := range r.MultipartForm.File {
|
||||
for _, fileHeader := range fileHeaderList {
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
continue // TODO: send server error or accumulate successful files
|
||||
}
|
||||
key := prefix + fileHeader.Filename
|
||||
go s3.Upload(file, key, "image/png", false)
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,34 +100,34 @@ func main() {
|
|||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
// case "/v1/ios/start":
|
||||
// switch r.Method {
|
||||
// case http.MethodPost:
|
||||
// startSessionHandlerIOS(w, r)
|
||||
// default:
|
||||
// w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
// }
|
||||
// case "/v1/ios/append":
|
||||
// switch r.Method {
|
||||
// case http.MethodPost:
|
||||
// pushMessagesHandler(w, r)
|
||||
// default:
|
||||
// w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
// }
|
||||
// case "/v1/ios/late":
|
||||
// switch r.Method {
|
||||
// case http.MethodPost:
|
||||
// pushLateMessagesHandler(w, r)
|
||||
// default:
|
||||
// w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
// }
|
||||
// case "/v1/ios/images":
|
||||
// switch r.Method {
|
||||
// case http.MethodPost:
|
||||
// iosImagesUploadHandler(w, r)
|
||||
// default:
|
||||
// w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
// }
|
||||
case "/v1/ios/start":
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
startSessionHandlerIOS(w, r)
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
case "/v1/ios/i":
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
pushMessagesHandler(w, r)
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
case "/v1/ios/late":
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
pushLateMessagesHandler(w, r)
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
case "/v1/ios/images":
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
iosImagesUploadHandler(w, r)
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue