From 733f911fadc872933481bcbe087d519ce00372df Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Mon, 2 May 2022 12:07:00 -0700 Subject: cleaned up main --- routes/api.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ routes/webfinger.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 routes/api.go create mode 100644 routes/webfinger.go (limited to 'routes') diff --git a/routes/api.go b/routes/api.go new file mode 100644 index 0000000..2fb0f3f --- /dev/null +++ b/routes/api.go @@ -0,0 +1,54 @@ +package routes + +import ( + "io/ioutil" + "net/http" + "time" + + "github.com/FChannel0/FChannel-Server/config" + "github.com/gofiber/fiber/v2" +) + +func Media(c *fiber.Ctx) error { + if c.Query("hash") != "" { + return RouteImages(c, c.Query("hash")) + } + + return c.SendStatus(404) +} + +func RouteImages(ctx *fiber.Ctx, media string) error { + req, err := http.NewRequest("GET", config.MediaHashs[media], nil) + if err != nil { + return err + } + + client := http.Client{ + Timeout: 5 * time.Second, + } + + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + fileBytes, err := ioutil.ReadFile("./static/notfound.png") + if err != nil { + return err + } + + _, err = ctx.Write(fileBytes) + return err + } + + body, _ := ioutil.ReadAll(resp.Body) + for name, values := range resp.Header { + for _, value := range values { + ctx.Append(name, value) + } + } + + return ctx.Send(body) +} diff --git a/routes/webfinger.go b/routes/webfinger.go new file mode 100644 index 0000000..221b7dc --- /dev/null +++ b/routes/webfinger.go @@ -0,0 +1,57 @@ +package routes + +import ( + "encoding/json" + "strings" + + "github.com/FChannel0/FChannel-Server/activitypub" + "github.com/FChannel0/FChannel-Server/config" + "github.com/FChannel0/FChannel-Server/webfinger" + "github.com/gofiber/fiber/v2" +) + +func Webfinger(c *fiber.Ctx) error { + acct := c.Query("resource") + + if len(acct) < 1 { + c.Status(fiber.StatusBadRequest) + return c.Send([]byte("resource needs a value")) + } + + acct = strings.Replace(acct, "acct:", "", -1) + + actorDomain := strings.Split(acct, "@") + + if len(actorDomain) < 2 { + c.Status(fiber.StatusBadRequest) + return c.Send([]byte("accpets only subject form of acct:board@instance")) + } + + if actorDomain[0] == "main" { + actorDomain[0] = "" + } else { + actorDomain[0] = "/" + actorDomain[0] + } + + if res, err := activitypub.IsActorLocal(config.TP + "" + actorDomain[1] + "" + actorDomain[0]); err == nil && !res { + c.Status(fiber.StatusBadRequest) + return c.Send([]byte("actor not local")) + } else if err != nil { + return err + } + + var finger webfinger.Webfinger + var link webfinger.WebfingerLink + + finger.Subject = "acct:" + actorDomain[0] + "@" + actorDomain[1] + link.Rel = "self" + link.Type = "application/activity+json" + link.Href = config.TP + "" + actorDomain[1] + "" + actorDomain[0] + + finger.Links = append(finger.Links, link) + + enc, _ := json.Marshal(finger) + + c.Set("Content-Type", config.ActivityStreams) + return c.Send(enc) +} -- cgit v1.2.3