satdress/main.go

160 lines
4.2 KiB
Go
Raw Normal View History

2021-08-13 16:21:00 +00:00
package main
import (
"embed"
2021-08-13 16:21:00 +00:00
"fmt"
"net/http"
"os"
2021-08-14 23:52:16 +00:00
"strings"
2021-08-13 16:21:00 +00:00
"time"
"github.com/cockroachdb/pebble"
"github.com/fiatjaf/makeinvoice"
2021-08-13 16:21:00 +00:00
"github.com/gorilla/mux"
"github.com/kelseyhightower/envconfig"
_ "github.com/lib/pq"
"github.com/rs/cors"
2021-08-13 16:21:00 +00:00
"github.com/rs/zerolog"
)
type Settings struct {
2022-01-18 23:50:47 +00:00
Host string `envconfig:"HOST" default:"0.0.0.0"`
Port string `envconfig:"PORT" required:"true"`
Domain string `envconfig:"DOMAIN" required:"true"`
// GlobalUsers means that user@ part is globally unique across all domains
// WARNING: if you toggle this existing users won't work anymore for safety reasons!
GlobalUsers bool `envconfig:"GLOBAL_USERS" required:"false" default:false`
2021-08-14 23:52:16 +00:00
Secret string `envconfig:"SECRET" required:"true"`
SiteOwnerName string `envconfig:"SITE_OWNER_NAME" required:"true"`
SiteOwnerURL string `envconfig:"SITE_OWNER_URL" required:"true"`
SiteName string `envconfig:"SITE_NAME" required:"true"`
2022-01-18 23:50:47 +00:00
ForceMigrate bool `envconfig:"FORCE_MIGRATE" required:"false" default:false`
TorProxyURL string `envconfig:"TOR_PROXY_URL"`
2021-08-13 16:21:00 +00:00
}
var (
s Settings
db *pebble.DB
router = mux.NewRouter()
log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stderr})
)
2021-08-13 16:21:00 +00:00
//go:embed index.html
var indexHTML string
//go:embed grab.html
var grabHTML string
//go:embed static
var static embed.FS
2021-08-13 16:21:00 +00:00
func main() {
err := envconfig.Process("", &s)
if err != nil {
log.Fatal().Err(err).Msg("couldn't process envconfig.")
}
// increase default makeinvoice client timeout because people are using tor
makeinvoice.Client = &http.Client{Timeout: 25 * time.Second}
s.Domain = strings.ToLower(s.Domain)
if s.TorProxyURL != "" {
makeinvoice.TorProxyURL = s.TorProxyURL
}
2022-01-18 23:50:47 +00:00
dbName := fmt.Sprintf("%v-multiple.db", s.SiteName)
if _, err := os.Stat(dbName); os.IsNotExist(err) || s.ForceMigrate {
for _, one := range getDomains(s.Domain) {
tryMigrate(one, dbName)
}
}
db, err = pebble.Open(dbName, nil)
2021-08-13 16:21:00 +00:00
if err != nil {
2022-01-18 23:50:47 +00:00
log.Fatal().Err(err).Str("path", dbName).Msg("failed to open db.")
2021-08-13 16:21:00 +00:00
}
2022-01-18 23:50:47 +00:00
router.Path("/.well-known/lnurlp/{user}").Methods("GET").
2021-08-13 16:21:00 +00:00
HandlerFunc(handleLNURL)
router.Path("/").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
renderHTML(w, indexHTML, map[string]interface{}{})
2021-08-13 16:21:00 +00:00
},
)
router.PathPrefix("/static/").Handler(http.FileServer(http.FS(static)))
2021-08-13 16:21:00 +00:00
router.Path("/grab").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
2022-01-18 23:50:47 +00:00
if name == "" || r.FormValue("kind") == "" {
sendError(w, 500, "internal error")
return
}
2022-01-18 23:50:47 +00:00
// might not get domain back
domain := r.FormValue("domain")
if domain == "" {
if !strings.Contains(s.Domain, ",") {
domain = s.Domain
} else {
sendError(w, 500, "internal error")
return
}
}
pin, inv, err := SaveName(name, domain, &Params{
2022-03-16 00:16:02 +00:00
Kind: r.FormValue("kind"),
Host: r.FormValue("host"),
Key: r.FormValue("key"),
Pak: r.FormValue("pak"),
Waki: r.FormValue("waki"),
NodeId: r.FormValue("nodeid"),
Rune: r.FormValue("rune"),
2021-08-20 12:32:45 +00:00
}, r.FormValue("pin"))
if err != nil {
2021-08-13 16:21:00 +00:00
w.WriteHeader(500)
2021-08-20 12:32:45 +00:00
fmt.Fprint(w, err.Error())
2021-08-13 16:21:00 +00:00
return
}
renderHTML(w, grabHTML, struct {
2022-01-18 23:50:47 +00:00
PIN string `json:"pin"`
Invoice string `json:"invoice"`
Name string `json:"name"`
ActualDomain string `json:"actual_domain"`
}{pin, inv, name, domain})
2021-08-13 16:21:00 +00:00
},
)
api := router.PathPrefix("/api/v1").Subrouter()
api.Use(authenticate)
// unauthenticated
api.HandleFunc("/claim", ClaimAddress).Methods("POST")
// authenticated routes; X-Pin in header or in json request body
2022-01-18 23:50:47 +00:00
api.HandleFunc("/users/{name}@{domain}", GetUser).Methods("GET")
api.HandleFunc("/users/{name}@{domain}", UpdateUser).Methods("PUT")
api.HandleFunc("/users/{name}@{domain}", DeleteUser).Methods("DELETE")
2021-08-13 16:21:00 +00:00
srv := &http.Server{
Handler: cors.Default().Handler(router),
2021-08-13 16:21:00 +00:00
Addr: s.Host + ":" + s.Port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Debug().Str("addr", srv.Addr).Msg("listening")
srv.ListenAndServe()
}
2022-01-18 23:50:47 +00:00
func getDomains(s string) []string {
splitFn := func(c rune) bool {
return c == ','
}
return strings.FieldsFunc(s, splitFn)
}