Rename variables.

This commit is contained in:
Naoki Kosaka 2018-12-16 20:45:08 +09:00
parent c482673bc9
commit 2241d02fb2
5 changed files with 54 additions and 58 deletions

View File

@ -91,7 +91,7 @@ func pushRelayJob(sourceInbox string, body []byte) {
}, },
}, },
} }
_, err := macServer.SendTask(job) _, err := machineryServer.SendTask(job)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
@ -116,7 +116,7 @@ func pushRegistorJob(inboxURL string, body []byte) {
}, },
}, },
} }
_, err := macServer.SendTask(job) _, err := machineryServer.SendTask(job)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
@ -182,7 +182,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
case "Follow": case "Follow":
err = followAcceptable(activity, actor) err = followAcceptable(activity, actor)
if err != nil { if err != nil {
resp := activity.GenerateResponse(hostname, "Reject") resp := activity.GenerateResponse(hostURL, "Reject")
jsonData, _ := json.Marshal(&resp) jsonData, _ := json.Marshal(&resp)
go pushRegistorJob(actor.Inbox, jsonData) go pushRegistorJob(actor.Inbox, jsonData)
fmt.Println("Reject Follow Request : ", err.Error(), activity.Actor) fmt.Println("Reject Follow Request : ", err.Error(), activity.Actor)
@ -192,7 +192,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
} else { } else {
if suitableFollow(activity, actor) { if suitableFollow(activity, actor) {
if relayState.RelayConfig.ManuallyAccept { if relayState.RelayConfig.ManuallyAccept {
redClient.HMSet("relay:pending:"+domain.Host, map[string]interface{}{ redisClient.HMSet("relay:pending:"+domain.Host, map[string]interface{}{
"inbox_url": actor.Endpoints.SharedInbox, "inbox_url": actor.Endpoints.SharedInbox,
"activity_id": activity.ID, "activity_id": activity.ID,
"type": "Follow", "type": "Follow",
@ -201,7 +201,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
}) })
fmt.Println("Pending Follow Request : ", activity.Actor) fmt.Println("Pending Follow Request : ", activity.Actor)
} else { } else {
resp := activity.GenerateResponse(hostname, "Accept") resp := activity.GenerateResponse(hostURL, "Accept")
jsonData, _ := json.Marshal(&resp) jsonData, _ := json.Marshal(&resp)
go pushRegistorJob(actor.Inbox, jsonData) go pushRegistorJob(actor.Inbox, jsonData)
relayState.AddSubscription(state.Subscription{ relayState.AddSubscription(state.Subscription{
@ -213,7 +213,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
fmt.Println("Accept Follow Request : ", activity.Actor) fmt.Println("Accept Follow Request : ", activity.Actor)
} }
} else { } else {
resp := activity.GenerateResponse(hostname, "Reject") resp := activity.GenerateResponse(hostURL, "Reject")
jsonData, _ := json.Marshal(&resp) jsonData, _ := json.Marshal(&resp)
go pushRegistorJob(actor.Inbox, jsonData) go pushRegistorJob(actor.Inbox, jsonData)
fmt.Println("Reject Follow Request : ", activity.Actor) fmt.Println("Reject Follow Request : ", activity.Actor)
@ -231,7 +231,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
writer.WriteHeader(400) writer.WriteHeader(400)
writer.Write([]byte(err.Error())) writer.Write([]byte(err.Error()))
} else { } else {
redClient.Del("relay:subscription:" + domain.Host) redisClient.Del("relay:subscription:" + domain.Host)
fmt.Println("Accept Unfollow Request : ", activity.Actor) fmt.Println("Accept Unfollow Request : ", activity.Actor)
writer.WriteHeader(202) writer.WriteHeader(202)
@ -265,7 +265,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
} }
switch nestedObject.Type { switch nestedObject.Type {
case "Note": case "Note":
resp := nestedObject.GenerateAnnounce(hostname) resp := nestedObject.GenerateAnnounce(hostURL)
jsonData, _ := json.Marshal(&resp) jsonData, _ := json.Marshal(&resp)
go pushRelayJob(domain.Host, jsonData) go pushRelayJob(domain.Host, jsonData)
fmt.Println("Accept Announce Note : ", activity.Actor) fmt.Println("Accept Announce Note : ", activity.Actor)

View File

@ -344,7 +344,7 @@ func TestHandleInboxValidFollow(t *testing.T) {
if r.StatusCode != 202 { if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode)) t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
} }
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 { if res != 1 {
t.Fatalf("Failed - Subscription not works.") t.Fatalf("Failed - Subscription not works.")
} }
@ -372,11 +372,11 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
if r.StatusCode != 202 { if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode)) t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
} }
res, _ := redClient.Exists("relay:pending:" + domain.Host).Result() res, _ := redisClient.Exists("relay:pending:" + domain.Host).Result()
if res != 1 { if res != 1 {
t.Fatalf("Failed - Pending not works.") t.Fatalf("Failed - Pending not works.")
} }
res, _ = redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ = redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 { if res != 0 {
t.Fatalf("Failed - Pending was skipped.") t.Fatalf("Failed - Pending was skipped.")
} }
@ -404,7 +404,7 @@ func TestHandleInboxInvalidFollow(t *testing.T) {
if r.StatusCode != 202 { if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode)) t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
} }
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 { if res != 0 {
t.Fatalf("Failed - Subscription not blocked.") t.Fatalf("Failed - Subscription not blocked.")
} }
@ -430,7 +430,7 @@ func TestHandleInboxValidFollowBlocked(t *testing.T) {
if r.StatusCode != 202 { if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode)) t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
} }
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 { if res != 0 {
t.Fatalf("Failed - Subscription not blocked.") t.Fatalf("Failed - Subscription not blocked.")
} }
@ -461,7 +461,7 @@ func TestHandleInboxValidUnfollow(t *testing.T) {
if r.StatusCode != 202 { if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode)) t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
} }
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 { if res != 0 {
t.Fatalf("Failed - Subscription not succeed.") t.Fatalf("Failed - Subscription not succeed.")
} }
@ -491,7 +491,7 @@ func TestHandleInboxInvalidUnfollow(t *testing.T) {
if r.StatusCode != 400 { if r.StatusCode != 400 {
t.Fatalf("Failed - StatusCode is not 400") t.Fatalf("Failed - StatusCode is not 400")
} }
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 { if res != 1 {
t.Fatalf("Failed - Block hacked unfollow not succeed.") t.Fatalf("Failed - Block hacked unfollow not succeed.")
} }
@ -521,7 +521,7 @@ func TestHandleInboxUnfollowAsActor(t *testing.T) {
if r.StatusCode != 400 { if r.StatusCode != 400 {
t.Fatalf("Failed - StatusCode is not 400") t.Fatalf("Failed - StatusCode is not 400")
} }
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 { if res != 1 {
t.Fatalf("Failed - Block actor unfollow not succeed.") t.Fatalf("Failed - Block actor unfollow not succeed.")
} }
@ -557,8 +557,8 @@ func TestHandleInboxValidCreate(t *testing.T) {
} }
relayState.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
relayState.DelSubscription("example.org") relayState.DelSubscription("example.org")
redClient.Del("relay:subscription:" + domain.Host).Result() redisClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:subscription:example.org").Result() redisClient.Del("relay:subscription:example.org").Result()
} }
func TestHandleInboxlimitedCreate(t *testing.T) { func TestHandleInboxlimitedCreate(t *testing.T) {
@ -697,7 +697,7 @@ func TestHandleInboxUndo(t *testing.T) {
if r.StatusCode != 202 { if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode)) t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
} }
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result() res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 { if res != 1 {
t.Fatalf("Failed - Missing unsubscribed.") t.Fatalf("Failed - Missing unsubscribed.")
} }

