From f18bd116fd82ed4481d839e773f0184a462f5212 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Fri, 12 Jan 2024 13:21:51 -0800 Subject: [PATCH] supporting device restrictions --- net/server/internal/api_getAccountStatus.go | 2 +- net/server/internal/api_getNodeConfig.go | 4 ++-- net/server/internal/api_setNodeConfig.go | 2 +- net/server/internal/appValues.go | 2 +- net/server/internal/keyUtil.go | 2 +- net/server/internal/routers.go | 4 ++-- net/server/internal/store/alloc.go | 19 +++++++++++++++- net/server/main.go | 25 ++++++--------------- net/web/src/session/cards/useCards.hook.js | 2 -- 9 files changed, 33 insertions(+), 29 deletions(-) diff --git a/net/server/internal/api_getAccountStatus.go b/net/server/internal/api_getAccountStatus.go index 98a610b2..0d30c1c4 100644 --- a/net/server/internal/api_getAccountStatus.go +++ b/net/server/internal/api_getAccountStatus.go @@ -37,7 +37,7 @@ func GetAccountStatus(w http.ResponseWriter, r *http.Request) { status.Searchable = account.Searchable status.Sealable = true status.EnableIce = getBoolConfigValue(CNFEnableIce, false) - status.AllowUnsealed = getBoolConfigValue(CNFAllowUnsealed, true) + status.AllowUnsealed = getBoolConfigValue(CNFAllowUnsealed, false) status.PushEnabled = session.PushEnabled status.Seal = seal WriteResponse(w, status) diff --git a/net/server/internal/api_getNodeConfig.go b/net/server/internal/api_getNodeConfig.go index fbddc58e..ffe3d0c2 100644 --- a/net/server/internal/api_getNodeConfig.go +++ b/net/server/internal/api_getNodeConfig.go @@ -17,11 +17,11 @@ func GetNodeConfig(w http.ResponseWriter, r *http.Request) { var config NodeConfig config.Domain = getStrConfigValue(CNFDomain, "") config.AccountStorage = getNumConfigValue(CNFStorage, 0) - config.AllowUnsealed = getBoolConfigValue(CNFAllowUnsealed, true) + config.AllowUnsealed = getBoolConfigValue(CNFAllowUnsealed, false) config.EnableImage = getBoolConfigValue(CNFEnableImage, true) config.EnableAudio = getBoolConfigValue(CNFEnableAudio, true) config.EnableVideo = getBoolConfigValue(CNFEnableVideo, true) - config.KeyType = getStrConfigValue(CNFKeyType, APPRSA4096) + config.KeyType = getStrConfigValue(CNFKeyType, APPRSA2048) config.PushSupported = getBoolConfigValue(CNFPushSupported, true) config.EnableIce = getBoolConfigValue(CNFEnableIce, false) config.IceUrl = getStrConfigValue(CNFIceUrl, "") diff --git a/net/server/internal/api_setNodeConfig.go b/net/server/internal/api_setNodeConfig.go index e0a14b84..87cd0460 100644 --- a/net/server/internal/api_setNodeConfig.go +++ b/net/server/internal/api_setNodeConfig.go @@ -73,7 +73,7 @@ func SetNodeConfig(w http.ResponseWriter, r *http.Request) { if res := tx.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "config_id"}}, DoUpdates: clause.AssignmentColumns([]string{"bool_value"}), - }).Create(&store.Config{ConfigID: CNFAllowUnsealed, BoolValue: config.AllowUnsealed}).Error; res != nil { + }).Create(&store.Config{ConfigID: CNFAllowUnsealed, BoolValue: false}).Error; res != nil { return res } diff --git a/net/server/internal/appValues.go b/net/server/internal/appValues.go index f96fdd8b..5feed709 100644 --- a/net/server/internal/appValues.go +++ b/net/server/internal/appValues.go @@ -145,7 +145,7 @@ const APPQueuePhoto = "photo" const APPQueueDefault = "" //APPDefaultPath config for default path to store assets -const APPDefaultPath = "./asset" +const APPDefaultPath = "/tmp/databag/assets" //AppCardStatus compares cards status with string func AppCardStatus(status string) bool { diff --git a/net/server/internal/keyUtil.go b/net/server/internal/keyUtil.go index b7287b50..1a45de77 100644 --- a/net/server/internal/keyUtil.go +++ b/net/server/internal/keyUtil.go @@ -10,7 +10,7 @@ import ( //GenerateRsaKeyPair creates a public/private key for a new account func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, string, error) { - keyType := getStrConfigValue(CNFKeyType, "RSA4096"); + keyType := getStrConfigValue(CNFKeyType, "RSA2048"); if keyType == "RSA2048" { privkey, _ := rsa.GenerateKey(rand.Reader, 2048) return privkey, &privkey.PublicKey, "RSA2048", nil diff --git a/net/server/internal/routers.go b/net/server/internal/routers.go index 01d93f80..290217db 100644 --- a/net/server/internal/routers.go +++ b/net/server/internal/routers.go @@ -16,7 +16,7 @@ type route struct { type routes []route //NewRouter allocate router for databag API -func NewRouter() *mux.Router { +func NewRouter(path string) *mux.Router { go SendNotifications() @@ -33,7 +33,7 @@ func NewRouter() *mux.Router { Handler(handler) } - fs := http.FileServer(http.Dir("/app/databag/net/web/build/")) + fs := http.FileServer(http.Dir(path)); router.PathPrefix("/").Handler(http.StripPrefix("/", fs)) return router diff --git a/net/server/internal/store/alloc.go b/net/server/internal/store/alloc.go index 8e4691e4..a1021959 100644 --- a/net/server/internal/store/alloc.go +++ b/net/server/internal/store/alloc.go @@ -4,13 +4,14 @@ import ( "fmt" "gorm.io/gorm" "gorm.io/gorm/logger" + "gorm.io/gorm/clause" "github.com/glebarez/sqlite" ) var DB *gorm.DB; func SetPath(path string) { - db, err := gorm.Open(sqlite.Open(path), &gorm.Config{ + db, err := gorm.Open(sqlite.Open(path + "/databag.db"), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) if err != nil { @@ -18,6 +19,22 @@ func SetPath(path string) { panic("failed to connect database") } AutoMigrate(db) + + // upsert key type + err = db.Transaction(func(tx *gorm.DB) error { + if res := tx.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "config_id"}}, + DoUpdates: clause.AssignmentColumns([]string{"str_value"}), + }).Create(&Config{ConfigID: "asset_path", StrValue: path + "/assets"}).Error; res != nil { + return res + } + return nil + }) + if err != nil { + fmt.Println(err); + panic("failed to set database path") + } + DB = db } diff --git a/net/server/main.go b/net/server/main.go index e914f69b..a1b57c0f 100644 --- a/net/server/main.go +++ b/net/server/main.go @@ -10,26 +10,15 @@ import ( ) func main() { - - store.SetPath("/var/lib/databag/databag.db") - - router := app.NewRouter() - - origins := handlers.AllowedOrigins([]string{"*"}) - methods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"}) - args := os.Args if len(args) == 3 { - port := ":" + args[2] - path := "/etc/letsencrypt/live/" + args[1] - log.Printf("starting server at: " + path + " " + port); - log.Fatal(http.ListenAndServeTLS(port, path + "/fullchain.pem", path + "/privkey.pem", handlers.CORS(origins, methods)(router))) - } else if len(args) == 2 { - path := "/etc/letsencrypt/live/" + args[1] - log.Printf("starting server at: " + path); - log.Fatal(http.ListenAndServeTLS(":443", path + "/fullchain.pem", path + "/privkey.pem", handlers.CORS(origins, methods)(router))) + port := ":" + args[1] + store.SetPath(args[2]) + router := app.NewRouter("/opt/databag/web/build") + origins := handlers.AllowedOrigins([]string{"*"}) + methods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"}) + log.Fatal(http.ListenAndServe(port, handlers.CORS(origins, methods)(router))) } else { - log.Printf("starting server"); - log.Fatal(http.ListenAndServe(":7000", handlers.CORS(origins, methods)(router))) + log.Printf("usage: databag "); } } diff --git a/net/web/src/session/cards/useCards.hook.js b/net/web/src/session/cards/useCards.hook.js index 9088c477..fa2c33fe 100644 --- a/net/web/src/session/cards/useCards.hook.js +++ b/net/web/src/session/cards/useCards.hook.js @@ -141,8 +141,6 @@ export function useCards() { channel.state.channels.forEach((entry, id) => { const cards = entry?.data?.channelDetail?.contacts?.cards || []; const subject = entry?.data?.channelDetail?.data || ''; - const type = entry?.data?.channelDetail?.dataType || ''; - if (cards.length === 1 && cards[0] === cardId && subject === '{"subject":null}') { channelId = entry.id; }