Use viper as Configuration manager. (#11)

* Use viper in CLI.

* Use viper in Server.

* Use viper in Worker.
This commit is contained in:
Naoki Kosaka 2018-12-23 14:52:43 +09:00 committed by GitHub
parent 93a00a13e2
commit e0c0f1a68d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 341 additions and 246 deletions

View File

@ -52,7 +52,7 @@ func (actor *Actor) GenerateSelfKey(hostname *url.URL, publickey *rsa.PublicKey)
}
// RetrieveRemoteActor : Retrieve Actor from remote instance.
func (actor *Actor) RetrieveRemoteActor(url string, cache *cache.Cache) error {
func (actor *Actor) RetrieveRemoteActor(url string, uaString string, cache *cache.Cache) error {
var err error
cacheData, found := cache.Get(url)
if found {
@ -65,7 +65,7 @@ func (actor *Actor) RetrieveRemoteActor(url string, cache *cache.Cache) error {
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UaString)
req.Header.Set("User-Agent", uaString)
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {

87
Gopkg.lock generated
View File

@ -126,6 +126,14 @@
pruneopts = "UT"
revision = "bc664df9673713a0ccf26e3b55a673ec7301088b"
[[projects]]
digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd"
name = "github.com/fsnotify/fsnotify"
packages = ["."]
pruneopts = "UT"
revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9"
version = "v1.4.7"
[[projects]]
digest = "1:34a9a60fade37f8009ed4a19e02924198aba3eabfcc120ee5c6002b7de17212d"
name = "github.com/go-redis/redis"
@ -189,6 +197,25 @@
revision = "c8a15bac9b9fe955bd9f900272f9a306465d28cf"
version = "v2.0.3"
[[projects]]
digest = "1:c0d19ab64b32ce9fe5cf4ddceba78d5bc9807f0016db6b1183599da3dcc24d10"
name = "github.com/hashicorp/hcl"
packages = [
".",
"hcl/ast",
"hcl/parser",
"hcl/printer",
"hcl/scanner",
"hcl/strconv",
"hcl/token",
"json/parser",
"json/scanner",
"json/token",
]
pruneopts = "UT"
revision = "8cb6e5b959231cc1119e43259c4a608f9c51a241"
version = "v1.0.0"
[[projects]]
digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be"
name = "github.com/inconshreveable/mousetrap"
@ -212,6 +239,22 @@
revision = "f611eb38b3875cc3bd991ca91c51d06446afa14c"
version = "v1.3.0"
[[projects]]
digest = "1:c568d7727aa262c32bdf8a3f7db83614f7af0ed661474b24588de635c20024c7"
name = "github.com/magiconair/properties"
packages = ["."]
pruneopts = "UT"
revision = "c2353362d570a7bfa228149c62842019201cfb71"
version = "v1.8.0"
[[projects]]
digest = "1:53bc4cd4914cd7cd52139990d5170d6dc99067ae31c56530621b18b35fc30318"
name = "github.com/mitchellh/mapstructure"
packages = ["."]
pruneopts = "UT"
revision = "3536a929edddb9a5b34bd6861dc4a9647cb459fe"
version = "v1.1.2"
[[projects]]
digest = "1:450b7623b185031f3a456801155c8320209f75d0d4c4e633c6b1e59d44d6e392"
name = "github.com/opentracing/opentracing-go"
@ -232,6 +275,14 @@
revision = "a3647f8e31d79543b2d0f0ae2fe5c379d72cedc0"
version = "v2.1.0"
[[projects]]
digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e"
name = "github.com/pelletier/go-toml"
packages = ["."]
pruneopts = "UT"
revision = "c01d1270ff3e442a8a57cddc1c92dc1138598194"
version = "v1.2.0"
[[projects]]
digest = "1:274f67cb6fed9588ea2521ecdac05a6d62a8c51c074c1fccc6a49a40ba80e925"
name = "github.com/satori/go.uuid"
@ -240,6 +291,25 @@
revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3"
version = "v1.2.0"
[[projects]]
digest = "1:d707dbc1330c0ed177d4642d6ae102d5e2c847ebd0eb84562d0dc4f024531cfc"
name = "github.com/spf13/afero"
packages = [
".",
"mem",
]
pruneopts = "UT"
revision = "a5d6946387efe7d64d09dcba68cdd523dc1273a3"
version = "v1.2.0"
[[projects]]
digest = "1:08d65904057412fc0270fc4812a1c90c594186819243160dc779a402d4b6d0bc"
name = "github.com/spf13/cast"
packages = ["."]
pruneopts = "UT"
revision = "8c9545af88b134710ab1cd196795e7f2388358d7"
version = "v1.3.0"
[[projects]]
digest = "1:645cabccbb4fa8aab25a956cbcbdf6a6845ca736b2c64e197ca7cbb9d210b939"
name = "github.com/spf13/cobra"
@ -248,6 +318,14 @@
revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385"
version = "v0.0.3"
[[projects]]
digest = "1:68ea4e23713989dc20b1bded5d9da2c5f9be14ff9885beef481848edd18c26cb"
name = "github.com/spf13/jwalterweatherman"
packages = ["."]
pruneopts = "UT"
revision = "4a4406e478ca629068e7768fc33f3f044173c0a6"
version = "v1.0.0"
[[projects]]
digest = "1:c1b1102241e7f645bc8e0c22ae352e8f0dc6484b6cb4d132fa9f24174e0119e2"
name = "github.com/spf13/pflag"
@ -256,6 +334,14 @@
revision = "298182f68c66c05229eb03ac171abe6e309ee79a"
version = "v1.0.3"
[[projects]]
digest = "1:de37e343c64582d7026bf8ab6ac5b22a72eac54f3a57020db31524affed9f423"
name = "github.com/spf13/viper"
packages = ["."]
pruneopts = "UT"
revision = "6d33b5a963d922d182c91e8a1c88d81fd150cfd4"
version = "v1.3.1"
[[projects]]
branch = "master"
digest = "1:525ac3364813b4688df380594e562133e07830dfce0722effda64b37634c13d0"
@ -513,6 +599,7 @@
"github.com/patrickmn/go-cache",
"github.com/satori/go.uuid",
"github.com/spf13/cobra",
"github.com/spf13/viper",
"github.com/yukimochi/httpsig",
]
solver-name = "gps-cdcl"

View File

@ -2,24 +2,41 @@ package main
import (
"crypto/rsa"
"fmt"
"net/url"
"os"
"github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/State"
)
var hostname *url.URL
var hostkey *rsa.PrivateKey
var redClient *redis.Client
var macServer *machinery.Server
var relayState state.RelayState
func initConfig() {
viper.BindEnv("actor_pem")
viper.BindEnv("relay_domain")
viper.BindEnv("redis_url")
hostkey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem"))
hostname, _ = url.Parse("https://" + viper.GetString("relay_domain"))
redClient := redis.NewClient(&redis.Options{
Addr: viper.GetString("redis_url"),
})
var macConfig = &config.Config{
Broker: "redis://" + viper.GetString("redis_url"),
DefaultQueue: "relay",
ResultBackend: "redis://" + viper.GetString("redis_url"),
ResultsExpireIn: 5,
}
macServer, _ = machinery.NewServer(macConfig)
relayState = state.NewState(redClient)
}
func buildNewCmd() *cobra.Command {
var app = &cobra.Command{}
app.AddCommand(domainCmdInit())
@ -29,45 +46,7 @@ func buildNewCmd() *cobra.Command {
}
func main() {
pemPath := os.Getenv("ACTOR_PEM")
if pemPath == "" {
panic("Require ACTOR_PEM environment variable.")
}
relayDomain := os.Getenv("RELAY_DOMAIN")
if relayDomain == "" {
panic("Require RELAY_DOMAIN environment variable.")
}
redisURL := os.Getenv("REDIS_URL")
if redisURL == "" {
redisURL = "127.0.0.1:6379"
}
var err error
hostkey, err = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
if err != nil {
panic("Can't read Hostkey Pemfile")
}
hostname, err = url.Parse("https://" + relayDomain)
if err != nil {
panic("Can't parse Relay Domain")
}
redClient = redis.NewClient(&redis.Options{
Addr: redisURL,
})
var macConfig = &config.Config{
Broker: "redis://" + redisURL,
DefaultQueue: "relay",
ResultBackend: "redis://" + redisURL,
ResultsExpireIn: 5,
}
macServer, err = machinery.NewServer(macConfig)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
relayState = state.NewState(redClient)
initConfig()
var app = buildNewCmd()
app.Execute()
}

View File

@ -1,38 +1,19 @@
package main
import (
"net/url"
"os"
"testing"
"github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/State"
"github.com/spf13/viper"
)
func TestMain(m *testing.M) {
os.Setenv("ACTOR_PEM", "misc/testKey.pem")
os.Setenv("RELAY_DOMAIN", "relay.yukimochi.example.org")
pemPath := os.Getenv("ACTOR_PEM")
relayDomain := os.Getenv("RELAY_DOMAIN")
redisURL := os.Getenv("REDIS_URL")
hostkey, _ = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
hostname, _ = url.Parse("https://" + relayDomain)
redClient = redis.NewClient(&redis.Options{
Addr: redisURL,
})
var macConfig = &config.Config{
Broker: "redis://" + redisURL,
DefaultQueue: "relay",
ResultBackend: "redis://" + redisURL,
ResultsExpireIn: 5,
}
macServer, _ = machinery.NewServer(macConfig)
redClient.FlushAll().Result()
relayState = state.NewState(redClient)
viper.Set("Actor_pem", "misc/testKey.pem")
viper.Set("Relay_domain", "relay.yukimochi.example.org")
initConfig()
relayState.RedisClient.FlushAll().Result()
code := m.Run()
os.Exit(code)
redClient.FlushAll().Result()
relayState.RedisClient.FlushAll().Result()
}

View File

@ -23,7 +23,7 @@ func followCmdInit() *cobra.Command {
Use: "list",
Short: "List follow request",
Long: "List follow request.",
RunE: listFollowsC,
RunE: listFollows,
}
follow.AddCommand(followList)
@ -32,7 +32,7 @@ func followCmdInit() *cobra.Command {
Short: "Accept follow request",
Long: "Accept follow request by domain.",
Args: cobra.MinimumNArgs(1),
RunE: acceptFollowC,
RunE: acceptFollow,
}
follow.AddCommand(followAccept)
@ -41,7 +41,7 @@ func followCmdInit() *cobra.Command {
Short: "Reject follow request",
Long: "Reject follow request by domain.",
Args: cobra.MinimumNArgs(1),
RunE: rejectFollowC,
RunE: rejectFollow,
}
follow.AddCommand(followReject)
@ -71,24 +71,6 @@ func pushRegistorJob(inboxURL string, body []byte) {
}
}
func listFollowsC(cmd *cobra.Command, args []string) error {
var domains []string
cmd.Println(" - Follow request :")
follows, err := relayState.RedisClient.Keys("relay:pending:*").Result()
if err != nil {
return err
}
for _, follow := range follows {
domains = append(domains, strings.Replace(follow, "relay:pending:", "", 1))
}
for _, domain := range domains {
cmd.Println(domain)
}
cmd.Println(fmt.Sprintf("Total : %d", len(domains)))
return nil
}
func createFollowRequestResponse(domain string, response string) error {
data, err := relayState.RedisClient.HGetAll("relay:pending:" + domain).Result()
if err != nil {
@ -118,7 +100,25 @@ func createFollowRequestResponse(domain string, response string) error {
return nil
}
func acceptFollowC(cmd *cobra.Command, args []string) error {
func listFollows(cmd *cobra.Command, args []string) error {
var domains []string
cmd.Println(" - Follow request :")
follows, err := relayState.RedisClient.Keys("relay:pending:*").Result()
if err != nil {
return err
}
for _, follow := range follows {
domains = append(domains, strings.Replace(follow, "relay:pending:", "", 1))
}
for _, domain := range domains {
cmd.Println(domain)
}
cmd.Println(fmt.Sprintf("Total : %d", len(domains)))
return nil
}
func acceptFollow(cmd *cobra.Command, args []string) error {
var err error
var domains []string
follows, err := relayState.RedisClient.Keys("relay:pending:*").Result()
@ -143,10 +143,10 @@ func acceptFollowC(cmd *cobra.Command, args []string) error {
return nil
}
func rejectFollowC(cmd *cobra.Command, args []string) error {
func rejectFollow(cmd *cobra.Command, args []string) error {
var err error
var domains []string
follows, err := redClient.Keys("relay:pending:*").Result()
follows, err := relayState.RedisClient.Keys("relay:pending:*").Result()
if err != nil {
return err
}

111
cli/follow_test.go Normal file
View File

@ -0,0 +1,111 @@
package main
import (
"bytes"
"strings"
"testing"
)
func TestListFollows(t *testing.T) {
app := buildNewCmd()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
relayState.RedisClient.HMSet("relay:pending:example.com", map[string]interface{}{
"inbox_url": "https://example.com/inbox",
"activity_id": "https://example.com/UUID",
"type": "Follow",
"actor": "https://example.com/user/example",
"object": "https://" + hostname.Host + "/actor",
})
app.SetArgs([]string{"follow", "list"})
app.Execute()
output := buffer.String()
valid := ` - Follow request :
example.com
Total : 1
`
if output != valid {
t.Fatalf("Invalid Responce.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestAcceptFollow(t *testing.T) {
app := buildNewCmd()
relayState.RedisClient.HMSet("relay:pending:example.com", map[string]interface{}{
"inbox_url": "https://example.com/inbox",
"activity_id": "https://example.com/UUID",
"type": "Follow",
"actor": "https://example.com/user/example",
"object": "https://" + hostname.Host + "/actor",
})
app.SetArgs([]string{"follow", "accept", "example.com"})
app.Execute()
valid, _ := relayState.RedisClient.Exists("relay:pending:example.com").Result()
if valid != 0 {
t.Fatalf("Not removed follow request.")
}
valid, _ = relayState.RedisClient.Exists("relay:subscription:example.com").Result()
if valid != 1 {
t.Fatalf("Not created subscription.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestRejectFollow(t *testing.T) {
app := buildNewCmd()
relayState.RedisClient.HMSet("relay:pending:example.com", map[string]interface{}{
"inbox_url": "https://example.com/inbox",
"activity_id": "https://example.com/UUID",
"type": "Follow",
"actor": "https://example.com/user/example",
"object": "https://" + hostname.Host + "/actor",
})
app.SetArgs([]string{"follow", "reject", "example.com"})
app.Execute()
valid, _ := relayState.RedisClient.Exists("relay:pending:example.com").Result()
if valid != 0 {
t.Fatalf("No response follow request.")
}
valid, _ = relayState.RedisClient.Exists("relay:subscription:example.com").Result()
if valid != 0 {
t.Fatalf("Created subscription.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestInvalidFollow(t *testing.T) {
app := buildNewCmd()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"follow", "accept", "unknown.tld"})
app.Execute()
output := buffer.String()
if strings.Split(output, "\n")[0] != "Invalid domain given" {
t.Fatalf("Invalid Responce.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}

View File

@ -26,7 +26,7 @@ func decodeActivity(request *http.Request) (*activitypub.Activity, *activitypub.
}
KeyID := verifier.KeyId()
remoteActor := new(activitypub.Actor)
err = remoteActor.RetrieveRemoteActor(KeyID, actorCache)
err = remoteActor.RetrieveRemoteActor(KeyID, uaString, actorCache)
if err != nil {
return nil, nil, nil, err
}

View File

@ -192,7 +192,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
} else {
if suitableFollow(activity, actor) {
if relayState.RelayConfig.ManuallyAccept {
redisClient.HMSet("relay:pending:"+domain.Host, map[string]interface{}{
relayState.RedisClient.HMSet("relay:pending:"+domain.Host, map[string]interface{}{
"inbox_url": actor.Endpoints.SharedInbox,
"activity_id": activity.ID,
"type": "Follow",
@ -231,7 +231,7 @@ func handleInbox(writer http.ResponseWriter, request *http.Request, activityDeco
writer.WriteHeader(400)
writer.Write([]byte(err.Error()))
} else {
redisClient.Del("relay:subscription:" + domain.Host)
relayState.RedisClient.Del("relay:subscription:" + domain.Host)
fmt.Println("Accept Unfollow Request : ", activity.Actor)
writer.WriteHeader(202)

View File

@ -27,7 +27,7 @@ func TestHandleWebfingerGet(t *testing.T) {
req, _ := http.NewRequest("GET", s.URL, nil)
q := req.URL.Query()
q.Add("resource", "acct:relay@"+os.Getenv("RELAY_DOMAIN"))
q.Add("resource", "acct:relay@"+hostURL.Host)
req.URL.RawQuery = q.Encode()
client := new(http.Client)
r, err := client.Do(req)
@ -50,7 +50,7 @@ func TestHandleWebfingerGet(t *testing.T) {
}
domain, _ := url.Parse(wfresource.Links[0].Href)
if domain.Host != os.Getenv("RELAY_DOMAIN") {
if domain.Host != hostURL.Host {
t.Fatalf("WebfingerResource's Host not valid.")
}
}
@ -112,7 +112,7 @@ func TestHandleActorGet(t *testing.T) {
}
domain, _ := url.Parse(actor.ID)
if domain.Host != os.Getenv("RELAY_DOMAIN") {
if domain.Host != hostURL.Host {
t.Fatalf("Actor's Host not valid.")
}
}
@ -344,7 +344,7 @@ func TestHandleInboxValidFollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 {
t.Fatalf("Failed - Subscription not works.")
}
@ -372,11 +372,11 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redisClient.Exists("relay:pending:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:pending:" + domain.Host).Result()
if res != 1 {
t.Fatalf("Failed - Pending not works.")
}
res, _ = redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ = relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 {
t.Fatalf("Failed - Pending was skipped.")
}
@ -404,7 +404,7 @@ func TestHandleInboxInvalidFollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 {
t.Fatalf("Failed - Subscription not blocked.")
}
@ -430,7 +430,7 @@ func TestHandleInboxValidFollowBlocked(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 {
t.Fatalf("Failed - Subscription not blocked.")
}
@ -461,7 +461,7 @@ func TestHandleInboxValidUnfollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 0 {
t.Fatalf("Failed - Subscription not succeed.")
}
@ -491,7 +491,7 @@ func TestHandleInboxInvalidUnfollow(t *testing.T) {
if r.StatusCode != 400 {
t.Fatalf("Failed - StatusCode is not 400")
}
res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 {
t.Fatalf("Failed - Block hacked unfollow not succeed.")
}
@ -521,7 +521,7 @@ func TestHandleInboxUnfollowAsActor(t *testing.T) {
if r.StatusCode != 400 {
t.Fatalf("Failed - StatusCode is not 400")
}
res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 {
t.Fatalf("Failed - Block actor unfollow not succeed.")
}
@ -557,8 +557,8 @@ func TestHandleInboxValidCreate(t *testing.T) {
}
relayState.DelSubscription(domain.Host)
relayState.DelSubscription("example.org")
redisClient.Del("relay:subscription:" + domain.Host).Result()
redisClient.Del("relay:subscription:example.org").Result()
relayState.RedisClient.Del("relay:subscription:" + domain.Host).Result()
relayState.RedisClient.Del("relay:subscription:example.org").Result()
}
func TestHandleInboxlimitedCreate(t *testing.T) {
@ -697,7 +697,7 @@ func TestHandleInboxUndo(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("Failed - StatusCode is not 202 - " + strconv.Itoa(r.StatusCode))
}
res, _ := redisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := relayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
if res != 1 {
t.Fatalf("Failed - Missing unsubscribed.")
}

90
main.go
View File

@ -5,13 +5,13 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"time"
"github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
"github.com/yukimochi/Activity-Relay/ActivityPub"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/State"
@ -25,72 +25,37 @@ var WebfingerResource activitypub.WebfingerResource
var hostURL *url.URL
var hostPrivatekey *rsa.PrivateKey
var redisClient *redis.Client
var actorCache *cache.Cache
var machineryServer *machinery.Server
var relayState state.RelayState
var uaString string
func main() {
pemPath := os.Getenv("ACTOR_PEM")
if pemPath == "" {
panic("Require ACTOR_PEM environment variable.")
}
relayDomain := os.Getenv("RELAY_DOMAIN")
if relayDomain == "" {
panic("Require RELAY_DOMAIN environment variable.")
}
relayBind := os.Getenv("RELAY_BIND")
if relayBind == "" {
relayBind = "0.0.0.0:8080"
}
redisURL := os.Getenv("REDIS_URL")
if redisURL == "" {
redisURL = "127.0.0.1:6379"
}
var err error
hostPrivatekey, err = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
if err != nil {
panic("Can't read Hostkey Pemfile")
}
hostURL, err = url.Parse("https://" + relayDomain)
if err != nil {
panic("Can't parse Relay Domain")
}
redisClient = redis.NewClient(&redis.Options{
Addr: redisURL,
func initConfig() {
viper.BindEnv("actor_pem")
viper.BindEnv("relay_domain")
viper.BindEnv("redis_url")
viper.BindEnv("relay_bind")
hostURL, _ = url.Parse("https://" + viper.GetString("relay_domain"))
hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem"))
redisClient := redis.NewClient(&redis.Options{
Addr: viper.GetString("redis_url"),
})
actorCache = cache.New(5*time.Minute, 10*time.Minute)
var macConfig = &config.Config{
Broker: "redis://" + redisURL,
machineryConfig := &config.Config{
Broker: "redis://" + viper.GetString("redis_url"),
DefaultQueue: "relay",
ResultBackend: "redis://" + redisURL,
ResultBackend: "redis://" + viper.GetString("redis_url"),
ResultsExpireIn: 5,
}
machineryServer, err = machinery.NewServer(macConfig)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
machineryServer, _ = machinery.NewServer(machineryConfig)
uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.0rc2; " + hostURL.Host + ")"
relayState = state.NewState(redisClient)
Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey)
WebfingerResource.GenerateFromActor(hostURL, &Actor)
// Load Config
relayState = state.NewState(redisClient)
http.HandleFunc("/.well-known/webfinger", handleWebfinger)
http.HandleFunc("/actor", handleActor)
http.HandleFunc("/inbox", func(w http.ResponseWriter, r *http.Request) {
handleInbox(w, r, decodeActivity)
})
fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Server]\n - Configrations")
fmt.Println("RELAY DOMAIN : ", relayDomain)
fmt.Println("REDIS URL : ", redisURL)
fmt.Println("BIND ADDRESS : ", relayBind)
fmt.Println("RELAY DOMAIN : ", hostURL.Host)
fmt.Println("REDIS URL : ", viper.GetString("redis_url"))
fmt.Println("BIND ADDRESS : ", viper.GetString("relay_bind"))
fmt.Println(" - Blocked Domain")
domains, _ := redisClient.HKeys("relay:config:blockedDomain").Result()
for _, domain := range domains {
@ -101,5 +66,18 @@ func main() {
for _, domain := range domains {
fmt.Println(domain)
}
http.ListenAndServe(relayBind, nil)
}
func main() {
// Load Config
initConfig()
actorCache = cache.New(5*time.Minute, 10*time.Minute)
http.HandleFunc("/.well-known/webfinger", handleWebfinger)
http.HandleFunc("/actor", handleActor)
http.HandleFunc("/inbox", func(w http.ResponseWriter, r *http.Request) {
handleInbox(w, r, decodeActivity)
})
http.ListenAndServe(viper.GetString("relay_bind"), nil)
}

View File

@ -1,43 +1,19 @@
package main
import (
"net/url"
"os"
"testing"
"github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/State"
"github.com/spf13/viper"
)
func TestMain(m *testing.M) {
os.Setenv("ACTOR_PEM", "misc/testKey.pem")
os.Setenv("RELAY_DOMAIN", "relay.yukimochi.example.org")
pemPath := os.Getenv("ACTOR_PEM")
relayDomain := os.Getenv("RELAY_DOMAIN")
redisURL := os.Getenv("REDIS_URL")
hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
hostURL, _ = url.Parse("https://" + relayDomain)
redisClient = redis.NewClient(&redis.Options{
Addr: redisURL,
})
var macConfig = &config.Config{
Broker: "redis://" + redisURL,
DefaultQueue: "relay",
ResultBackend: "redis://" + redisURL,
ResultsExpireIn: 5,
}
machineryServer, _ = machinery.NewServer(macConfig)
Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey)
WebfingerResource.GenerateFromActor(hostURL, &Actor)
viper.Set("Actor_pem", "misc/testKey.pem")
viper.Set("Relay_domain", "relay.yukimochi.example.org")
initConfig()
// Load Config
redisClient.FlushAll().Result()
relayState = state.NewState(redisClient)
code := m.Run()
os.Exit(code)
redisClient.FlushAll().Result()
relayState.RedisClient.FlushAll().Result()
}

View File

@ -1,4 +1,4 @@
package activitypub
package main
import (
"bytes"
@ -8,16 +8,12 @@ import (
"errors"
"fmt"
"net/http"
"os"
"time"
"github.com/Songmu/go-httpdate"
"github.com/yukimochi/httpsig"
)
// UaString : Use for User-Agent
var UaString = os.Getenv("RELAY_SERVICENAME") + " (golang net/http; Activity-Relay v0.2.0rc2; " + os.Getenv("RELAY_DOMAIN") + ")"
func appendSignature(request *http.Request, body *[]byte, KeyID string, publicKey *rsa.PrivateKey) error {
hash := sha256.New()
hash.Write(*body)
@ -36,11 +32,10 @@ func appendSignature(request *http.Request, body *[]byte, KeyID string, publicKe
return nil
}
// SendActivity : Send ActivityPub activity
func SendActivity(inboxURL string, KeyID string, body []byte, publicKey *rsa.PrivateKey) error {
func sendActivity(inboxURL string, KeyID string, body []byte, publicKey *rsa.PrivateKey) error {
req, _ := http.NewRequest("POST", inboxURL, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UaString)
req.Header.Set("User-Agent", uaString)
req.Header.Set("Date", httpdate.Time2Str(time.Now()))
appendSignature(req, &body, KeyID, publicKey)
client := &http.Client{Timeout: time.Duration(5) * time.Second}

View File

@ -11,6 +11,7 @@ import (
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/satori/go.uuid"
"github.com/spf13/viper"
"github.com/yukimochi/Activity-Relay/ActivityPub"
"github.com/yukimochi/Activity-Relay/KeyLoader"
)
@ -20,12 +21,14 @@ var Actor activitypub.Actor
var hostURL *url.URL
var hostPrivatekey *rsa.PrivateKey
var machineryServer *machinery.Server
var redisClient *redis.Client
var uaString string
func relayActivity(args ...string) error {
inboxURL := args[0]
body := args[1]
err := activitypub.SendActivity(inboxURL, Actor.ID, []byte(body), hostPrivatekey)
err := sendActivity(inboxURL, Actor.ID, []byte(body), hostPrivatekey)
if err != nil {
domain, _ := url.Parse(inboxURL)
mod, _ := redisClient.HSetNX("relay:statistics:"+domain.Host, "last_error", err.Error()).Result()
@ -39,63 +42,48 @@ func relayActivity(args ...string) error {
func registorActivity(args ...string) error {
inboxURL := args[0]
body := args[1]
err := activitypub.SendActivity(inboxURL, Actor.ID, []byte(body), hostPrivatekey)
err := sendActivity(inboxURL, Actor.ID, []byte(body), hostPrivatekey)
return err
}
func main() {
pemPath := os.Getenv("ACTOR_PEM")
if pemPath == "" {
panic("Require ACTOR_PEM environment variable.")
}
relayDomain := os.Getenv("RELAY_DOMAIN")
if relayDomain == "" {
panic("Require RELAY_DOMAIN environment variable.")
}
redisURL := os.Getenv("REDIS_URL")
if redisURL == "" {
redisURL = "127.0.0.1:6379"
}
var err error
hostPrivatekey, err = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
if err != nil {
panic("Can't read Hostkey Pemfile")
}
hostURL, err = url.Parse("https://" + relayDomain)
if err != nil {
panic("Can't parse Relay Domain")
}
func initConfig() {
viper.BindEnv("actor_pem")
viper.BindEnv("relay_domain")
viper.BindEnv("relay_servicename")
viper.BindEnv("redis_url")
hostURL, _ = url.Parse("https://" + viper.GetString("relay_domain"))
hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem"))
redisClient = redis.NewClient(&redis.Options{
Addr: redisURL,
Addr: viper.GetString("redis_url"),
})
Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey)
machineryConfig := &config.Config{
Broker: "redis://" + redisURL,
Broker: "redis://" + viper.GetString("redis_url"),
DefaultQueue: "relay",
ResultBackend: "redis://" + redisURL,
ResultBackend: "redis://" + viper.GetString("redis_url"),
ResultsExpireIn: 5,
}
server, err := machinery.NewServer(machineryConfig)
machineryServer, _ = machinery.NewServer(machineryConfig)
uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.0rc2; " + hostURL.Host + ")"
Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey)
fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Worker]\n - Configrations")
fmt.Println("RELAY DOMAIN : ", hostURL.Host)
fmt.Println("REDIS URL : ", viper.GetString("redis_url"))
}
func main() {
initConfig()
err := machineryServer.RegisterTask("registor", registorActivity)
if err != nil {
panic(err.Error())
}
err = server.RegisterTask("registor", registorActivity)
if err != nil {
panic(err.Error())
}
err = server.RegisterTask("relay", relayActivity)
err = machineryServer.RegisterTask("relay", relayActivity)
if err != nil {
panic(err.Error())
}
workerID := uuid.NewV4()
worker := server.NewWorker(workerID.String(), 200)
fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Worker]\n - Configrations")
fmt.Println("RELAY DOMAIN : ", relayDomain)
fmt.Println("REDIS URL : ", redisURL)
worker := machineryServer.NewWorker(workerID.String(), 200)
err = worker.Launch()
if err != nil {
fmt.Fprintln(os.Stderr, err)