-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsend.go
156 lines (127 loc) · 4.79 KB
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2023, 2024 Dima Krasner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless ruired by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fed
import (
"bytes"
"context"
"crypto/sha256"
"crypto/x509"
"database/sql"
"encoding/base64"
"encoding/pem"
"fmt"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/buildinfo"
"github.com/go-fed/httpsig"
"io"
"log/slog"
"net/http"
"time"
)
var userAgent = "tootik/" + buildinfo.Version
func (r *Resolver) send(log *slog.Logger, db *sql.DB, from *ap.Actor, req *http.Request) (*http.Response, error) {
urlString := req.URL.String()
if req.URL.Scheme != "https" {
return nil, fmt.Errorf("invalid scheme in %s: %s", urlString, req.URL.Scheme)
}
if req.URL.Host == "localhost" || req.URL.Host == "localhost.localdomain" || req.URL.Host == "127.0.0.1" || req.URL.Host == "::1" {
return nil, fmt.Errorf("invalid host in %s: %s", urlString, req.URL.Host)
}
req.Header.Set("Content-Type", `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`)
if from == nil {
log.Debug("Sending request", "url", urlString)
} else {
log.Debug("Sending request", "url", urlString, "from", from.ID)
var publicKeyID, privateKeyPemString string
if err := db.QueryRowContext(req.Context(), `select actor->>'publicKey.id', privkey from persons where id = ?`, from.ID).Scan(&publicKeyID, &privateKeyPemString); err != nil {
return nil, fmt.Errorf("failed to fetch key for %s: %w", from.ID, err)
}
signer, _, err := httpsig.NewSigner(
[]httpsig.Algorithm{httpsig.RSA_SHA256},
httpsig.DigestSha256,
[]string{httpsig.RequestTarget, "host", "date", "digest"},
httpsig.Signature,
int64(time.Hour*12/time.Second),
)
if err != nil {
return nil, fmt.Errorf("failed to sign body for %s: %w", urlString, err)
}
var body []byte
var hash [sha256.Size]byte
if req.Body == nil {
hash = sha256.Sum256(nil)
} else {
body, err = io.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body for %s: %w", urlString, err)
}
req.Body = io.NopCloser(bytes.NewReader(body))
hash = sha256.Sum256(body)
}
privateKeyPem, _ := pem.Decode([]byte(privateKeyPemString))
privateKey, err := x509.ParsePKCS8PrivateKey(privateKeyPem.Bytes)
if err != nil {
// fallback for openssl<3.0.0
privateKey, err = x509.ParsePKCS1PrivateKey(privateKeyPem.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to sign body for %s: %w", urlString, err)
}
}
req.Header.Add("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(hash[:]))
req.Header.Add("Date", time.Now().UTC().Format(http.TimeFormat))
req.Header.Add("Host", req.URL.Host)
if err := signer.SignRequest(privateKey, publicKeyID, req, nil); err != nil {
return nil, fmt.Errorf("failed to sign request for %s: %w", urlString, err)
}
}
resp, err := r.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request to %s: %w", urlString, err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
body, err := io.ReadAll(io.LimitReader(resp.Body, r.Config.MaxRequestBodySize))
resp.Body.Close()
if err != nil {
return resp, fmt.Errorf("failed to send request to %s: %d, %w", urlString, resp.StatusCode, err)
}
return resp, fmt.Errorf("failed to send request to %s: %d, %s", urlString, resp.StatusCode, string(body))
}
return resp, nil
}
// Send sends a signed request.
func (r *Resolver) Send(ctx context.Context, log *slog.Logger, db *sql.DB, from *ap.Actor, inbox string, body []byte) error {
if inbox == "" {
return fmt.Errorf("cannot send request to %s: empty URL", inbox)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, inbox, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("failed to send request to %s: %w", inbox, err)
}
if req.URL.Host == r.Domain {
log.Info("Skipping request", "inbox", inbox, "from", from.ID)
return nil
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Accept", `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`)
resp, err := r.send(log, db, from, req)
if err != nil {
return fmt.Errorf("failed to send request to %s: %w", inbox, err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to send request to %s: %w", inbox, err)
}
log.Info("Successfully sent message", "inbox", inbox, "body", string(respBody))
return nil
}