chore: add API path for sending packets

This commit is contained in:
0x1a8510f2 2023-01-15 13:56:26 +00:00
parent 6f62548178
commit 1dcf389f3a
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D
2 changed files with 60 additions and 0 deletions

View File

@ -19,6 +19,16 @@ type authSuccessResponse struct {
Access authStatus `json:"access"`
}
type sendRequest struct {
Target string `json:"target"`
Payload struct {
Read []string `json:"read"`
Write map[string]interface{} `json:"write"`
ListMem bool `json:"listMem"`
} `json:"payload"`
Conditions struct{} `json:"conditions"`
}
func handleAbout(w http.ResponseWriter) {
// Collect necessary information.
buildinfo, _ := debug.ReadBuildInfo()

View File

@ -105,6 +105,56 @@ func main() {
}
handleAbout(w)
case "send":
// Require auth as admin.
if !StatusInGroup(AuthStatus(r), AUTH_STATUS_A) {
w.WriteHeader(http.StatusUnauthorized)
return
}
// Get the data from the request body.
reqbody, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Parse the request body.
reqdata := sendRequest{}
err = json.Unmarshal(reqbody, &reqdata)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Prepare the packet to be sent to client.
req := s.Request(reqdata.Target, proto.PacketReq{
Payload: struct {
Read []string
Write map[string]interface{}
ListMem bool
}{
Read: reqdata.Payload.Read,
Write: reqdata.Payload.Write,
ListMem: reqdata.Payload.ListMem,
},
Conditions: reqdata.Conditions,
})
packetData, err := proto.Marshal(&req, pineconeId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Send the packet to client.
pm.Send(context.Background(), proto.Packet{
Peer: reqdata.Target,
Method: http.MethodPost,
Route: proto.ROUTE_REQUEST,
Data: packetData,
})
w.WriteHeader(http.StatusNoContent)
case "checkauth":
if !StatusInGroup(AuthStatus(r), AUTH_STATUS_A, AUTH_STATUS_V) {
w.WriteHeader(http.StatusUnauthorized)