26
main.go
View File

@ -21,10 +21,10 @@ var Actor activitypub.Actor
// WebfingerResource : Relay's Webfinger resource // WebfingerResource : Relay's Webfinger resource
var WebfingerResource activitypub.WebfingerResource var WebfingerResource activitypub.WebfingerResource
var hostname *url.URL var hostURL *url.URL
var hostkey *rsa.PrivateKey var hostPrivatekey *rsa.PrivateKey
var redClient *redis.Client var redisClient *redis.Client
var macServer *machinery.Server var machineryServer *machinery.Server
var relayState state.RelayState var relayState state.RelayState
func main() { func main() {
@ -46,15 +46,15 @@ func main() {
} }
var err error var err error
hostkey, err = keyloader.ReadPrivateKeyRSAfromPath(pemPath) hostPrivatekey, err = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
if err != nil { if err != nil {
panic("Can't read Hostkey Pemfile") panic("Can't read Hostkey Pemfile")
} }
hostname, err = url.Parse("https://" + relayDomain) hostURL, err = url.Parse("https://" + relayDomain)
if err != nil { if err != nil {
panic("Can't parse Relay Domain") panic("Can't parse Relay Domain")
} }
redClient = redis.NewClient(&redis.Options{ redisClient = redis.NewClient(&redis.Options{
Addr: redisURL, Addr: redisURL,
}) })
@ -65,16 +65,16 @@ func main() {
ResultsExpireIn: 5, ResultsExpireIn: 5,
} }
macServer, err = machinery.NewServer(macConfig) machineryServer, err = machinery.NewServer(macConfig)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
Actor.GenerateSelfKey(hostname, &hostkey.PublicKey) Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey)
WebfingerResource.GenerateFromActor(hostname, &Actor) WebfingerResource.GenerateFromActor(hostURL, &Actor)
// Load Config // Load Config
relayState = state.NewState(redClient) relayState = state.NewState(redisClient)
http.HandleFunc("/.well-known/webfinger", handleWebfinger) http.HandleFunc("/.well-known/webfinger", handleWebfinger)
http.HandleFunc("/actor", handleActor) http.HandleFunc("/actor", handleActor)
@ -87,12 +87,12 @@ func main() {
fmt.Println("REDIS URL : ", redisURL) fmt.Println("REDIS URL : ", redisURL)
fmt.Println("BIND ADDRESS : ", relayBind) fmt.Println("BIND ADDRESS : ", relayBind)
fmt.Println(" - Blocked Domain") fmt.Println(" - Blocked Domain")
domains, _ := redClient.HKeys("relay:config:blockedDomain").Result() domains, _ := redisClient.HKeys("relay:config:blockedDomain").Result()
for _, domain := range domains { for _, domain := range domains {
fmt.Println(domain) fmt.Println(domain)
} }
fmt.Println(" - Limited Domain") fmt.Println(" - Limited Domain")
domains, _ = redClient.HKeys("relay:config:limitedDomain").Result() domains, _ = redisClient.HKeys("relay:config:limitedDomain").Result()
for _, domain := range domains { for _, domain := range domains {
fmt.Println(domain) fmt.Println(domain)
} }

View File

@ -18,9 +18,9 @@ func TestMain(m *testing.M) {
pemPath := os.Getenv("ACTOR_PEM") pemPath := os.Getenv("ACTOR_PEM")
relayDomain := os.Getenv("RELAY_DOMAIN") relayDomain := os.Getenv("RELAY_DOMAIN")
redisURL := os.Getenv("REDIS_URL") redisURL := os.Getenv("REDIS_URL")
hostkey, _ = keyloader.ReadPrivateKeyRSAfromPath(pemPath) hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
hostname, _ = url.Parse("https://" + relayDomain) hostURL, _ = url.Parse("https://" + relayDomain)
redClient = redis.NewClient(&redis.Options{ redisClient = redis.NewClient(&redis.Options{
Addr: redisURL, Addr: redisURL,
}) })
var macConfig = &config.Config{ var macConfig = &config.Config{
@ -29,15 +29,15 @@ func TestMain(m *testing.M) {
ResultBackend: "redis://" + redisURL, ResultBackend: "redis://" + redisURL,
ResultsExpireIn: 5, ResultsExpireIn: 5,
} }
macServer, _ = machinery.NewServer(macConfig) machineryServer, _ = machinery.NewServer(macConfig)
Actor.GenerateSelfKey(hostname, &hostkey.PublicKey) Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey)
WebfingerResource.GenerateFromActor(hostname, &Actor) WebfingerResource.GenerateFromActor(hostURL, &Actor)
// Load Config // Load Config
redClient.FlushAll().Result() redisClient.FlushAll().Result()
relayState = state.NewState(redClient) relayState = state.NewState(redisClient)
code := m.Run() code := m.Run()
os.Exit(code) os.Exit(code)
redClient.FlushAll().Result() redisClient.FlushAll().Result()
} }

