Export/Import json & Refactoring-1812 (#4)

* Config Export.

* Import feature.

* Refactoring config accessibility.
This commit is contained in:
Naoki Kosaka 2018-12-09 18:22:15 +09:00 committed by GitHub
parent 99c49ba797
commit 439eeacbbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 347 additions and 155 deletions

View File

@ -1,13 +1,10 @@
package relayconf
import "github.com/go-redis/redis"
import (
"strings"
// RelayConfig : struct for relay configuration
type RelayConfig struct {
BlockService bool
ManuallyAccept bool
CreateAsAnnounce bool
}
"github.com/go-redis/redis"
)
type Config int
@ -17,41 +14,136 @@ const (
CreateAsAnnounce
)
func (c *RelayConfig) Load(r *redis.Client) {
blockService, err := r.HGet("relay:config", "block_service").Result()
if err != nil {
c.Set(r, BlockService, false)
blockService = "0"
}
manuallyAccept, err := r.HGet("relay:config", "manually_accept").Result()
if err != nil {
c.Set(r, ManuallyAccept, false)
manuallyAccept = "0"
}
createAsAnnounce, err := r.HGet("relay:config", "create_as_announce").Result()
if err != nil {
c.Set(r, CreateAsAnnounce, false)
createAsAnnounce = "0"
}
c.BlockService = blockService == "1"
c.ManuallyAccept = manuallyAccept == "1"
c.CreateAsAnnounce = createAsAnnounce == "1"
func NewConfig(r *redis.Client) ExportConfig {
var config ExportConfig
config.RedisClient = r
config.Load()
return config
}
func (c *RelayConfig) Set(r *redis.Client, key Config, value bool) {
type ExportConfig struct {
RedisClient *redis.Client
RelayConfig relayConfig `json:"relayConfig"`
LimitedDomains []string `json:"limitedDomains"`
BlockedDomains []string `json:"blockedDomains"`
Subscriptions []Subscription `json:"subscriptions"`
}
func (config *ExportConfig) Load() {
config.RelayConfig.load(config.RedisClient)
var limitedDomains []string
var blockedDomains []string
var subscriptions []Subscription
domains, _ := config.RedisClient.HKeys("relay:config:limitedDomain").Result()
for _, domain := range domains {
limitedDomains = append(limitedDomains, domain)
}
domains, _ = config.RedisClient.HKeys("relay:config:blockedDomain").Result()
for _, domain := range domains {
blockedDomains = append(blockedDomains, domain)
}
domains, _ = config.RedisClient.Keys("relay:subscription:*").Result()
for _, domain := range domains {
domainName := strings.Replace(domain, "relay:subscription:", "", 1)
inboxURL, _ := config.RedisClient.HGet(domain, "inbox_url").Result()
activityID, err := config.RedisClient.HGet(domain, "activity_id").Result()
if err != nil {
activityID = ""
}
actorID, err := config.RedisClient.HGet(domain, "actor_id").Result()
if err != nil {
actorID = ""
}
subscriptions = append(subscriptions, Subscription{domainName, inboxURL, activityID, actorID})
}
config.LimitedDomains = limitedDomains
config.BlockedDomains = blockedDomains
config.Subscriptions = subscriptions
}
func (config *ExportConfig) SetConfig(key Config, value bool) {
strValue := 0
if value {
strValue = 1
}
switch key {
case BlockService:
c.BlockService = value
r.HSet("relay:config", "block_service", strValue)
config.RedisClient.HSet("relay:config", "block_service", strValue).Result()
case ManuallyAccept:
c.ManuallyAccept = value
r.HSet("relay:config", "manually_accept", strValue)
config.RedisClient.HSet("relay:config", "manually_accept", strValue).Result()
case CreateAsAnnounce:
c.CreateAsAnnounce = value
r.HSet("relay:config", "create_as_announce", strValue)
config.RedisClient.HSet("relay:config", "create_as_announce", strValue).Result()
}
config.Load()
}
func (config *ExportConfig) AddSubscription(domain Subscription) {
config.RedisClient.HMSet("relay:subscription:"+domain.Domain, map[string]interface{}{
"inbox_url": domain.InboxURL,
"activity_id": domain.ActivityID,
"actor_id": domain.ActorID,
})
config.Load()
}
func (config *ExportConfig) DelSubscription(domain string) {
config.RedisClient.Del("relay:subscription:" + domain).Result()
config.RedisClient.Del("relay:pending:" + domain).Result()
config.Load()
}
func (config *ExportConfig) SetBlockedDomain(domain string, value bool) {
if value {
config.RedisClient.HSet("relay:config:blockedDomain", domain, "1").Result()
} else {
config.RedisClient.HDel("relay:config:blockedDomain", domain).Result()
}
config.Load()
}
func (config *ExportConfig) SetLimitedDomain(domain string, value bool) {
if value {
config.RedisClient.HSet("relay:config:limitedDomain", domain, "1").Result()
} else {
config.RedisClient.HDel("relay:config:limitedDomain", domain).Result()
}
config.Load()
}
type Subscription struct {
Domain string `json:"domain"`
InboxURL string `json:"inbox_url"`
ActivityID string `json:"activity_id"`
ActorID string `json:"actor_id"`
}
// RelayConfig : struct for relay configuration
type relayConfig struct {
BlockService bool `json:"blockService"`
ManuallyAccept bool `json:"manuallyAccept"`
CreateAsAnnounce bool `json:"createAsAnnounce"`
}
func (config *relayConfig) load(r *redis.Client) {
blockService, err := r.HGet("relay:config", "block_service").Result()
if err != nil {
blockService = "0"
}
manuallyAccept, err := r.HGet("relay:config", "manually_accept").Result()
if err != nil {
manuallyAccept = "0"
}
createAsAnnounce, err := r.HGet("relay:config", "create_as_announce").Result()
if err != nil {
createAsAnnounce = "0"
}
config.BlockService = blockService == "1"
config.ManuallyAccept = manuallyAccept == "1"
config.CreateAsAnnounce = createAsAnnounce == "1"
}

View File

@ -19,7 +19,7 @@ var hostname *url.URL
var hostkey *rsa.PrivateKey
var redClient *redis.Client
var macServer *machinery.Server
var relConfig relayconf.RelayConfig
var exportConfig relayconf.ExportConfig
func main() {
pemPath := os.Getenv("ACTOR_PEM")
@ -111,6 +111,22 @@ func main() {
Usage: "Show all relay configrations",
Action: listConfigs,
},
{
Name: "export",
Usage: "Export all relay information (json)",
Action: exportConfigs,
},
{
Name: "import",
Usage: "Import relay information (json)",
Action: importConfigs,
Flags: []cli.Flag{
cli.StringFlag{
Name: "json, j",
Usage: "json file path",
},
},
},
{
Name: "service-block",
Usage: "Enable blocking for service-type actor",
@ -181,6 +197,7 @@ func main() {
},
}
exportConfig = relayconf.NewConfig(redClient)
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)

View File

@ -9,6 +9,7 @@ import (
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/RelayConf"
)
func TestMain(m *testing.M) {
@ -30,7 +31,8 @@ func TestMain(m *testing.M) {
}
macServer, _ = machinery.NewServer(macConfig)
redClient.FlushAll().Result()
relConfig.Load(redClient)
exportConfig = relayconf.NewConfig(redClient)
code := m.Run()
os.Exit(code)
redClient.FlushAll().Result()
}

View File

@ -1,7 +1,10 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/urfave/cli"
"github.com/yukimochi/Activity-Relay/RelayConf"
@ -15,38 +18,86 @@ const (
func serviceBlock(c *cli.Context) {
if c.Bool("undo") {
relConfig.Set(redClient, BlockService, false)
exportConfig.SetConfig(BlockService, false)
fmt.Println("Blocking for service-type actor is Disabled.")
} else {
relConfig.Set(redClient, BlockService, true)
exportConfig.SetConfig(BlockService, true)
fmt.Println("Blocking for service-type actor is Enabled.")
}
}
func manuallyAccept(c *cli.Context) {
if c.Bool("undo") {
relConfig.Set(redClient, ManuallyAccept, false)
exportConfig.SetConfig(ManuallyAccept, false)
fmt.Println("Manually accept follow-request is Disabled.")
} else {
relConfig.Set(redClient, ManuallyAccept, true)
exportConfig.SetConfig(ManuallyAccept, true)
fmt.Println("Manually accept follow-request is Enabled.")
}
}
func createAsAnnounce(c *cli.Context) {
if c.Bool("undo") {
relConfig.Set(redClient, CreateAsAnnounce, false)
exportConfig.SetConfig(CreateAsAnnounce, false)
fmt.Println("Announce activity instead of relay create activity is Disabled.")
} else {
relConfig.Set(redClient, CreateAsAnnounce, true)
exportConfig.SetConfig(CreateAsAnnounce, true)
fmt.Println("Announce activity instead of relay create activity is Enabled.")
}
}
func listConfigs(c *cli.Context) {
relConfig.Load(redClient)
fmt.Println("Blocking for service-type actor : ", relConfig.BlockService)
fmt.Println("Manually accept follow-request : ", relConfig.ManuallyAccept)
fmt.Println("Announce activity instead of relay create activity : ", relConfig.CreateAsAnnounce)
fmt.Println("Blocking for service-type actor : ", exportConfig.RelayConfig.BlockService)
fmt.Println("Manually accept follow-request : ", exportConfig.RelayConfig.ManuallyAccept)
fmt.Println("Announce activity instead of relay create activity : ", exportConfig.RelayConfig.CreateAsAnnounce)
}
func exportConfigs(c *cli.Context) {
jsonData, _ := json.Marshal(&exportConfig)
fmt.Println(string(jsonData))
}
func importConfigs(c *cli.Context) {
file, err := os.Open(c.String("json"))
if err != nil {
fmt.Println(err)
return
}
jsonData, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
return
}
var data relayconf.ExportConfig
err = json.Unmarshal(jsonData, &data)
if err != nil {
fmt.Println(err)
return
}
if data.RelayConfig.BlockService {
exportConfig.SetConfig(BlockService, true)
}
if data.RelayConfig.ManuallyAccept {
exportConfig.SetConfig(ManuallyAccept, true)
}
if data.RelayConfig.CreateAsAnnounce {
exportConfig.SetConfig(CreateAsAnnounce, true)
}
for _, LimitedDomain := range data.LimitedDomains {
exportConfig.SetLimitedDomain(LimitedDomain, true)
redClient.HSet("relay:config:limitedDomain", LimitedDomain, "1")
}
for _, BlockedDomain := range data.BlockedDomains {
exportConfig.SetLimitedDomain(BlockedDomain, true)
redClient.HSet("relay:config:blockedDomain", BlockedDomain, "1")
}
for _, Subscription := range data.Subscriptions {
exportConfig.AddSubscription(relayconf.Subscription{
Domain: Subscription.Domain,
InboxURL: Subscription.InboxURL,
ActivityID: Subscription.ActivityID,
ActorID: Subscription.ActorID,
})
}
}

View File

@ -25,14 +25,14 @@ func TestServiceBlock(t *testing.T) {
fooCmd,
}
relConfig.Set(redClient, BlockService, false)
exportConfig.SetConfig(BlockService, false)
app.Run([]string{"", "service-block"})
if !relConfig.BlockService {
if !exportConfig.RelayConfig.BlockService {
t.Fatalf("Not Enabled ServiceBlock feature,")
}
app.Run([]string{"", "service-block", "-u"})
if relConfig.BlockService {
if exportConfig.RelayConfig.BlockService {
t.Fatalf("Not Disabled ServiceBlock feature,")
}
}
@ -54,14 +54,14 @@ func TestManuallyAccept(t *testing.T) {
fooCmd,
}
relConfig.Set(redClient, ManuallyAccept, false)
exportConfig.SetConfig(ManuallyAccept, false)
app.Run([]string{"", "manually-accept"})
if !relConfig.ManuallyAccept {
if !exportConfig.RelayConfig.ManuallyAccept {
t.Fatalf("Not Enabled Manually accept follow-request feature,")
}
app.Run([]string{"", "manually-accept", "-u"})
if relConfig.ManuallyAccept {
if exportConfig.RelayConfig.ManuallyAccept {
t.Fatalf("Not Disabled Manually accept follow-request feature,")
}
}
@ -83,14 +83,14 @@ func TestCreateAsAnnounce(t *testing.T) {
fooCmd,
}
relConfig.Set(redClient, CreateAsAnnounce, false)
exportConfig.SetConfig(CreateAsAnnounce, false)
app.Run([]string{"", "create-as-announce"})
if !relConfig.CreateAsAnnounce {
if !exportConfig.RelayConfig.CreateAsAnnounce {
t.Fatalf("Not Enabled Announce activity instead of relay create activity feature,")
}
app.Run([]string{"", "create-as-announce", "-u"})
if relConfig.CreateAsAnnounce {
if exportConfig.RelayConfig.CreateAsAnnounce {
t.Fatalf("Not Disabled Announce activity instead of relay create activity feature,")
}
}
@ -106,9 +106,9 @@ func TestListConfigs(t *testing.T) {
fooCmd,
}
relConfig.Set(redClient, BlockService, true)
relConfig.Set(redClient, ManuallyAccept, true)
relConfig.Set(redClient, CreateAsAnnounce, true)
exportConfig.SetConfig(BlockService, true)
exportConfig.SetConfig(ManuallyAccept, true)
exportConfig.SetConfig(CreateAsAnnounce, true)
out := capturer.CaptureStdout(func() {
app.Run([]string{"", "show"})
})
@ -130,9 +130,9 @@ func TestListConfigs(t *testing.T) {
}
}
relConfig.Set(redClient, BlockService, false)
relConfig.Set(redClient, ManuallyAccept, false)
relConfig.Set(redClient, CreateAsAnnounce, false)
exportConfig.SetConfig(BlockService, false)
exportConfig.SetConfig(ManuallyAccept, false)
exportConfig.SetConfig(CreateAsAnnounce, false)
out = capturer.CaptureStdout(func() {
app.Run([]string{"", "show"})
})

View File

@ -2,39 +2,26 @@ package main
import (
"fmt"
"strings"
"github.com/urfave/cli"
)
func listDomains(c *cli.Context) error {
var err error
var domains []string
var message string
switch c.String("type") {
case "limited":
fmt.Println(" - Limited domain :")
domains, err = redClient.HKeys("relay:config:limitedDomain").Result()
if err != nil {
return err
}
domains = exportConfig.LimitedDomains
case "blocked":
fmt.Println(" - Blocked domain :")
domains, err = redClient.HKeys("relay:config:blockedDomain").Result()
if err != nil {
return err
}
domains = exportConfig.BlockedDomains
default:
fmt.Println(" - Subscribed domain :")
temp, err := redClient.Keys("relay:subscription:*").Result()
if err != nil {
return err
}
temp := exportConfig.Subscriptions
for _, domain := range temp {
domains = append(domains, strings.Replace(domain, "relay:subscription:", "", 1))
domains = append(domains, domain.Domain)
}
}
fmt.Println(message)
for _, domain := range domains {
fmt.Println(domain)
}
@ -50,18 +37,18 @@ func setDomainType(c *cli.Context) error {
switch c.String("type") {
case "limited":
if c.Bool("undo") {
redClient.HDel("relay:config:limitedDomain", c.String("domain"))
exportConfig.SetLimitedDomain(c.String("domain"), false)
fmt.Println("Unset [" + c.String("domain") + "] as Limited domain.")
} else {
redClient.HSet("relay:config:limitedDomain", c.String("domain"), "1")
exportConfig.SetLimitedDomain(c.String("domain"), true)
fmt.Println("Set [" + c.String("domain") + "] as Limited domain.")
}
case "blocked":
if c.Bool("undo") {
redClient.HDel("relay:config:blockedDomain", c.String("domain"))
exportConfig.SetBlockedDomain(c.String("domain"), false)
fmt.Println("Unset [" + c.String("domain") + "] as Blocked domain.")
} else {
redClient.HSet("relay:config:blockedDomain", c.String("domain"), "1")
exportConfig.SetBlockedDomain(c.String("domain"), true)
fmt.Println("Set [" + c.String("domain") + "] as Blocked domain.")
}
default:

View File

@ -6,10 +6,10 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/RichardKnop/machinery/v1/tasks"
"github.com/yukimochi/Activity-Relay/ActivityPub"
"github.com/yukimochi/Activity-Relay/RelayConf"
)
func handleWebfinger(w http.ResponseWriter, r *http.Request) {
@ -60,15 +60,20 @@ func contains(entries interface{}, finder string) bool {
}
}
return false
case []relayconf.Subscription:
for i := 0; i < len(entry); i++ {
if entry[i].Domain == finder {
return true
}
}
return false
}
return false
}
func pushRelayJob(sourceInbox string, body []byte) {
domains, _ := redClient.Keys("relay:subscription:*").Result()
for _, domain := range domains {
if sourceInbox != strings.Replace(domain, "relay:subscription:", "", 1) {
inboxURL, _ := redClient.HGet(domain, "inbox_url").Result()
for _, domain := range exportConfig.Subscriptions {
if sourceInbox != domain.Domain {
job := &tasks.Signature{
Name: "relay",
RetryCount: 0,
@ -76,7 +81,7 @@ func pushRelayJob(sourceInbox string, body []byte) {
{
Name: "inboxURL",
Type: "string",
Value: inboxURL,
Value: domain.InboxURL,
},
{
Name: "body",
@ -134,8 +139,8 @@ func unFollowAcceptable(activity *activitypub.Activity, actor *activitypub.Actor
func suitableFollow(activity *activitypub.Activity, actor *activitypub.Actor) bool {
domain, _ := url.Parse(activity.Actor)
blocked, _ := redClient.HExists("relay:config:blockedDomain", domain.Host).Result()
if blocked {
fmt.Println(exportConfig.BlockedDomains, domain.Host)
if contains(exportConfig.BlockedDomains, domain.Host) {
return false
}
return true
@ -146,20 +151,18 @@ func relayAcceptable(activity *activitypub.Activity, actor *activitypub.Actor) e
return errors.New("Activity should contain https://www.w3.org/ns/activitystreams#Public as receiver")
}
domain, _ := url.Parse(activity.Actor)
exist, _ := redClient.Exists("relay:subscription:" + domain.Host).Result()
if exist == 0 {
return errors.New("To use the relay service, Subscribe me in advance")
if contains(exportConfig.Subscriptions, domain.Host) {
return nil
}
return nil
return errors.New("To use the relay service, Subscribe me in advance")
}
func suitableRelay(activity *activitypub.Activity, actor *activitypub.Actor) bool {
domain, _ := url.Parse(activity.Actor)
limited, _ := redClient.HExists("relay:config:limitedDomain", domain.Host).Result()
if limited {
if contains(exportConfig.LimitedDomains, domain.Host) {
return false
}
if relConfig.BlockService && actor.Type == "Service" {
if exportConfig.RelayConfig.BlockService && actor.Type == "Service" {
return false
}
return true
@ -173,6 +176,7 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
w.WriteHeader(400)
w.Write(nil)
} else {
exportConfig.Load()
domain, _ := url.Parse(activity.Actor)
switch activity.Type {
case "Follow":
@ -187,7 +191,7 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
w.Write(nil)
} else {
if suitableFollow(activity, actor) {
if relConfig.ManuallyAccept {
if exportConfig.RelayConfig.ManuallyAccept {
redClient.HMSet("relay:pending:"+domain.Host, map[string]interface{}{
"inbox_url": actor.Endpoints.SharedInbox,
"activity_id": activity.ID,
@ -200,7 +204,12 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
resp := activity.GenerateResponse(hostname, "Accept")
jsonData, _ := json.Marshal(&resp)
go pushRegistorJob(actor.Inbox, jsonData)
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", actor.Endpoints.SharedInbox)
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: actor.Endpoints.SharedInbox,
ActivityID: activity.ID,
ActorID: actor.ID,
})
fmt.Println("Accept Follow Request : ", activity.Actor)
}
} else {
@ -249,7 +258,7 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
w.Write([]byte(err.Error()))
} else {
if suitableRelay(activity, actor) {
if relConfig.CreateAsAnnounce && activity.Type == "Create" {
if exportConfig.RelayConfig.CreateAsAnnounce && activity.Type == "Create" {
nestedObject, err := activity.NestedActivity()
if err != nil {
fmt.Println("Fail Assert activity : activity.Actor")

View File

@ -8,6 +8,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"strconv"
"testing"
"github.com/yukimochi/Activity-Relay/ActivityPub"
@ -265,7 +266,7 @@ func TestSuitableRelayNoBlockService(t *testing.T) {
personActor := mockActor("Person")
serviceActor := mockActor("Service")
relConfig.Set(redClient, BlockService, false)
exportConfig.SetConfig(BlockService, false)
if suitableRelay(&activity, &personActor) != true {
t.Fatalf("Failed - Person status not relay")
@ -280,7 +281,7 @@ func TestSuitableRelayBlockService(t *testing.T) {
personActor := mockActor("Person")
serviceActor := mockActor("Service")
relConfig.Set(redClient, BlockService, true)
exportConfig.SetConfig(BlockService, true)
if suitableRelay(&activity, &personActor) != true {
t.Fatalf("Failed - Person status not relay")
@ -288,7 +289,7 @@ func TestSuitableRelayBlockService(t *testing.T) {
if suitableRelay(&activity, &serviceActor) != false {
t.Fatalf("Failed - Service status may relay when blocking mode")
}
relConfig.Set(redClient, BlockService, false)
exportConfig.SetConfig(BlockService, false)
}
func TestHandleInboxNoSignature(t *testing.T) {
@ -334,8 +335,6 @@ func TestHandleInboxValidFollow(t *testing.T) {
}))
defer s.Close()
relConfig.Set(redClient, ManuallyAccept, false)
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
r, err := client.Do(req)
@ -343,14 +342,13 @@ func TestHandleInboxValidFollow(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 {
t.Fatalf("Failed - Subscription not works.")
}
redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:pending:" + domain.Host).Result()
exportConfig.DelSubscription(domain.Host)
}
func TestHandleInboxValidManuallyFollow(t *testing.T) {
@ -363,7 +361,7 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
defer s.Close()
// Switch Manually
relConfig.Set(redClient, ManuallyAccept, true)
exportConfig.SetConfig(ManuallyAccept, true)
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -372,7 +370,7 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redClient.Exists("relay:pending:" + domain.Host).Result()
if res != 1 {
@ -382,9 +380,8 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
if res != 0 {
t.Fatalf("Failed - Pending was skipped.")
}
redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:pending:" + domain.Host).Result()
relConfig.Set(redClient, ManuallyAccept, false)
exportConfig.DelSubscription(domain.Host)
exportConfig.SetConfig(ManuallyAccept, false)
}
func TestHandleInboxInvalidFollow(t *testing.T) {
@ -396,7 +393,7 @@ func TestHandleInboxInvalidFollow(t *testing.T) {
}))
defer s.Close()
relConfig.Set(redClient, ManuallyAccept, false)
exportConfig.SetConfig(ManuallyAccept, false)
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -405,7 +402,7 @@ func TestHandleInboxInvalidFollow(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 {
@ -422,7 +419,7 @@ func TestHandleInboxValidFollowBlocked(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:config:blockedDomain", domain.Host, "1").Result()
exportConfig.SetBlockedDomain(domain.Host, true)
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -431,15 +428,14 @@ func TestHandleInboxValidFollowBlocked(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 {
t.Fatalf("Failed - Subscription not blocked.")
}
redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:pending:" + domain.Host).Result()
redClient.Del("relay:config:blockedDomain", domain.Host).Result()
exportConfig.DelSubscription(domain.Host)
exportConfig.SetBlockedDomain(domain.Host, false)
}
func TestHandleInboxValidUnfollow(t *testing.T) {
@ -451,7 +447,10 @@ func TestHandleInboxValidUnfollow(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -460,13 +459,13 @@ func TestHandleInboxValidUnfollow(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 {
t.Fatalf("Failed - Subscription not succeed.")
}
redClient.Del("relay:subscription:" + domain.Host).Result()
exportConfig.DelSubscription(domain.Host)
}
func TestHandleInboxInvalidUnfollow(t *testing.T) {
@ -478,7 +477,10 @@ func TestHandleInboxInvalidUnfollow(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -493,7 +495,7 @@ func TestHandleInboxInvalidUnfollow(t *testing.T) {
if res != 1 {
t.Fatalf("Failed - Block hacked unfollow not succeed.")
}
redClient.Del("relay:subscription:" + domain.Host).Result()
exportConfig.DelSubscription(domain.Host)
}
func TestHandleInboxUnfollowAsActor(t *testing.T) {
@ -505,7 +507,10 @@ func TestHandleInboxUnfollowAsActor(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -520,7 +525,7 @@ func TestHandleInboxUnfollowAsActor(t *testing.T) {
if res != 1 {
t.Fatalf("Failed - Block actor unfollow not succeed.")
}
redClient.Del("relay:subscription:" + domain.Host).Result()
exportConfig.DelSubscription(domain.Host)
}
func TestHandleInboxValidCreate(t *testing.T) {
@ -532,8 +537,14 @@ func TestHandleInboxValidCreate(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
redClient.HSet("relay:subscription:example.org", "inbox_url", "https://example.org/inbox").Result()
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
exportConfig.AddSubscription(relayconf.Subscription{
Domain: "example.org",
InboxURL: "https://example.org/inbox",
})
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -542,8 +553,10 @@ func TestHandleInboxValidCreate(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
exportConfig.DelSubscription(domain.Host)
exportConfig.DelSubscription("example.org")
redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:subscription:example.org").Result()
}
@ -557,8 +570,11 @@ func TestHandleInboxlimitedCreate(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
redClient.HSet("relay:config:limitedDomain", domain.Host, "1").Result()
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
exportConfig.SetLimitedDomain(domain.Host, true)
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -567,10 +583,10 @@ func TestHandleInboxlimitedCreate(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:config:limitedDomain", domain.Host).Result()
exportConfig.DelSubscription(domain.Host)
exportConfig.SetLimitedDomain(domain.Host, false)
}
func TestHandleInboxValidCreateAsAnnounceNote(t *testing.T) {
@ -582,9 +598,15 @@ func TestHandleInboxValidCreateAsAnnounceNote(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
redClient.HSet("relay:subscription:example.org", "inbox_url", "https://example.org/inbox").Result()
relConfig.Set(redClient, CreateAsAnnounce, true)
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
exportConfig.AddSubscription(relayconf.Subscription{
Domain: "example.org",
InboxURL: "https://example.org/inbox",
})
exportConfig.SetConfig(CreateAsAnnounce, true)
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -593,11 +615,11 @@ func TestHandleInboxValidCreateAsAnnounceNote(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:subscription:example.org").Result()
relConfig.Set(redClient, CreateAsAnnounce, false)
exportConfig.DelSubscription(domain.Host)
exportConfig.DelSubscription("example.org")
exportConfig.SetConfig(CreateAsAnnounce, false)
}
func TestHandleInboxValidCreateAsAnnounceNoNote(t *testing.T) {
@ -609,9 +631,15 @@ func TestHandleInboxValidCreateAsAnnounceNoNote(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
redClient.HSet("relay:subscription:example.org", "inbox_url", "https://example.org/inbox").Result()
relConfig.Set(redClient, CreateAsAnnounce, true)
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
exportConfig.AddSubscription(relayconf.Subscription{
Domain: "example.org",
InboxURL: "https://example.org/inbox",
})
exportConfig.SetConfig(CreateAsAnnounce, true)
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -620,11 +648,11 @@ func TestHandleInboxValidCreateAsAnnounceNoNote(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:subscription:example.org").Result()
relConfig.Set(redClient, CreateAsAnnounce, false)
exportConfig.DelSubscription(domain.Host)
exportConfig.DelSubscription("example.org")
exportConfig.SetConfig(CreateAsAnnounce, false)
}
func TestHandleInboxUnsubscriptionCreate(t *testing.T) {
@ -655,7 +683,10 @@ func TestHandleInboxUndo(t *testing.T) {
}))
defer s.Close()
redClient.HSet("relay:subscription:"+domain.Host, "inbox_url", "https://mastodon.test.yukimochi.io/inbox").Result()
exportConfig.AddSubscription(relayconf.Subscription{
Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox",
})
req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client)
@ -664,11 +695,11 @@ func TestHandleInboxUndo(t *testing.T) {
t.Fatalf("Failed - " + err.Error())
}
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202")
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 {
t.Fatalf("Failed - Missing unsubscribed.")
}
redClient.Del("relay:subscription:" + domain.Host).Result()
exportConfig.DelSubscription(domain.Host)
}

View File

@ -25,7 +25,7 @@ var hostname *url.URL
var hostkey *rsa.PrivateKey
var redClient *redis.Client
var macServer *machinery.Server
var relConfig relayconf.RelayConfig
var exportConfig relayconf.ExportConfig
func main() {
pemPath := os.Getenv("ACTOR_PEM")
@ -74,7 +74,7 @@ func main() {
WebfingerResource.GenerateFromActor(hostname, &Actor)
// Load Config
relConfig.Load(redClient)
exportConfig = relayconf.NewConfig(redClient)
http.HandleFunc("/.well-known/webfinger", handleWebfinger)
http.HandleFunc("/actor", handleActor)

View File

@ -9,6 +9,7 @@ import (
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/RelayConf"
)
func TestMain(m *testing.M) {
@ -33,8 +34,10 @@ func TestMain(m *testing.M) {
Actor.GenerateSelfKey(hostname, &hostkey.PublicKey)
WebfingerResource.GenerateFromActor(hostname, &Actor)
// Load Config
redClient.FlushAll().Result()
relConfig.Load(redClient)
exportConfig = relayconf.NewConfig(redClient)
code := m.Run()
os.Exit(code)
redClient.FlushAll().Result()
}