1
0
Fork 0
forked from External/ergo

bake in url info

This commit is contained in:
CEF Server 2024-08-09 10:47:52 +00:00
parent a529158fe1
commit f9a0be2473
5 changed files with 117 additions and 12 deletions

View file

@ -570,6 +570,16 @@ func batchHandler(server *Server, client *Client, msg ircmsg.Message, rb *Respon
// XXX changing the label inside a handler is a bit dodgy, but it works here
// because there's no way we could have triggered a flush up to this point
rb.Label = batch.responseLabel
for _, msg := range batch.message.Split {
signatures := utils.GenerateImagorSignatures(msg.Message)
if len(signatures) > 0 {
if msg.Tags == nil {
msg.Tags = make(map[string]string)
}
msg.Tags["signatures"] = signatures
}
}
dispatchMessageToTarget(client, batch.tags, histType, batch.command, batch.target, batch.message, rb)
}
}
@ -2318,6 +2328,13 @@ func messageHandler(server *Server, client *Client, msg ircmsg.Message, rb *Resp
// each target gets distinct msgids
splitMsg := utils.MakeMessage(message)
signatures := utils.GenerateImagorSignaturesFromMessage(&msg)
if len(signatures) > 0 {
if clientOnlyTags == nil {
clientOnlyTags = make(map[string]string)
}
clientOnlyTags["signatures"] = signatures
}
dispatchMessageToTarget(client, clientOnlyTags, histType, msg.Command, targetString, splitMsg, rb)
}
return false

View file

@ -4,6 +4,14 @@
package utils
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/cshum/imagor/imagorpath"
"github.com/ergochat/irc-go/ircmsg"
"net/http"
"os"
"regexp"
"strings"
"time"
)
@ -170,3 +178,78 @@ func BuildTokenLines(lineLen int, tokens []string, delim string) []string {
}
return tl.Lines()
}
var urlRegex = regexp.MustCompile("https?:\\/\\/[\\w-]+(\\.[\\w-]+)+([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?")
func GenerateImagorSignaturesFromMessage(message *ircmsg.Message) string {
line, err := message.Line()
if err == nil {
return GenerateImagorSignatures(line)
}
return ""
}
var secretKey = os.Getenv("IMAGOR_SECRET")
var baseUrl = os.Getenv("IMAGOR_URL")
func GetUrlMime(url string) string {
// hacky, should fix
if !strings.Contains(url, "?") {
url += "?"
}
params := imagorpath.Params{
Image: url,
Meta: true,
}
metaPath := imagorpath.Generate(params, imagorpath.NewHMACSigner(sha256.New, 0, secretKey))
client := http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get(baseUrl + metaPath)
if err != nil {
println("Failed on the initial get")
println(err.Error())
return ""
}
defer resp.Body.Close()
var meta map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&meta)
if err != nil {
println("Failed on the JSON decode")
return ""
}
contentType, valid := meta["format"].(string)
fmt.Printf("%+v\n", meta)
if !valid {
println("No content type")
return ""
}
return contentType
}
// Process a message to add Imagor signatures
func GenerateImagorSignatures(str string) string {
urls := urlRegex.FindAllString(str, -1)
var sigs []string
for _, url := range urls {
params := imagorpath.Params{
Image: url,
FitIn: true,
Width: 600,
Height: 600,
}
path := imagorpath.Generate(params, imagorpath.NewHMACSigner(sha256.New, 0, secretKey))
signature := path[:strings.IndexByte(path, '/')]
contentType := GetUrlMime(url)
if contentType != "" {
sigs = append(sigs, signature+"|"+strings.ReplaceAll(contentType, "/", "_"))
} else {
sigs = append(sigs, signature)
}
}
if len(sigs) > 0 {
return strings.Join(sigs, ",")
}
return ""
}