chore: try switching to fiber

This commit is contained in:
0x1a8510f2 2023-05-30 03:35:58 +01:00
parent ca058a7e14
commit 7a0ea49b77
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D

View File

@ -19,6 +19,7 @@ import (
"dev.l1qu1d.net/wraith-labs/wraith-module-pinecomms/internal/misc"
"dev.l1qu1d.net/wraith-labs/wraith-module-pinecomms/internal/proto"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
pineconeConnections "github.com/matrix-org/pinecone/connections"
@ -105,45 +106,39 @@ func (pm *radio) Start() {
pQUIC := pineconeSessions.NewSessions(c.logger, pRouter, []string{PROTOCOL_NAME})
pMulticast := pineconeMulticast.NewMulticast(c.logger, pRouter)
pManager := pineconeConnections.NewConnectionManager(pRouter, nil)
// Set up rx queue handling.
pMux := mux.NewRouter().SkipClean(true).UseEncodedPath()
pMux.PathPrefix(proto.ROUTE_PREFIX).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Read the payload from request body.
data, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Fill out packet metadata.
p := proto.Packet{
Peer: r.RemoteAddr,
Method: r.Method,
Route: strings.TrimPrefix(r.URL.EscapedPath(), proto.ROUTE_PREFIX),
Data: data,
}
// Respond so the requester doesn't have to wait for the queue to empty.
w.WriteHeader(http.StatusNoContent)
// Add to the queue.
pm.rxq <- p
})
pHTTP := pQUIC.Protocol(PROTOCOL_NAME).HTTP()
pHTTP.Mux().Handle("/", pMux)
app := fiber.New(fiber.Config{
// Pinecone HTTP server.
httpApp := fiber.New(fiber.Config{
DisableStartupMessage: true,
DisablePreParseMultipartForm: true,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 10 * time.Second,
})
httpApp.All(proto.ROUTE_PREFIX, func(ctx *fiber.Ctx) error {
fmt.Println("YOOOOOOOOOOOO")
// Read the payload from request body.
data := ctx.Body()
// Pinecone HTTP server.
pineconeHttpServer := app.Server()
// Fill out packet metadata.
p := proto.Packet{
Peer: ctx.IP(),
Method: ctx.Method(),
Route: strings.TrimPrefix(ctx.Route().Path, proto.ROUTE_PREFIX),
Data: data,
}
// Respond so the requester doesn't have to wait for the queue to empty.
ctx.SendStatus(http.StatusNoContent)
// Add to the queue.
pm.rxq <- p
return nil
})
pineconeHttpServer := httpApp.Server()
pHTTP.Mux().Handle("/", adaptor.FiberApp(httpApp))
// Start pinecone HTTP server in goroutine.
wg.Add(1)