2020-10-06 14:51:32 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-10-06 14:51:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var requestFrom = flag.String("from", "", "the server name that the request should originate from")
|
|
|
|
var requestKey = flag.String("key", "matrix_key.pem", "the private key to use when signing the request")
|
|
|
|
var requestPost = flag.Bool("post", false, "send a POST request instead of GET (pipe input into stdin or type followed by Ctrl-D)")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if requestFrom == nil || *requestFrom == "" {
|
|
|
|
fmt.Println("expecting: furl -from origin.com [-key matrix_key.pem] https://path/to/url")
|
|
|
|
fmt.Println("supported flags:")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:26:59 +01:00
|
|
|
data, err := os.ReadFile(*requestKey)
|
2020-10-06 14:51:32 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var privateKey ed25519.PrivateKey
|
|
|
|
keyBlock, _ := pem.Decode(data)
|
|
|
|
if keyBlock == nil {
|
|
|
|
panic("keyBlock is nil")
|
|
|
|
}
|
|
|
|
if keyBlock.Type == "MATRIX PRIVATE KEY" {
|
|
|
|
_, privateKey, err = ed25519.GenerateKey(bytes.NewReader(keyBlock.Bytes))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic("unexpected key block")
|
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
serverName := spec.ServerName(*requestFrom)
|
2023-04-06 09:55:01 +01:00
|
|
|
client := fclient.NewFederationClient(
|
|
|
|
[]*fclient.SigningIdentity{
|
2022-11-15 15:05:23 +00:00
|
|
|
{
|
|
|
|
ServerName: serverName,
|
|
|
|
KeyID: gomatrixserverlib.KeyID(keyBlock.Headers["Key-ID"]),
|
|
|
|
PrivateKey: privateKey,
|
|
|
|
},
|
|
|
|
},
|
2020-10-06 14:51:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
u, err := url.Parse(flag.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var bodyObj interface{}
|
|
|
|
var bodyBytes []byte
|
|
|
|
method := "GET"
|
|
|
|
if *requestPost {
|
|
|
|
method = "POST"
|
|
|
|
fmt.Println("Waiting for JSON input. Press Enter followed by Ctrl-D when done...")
|
|
|
|
|
|
|
|
scan := bufio.NewScanner(os.Stdin)
|
|
|
|
for scan.Scan() {
|
|
|
|
bytes := scan.Bytes()
|
|
|
|
bodyBytes = append(bodyBytes, bytes...)
|
|
|
|
}
|
|
|
|
fmt.Println("Done!")
|
|
|
|
if err = json.Unmarshal(bodyBytes, &bodyObj); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
req := fclient.NewFederationRequest(
|
2020-10-06 14:51:32 +01:00
|
|
|
method,
|
2022-11-15 15:05:23 +00:00
|
|
|
serverName,
|
2023-04-19 15:50:33 +01:00
|
|
|
spec.ServerName(u.Host),
|
2020-10-06 14:51:32 +01:00
|
|
|
u.RequestURI(),
|
|
|
|
)
|
|
|
|
|
|
|
|
if *requestPost {
|
|
|
|
if err = req.SetContent(bodyObj); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = req.Sign(
|
2023-04-19 15:50:33 +01:00
|
|
|
spec.ServerName(*requestFrom),
|
2020-10-06 14:51:32 +01:00
|
|
|
gomatrixserverlib.KeyID(keyBlock.Headers["Key-ID"]),
|
|
|
|
privateKey,
|
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpReq, err := req.HTTPRequest()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var res interface{}
|
|
|
|
err = client.DoRequestAndParseResponse(
|
|
|
|
context.TODO(),
|
|
|
|
httpReq,
|
|
|
|
&res,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := json.MarshalIndent(res, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(j))
|
|
|
|
}
|