2021-08-14 21:55:05 +00:00
|
|
|
package main
|
|
|
|
|
2021-08-20 12:32:45 +00:00
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-01-18 23:50:47 +00:00
|
|
|
"os"
|
2021-08-21 16:52:21 +00:00
|
|
|
"strings"
|
2021-08-20 12:32:45 +00:00
|
|
|
|
|
|
|
"github.com/cockroachdb/pebble"
|
|
|
|
)
|
|
|
|
|
2021-08-14 21:55:05 +00:00
|
|
|
type Params struct {
|
2022-03-16 00:16:02 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Domain string `json:"domain,omitempty"`
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
|
|
|
|
Host string `json:"host"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Pak string `json:"pak"`
|
|
|
|
Waki string `json:"waki"`
|
|
|
|
NodeId string `json:"nodeid"`
|
|
|
|
Rune string `json:"rune"`
|
|
|
|
|
2021-10-27 09:48:21 +00:00
|
|
|
Pin string `json:"pin"`
|
|
|
|
MinSendable string `json:"minSendable"`
|
|
|
|
MaxSendable string `json:"maxSendable"`
|
2021-08-14 21:55:05 +00:00
|
|
|
}
|
2021-08-20 12:32:45 +00:00
|
|
|
|
2021-08-25 10:00:16 +00:00
|
|
|
func SaveName(
|
|
|
|
name string,
|
2022-01-18 23:50:47 +00:00
|
|
|
domain string,
|
2021-08-25 10:00:16 +00:00
|
|
|
params *Params,
|
|
|
|
providedPin string,
|
|
|
|
) (pin string, inv string, err error) {
|
2021-08-21 16:52:21 +00:00
|
|
|
name = strings.ToLower(name)
|
2022-01-18 23:50:47 +00:00
|
|
|
domain = strings.ToLower(domain)
|
2021-08-20 12:32:45 +00:00
|
|
|
|
2022-01-18 23:50:47 +00:00
|
|
|
key := []byte(getID(name, domain))
|
|
|
|
|
|
|
|
pin = ComputePIN(name, domain)
|
2021-08-20 12:32:45 +00:00
|
|
|
|
|
|
|
if _, closer, err := db.Get(key); err == nil {
|
|
|
|
defer closer.Close()
|
|
|
|
if pin != providedPin {
|
2021-10-27 09:48:21 +00:00
|
|
|
return "", "", errors.New("name already exists! must provide pin")
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 09:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", errors.New("that name does not exist")
|
|
|
|
}
|
2021-08-20 12:32:45 +00:00
|
|
|
|
2021-08-28 12:50:17 +00:00
|
|
|
params.Name = name
|
2022-01-18 23:50:47 +00:00
|
|
|
params.Domain = domain
|
2021-08-28 12:50:17 +00:00
|
|
|
|
2021-08-20 12:32:45 +00:00
|
|
|
// check if the given data works
|
2021-08-28 12:50:17 +00:00
|
|
|
if inv, err = makeInvoice(params, 1000, &pin); err != nil {
|
2021-08-25 10:00:16 +00:00
|
|
|
return "", "", fmt.Errorf("couldn't make an invoice with the given data: %w", err)
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save it
|
|
|
|
data, _ := json.Marshal(params)
|
|
|
|
if err := db.Set(key, data, pebble.Sync); err != nil {
|
2021-08-25 10:00:16 +00:00
|
|
|
return "", "", err
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 10:00:16 +00:00
|
|
|
return pin, inv, nil
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 23:50:47 +00:00
|
|
|
func GetName(name, domain string) (*Params, error) {
|
|
|
|
val, closer, err := db.Get([]byte(getID(name, domain)))
|
2021-08-20 12:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
2021-08-20 13:12:06 +00:00
|
|
|
|
|
|
|
var params Params
|
|
|
|
if err := json.Unmarshal(val, ¶ms); err != nil {
|
2021-08-20 12:32:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params.Name = name
|
2022-01-18 23:50:47 +00:00
|
|
|
params.Domain = domain
|
2021-08-20 13:12:06 +00:00
|
|
|
return ¶ms, nil
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
2021-10-27 09:48:21 +00:00
|
|
|
|
2022-01-18 23:50:47 +00:00
|
|
|
func DeleteName(name, domain string) error {
|
|
|
|
key := []byte(getID(name, domain))
|
2021-10-27 09:48:21 +00:00
|
|
|
|
|
|
|
if err := db.Delete(key, pebble.Sync); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-18 23:50:47 +00:00
|
|
|
func ComputePIN(name, domain string) string {
|
2021-10-27 09:48:21 +00:00
|
|
|
mac := hmac.New(sha256.New, []byte(s.Secret))
|
2022-01-18 23:50:47 +00:00
|
|
|
mac.Write([]byte(getID(name, domain)))
|
2021-10-27 09:48:21 +00:00
|
|
|
return hex.EncodeToString(mac.Sum(nil))
|
|
|
|
}
|
2022-01-18 23:50:47 +00:00
|
|
|
|
|
|
|
func getID(name, domain string) string {
|
|
|
|
if s.GlobalUsers {
|
|
|
|
return strings.ToLower(name)
|
|
|
|
} else {
|
|
|
|
return strings.ToLower(fmt.Sprintf("%s@%s", name, domain))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tryMigrate(old, new string) {
|
|
|
|
if _, err := os.Stat(old); os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Str("db", old).Msg("Migrating db")
|
|
|
|
|
|
|
|
newDb, err := pebble.Open(new, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Str("path", new).Msg("failed to open db.")
|
|
|
|
}
|
|
|
|
defer newDb.Close()
|
|
|
|
|
|
|
|
oldDb, err := pebble.Open(old, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Str("path", old).Msg("failed to open db.")
|
|
|
|
}
|
|
|
|
defer oldDb.Close()
|
|
|
|
|
|
|
|
iter := oldDb.NewIter(nil)
|
|
|
|
defer iter.Close()
|
|
|
|
|
|
|
|
for iter.First(); iter.Valid(); iter.Next() {
|
2022-01-20 12:25:30 +00:00
|
|
|
log.Debug().Str("key", string(iter.Key())).Msg("Migrating key")
|
2022-01-18 23:50:47 +00:00
|
|
|
var params Params
|
|
|
|
if err := json.Unmarshal(iter.Value(), ¶ms); err != nil {
|
|
|
|
log.Debug().Err(err).Msg("Unmarshal error")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
params.Domain = old // old database name was domain
|
|
|
|
|
|
|
|
// save it
|
|
|
|
data, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("Marshal error")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := newDb.Set([]byte(getID(params.Name, params.Domain)), data, pebble.Sync); err != nil {
|
|
|
|
log.Debug().Err(err).Msg("Set error")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|