2021-08-13 16:21:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"encoding/json"
|
|
|
|
"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/gorilla/mux"
|
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Settings struct {
|
2021-08-14 23:52:16 +00:00
|
|
|
Host string `envconfig:"HOST" default:"0.0.0.0"`
|
|
|
|
Port string `envconfig:"PORT" required:"true"`
|
|
|
|
Domain string `envconfig:"DOMAIN" required:"true"`
|
|
|
|
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"`
|
2021-08-13 16:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var s Settings
|
|
|
|
var db *pebble.DB
|
|
|
|
var router = mux.NewRouter()
|
|
|
|
var log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
|
|
|
|
//go:embed index.html
|
|
|
|
var html string
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
err := envconfig.Process("", &s)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("couldn't process envconfig.")
|
|
|
|
}
|
|
|
|
|
2021-08-24 23:56:58 +00:00
|
|
|
s.Domain = strings.ToLower(s.Domain)
|
|
|
|
|
2021-08-13 16:21:00 +00:00
|
|
|
db, err = pebble.Open(s.Domain, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Str("path", s.Domain).Msg("failed to open db.")
|
|
|
|
}
|
|
|
|
|
|
|
|
router.Path("/.well-known/lnurlp/{username}").Methods("GET").
|
|
|
|
HandlerFunc(handleLNURL)
|
|
|
|
|
|
|
|
router.Path("/").HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("content-type", "text/html")
|
2021-08-14 23:52:16 +00:00
|
|
|
serverData, _ := json.Marshal(struct {
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
SiteOwnerName string `json:"siteOwnerName"`
|
|
|
|
SiteOwnerURL string `json:"siteOwnerURL"`
|
|
|
|
SiteName string `json:"siteName"`
|
|
|
|
}{
|
|
|
|
Domain: s.Domain,
|
|
|
|
SiteOwnerName: s.SiteOwnerName,
|
|
|
|
SiteOwnerURL: s.SiteOwnerURL,
|
|
|
|
SiteName: s.SiteName,
|
|
|
|
})
|
2021-08-15 22:41:16 +00:00
|
|
|
fmt.Fprint(w,
|
2021-08-14 23:52:16 +00:00
|
|
|
strings.ReplaceAll(
|
|
|
|
strings.ReplaceAll(
|
|
|
|
html, "{} // REPLACED WITH SERVER DATA", string(serverData),
|
|
|
|
),
|
|
|
|
"Satdress", s.SiteName,
|
|
|
|
),
|
|
|
|
)
|
2021-08-13 16:21:00 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
router.Path("/grab").HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
2021-08-20 12:32:45 +00:00
|
|
|
pin, err := SaveName(r.FormValue("name"), &Params{
|
2021-08-13 16:21:00 +00:00
|
|
|
Kind: r.FormValue("kind"),
|
|
|
|
Host: r.FormValue("host"),
|
|
|
|
Key: r.FormValue("key"),
|
2021-08-14 21:55:05 +00:00
|
|
|
Pak: r.FormValue("pak"),
|
|
|
|
Waki: r.FormValue("waki"),
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w,
|
|
|
|
"name saved! this is your secret pin key for this name: %s",
|
|
|
|
pin)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: router,
|
|
|
|
Addr: s.Host + ":" + s.Port,
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
log.Debug().Str("addr", srv.Addr).Msg("listening")
|
|
|
|
srv.ListenAndServe()
|
|
|
|
}
|