-
Notifications
You must be signed in to change notification settings - Fork 4
/
delivery.go
134 lines (113 loc) · 3.11 KB
/
delivery.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
package delivery
import (
"bytes"
"crypto"
"crypto/rsa"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/go-fed/httpsig"
"github.com/hvxahv/hvx/errors"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
)
const (
FailedToDelivery = "THERE_ARE_REQUESTS_THAT_FAILED_TO_SEND"
DeliverySuccessfully = "SEND_SUCCESSFULLY"
)
// Delivery ...
// https://www.w3.org/TR/activitypub/#delivery
type Delivery struct {
PublicKeyId string
PrivateKey string
Body []byte
}
func New(publicKeyId string, privateKey string, body []byte) *Delivery {
return &Delivery{PublicKeyId: publicKeyId, PrivateKey: privateKey, Body: body}
}
func (i *Delivery) Do(inbox string) (*http.Response, error) {
remote, err := IsRemote(inbox)
if err != nil {
return nil, errors.New("INBOX_URL_FAILS_WHEN_PARSING")
}
if !remote {
// TODO - SEND TO LOCAL USER INBOX...
return &http.Response{StatusCode: 202}, nil
}
hostname, err := parseInboxHostname(inbox)
if err != nil {
return nil, err
}
client := http.Client{}
singer, _, _ := httpsig.NewSigner([]httpsig.Algorithm{httpsig.RSA_SHA256}, "SHA-256", []string{"(request-target)", "date", "host", "digest"}, httpsig.Signature, 120)
req, err := http.NewRequest("POST", inbox, bytes.NewReader(i.Body))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/activity+json")
req.Header.Add("Content-Type", "application/ld+json")
req.Header.Add("Content-Type", "application/json; charset=utf-8")
req.Header.Add("Date", time.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05")+" GMT")
req.Header.Add("User-Agent", "hvxahv/0.0.0")
req.Header.Add("Host", hostname)
req.Header.Add("Accept", "application/activity+json; charset=utf-8")
if err := singer.SignRequest(GetPrivateKey(i.PrivateKey), i.PublicKeyId, req, i.Body); err != nil {
return nil, err
}
if err := Verify(req, i.PrivateKey); err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
stdout := os.Stdout
_, err = io.Copy(stdout, res.Body)
return res, err
}
func parseInboxHostname(inbox string) (string, error) {
u, err := url.Parse(inbox)
if err != nil {
return "", err
}
hostname := strings.TrimPrefix(u.Hostname(), "www.")
return hostname, nil
}
// IsRemote ...
func IsRemote(inbox string) (bool, error) {
domain := viper.GetString("domain")
hostname, err := parseInboxHostname(inbox)
if err != nil {
return false, err
}
if domain != hostname {
return true, nil
}
return false, nil
}
// GetPrivateKey ParseRawPrivateKey -> crypto.PrivateKey.
func GetPrivateKey(privateKey string) crypto.PrivateKey {
var cpk crypto.PrivateKey
key, err := ssh.ParseRawPrivateKey([]byte(privateKey))
cpk = key.(*rsa.PrivateKey)
if err != nil {
errors.Throw("", err)
}
return cpk
}
// Verify ...
func Verify(r *http.Request, privateKey string) error {
verifier, err := httpsig.NewVerifier(r)
if err != nil {
return err
}
priv := GetPrivateKey(privateKey)
pub := priv.(*rsa.PrivateKey).PublicKey
var algo httpsig.Algorithm = httpsig.RSA_SHA256
var pubKey crypto.PublicKey = &pub
return verifier.Verify(pubKey, algo)
}