Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f48e36da8
|
||
|
|
7e3eee56e0
|
||
|
|
616997fdc6
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -16,3 +16,4 @@
|
||||
|
||||
sour.is-keyproofs
|
||||
.env
|
||||
/pub
|
||||
3
Makefile
3
Makefile
@@ -1,7 +1,7 @@
|
||||
NAME=sour.is-keyproofs
|
||||
BUMP?=current
|
||||
DATE:=$(shell date -u +%FT%TZ)
|
||||
HASH:=$(shell git rev-pars HEAD 2> /dev/null)
|
||||
HASH:=$(shell git rev-parse HEAD 2> /dev/null)
|
||||
VERSION:=$(shell BUMP=$(BUMP) ./version.sh)
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ version:
|
||||
@echo $(VERSION)
|
||||
tag:
|
||||
git tag -a v$(VERSION) -m "Version: $(VERSION)"
|
||||
git push --tag
|
||||
release:
|
||||
@make tag BUMP=patch
|
||||
run:
|
||||
|
||||
25
example.env
25
example.env
@@ -1,5 +1,7 @@
|
||||
# Rename to '.env' or pass required items to environment when running.
|
||||
|
||||
# Basic Configuration.
|
||||
|
||||
# REDDIT_APIKEY [REQUIRED]
|
||||
# REDDIT_SECRET [REQUIRED]
|
||||
# To prevent reddits low ratelimits for non-authenticated requests
|
||||
@@ -26,6 +28,12 @@ HTTP_LISTEN=
|
||||
|
||||
BASE_URL=
|
||||
|
||||
# AVATAR_PATH [OPTIONAL]
|
||||
# To set the path for avatar/bg/cover image directories to serve. (default: pub)
|
||||
# Path should allow read/write to application. The folders will be generated automatically.
|
||||
|
||||
# Advanced Options. These are used to customize the application in non-standard deployments
|
||||
|
||||
# XMPP_URL [OPTIONAL]
|
||||
# To set XMPP http url for VCard verification. (default: BASE_URL)
|
||||
|
||||
@@ -35,3 +43,20 @@ XMPP_URL=
|
||||
# To set DNS http url for DNS verification. (default: BASE_URL)
|
||||
|
||||
XMPP_URL=
|
||||
|
||||
# Avatar app
|
||||
# DISABLE_AVATAR [OPTIONAL]
|
||||
# Disable the Avatar application. Set to any value other than "false"
|
||||
|
||||
# DNS app
|
||||
# DISABLE_DNS [OPTIONAL]
|
||||
# Disable the DNS application. Set to any value other than "false"
|
||||
|
||||
# Keyproofs app
|
||||
# DISABLE_KEYPROOFS [OPTIONAL]
|
||||
# Disable the KeyProofs application. Set to any value other than "false"
|
||||
|
||||
# XMPP VCard app
|
||||
# DISABLE_VCARD [OPTIONAL]
|
||||
# Disable the VCard application. Set to any value other than "false"
|
||||
# If disabled the username/password are no longer required.
|
||||
|
||||
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module github.com/sour-is/keyproofs
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.4.7
|
||||
github.com/go-chi/chi v4.1.2+incompatible
|
||||
github.com/google/go-cmp v0.5.4 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4
|
||||
|
||||
1
go.sum
1
go.sum
@@ -138,6 +138,7 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20190927073244-c990c680b611/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
||||
52
main.go
52
main.go
@@ -86,38 +86,48 @@ func run(ctx context.Context) error {
|
||||
Credential: xmpp.Password(os.Getenv("XMPP_PASSWORD")),
|
||||
})
|
||||
|
||||
// configure cors middleware
|
||||
corsMiddleware := cors.New(cors.Options{
|
||||
AllowCredentials: true,
|
||||
AllowedMethods: strings.Fields(env("CORS_METHODS", "GET")),
|
||||
AllowedOrigins: strings.Fields(env("CORS_ORIGIN", "*")),
|
||||
}).Handler
|
||||
|
||||
mux := chi.NewRouter()
|
||||
mux.Use(
|
||||
cfg.ApplyHTTP,
|
||||
secHeaders,
|
||||
corsMiddleware,
|
||||
cors.New(cors.Options{
|
||||
AllowCredentials: true,
|
||||
AllowedMethods: strings.Fields(env("CORS_METHODS", "GET")),
|
||||
AllowedOrigins: strings.Fields(env("CORS_ORIGIN", "*")),
|
||||
}).Handler,
|
||||
middleware.RequestID,
|
||||
middleware.RealIP,
|
||||
middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: accessLog(log.Info)}),
|
||||
middleware.Recoverer,
|
||||
)
|
||||
|
||||
// Create cache for promise engine
|
||||
arc, _ := lru.NewARC(4096)
|
||||
c := cache.New(arc)
|
||||
|
||||
keyproofApp := keyproofs.NewKeyProofApp(ctx, c)
|
||||
dnsApp := keyproofs.NewDNSApp(ctx)
|
||||
vcardApp, err := keyproofs.NewVCardApp(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
if env("DISABLE_KEYPROOF", "false") == "false" {
|
||||
// Create cache for promise engine
|
||||
arc, _ := lru.NewARC(4096)
|
||||
c := cache.New(arc)
|
||||
keyproofs.NewKeyProofApp(ctx, c).Routes(mux)
|
||||
}
|
||||
|
||||
keyproofApp.Routes(mux)
|
||||
dnsApp.Routes(mux)
|
||||
vcardApp.Routes(mux)
|
||||
if env("DISABLE_DNS", "false") == "false" {
|
||||
keyproofs.NewDNSApp(ctx).Routes(mux)
|
||||
}
|
||||
|
||||
if env("DISABLE_AVATAR", "false") == "false" {
|
||||
avatarApp, err := keyproofs.NewAvatarApp(ctx, env("AVATAR_PATH", "pub"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
avatarApp.Routes(mux)
|
||||
}
|
||||
|
||||
if env("DISABLE_VCARD", "false") == "false" {
|
||||
vcardApp, err := keyproofs.NewVCardApp(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vcardApp.Routes(mux)
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("app", cfg.GetString("app-name")).
|
||||
@@ -127,7 +137,7 @@ func run(ctx context.Context) error {
|
||||
Str("listen", listen).
|
||||
Msg("startup")
|
||||
|
||||
err = New(&http.Server{
|
||||
err := New(&http.Server{
|
||||
Addr: listen,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
|
||||
230
pkg/keyproofs/routes-avatar.go
Normal file
230
pkg/keyproofs/routes-avatar.go
Normal file
@@ -0,0 +1,230 @@
|
||||
package keyproofs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/sour-is/keyproofs/pkg/graceful"
|
||||
)
|
||||
|
||||
type avatarApp struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func NewAvatarApp(ctx context.Context, path string) (*avatarApp, error) {
|
||||
log := log.Ctx(ctx)
|
||||
|
||||
path = filepath.Clean(path)
|
||||
app := &avatarApp{path: path}
|
||||
err := app.CheckFiles(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
watch, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, typ := range []string{"avatar", "bg", "cover"} {
|
||||
err = watch.Add(filepath.Join(path, typ))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Msg("startup avatar watcher")
|
||||
wg := graceful.WaitGroup(ctx)
|
||||
wg.Go(func() error {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Debug().Msg("shutdown avatar watcher")
|
||||
return nil
|
||||
case op := <-watch.Events:
|
||||
log.Print(op)
|
||||
switch op.Op {
|
||||
case fsnotify.Create:
|
||||
path = filepath.Dir(op.Name)
|
||||
kind := filepath.Base(path)
|
||||
name := filepath.Base(op.Name)
|
||||
if err := createLinks(app.path, kind, name); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
case fsnotify.Remove, fsnotify.Rename:
|
||||
path = filepath.Dir(op.Name)
|
||||
kind := filepath.Base(path)
|
||||
name := filepath.Base(op.Name)
|
||||
if err := removeLinks(app.path, kind, name); err != nil {
|
||||
log.Error().Err(err).Send()
|
||||
}
|
||||
default:
|
||||
}
|
||||
case err := <-watch.Errors:
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (app *avatarApp) CheckFiles(ctx context.Context) error {
|
||||
log := log.Ctx(ctx)
|
||||
|
||||
for _, name := range []string{".links", "avatar", "bg", "cover"} {
|
||||
log.Debug().Msgf("mkdir: %s", filepath.Join(app.path, name))
|
||||
err := os.MkdirAll(filepath.Join(app.path, name), 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return filepath.Walk(app.path, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
if info.Name() == ".links" {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
path = filepath.Dir(path)
|
||||
kind := filepath.Base(path)
|
||||
name := info.Name()
|
||||
|
||||
log.Debug().Msgf("link: %s %s %s", app.path, kind, name)
|
||||
|
||||
return createLinks(app.path, kind, name)
|
||||
})
|
||||
}
|
||||
|
||||
func (app *avatarApp) get(w http.ResponseWriter, r *http.Request) {
|
||||
log := log.Ctx(r.Context())
|
||||
|
||||
log.Print(r.Host)
|
||||
|
||||
kind := chi.URLParam(r, "kind")
|
||||
hash := chi.URLParam(r, "hash")
|
||||
|
||||
if strings.ContainsRune(hash, '@') {
|
||||
avatarHost, _, err := styleSRV(r.Context(), hash)
|
||||
if err != nil {
|
||||
writeText(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
hash = hashSHA1(strings.ToLower(hash))
|
||||
http.Redirect(w, r, fmt.Sprintf("https://%s/%s/%s?%s", avatarHost, kind, hash, r.URL.RawQuery), 301)
|
||||
return
|
||||
}
|
||||
|
||||
fname := filepath.Join(app.path, ".links", strings.Join([]string{kind, hash}, "-"))
|
||||
log.Debug().Msgf("path: %s", fname)
|
||||
|
||||
f, err := os.Open(fname)
|
||||
if err != nil {
|
||||
writeText(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(w, f)
|
||||
if err != nil {
|
||||
writeText(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (app *avatarApp) Routes(r *chi.Mux) {
|
||||
r.MethodFunc("GET", "/{kind:avatar|bg|cover}/{hash}", app.get)
|
||||
}
|
||||
|
||||
func hashMD5(name string) string {
|
||||
h := md5.New()
|
||||
_, _ = h.Write([]byte(name))
|
||||
|
||||
return fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
||||
func hashSHA1(name string) string {
|
||||
h := sha1.New()
|
||||
_, _ = h.Write([]byte(name))
|
||||
|
||||
return fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
||||
|
||||
func createLinks(path, kind, name string) error {
|
||||
if !strings.ContainsRune(name, '@') {
|
||||
return nil
|
||||
}
|
||||
|
||||
src := filepath.Join("..", kind, name)
|
||||
name = strings.ToLower(name)
|
||||
|
||||
hash := hashMD5(name)
|
||||
link := filepath.Join(path, ".links", strings.Join([]string{kind, hash}, "-"))
|
||||
err := replaceLink(src, link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hash = hashSHA1(name)
|
||||
link = filepath.Join(path, ".links", strings.Join([]string{kind, hash}, "-"))
|
||||
err = replaceLink(src, link)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func removeLinks(path, kind, name string) error {
|
||||
if !strings.ContainsRune(name, '@') {
|
||||
return nil
|
||||
}
|
||||
name = strings.ToLower(name)
|
||||
|
||||
hash := hashMD5(name)
|
||||
link := filepath.Join(path, ".links", strings.Join([]string{kind, hash}, "-"))
|
||||
err := os.Remove(link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hash = hashSHA1(name)
|
||||
link = filepath.Join(path, ".links", strings.Join([]string{kind, hash}, "-"))
|
||||
err = os.Remove(link)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func replaceLink(src, link string) error {
|
||||
if dst, err := os.Readlink(link); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
err = os.Symlink(src, link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if dst != src {
|
||||
err = os.Remove(link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Symlink(src, link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -147,6 +147,7 @@ func (app *keyproofApp) getProofs(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
page := page{Style: defaultStyle}
|
||||
page.AppName = fmt.Sprintf("%s v%s", cfg.GetString("app-name"), cfg.GetString("app-version"))
|
||||
page.AppBuild = fmt.Sprintf("%s %s", cfg.GetString("build-date"), cfg.GetString("build-hash"))
|
||||
|
||||
// Wait for either entity to resolve or timeout
|
||||
select {
|
||||
|
||||
@@ -37,7 +37,6 @@ func getStyle(ctx context.Context, email string) (*Style, error) {
|
||||
hash := md5.New()
|
||||
email = strings.TrimSpace(strings.ToLower(email))
|
||||
_, _ = hash.Write([]byte(email))
|
||||
|
||||
id := hash.Sum(nil)
|
||||
|
||||
style := &Style{}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package keyproofs
|
||||
|
||||
type page struct {
|
||||
AppName string
|
||||
Entity *Entity
|
||||
Style *Style
|
||||
Proofs *Proofs
|
||||
AppName string
|
||||
AppBuild string
|
||||
Entity *Entity
|
||||
Style *Style
|
||||
Proofs *Proofs
|
||||
|
||||
Markdown string
|
||||
HasProofs bool
|
||||
@@ -50,6 +51,7 @@ var pageTPL = `
|
||||
.shade { background-color: {{index .Palette 3}}80; border-radius: .25rem;}
|
||||
.lead { padding:0; margin:0; }
|
||||
.scroll { height: 20em; overflow: scroll; }
|
||||
|
||||
@media only screen and (max-width: 991px) {
|
||||
.jumbotron h1 { font-size: 2rem; }
|
||||
.jumbotron .lead { font-size: 1.0rem; }
|
||||
@@ -80,7 +82,7 @@ var pageTPL = `
|
||||
{{template "content" .}}
|
||||
|
||||
<div class="card-footer text-muted text-center">
|
||||
<a href="/">{{.AppName}}</a>
|
||||
<a href="/" alt="{{.AppBuild}}">{{.AppName}}</a>
|
||||
| © 2020 Sour.is
|
||||
| <a href="/id/me@sour.is">About me</a>
|
||||
| <a href="https://github.com/sour-is/keyproofs">GitHub</a>
|
||||
|
||||
Reference in New Issue
Block a user