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" "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 := sha256.New()
hash.Write(*body) hash.Write(*body)
b := hash.Sum(nil) b := hash.Sum(nil)
r.Header.Set("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(b)) request.Header.Set("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(b))
r.Header.Set("Host", r.Host) 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) signer, _, err := httpsig.NewSigner([]httpsig.Algorithm{httpsig.RSA_SHA256}, []string{httpsig.RequestTarget, "Host", "Date", "Digest", "Content-Type"}, httpsig.Signature)
if err != nil { if err != nil {
return err return err
} }
err = signer.SignRequest(pKey, KeyID, r) err = signer.SignRequest(publicKey, KeyID, request)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
// SendActivity : Send ActivityPub Activity. // SendActivity : Send ActivityPub activity
func SendActivity(inboxURL string, KeyID string, refBytes []byte, pKey *rsa.PrivateKey) error { func SendActivity(inboxURL string, KeyID string, body []byte, publicKey *rsa.PrivateKey) error {
req, _ := http.NewRequest("POST", inboxURL, bytes.NewBuffer(refBytes)) req, _ := http.NewRequest("POST", inboxURL, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/activity+json, application/ld+json") 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())) 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} client := &http.Client{Timeout: time.Duration(5) * time.Second}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {

View File

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

View File

@ -34,13 +34,13 @@ func ReadPublicKeyRSAfromString(pemString string) (*rsa.PublicKey, error) {
return pub, nil return pub, nil
} }
func GeneratePublicKeyPEMString(pub *rsa.PublicKey) string { func GeneratePublicKeyPEMString(publicKey *rsa.PublicKey) string {
pubkeyBytes := x509.MarshalPKCS1PublicKey(pub) publicKeyByte := x509.MarshalPKCS1PublicKey(publicKey)
pubkeyPem := pem.EncodeToMemory( publicKeyPem := pem.EncodeToMemory(
&pem.Block{ &pem.Block{
Type: "RSA PUBLIC KEY", Type: "RSA PUBLIC KEY",
Bytes: pubkeyBytes, Bytes: publicKeyByte,
}, },
) )
return string(pubkeyPem) return string(publicKeyPem)
} }

View File

@ -1,4 +1,4 @@
package relayconf package state
import ( import (
"strings" "strings"
@ -6,23 +6,29 @@ import (
"github.com/go-redis/redis" "github.com/go-redis/redis"
) )
// Config : Enum for RelayConfig
type Config int type Config int
const ( const (
// BlockService : Blocking for service-type actor
BlockService Config = iota BlockService Config = iota
// ManuallyAccept : Manually accept follow-request
ManuallyAccept ManuallyAccept
// CreateAsAnnounce : Announce activity instead of relay create activity
CreateAsAnnounce CreateAsAnnounce
) )
func NewConfig(r *redis.Client) ExportConfig { // NewState : Create new RelayState instance with redis client
var config ExportConfig func NewState(redisClient *redis.Client) RelayState {
config.RedisClient = r var config RelayState
config.RedisClient = redisClient
config.Load() config.Load()
return config return config
} }
type ExportConfig struct { // RelayState : Store subscriptions and relay configrations
type RelayState struct {
RedisClient *redis.Client RedisClient *redis.Client
RelayConfig relayConfig `json:"relayConfig"` RelayConfig relayConfig `json:"relayConfig"`
@ -31,7 +37,8 @@ type ExportConfig struct {
Subscriptions []Subscription `json:"subscriptions"` Subscriptions []Subscription `json:"subscriptions"`
} }
func (config *ExportConfig) Load() { // Load : Refrash content from redis
func (config *RelayState) Load() {
config.RelayConfig.load(config.RedisClient) config.RelayConfig.load(config.RedisClient)
var limitedDomains []string var limitedDomains []string
var blockedDomains []string var blockedDomains []string
@ -63,7 +70,8 @@ func (config *ExportConfig) Load() {
config.Subscriptions = subscriptions config.Subscriptions = subscriptions
} }
func (config *ExportConfig) SetConfig(key Config, value bool) { // SetConfig : Set relay configration
func (config *RelayState) SetConfig(key Config, value bool) {
strValue := 0 strValue := 0
if value { if value {
strValue = 1 strValue = 1
@ -79,7 +87,8 @@ func (config *ExportConfig) SetConfig(key Config, value bool) {
config.Load() config.Load()
} }
func (config *ExportConfig) AddSubscription(domain Subscription) { // AddSubscription : Add new instance for subscription list
func (config *RelayState) AddSubscription(domain Subscription) {
config.RedisClient.HMSet("relay:subscription:"+domain.Domain, map[string]interface{}{ config.RedisClient.HMSet("relay:subscription:"+domain.Domain, map[string]interface{}{
"inbox_url": domain.InboxURL, "inbox_url": domain.InboxURL,
"activity_id": domain.ActivityID, "activity_id": domain.ActivityID,
@ -89,14 +98,16 @@ func (config *ExportConfig) AddSubscription(domain Subscription) {
config.Load() config.Load()
} }
func (config *ExportConfig) DelSubscription(domain string) { // DelSubscription : Delete instance from subscription list
func (config *RelayState) DelSubscription(domain string) {
config.RedisClient.Del("relay:subscription:" + domain).Result() config.RedisClient.Del("relay:subscription:" + domain).Result()
config.RedisClient.Del("relay:pending:" + domain).Result() config.RedisClient.Del("relay:pending:" + domain).Result()
config.Load() config.Load()
} }
func (config *ExportConfig) SetBlockedDomain(domain string, value bool) { // SetBlockedDomain : Set/Unset instance for blocked domain
func (config *RelayState) SetBlockedDomain(domain string, value bool) {
if value { if value {
config.RedisClient.HSet("relay:config:blockedDomain", domain, "1").Result() config.RedisClient.HSet("relay:config:blockedDomain", domain, "1").Result()
} else { } else {
@ -106,7 +117,8 @@ func (config *ExportConfig) SetBlockedDomain(domain string, value bool) {
config.Load() config.Load()
} }
func (config *ExportConfig) SetLimitedDomain(domain string, value bool) { // SetLimitedDomain : Set/Unset instance for limited domain
func (config *RelayState) SetLimitedDomain(domain string, value bool) {
if value { if value {
config.RedisClient.HSet("relay:config:limitedDomain", domain, "1").Result() config.RedisClient.HSet("relay:config:limitedDomain", domain, "1").Result()
} else { } else {
@ -116,6 +128,7 @@ func (config *ExportConfig) SetLimitedDomain(domain string, value bool) {
config.Load() config.Load()
} }
// Subscription : Instance subscription information
type Subscription struct { type Subscription struct {
Domain string `json:"domain"` Domain string `json:"domain"`
InboxURL string `json:"inbox_url"` InboxURL string `json:"inbox_url"`
@ -123,23 +136,22 @@ type Subscription struct {
ActorID string `json:"actor_id"` ActorID string `json:"actor_id"`
} }
// RelayConfig : struct for relay configuration
type relayConfig struct { type relayConfig struct {
BlockService bool `json:"blockService"` BlockService bool `json:"blockService"`
ManuallyAccept bool `json:"manuallyAccept"` ManuallyAccept bool `json:"manuallyAccept"`
CreateAsAnnounce bool `json:"createAsAnnounce"` CreateAsAnnounce bool `json:"createAsAnnounce"`
} }
func (config *relayConfig) load(r *redis.Client) { func (config *relayConfig) load(redisClient *redis.Client) {
blockService, err := r.HGet("relay:config", "block_service").Result() blockService, err := redisClient.HGet("relay:config", "block_service").Result()
if err != nil { if err != nil {
blockService = "0" blockService = "0"
} }
manuallyAccept, err := r.HGet("relay:config", "manually_accept").Result() manuallyAccept, err := redisClient.HGet("relay:config", "manually_accept").Result()
if err != nil { if err != nil {
manuallyAccept = "0" manuallyAccept = "0"
} }
createAsAnnounce, err := r.HGet("relay:config", "create_as_announce").Result() createAsAnnounce, err := redisClient.HGet("relay:config", "create_as_announce").Result()
if err != nil { if err != nil {
createAsAnnounce = "0" createAsAnnounce = "0"
} }

View File

@ -7,19 +7,19 @@ import (
"net/url" "net/url"
"os" "os"
machinery "github.com/RichardKnop/machinery/v1" "github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config" "github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis" "github.com/go-redis/redis"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/yukimochi/Activity-Relay/KeyLoader" "github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/RelayConf" "github.com/yukimochi/Activity-Relay/State"
) )
var hostname *url.URL var hostname *url.URL
var hostkey *rsa.PrivateKey var hostkey *rsa.PrivateKey
var redClient *redis.Client var redClient *redis.Client
var macServer *machinery.Server var macServer *machinery.Server
var exportConfig relayconf.ExportConfig var relayState state.RelayState
func main() { func main() {
pemPath := os.Getenv("ACTOR_PEM") pemPath := os.Getenv("ACTOR_PEM")
@ -140,7 +140,7 @@ func main() {
}, },
{ {
Name: "manually-accept", Name: "manually-accept",
Usage: "Enable Manually accept follow-request", Usage: "Enable manually accept follow-request",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "undo, u", Name: "undo, u",
@ -151,7 +151,7 @@ func main() {
}, },
{ {
Name: "create-as-announce", Name: "create-as-announce",
Usage: "Enable Announce activity instead of relay create activity (Not recommended)", Usage: "Enable announce activity instead of relay create activity (Not recommend)",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "undo, u", Name: "undo, u",
@ -197,7 +197,7 @@ func main() {
}, },
} }
exportConfig = relayconf.NewConfig(redClient) relayState = state.NewState(redClient)
err = app.Run(os.Args) err = app.Run(os.Args)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

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

View File

@ -7,53 +7,53 @@ import (
"os" "os"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/yukimochi/Activity-Relay/RelayConf" "github.com/yukimochi/Activity-Relay/State"
) )
const ( const (
BlockService relayconf.Config = iota BlockService state.Config = iota
ManuallyAccept ManuallyAccept
CreateAsAnnounce CreateAsAnnounce
) )
func serviceBlock(c *cli.Context) { func serviceBlock(c *cli.Context) {
if c.Bool("undo") { if c.Bool("undo") {
exportConfig.SetConfig(BlockService, false) relayState.SetConfig(BlockService, false)
fmt.Println("Blocking for service-type actor is Disabled.") fmt.Println("Blocking for service-type actor is Disabled.")
} else { } else {
exportConfig.SetConfig(BlockService, true) relayState.SetConfig(BlockService, true)
fmt.Println("Blocking for service-type actor is Enabled.") fmt.Println("Blocking for service-type actor is Enabled.")
} }
} }
func manuallyAccept(c *cli.Context) { func manuallyAccept(c *cli.Context) {
if c.Bool("undo") { if c.Bool("undo") {
exportConfig.SetConfig(ManuallyAccept, false) relayState.SetConfig(ManuallyAccept, false)
fmt.Println("Manually accept follow-request is Disabled.") fmt.Println("Manually accept follow-request is Disabled.")
} else { } else {
exportConfig.SetConfig(ManuallyAccept, true) relayState.SetConfig(ManuallyAccept, true)
fmt.Println("Manually accept follow-request is Enabled.") fmt.Println("Manually accept follow-request is Enabled.")
} }
} }
func createAsAnnounce(c *cli.Context) { func createAsAnnounce(c *cli.Context) {
if c.Bool("undo") { if c.Bool("undo") {
exportConfig.SetConfig(CreateAsAnnounce, false) relayState.SetConfig(CreateAsAnnounce, false)
fmt.Println("Announce activity instead of relay create activity is Disabled.") fmt.Println("Announce activity instead of relay create activity is Disabled.")
} else { } else {
exportConfig.SetConfig(CreateAsAnnounce, true) relayState.SetConfig(CreateAsAnnounce, true)
fmt.Println("Announce activity instead of relay create activity is Enabled.") fmt.Println("Announce activity instead of relay create activity is Enabled.")
} }
} }
func listConfigs(c *cli.Context) { func listConfigs(c *cli.Context) {
fmt.Println("Blocking for service-type actor : ", exportConfig.RelayConfig.BlockService) fmt.Println("Blocking for service-type actor : ", relayState.RelayConfig.BlockService)
fmt.Println("Manually accept follow-request : ", exportConfig.RelayConfig.ManuallyAccept) fmt.Println("Manually accept follow-request : ", relayState.RelayConfig.ManuallyAccept)
fmt.Println("Announce activity instead of relay create activity : ", exportConfig.RelayConfig.CreateAsAnnounce) fmt.Println("Announce activity instead of relay create activity : ", relayState.RelayConfig.CreateAsAnnounce)
} }
func exportConfigs(c *cli.Context) { func exportConfigs(c *cli.Context) {
jsonData, _ := json.Marshal(&exportConfig) jsonData, _ := json.Marshal(&relayState)
fmt.Println(string(jsonData)) fmt.Println(string(jsonData))
} }
@ -68,7 +68,7 @@ func importConfigs(c *cli.Context) {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
return return
} }
var data relayconf.ExportConfig var data state.RelayState
err = json.Unmarshal(jsonData, &data) err = json.Unmarshal(jsonData, &data)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
@ -76,24 +76,24 @@ func importConfigs(c *cli.Context) {
} }
if data.RelayConfig.BlockService { if data.RelayConfig.BlockService {
exportConfig.SetConfig(BlockService, true) relayState.SetConfig(BlockService, true)
} }
if data.RelayConfig.ManuallyAccept { if data.RelayConfig.ManuallyAccept {
exportConfig.SetConfig(ManuallyAccept, true) relayState.SetConfig(ManuallyAccept, true)
} }
if data.RelayConfig.CreateAsAnnounce { if data.RelayConfig.CreateAsAnnounce {
exportConfig.SetConfig(CreateAsAnnounce, true) relayState.SetConfig(CreateAsAnnounce, true)
} }
for _, LimitedDomain := range data.LimitedDomains { for _, LimitedDomain := range data.LimitedDomains {
exportConfig.SetLimitedDomain(LimitedDomain, true) relayState.SetLimitedDomain(LimitedDomain, true)
redClient.HSet("relay:config:limitedDomain", LimitedDomain, "1") redClient.HSet("relay:config:limitedDomain", LimitedDomain, "1")
} }
for _, BlockedDomain := range data.BlockedDomains { for _, BlockedDomain := range data.BlockedDomains {
exportConfig.SetLimitedDomain(BlockedDomain, true) relayState.SetLimitedDomain(BlockedDomain, true)
redClient.HSet("relay:config:blockedDomain", BlockedDomain, "1") redClient.HSet("relay:config:blockedDomain", BlockedDomain, "1")
} }
for _, Subscription := range data.Subscriptions { for _, Subscription := range data.Subscriptions {
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: Subscription.Domain, Domain: Subscription.Domain,
InboxURL: Subscription.InboxURL, InboxURL: Subscription.InboxURL,
ActivityID: Subscription.ActivityID, ActivityID: Subscription.ActivityID,

View File

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

View File

@ -11,13 +11,13 @@ func listDomains(c *cli.Context) error {
switch c.String("type") { switch c.String("type") {
case "limited": case "limited":
fmt.Println(" - Limited domain :") fmt.Println(" - Limited domain :")
domains = exportConfig.LimitedDomains domains = relayState.LimitedDomains
case "blocked": case "blocked":
fmt.Println(" - Blocked domain :") fmt.Println(" - Blocked domain :")
domains = exportConfig.BlockedDomains domains = relayState.BlockedDomains
default: default:
fmt.Println(" - Subscribed domain :") fmt.Println(" - Subscribed domain :")
temp := exportConfig.Subscriptions temp := relayState.Subscriptions
for _, domain := range temp { for _, domain := range temp {
domains = append(domains, domain.Domain) domains = append(domains, domain.Domain)
} }
@ -37,18 +37,18 @@ func setDomainType(c *cli.Context) error {
switch c.String("type") { switch c.String("type") {
case "limited": case "limited":
if c.Bool("undo") { if c.Bool("undo") {
exportConfig.SetLimitedDomain(c.String("domain"), false) relayState.SetLimitedDomain(c.String("domain"), false)
fmt.Println("Unset [" + c.String("domain") + "] as Limited domain.") fmt.Println("Unset [" + c.String("domain") + "] as Limited domain.")
} else { } else {
exportConfig.SetLimitedDomain(c.String("domain"), true) relayState.SetLimitedDomain(c.String("domain"), true)
fmt.Println("Set [" + c.String("domain") + "] as Limited domain.") fmt.Println("Set [" + c.String("domain") + "] as Limited domain.")
} }
case "blocked": case "blocked":
if c.Bool("undo") { if c.Bool("undo") {
exportConfig.SetBlockedDomain(c.String("domain"), false) relayState.SetBlockedDomain(c.String("domain"), false)
fmt.Println("Unset [" + c.String("domain") + "] as Blocked domain.") fmt.Println("Unset [" + c.String("domain") + "] as Blocked domain.")
} else { } else {
exportConfig.SetBlockedDomain(c.String("domain"), true) relayState.SetBlockedDomain(c.String("domain"), true)
fmt.Println("Set [" + c.String("domain") + "] as Blocked domain.") fmt.Println("Set [" + c.String("domain") + "] as Blocked domain.")
} }
default: default:

View File

@ -70,13 +70,11 @@ func acceptFollow(c *cli.Context) error {
return err return err
} }
activity := activitypub.Activity{ activity := activitypub.Activity{
[]string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}, Context: []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"},
data["activity_id"], ID: data["activity_id"],
data["actor"], Actor: data["actor"],
data["type"], Type: data["type"],
data["object"], Object: data["object"],
nil,
nil,
} }
resp := activity.GenerateResponse(hostname, "Accept") resp := activity.GenerateResponse(hostname, "Accept")
@ -85,11 +83,10 @@ func acceptFollow(c *cli.Context) error {
pushRegistorJob(data["inbox_url"], jsonData) pushRegistorJob(data["inbox_url"], jsonData)
redClient.HSet("relay:subscription:"+domain, "inbox_url", data["inbox_url"]) redClient.HSet("relay:subscription:"+domain, "inbox_url", data["inbox_url"])
redClient.Del("relay:pending:" + domain) redClient.Del("relay:pending:" + domain)
return nil
} else { } else {
fmt.Println("No domain given.") fmt.Println("No domain given.")
return nil
} }
return nil
} }
func rejectFollow(c *cli.Context) error { func rejectFollow(c *cli.Context) error {
@ -110,13 +107,11 @@ func rejectFollow(c *cli.Context) error {
return err return err
} }
activity := activitypub.Activity{ activity := activitypub.Activity{
[]string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}, Context: []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"},
data["activity_id"], ID: data["activity_id"],
data["actor"], Actor: data["actor"],
data["type"], Type: data["type"],
data["object"], Object: data["object"],
nil,
nil,
} }
resp := activity.GenerateResponse(hostname, "Reject") resp := activity.GenerateResponse(hostname, "Reject")
@ -124,9 +119,8 @@ func rejectFollow(c *cli.Context) error {
pushRegistorJob(data["inbox_url"], jsonData) pushRegistorJob(data["inbox_url"], jsonData)
redClient.Del("relay:pending:" + domain) redClient.Del("relay:pending:" + domain)
return nil
} else { } else {
fmt.Println("No domain given.") fmt.Println("No domain given.")
return nil
} }
return nil
} }

View File

@ -13,14 +13,14 @@ import (
"github.com/yukimochi/httpsig" "github.com/yukimochi/httpsig"
) )
func decodeActivity(r *http.Request) (*activitypub.Activity, *activitypub.Actor, []byte, error) { func decodeActivity(request *http.Request) (*activitypub.Activity, *activitypub.Actor, []byte, error) {
r.Header.Set("Host", r.Host) request.Header.Set("Host", request.Host)
dataLen, _ := strconv.Atoi(r.Header.Get("Content-Length")) dataLen, _ := strconv.Atoi(request.Header.Get("Content-Length"))
body := make([]byte, dataLen) body := make([]byte, dataLen)
r.Body.Read(body) request.Body.Read(body)
// Verify HTTPSignature // Verify HTTPSignature
verifier, err := httpsig.NewVerifier(r) verifier, err := httpsig.NewVerifier(request)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@ -40,7 +40,7 @@ func decodeActivity(r *http.Request) (*activitypub.Activity, *activitypub.Actor,
} }
// Verify Digest // Verify Digest
givenDigest := r.Header.Get("Digest") givenDigest := request.Header.Get("Digest")
hash := sha256.New() hash := sha256.New()
hash.Write(body) hash.Write(body)
b := hash.Sum(nil) b := hash.Sum(nil)

102
handle.go
View File

@ -10,14 +10,14 @@ import (
"github.com/RichardKnop/machinery/v1/tasks" "github.com/RichardKnop/machinery/v1/tasks"
"github.com/yukimochi/Activity-Relay/ActivityPub" "github.com/yukimochi/Activity-Relay/ActivityPub"
"github.com/yukimochi/Activity-Relay/RelayConf" "github.com/yukimochi/Activity-Relay/State"
) )
func handleWebfinger(w http.ResponseWriter, r *http.Request) { func handleWebfinger(writer http.ResponseWriter, request *http.Request) {
resource := r.URL.Query()["resource"] resource := request.URL.Query()["resource"]
if r.Method != "GET" || len(resource) == 0 { if request.Method != "GET" || len(resource) == 0 {
w.WriteHeader(400) writer.WriteHeader(400)
w.Write(nil) writer.Write(nil)
} else { } else {
request := resource[0] request := resource[0]
if request == WebfingerResource.Subject { if request == WebfingerResource.Subject {
@ -25,28 +25,28 @@ func handleWebfinger(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
w.Header().Add("Content-Type", "application/json") writer.Header().Add("Content-Type", "application/json")
w.WriteHeader(200) writer.WriteHeader(200)
w.Write(wfresource) writer.Write(wfresource)
} else { } else {
w.WriteHeader(404) writer.WriteHeader(404)
w.Write(nil) writer.Write(nil)
} }
} }
} }
func handleActor(w http.ResponseWriter, r *http.Request) { func handleActor(writer http.ResponseWriter, request *http.Request) {
if r.Method == "GET" { if request.Method == "GET" {
actor, err := json.Marshal(&Actor) actor, err := json.Marshal(&Actor)
if err != nil { if err != nil {
panic(err) panic(err)
} }
w.Header().Add("Content-Type", "application/activity+json") writer.Header().Add("Content-Type", "application/activity+json")
w.WriteHeader(200) writer.WriteHeader(200)
w.Write(actor) writer.Write(actor)
} else { } else {
w.WriteHeader(400) writer.WriteHeader(400)
w.Write(nil) writer.Write(nil)
} }
} }
@ -61,7 +61,7 @@ func contains(entries interface{}, finder string) bool {
} }
} }
return false return false
case []relayconf.Subscription: case []state.Subscription:
for i := 0; i < len(entry); i++ { for i := 0; i < len(entry); i++ {
if entry[i].Domain == finder { if entry[i].Domain == finder {
return true return true
@ -73,7 +73,7 @@ func contains(entries interface{}, finder string) bool {
} }
func pushRelayJob(sourceInbox string, body []byte) { func pushRelayJob(sourceInbox string, body []byte) {
for _, domain := range exportConfig.Subscriptions { for _, domain := range relayState.Subscriptions {
if sourceInbox != domain.Domain { if sourceInbox != domain.Domain {
job := &tasks.Signature{ job := &tasks.Signature{
Name: "relay", Name: "relay",
@ -140,7 +140,7 @@ func unFollowAcceptable(activity *activitypub.Activity, actor *activitypub.Actor
func suitableFollow(activity *activitypub.Activity, actor *activitypub.Actor) bool { func suitableFollow(activity *activitypub.Activity, actor *activitypub.Actor) bool {
domain, _ := url.Parse(activity.Actor) domain, _ := url.Parse(activity.Actor)
if contains(exportConfig.BlockedDomains, domain.Host) { if contains(relayState.BlockedDomains, domain.Host) {
return false return false
} }
return true return true
@ -151,7 +151,7 @@ func relayAcceptable(activity *activitypub.Activity, actor *activitypub.Actor) e
return errors.New("Activity should contain https://www.w3.org/ns/activitystreams#Public as receiver") return errors.New("Activity should contain https://www.w3.org/ns/activitystreams#Public as receiver")
} }
domain, _ := url.Parse(activity.Actor) domain, _ := url.Parse(activity.Actor)
if contains(exportConfig.Subscriptions, domain.Host) { if contains(relayState.Subscriptions, domain.Host) {
return nil return nil
} }
return errors.New("To use the relay service, Subscribe me in advance") return errors.New("To use the relay service, Subscribe me in advance")
@ -159,24 +159,24 @@ func relayAcceptable(activity *activitypub.Activity, actor *activitypub.Actor) e
func suitableRelay(activity *activitypub.Activity, actor *activitypub.Actor) bool { func suitableRelay(activity *activitypub.Activity, actor *activitypub.Actor) bool {
domain, _ := url.Parse(activity.Actor) domain, _ := url.Parse(activity.Actor)
if contains(exportConfig.LimitedDomains, domain.Host) { if contains(relayState.LimitedDomains, domain.Host) {
return false return false
} }
if exportConfig.RelayConfig.BlockService && actor.Type == "Service" { if relayState.RelayConfig.BlockService && actor.Type == "Service" {
return false return false
} }
return true return true
} }
func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*http.Request) (*activitypub.Activity, *activitypub.Actor, []byte, error)) { func handleInbox(writer http.ResponseWriter, request *http.Request, activityDecoder func(*http.Request) (*activitypub.Activity, *activitypub.Actor, []byte, error)) {
switch r.Method { switch request.Method {
case "POST": case "POST":
activity, actor, body, err := activityDecoder(r) activity, actor, body, err := activityDecoder(request)
if err != nil { if err != nil {
w.WriteHeader(400) writer.WriteHeader(400)
w.Write(nil) writer.Write(nil)
} else { } else {
exportConfig.Load() relayState.Load()
domain, _ := url.Parse(activity.Actor) domain, _ := url.Parse(activity.Actor)
switch activity.Type { switch activity.Type {
case "Follow": case "Follow":
@ -187,11 +187,11 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
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)
w.WriteHeader(202) writer.WriteHeader(202)
w.Write(nil) writer.Write(nil)
} else { } else {
if suitableFollow(activity, actor) { if suitableFollow(activity, actor) {
if exportConfig.RelayConfig.ManuallyAccept { if relayState.RelayConfig.ManuallyAccept {
redClient.HMSet("relay:pending:"+domain.Host, map[string]interface{}{ redClient.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,
@ -204,7 +204,7 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
resp := activity.GenerateResponse(hostname, "Accept") resp := activity.GenerateResponse(hostname, "Accept")
jsonData, _ := json.Marshal(&resp) jsonData, _ := json.Marshal(&resp)
go pushRegistorJob(actor.Inbox, jsonData) go pushRegistorJob(actor.Inbox, jsonData)
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: actor.Endpoints.SharedInbox, InboxURL: actor.Endpoints.SharedInbox,
ActivityID: activity.ID, ActivityID: activity.ID,
@ -219,8 +219,8 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
fmt.Println("Reject Follow Request : ", activity.Actor) fmt.Println("Reject Follow Request : ", activity.Actor)
} }
w.WriteHeader(202) writer.WriteHeader(202)
w.Write(nil) writer.Write(nil)
} }
case "Undo": case "Undo":
nestedActivity, _ := activity.NestedActivity() nestedActivity, _ := activity.NestedActivity()
@ -228,37 +228,37 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
err = unFollowAcceptable(nestedActivity, actor) err = unFollowAcceptable(nestedActivity, actor)
if err != nil { if err != nil {
fmt.Println("Reject Unfollow Request : ", err.Error()) fmt.Println("Reject Unfollow Request : ", err.Error())
w.WriteHeader(400) writer.WriteHeader(400)
w.Write([]byte(err.Error())) writer.Write([]byte(err.Error()))
} else { } else {
redClient.Del("relay:subscription:" + domain.Host) redClient.Del("relay:subscription:" + domain.Host)
fmt.Println("Accept Unfollow Request : ", activity.Actor) fmt.Println("Accept Unfollow Request : ", activity.Actor)
w.WriteHeader(202) writer.WriteHeader(202)
w.Write(nil) writer.Write(nil)
} }
} else { } else {
err = relayAcceptable(activity, actor) err = relayAcceptable(activity, actor)
if err != nil { if err != nil {
w.WriteHeader(400) writer.WriteHeader(400)
w.Write([]byte(err.Error())) writer.Write([]byte(err.Error()))
} else { } else {
domain, _ := url.Parse(activity.Actor) domain, _ := url.Parse(activity.Actor)
go pushRelayJob(domain.Host, body) go pushRelayJob(domain.Host, body)
fmt.Println("Accept Relay Status : ", activity.Actor) fmt.Println("Accept Relay Status : ", activity.Actor)
w.WriteHeader(202) writer.WriteHeader(202)
w.Write(nil) writer.Write(nil)
} }
} }
case "Create", "Update", "Delete", "Announce": case "Create", "Update", "Delete", "Announce":
err = relayAcceptable(activity, actor) err = relayAcceptable(activity, actor)
if err != nil { if err != nil {
w.WriteHeader(400) writer.WriteHeader(400)
w.Write([]byte(err.Error())) writer.Write([]byte(err.Error()))
} else { } else {
if suitableRelay(activity, actor) { if suitableRelay(activity, actor) {
if exportConfig.RelayConfig.CreateAsAnnounce && activity.Type == "Create" { if relayState.RelayConfig.CreateAsAnnounce && activity.Type == "Create" {
nestedObject, err := activity.NestedActivity() nestedObject, err := activity.NestedActivity()
if err != nil { if err != nil {
fmt.Println("Fail Assert activity : activity.Actor") fmt.Println("Fail Assert activity : activity.Actor")
@ -280,13 +280,13 @@ func handleInbox(w http.ResponseWriter, r *http.Request, activityDecoder func(*h
fmt.Println("Skipping Relay Status : ", activity.Actor) fmt.Println("Skipping Relay Status : ", activity.Actor)
} }
w.WriteHeader(202) writer.WriteHeader(202)
w.Write(nil) writer.Write(nil)
} }
} }
} }
default: default:
w.WriteHeader(404) writer.WriteHeader(404)
w.Write(nil) writer.Write(nil)
} }
} }

View File

@ -12,11 +12,11 @@ import (
"testing" "testing"
"github.com/yukimochi/Activity-Relay/ActivityPub" "github.com/yukimochi/Activity-Relay/ActivityPub"
"github.com/yukimochi/Activity-Relay/RelayConf" "github.com/yukimochi/Activity-Relay/State"
) )
const ( const (
BlockService relayconf.Config = iota BlockService state.Config = iota
ManuallyAccept ManuallyAccept
CreateAsAnnounce CreateAsAnnounce
) )
@ -266,7 +266,7 @@ func TestSuitableRelayNoBlockService(t *testing.T) {
personActor := mockActor("Person") personActor := mockActor("Person")
serviceActor := mockActor("Service") serviceActor := mockActor("Service")
exportConfig.SetConfig(BlockService, false) relayState.SetConfig(BlockService, false)
if suitableRelay(&activity, &personActor) != true { if suitableRelay(&activity, &personActor) != true {
t.Fatalf("Failed - Person status not relay") t.Fatalf("Failed - Person status not relay")
@ -281,7 +281,7 @@ func TestSuitableRelayBlockService(t *testing.T) {
personActor := mockActor("Person") personActor := mockActor("Person")
serviceActor := mockActor("Service") serviceActor := mockActor("Service")
exportConfig.SetConfig(BlockService, true) relayState.SetConfig(BlockService, true)
if suitableRelay(&activity, &personActor) != true { if suitableRelay(&activity, &personActor) != true {
t.Fatalf("Failed - Person status not relay") t.Fatalf("Failed - Person status not relay")
@ -289,7 +289,7 @@ func TestSuitableRelayBlockService(t *testing.T) {
if suitableRelay(&activity, &serviceActor) != false { if suitableRelay(&activity, &serviceActor) != false {
t.Fatalf("Failed - Service status may relay when blocking mode") t.Fatalf("Failed - Service status may relay when blocking mode")
} }
exportConfig.SetConfig(BlockService, false) relayState.SetConfig(BlockService, false)
} }
func TestHandleInboxNoSignature(t *testing.T) { func TestHandleInboxNoSignature(t *testing.T) {
@ -348,7 +348,7 @@ func TestHandleInboxValidFollow(t *testing.T) {
if res != 1 { if res != 1 {
t.Fatalf("Failed - Subscription not works.") t.Fatalf("Failed - Subscription not works.")
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
} }
func TestHandleInboxValidManuallyFollow(t *testing.T) { func TestHandleInboxValidManuallyFollow(t *testing.T) {
@ -361,7 +361,7 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
defer s.Close() defer s.Close()
// Switch Manually // Switch Manually
exportConfig.SetConfig(ManuallyAccept, true) relayState.SetConfig(ManuallyAccept, true)
req, _ := http.NewRequest("POST", s.URL, nil) req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client) client := new(http.Client)
@ -380,8 +380,8 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
if res != 0 { if res != 0 {
t.Fatalf("Failed - Pending was skipped.") t.Fatalf("Failed - Pending was skipped.")
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
exportConfig.SetConfig(ManuallyAccept, false) relayState.SetConfig(ManuallyAccept, false)
} }
func TestHandleInboxInvalidFollow(t *testing.T) { func TestHandleInboxInvalidFollow(t *testing.T) {
@ -393,7 +393,7 @@ func TestHandleInboxInvalidFollow(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.SetConfig(ManuallyAccept, false) relayState.SetConfig(ManuallyAccept, false)
req, _ := http.NewRequest("POST", s.URL, nil) req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client) client := new(http.Client)
@ -419,7 +419,7 @@ func TestHandleInboxValidFollowBlocked(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.SetBlockedDomain(domain.Host, true) relayState.SetBlockedDomain(domain.Host, true)
req, _ := http.NewRequest("POST", s.URL, nil) req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client) client := new(http.Client)
@ -434,8 +434,8 @@ func TestHandleInboxValidFollowBlocked(t *testing.T) {
if res != 0 { if res != 0 {
t.Fatalf("Failed - Subscription not blocked.") t.Fatalf("Failed - Subscription not blocked.")
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
exportConfig.SetBlockedDomain(domain.Host, false) relayState.SetBlockedDomain(domain.Host, false)
} }
func TestHandleInboxValidUnfollow(t *testing.T) { func TestHandleInboxValidUnfollow(t *testing.T) {
@ -447,7 +447,7 @@ func TestHandleInboxValidUnfollow(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
@ -465,7 +465,7 @@ func TestHandleInboxValidUnfollow(t *testing.T) {
if res != 0 { if res != 0 {
t.Fatalf("Failed - Subscription not succeed.") t.Fatalf("Failed - Subscription not succeed.")
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
} }
func TestHandleInboxInvalidUnfollow(t *testing.T) { func TestHandleInboxInvalidUnfollow(t *testing.T) {
@ -477,7 +477,7 @@ func TestHandleInboxInvalidUnfollow(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
@ -495,7 +495,7 @@ func TestHandleInboxInvalidUnfollow(t *testing.T) {
if res != 1 { if res != 1 {
t.Fatalf("Failed - Block hacked unfollow not succeed.") t.Fatalf("Failed - Block hacked unfollow not succeed.")
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
} }
func TestHandleInboxUnfollowAsActor(t *testing.T) { func TestHandleInboxUnfollowAsActor(t *testing.T) {
@ -507,7 +507,7 @@ func TestHandleInboxUnfollowAsActor(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
@ -525,7 +525,7 @@ func TestHandleInboxUnfollowAsActor(t *testing.T) {
if res != 1 { if res != 1 {
t.Fatalf("Failed - Block actor unfollow not succeed.") t.Fatalf("Failed - Block actor unfollow not succeed.")
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
} }
func TestHandleInboxValidCreate(t *testing.T) { func TestHandleInboxValidCreate(t *testing.T) {
@ -537,11 +537,11 @@ func TestHandleInboxValidCreate(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: "example.org", Domain: "example.org",
InboxURL: "https://example.org/inbox", InboxURL: "https://example.org/inbox",
}) })
@ -555,8 +555,8 @@ func TestHandleInboxValidCreate(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))
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
exportConfig.DelSubscription("example.org") relayState.DelSubscription("example.org")
redClient.Del("relay:subscription:" + domain.Host).Result() redClient.Del("relay:subscription:" + domain.Host).Result()
redClient.Del("relay:subscription:example.org").Result() redClient.Del("relay:subscription:example.org").Result()
} }
@ -570,11 +570,11 @@ func TestHandleInboxlimitedCreate(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
exportConfig.SetLimitedDomain(domain.Host, true) relayState.SetLimitedDomain(domain.Host, true)
req, _ := http.NewRequest("POST", s.URL, nil) req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client) client := new(http.Client)
@ -585,8 +585,8 @@ func TestHandleInboxlimitedCreate(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))
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
exportConfig.SetLimitedDomain(domain.Host, false) relayState.SetLimitedDomain(domain.Host, false)
} }
func TestHandleInboxValidCreateAsAnnounceNote(t *testing.T) { func TestHandleInboxValidCreateAsAnnounceNote(t *testing.T) {
@ -598,15 +598,15 @@ func TestHandleInboxValidCreateAsAnnounceNote(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: "example.org", Domain: "example.org",
InboxURL: "https://example.org/inbox", InboxURL: "https://example.org/inbox",
}) })
exportConfig.SetConfig(CreateAsAnnounce, true) relayState.SetConfig(CreateAsAnnounce, true)
req, _ := http.NewRequest("POST", s.URL, nil) req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client) client := new(http.Client)
@ -617,9 +617,9 @@ func TestHandleInboxValidCreateAsAnnounceNote(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))
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
exportConfig.DelSubscription("example.org") relayState.DelSubscription("example.org")
exportConfig.SetConfig(CreateAsAnnounce, false) relayState.SetConfig(CreateAsAnnounce, false)
} }
func TestHandleInboxValidCreateAsAnnounceNoNote(t *testing.T) { func TestHandleInboxValidCreateAsAnnounceNoNote(t *testing.T) {
@ -631,15 +631,15 @@ func TestHandleInboxValidCreateAsAnnounceNoNote(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: "example.org", Domain: "example.org",
InboxURL: "https://example.org/inbox", InboxURL: "https://example.org/inbox",
}) })
exportConfig.SetConfig(CreateAsAnnounce, true) relayState.SetConfig(CreateAsAnnounce, true)
req, _ := http.NewRequest("POST", s.URL, nil) req, _ := http.NewRequest("POST", s.URL, nil)
client := new(http.Client) client := new(http.Client)
@ -650,9 +650,9 @@ func TestHandleInboxValidCreateAsAnnounceNoNote(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))
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
exportConfig.DelSubscription("example.org") relayState.DelSubscription("example.org")
exportConfig.SetConfig(CreateAsAnnounce, false) relayState.SetConfig(CreateAsAnnounce, false)
} }
func TestHandleInboxUnsubscriptionCreate(t *testing.T) { func TestHandleInboxUnsubscriptionCreate(t *testing.T) {
@ -683,7 +683,7 @@ func TestHandleInboxUndo(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
exportConfig.AddSubscription(relayconf.Subscription{ relayState.AddSubscription(state.Subscription{
Domain: domain.Host, Domain: domain.Host,
InboxURL: "https://mastodon.test.yukimochi.io/inbox", InboxURL: "https://mastodon.test.yukimochi.io/inbox",
}) })
@ -701,5 +701,5 @@ func TestHandleInboxUndo(t *testing.T) {
if res != 1 { if res != 1 {
t.Fatalf("Failed - Missing unsubscribed.") t.Fatalf("Failed - Missing unsubscribed.")
} }
exportConfig.DelSubscription(domain.Host) relayState.DelSubscription(domain.Host)
} }

View File

@ -12,7 +12,7 @@ import (
"github.com/go-redis/redis" "github.com/go-redis/redis"
"github.com/yukimochi/Activity-Relay/ActivityPub" "github.com/yukimochi/Activity-Relay/ActivityPub"
"github.com/yukimochi/Activity-Relay/KeyLoader" "github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/RelayConf" "github.com/yukimochi/Activity-Relay/State"
) )
// Actor : Relay's Actor // Actor : Relay's Actor
@ -25,7 +25,7 @@ var hostname *url.URL
var hostkey *rsa.PrivateKey var hostkey *rsa.PrivateKey
var redClient *redis.Client var redClient *redis.Client
var macServer *machinery.Server var macServer *machinery.Server
var exportConfig relayconf.ExportConfig var relayState state.RelayState
func main() { func main() {
pemPath := os.Getenv("ACTOR_PEM") pemPath := os.Getenv("ACTOR_PEM")
@ -74,7 +74,7 @@ func main() {
WebfingerResource.GenerateFromActor(hostname, &Actor) WebfingerResource.GenerateFromActor(hostname, &Actor)
// Load Config // Load Config
exportConfig = relayconf.NewConfig(redClient) relayState = state.NewState(redClient)
http.HandleFunc("/.well-known/webfinger", handleWebfinger) http.HandleFunc("/.well-known/webfinger", handleWebfinger)
http.HandleFunc("/actor", handleActor) http.HandleFunc("/actor", handleActor)

View File

@ -9,7 +9,7 @@ import (
"github.com/RichardKnop/machinery/v1/config" "github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis" "github.com/go-redis/redis"
"github.com/yukimochi/Activity-Relay/KeyLoader" "github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/RelayConf" "github.com/yukimochi/Activity-Relay/State"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@ -36,7 +36,7 @@ func TestMain(m *testing.M) {
// Load Config // Load Config
redClient.FlushAll().Result() redClient.FlushAll().Result()
exportConfig = relayconf.NewConfig(redClient) relayState = state.NewState(redClient)
code := m.Run() code := m.Run()
os.Exit(code) os.Exit(code)
redClient.FlushAll().Result() redClient.FlushAll().Result()