View File

@ -15,26 +15,22 @@ import (
"github.com/yukimochi/Activity-Relay/KeyLoader" "github.com/yukimochi/Activity-Relay/KeyLoader"
) )
// Hostname : Hostname of Relay
var Hostname *url.URL
// Hostkey : PrivateKey of Relay
var Hostkey *rsa.PrivateKey
// Actor : Relay's Actor // Actor : Relay's Actor
var Actor activitypub.Actor var Actor activitypub.Actor
var redClient *redis.Client var hostURL *url.URL
var hostPrivatekey *rsa.PrivateKey
var redisClient *redis.Client
func relayActivity(args ...string) error { func relayActivity(args ...string) error {
inboxURL := args[0] inboxURL := args[0]
body := args[1] body := args[1]
err := activitypub.SendActivity(inboxURL, Actor.ID, []byte(body), Hostkey) err := activitypub.SendActivity(inboxURL, Actor.ID, []byte(body), hostPrivatekey)
if err != nil { if err != nil {
domain, _ := url.Parse(inboxURL) domain, _ := url.Parse(inboxURL)
mod, _ := redClient.HSetNX("relay:statistics:"+domain.Host, "last_error", err.Error()).Result() mod, _ := redisClient.HSetNX("relay:statistics:"+domain.Host, "last_error", err.Error()).Result()
if mod { if mod {
redClient.Expire("relay:statistics:"+domain.Host, time.Duration(time.Minute)) redisClient.Expire("relay:statistics:"+domain.Host, time.Duration(time.Minute))
} }
} }
return err return err
@ -43,7 +39,7 @@ func relayActivity(args ...string) error {
func registorActivity(args ...string) error { func registorActivity(args ...string) error {
inboxURL := args[0] inboxURL := args[0]
body := args[1] body := args[1]
err := activitypub.SendActivity(inboxURL, Actor.ID, []byte(body), Hostkey) err := activitypub.SendActivity(inboxURL, Actor.ID, []byte(body), hostPrivatekey)
return err return err
} }
@ -62,26 +58,26 @@ func main() {
} }
var err error var err error
Hostkey, err = keyloader.ReadPrivateKeyRSAfromPath(pemPath) hostPrivatekey, err = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
if err != nil { if err != nil {
panic("Can't read Hostkey Pemfile") panic("Can't read Hostkey Pemfile")
} }
Hostname, err = url.Parse("https://" + relayDomain) hostURL, err = url.Parse("https://" + relayDomain)
if err != nil { if err != nil {
panic("Can't parse Relay Domain") panic("Can't parse Relay Domain")
} }
redClient = redis.NewClient(&redis.Options{ redisClient = redis.NewClient(&redis.Options{
Addr: redisURL, Addr: redisURL,
}) })
Actor.GenerateSelfKey(Hostname, &Hostkey.PublicKey) Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey)
var macConfig = &config.Config{ machineryConfig := &config.Config{
Broker: "redis://" + redisURL, Broker: "redis://" + redisURL,
DefaultQueue: "relay", DefaultQueue: "relay",
ResultBackend: "redis://" + redisURL, ResultBackend: "redis://" + redisURL,
ResultsExpireIn: 5, ResultsExpireIn: 5,
} }
server, err := machinery.NewServer(macConfig) server, err := machinery.NewServer(machineryConfig)
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }