From 6ca6c9aad0667cca296a28d5073f05ea7ab29874 Mon Sep 17 00:00:00 2001 From: Naoki Kosaka Date: Sun, 2 Jun 2019 21:08:40 +0900 Subject: [PATCH] Update build step. --- Dockerfile | 6 +++--- cli/cli.go | 45 +++++++++++++++++++++++++-------------------- cli/follow.go | 2 +- decode.go | 8 +++++--- main.go | 42 +++++++++++++++++++++++------------------- worker/sender.go | 3 ++- worker/worker.go | 34 +++++++++++++++++++--------------- 7 files changed, 78 insertions(+), 62 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6defd0c..f9420ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,9 +5,9 @@ COPY . /Activity-Relay RUN mkdir -p /rootfs/usr/bin && \ apk add -U --no-cache git && \ - go build -o /rootfs/usr/bin/server . && \ - go build -o /rootfs/usr/bin/worker ./worker && \ - go build -o /rootfs/usr/bin/ar-cli ./cli + go build -o /rootfs/usr/bin/server -ldflags "-X main.version=$(git describe --tags HEAD)" . && \ + go build -o /rootfs/usr/bin/worker -ldflags "-X main.version=$(git describe --tags HEAD)" ./worker && \ + go build -o /rootfs/usr/bin/ar-cli -ldflags "-X main.version=$(git describe --tags HEAD)" ./cli FROM alpine diff --git a/cli/cli.go b/cli/cli.go index cf767a5..9a51c55 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -15,13 +15,17 @@ import ( state "github.com/yukimochi/Activity-Relay/State" ) -// Actor : Relay's Actor -var Actor activitypub.Actor +var ( + version string -var hostname *url.URL -var hostkey *rsa.PrivateKey -var macServer *machinery.Server -var relayState state.RelayState + // Actor : Relay's Actor + Actor activitypub.Actor + + hostname *url.URL + hostkey *rsa.PrivateKey + relayState state.RelayState + machineryServer *machinery.Server +) func initConfig() { viper.SetConfigName("config") @@ -30,41 +34,42 @@ func initConfig() { if err != nil { fmt.Println("Config file is not exists. Use environment variables.") viper.BindEnv("actor_pem") - viper.BindEnv("relay_domain") - viper.BindEnv("relay_bind") - viper.BindEnv("relay_servicename") viper.BindEnv("redis_url") + viper.BindEnv("relay_bind") + viper.BindEnv("relay_domain") + viper.BindEnv("relay_servicename") } else { Actor.Summary = viper.GetString("relay_summary") - Actor.Icon = activitypub.Image{viper.GetString("relay_icon")} - Actor.Image = activitypub.Image{viper.GetString("relay_image")} + Actor.Icon = activitypub.Image{URL: viper.GetString("relay_icon")} + Actor.Image = activitypub.Image{URL: viper.GetString("relay_image")} } Actor.Name = viper.GetString("relay_servicename") - hostkey, err := keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem")) - if err != nil { - panic(err) - } hostname, err = url.Parse("https://" + viper.GetString("relay_domain")) if err != nil { panic(err) } - redOption, err := redis.ParseURL(viper.GetString("redis_url")) + hostkey, err := keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem")) if err != nil { panic(err) } - redClient := redis.NewClient(redOption) - var macConfig = &config.Config{ + redisOption, err := redis.ParseURL(viper.GetString("redis_url")) + if err != nil { + panic(err) + } + redisClient := redis.NewClient(redisOption) + relayState = state.NewState(redisClient) + var machineryConfig = &config.Config{ Broker: viper.GetString("redis_url"), DefaultQueue: "relay", ResultBackend: viper.GetString("redis_url"), ResultsExpireIn: 5, } - macServer, err = machinery.NewServer(macConfig) + machineryServer, err = machinery.NewServer(machineryConfig) if err != nil { panic(err) } - relayState = state.NewState(redClient) + Actor.GenerateSelfKey(hostname, &hostkey.PublicKey) } diff --git a/cli/follow.go b/cli/follow.go index 640457a..0d5f08d 100644 --- a/cli/follow.go +++ b/cli/follow.go @@ -74,7 +74,7 @@ func pushRegistorJob(inboxURL string, body []byte) { }, }, } - _, err := macServer.SendTask(job) + _, err := machineryServer.SendTask(job) if err != nil { fmt.Fprintln(os.Stderr, err) } diff --git a/decode.go b/decode.go index ea00c7d..7f679d1 100644 --- a/decode.go +++ b/decode.go @@ -5,11 +5,13 @@ import ( "encoding/base64" "encoding/json" "errors" + "fmt" "net/http" "strconv" - "github.com/yukimochi/Activity-Relay/ActivityPub" - "github.com/yukimochi/Activity-Relay/KeyLoader" + "github.com/spf13/viper" + activitypub "github.com/yukimochi/Activity-Relay/ActivityPub" + keyloader "github.com/yukimochi/Activity-Relay/KeyLoader" "github.com/yukimochi/httpsig" ) @@ -26,7 +28,7 @@ func decodeActivity(request *http.Request) (*activitypub.Activity, *activitypub. } KeyID := verifier.KeyId() remoteActor := new(activitypub.Actor) - err = remoteActor.RetrieveRemoteActor(KeyID, uaString, actorCache) + err = remoteActor.RetrieveRemoteActor(KeyID, fmt.Sprintf("%s (golang net/http; Activity-Relay %s; %s)", viper.GetString("relay_servicename"), version, hostURL.Host), actorCache) if err != nil { return nil, nil, nil, err } diff --git a/main.go b/main.go index a5ee54d..d5ff4f1 100644 --- a/main.go +++ b/main.go @@ -17,18 +17,21 @@ import ( state "github.com/yukimochi/Activity-Relay/State" ) -// Actor : Relay's Actor -var Actor activitypub.Actor +var ( + version string -// WebfingerResource : Relay's Webfinger resource -var WebfingerResource activitypub.WebfingerResource + // Actor : Relay's Actor + Actor activitypub.Actor -var hostURL *url.URL -var hostPrivatekey *rsa.PrivateKey -var actorCache *cache.Cache -var machineryServer *machinery.Server -var relayState state.RelayState -var uaString string + // WebfingerResource : Relay's Webfinger resource + WebfingerResource activitypub.WebfingerResource + + hostURL *url.URL + hostPrivatekey *rsa.PrivateKey + relayState state.RelayState + machineryServer *machinery.Server + actorCache *cache.Cache +) func initConfig() { viper.SetConfigName("config") @@ -37,14 +40,14 @@ func initConfig() { if err != nil { fmt.Println("Config file is not exists. Use environment variables.") viper.BindEnv("actor_pem") - viper.BindEnv("relay_domain") - viper.BindEnv("relay_bind") - viper.BindEnv("relay_servicename") viper.BindEnv("redis_url") + viper.BindEnv("relay_bind") + viper.BindEnv("relay_domain") + viper.BindEnv("relay_servicename") } else { Actor.Summary = viper.GetString("relay_summary") - Actor.Icon = activitypub.Image{viper.GetString("relay_icon")} - Actor.Image = activitypub.Image{viper.GetString("relay_image")} + Actor.Icon = activitypub.Image{URL: viper.GetString("relay_icon")} + Actor.Image = activitypub.Image{URL: viper.GetString("relay_image")} } Actor.Name = viper.GetString("relay_servicename") @@ -55,6 +58,7 @@ func initConfig() { panic(err) } redisClient := redis.NewClient(redisOption) + relayState = state.NewState(redisClient) machineryConfig := &config.Config{ Broker: viper.GetString("redis_url"), DefaultQueue: "relay", @@ -65,13 +69,13 @@ func initConfig() { if err != nil { panic(err) } - uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.3; " + hostURL.Host + ")" - relayState = state.NewState(redisClient) - actorCache = cache.New(5*time.Minute, 10*time.Minute) + Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey) + actorCache = cache.New(5*time.Minute, 10*time.Minute) WebfingerResource.GenerateFromActor(hostURL, &Actor) - fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Server]\n - Configrations") + fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Server]", version) + fmt.Println(" - Configrations") fmt.Println("RELAY DOMAIN : ", hostURL.Host) fmt.Println("REDIS URL : ", viper.GetString("redis_url")) fmt.Println("BIND ADDRESS : ", viper.GetString("relay_bind")) diff --git a/worker/sender.go b/worker/sender.go index 30084f2..c5fcc94 100644 --- a/worker/sender.go +++ b/worker/sender.go @@ -11,6 +11,7 @@ import ( "time" httpdate "github.com/Songmu/go-httpdate" + "github.com/spf13/viper" "github.com/yukimochi/httpsig" ) @@ -35,7 +36,7 @@ func appendSignature(request *http.Request, body *[]byte, KeyID string, publicKe 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") - req.Header.Set("User-Agent", uaString) + req.Header.Set("User-Agent", fmt.Sprintf("%s (golang net/http; Activity-Relay %s; %s)", viper.GetString("relay_servicename"), version, hostURL.Host)) req.Header.Set("Date", httpdate.Time2Str(time.Now())) appendSignature(req, &body, KeyID, publicKey) client := &http.Client{Timeout: time.Duration(5) * time.Second} diff --git a/worker/worker.go b/worker/worker.go index 4debbf3..d8baa8f 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -17,14 +17,17 @@ import ( keyloader "github.com/yukimochi/Activity-Relay/KeyLoader" ) -// Actor : Relay's Actor -var Actor activitypub.Actor +var ( + version string -var hostURL *url.URL -var hostPrivatekey *rsa.PrivateKey -var machineryServer *machinery.Server -var redisClient *redis.Client -var uaString string + // Actor : Relay's Actor + Actor activitypub.Actor + + hostURL *url.URL + hostPrivatekey *rsa.PrivateKey + redisClient *redis.Client + machineryServer *machinery.Server +) func relayActivity(args ...string) error { inboxURL := args[0] @@ -54,14 +57,14 @@ func initConfig() { if err != nil { fmt.Println("Config file is not exists. Use environment variables.") viper.BindEnv("actor_pem") - viper.BindEnv("relay_domain") - viper.BindEnv("relay_bind") - viper.BindEnv("relay_servicename") viper.BindEnv("redis_url") + viper.BindEnv("relay_bind") + viper.BindEnv("relay_domain") + viper.BindEnv("relay_servicename") } else { Actor.Summary = viper.GetString("relay_summary") - Actor.Icon = activitypub.Image{viper.GetString("relay_icon")} - Actor.Image = activitypub.Image{viper.GetString("relay_image")} + Actor.Icon = activitypub.Image{URL: viper.GetString("relay_icon")} + Actor.Image = activitypub.Image{URL: viper.GetString("relay_image")} } Actor.Name = viper.GetString("relay_servicename") @@ -82,12 +85,13 @@ func initConfig() { if err != nil { panic(err) } + + Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey) newNullLogger := NewNullLogger() log.DEBUG = newNullLogger - uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.3; " + hostURL.Host + ")" - Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey) - fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Worker]\n - Configrations") + fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Worker]", version) + fmt.Println(" - Configrations") fmt.Println("RELAY DOMAIN : ", hostURL.Host) fmt.Println("REDIS URL : ", viper.GetString("redis_url")) }