Fix styles.

This commit is contained in:
Naoki Kosaka
2018-12-09 20:40:17 +09:00
parent edafcf140f
commit 6ce65295bb
15 changed files with 233 additions and 220 deletions

View File

@ -15,33 +15,34 @@ import (
"github.com/yukimochi/httpsig"
)
var UA_STRING = os.Getenv("RELAY_SERVICENAME") + " (golang net/http; Activity-Relay v0.1.1; " + os.Getenv("RELAY_DOMAIN") + ")"
// UaString : Use for User-Agent
var UaString = os.Getenv("RELAY_SERVICENAME") + " (golang net/http; Activity-Relay v0.1.1; " + os.Getenv("RELAY_DOMAIN") + ")"
func appendSignature(r *http.Request, body *[]byte, KeyID string, pKey *rsa.PrivateKey) error {
func appendSignature(request *http.Request, body *[]byte, KeyID string, publicKey *rsa.PrivateKey) error {
hash := sha256.New()
hash.Write(*body)
b := hash.Sum(nil)
r.Header.Set("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(b))
r.Header.Set("Host", r.Host)
request.Header.Set("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(b))
request.Header.Set("Host", request.Host)
signer, _, err := httpsig.NewSigner([]httpsig.Algorithm{httpsig.RSA_SHA256}, []string{httpsig.RequestTarget, "Host", "Date", "Digest", "Content-Type"}, httpsig.Signature)
if err != nil {
return err
}
err = signer.SignRequest(pKey, KeyID, r)
err = signer.SignRequest(publicKey, KeyID, request)
if err != nil {
return err
}
return nil
}
// SendActivity : Send ActivityPub Activity.
func SendActivity(inboxURL string, KeyID string, refBytes []byte, pKey *rsa.PrivateKey) error {
req, _ := http.NewRequest("POST", inboxURL, bytes.NewBuffer(refBytes))
// SendActivity : Send ActivityPub activity
func SendActivity(inboxURL string, KeyID string, body []byte, publicKey *rsa.PrivateKey) error {
req, _ := http.NewRequest("POST", inboxURL, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UA_STRING)
req.Header.Set("User-Agent", UaString)
req.Header.Set("Date", httpdate.Time2Str(time.Now()))
appendSignature(req, &refBytes, KeyID, pKey)
appendSignature(req, &body, KeyID, publicKey)
client := &http.Client{Timeout: time.Duration(5) * time.Second}
resp, err := client.Do(req)
if err != nil {

View File

@ -35,23 +35,25 @@ type Actor struct {
PublicKey PublicKey `json:"publicKey"`
}
func (a *Actor) GenerateSelfKey(hostname *url.URL, publickey *rsa.PublicKey) {
a.Context = []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}
a.ID = hostname.String() + "/actor"
a.Type = "Service"
a.PreferredUsername = "relay"
a.Inbox = hostname.String() + "/inbox"
a.PublicKey = PublicKey{
// GenerateSelfKey : Generate relay Actor from Publickey.
func (actor *Actor) GenerateSelfKey(hostname *url.URL, publickey *rsa.PublicKey) {
actor.Context = []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}
actor.ID = hostname.String() + "/actor"
actor.Type = "Service"
actor.PreferredUsername = "relay"
actor.Inbox = hostname.String() + "/inbox"
actor.PublicKey = PublicKey{
hostname.String() + "/actor#main-key",
hostname.String() + "/actor",
keyloader.GeneratePublicKeyPEMString(publickey),
}
}
func (a *Actor) RetrieveRemoteActor(url string) error {
// RetrieveRemoteActor : Retrieve Actor from remote instance.
func (actor *Actor) RetrieveRemoteActor(url string) error {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UA_STRING)
req.Header.Set("User-Agent", UaString)
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
@ -60,7 +62,7 @@ func (a *Actor) RetrieveRemoteActor(url string) error {
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(data, &a)
err = json.Unmarshal(data, &actor)
if err != nil {
return err
}
@ -78,32 +80,35 @@ type Activity struct {
Cc []string `json:"cc"`
}
func (a *Activity) GenerateResponse(host *url.URL, responseType string) Activity {
// GenerateResponse : Generate activity response.
func (activity *Activity) GenerateResponse(host *url.URL, responseType string) Activity {
return Activity{
[]string{"https://www.w3.org/ns/activitystreams"},
host.String() + "/activities/" + uuid.NewV4().String(),
host.String() + "/actor",
responseType,
&a,
&activity,
nil,
nil,
}
}
func (a *Activity) GenerateAnnounce(host *url.URL) Activity {
// GenerateAnnounce : Generate Announce of activity.
func (activity *Activity) GenerateAnnounce(host *url.URL) Activity {
return Activity{
[]string{"https://www.w3.org/ns/activitystreams"},
host.String() + "/activities/" + uuid.NewV4().String(),
host.String() + "/actor",
"Announce",
a.ID,
activity.ID,
[]string{host.String() + "/actor/followers"},
nil,
}
}
func (a *Activity) NestedActivity() (*Activity, error) {
mappedObject := a.Object.(map[string]interface{})
// NestedActivity : Unwrap nested activity.
func (activity *Activity) NestedActivity() (*Activity, error) {
mappedObject := activity.Object.(map[string]interface{})
if id, ok := mappedObject["id"].(string); ok {
if nestedType, ok := mappedObject["type"].(string); ok {
actor, ok := mappedObject["actor"].(string)
@ -153,9 +158,10 @@ type WebfingerResource struct {
Links []WebfingerLink `json:"links"`
}
func (a *WebfingerResource) GenerateFromActor(hostname *url.URL, actor *Actor) {
a.Subject = "acct:" + actor.PreferredUsername + "@" + hostname.Host
a.Links = []WebfingerLink{
// GenerateFromActor : Generate Webfinger resource from Actor.
func (resource *WebfingerResource) GenerateFromActor(hostname *url.URL, actor *Actor) {
resource.Subject = "acct:" + actor.PreferredUsername + "@" + hostname.Host
resource.Links = []WebfingerLink{
WebfingerLink{
"self",
"application/activity+json",