2022-06-13 20:11:56 +02:00
|
|
|
package sstore
|
|
|
|
|
|
|
|
import (
|
2022-07-01 21:17:19 +02:00
|
|
|
"context"
|
2022-08-16 03:42:25 +02:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
2022-07-06 01:54:49 +02:00
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
2022-07-01 23:07:13 +02:00
|
|
|
"log"
|
2022-08-17 00:08:28 +02:00
|
|
|
"os"
|
|
|
|
"os/user"
|
2022-07-01 02:02:19 +02:00
|
|
|
"path"
|
2022-09-14 21:56:05 +02:00
|
|
|
"regexp"
|
2022-08-24 11:14:16 +02:00
|
|
|
"strings"
|
2022-06-21 06:57:23 +02:00
|
|
|
"sync"
|
2022-06-13 20:11:56 +02:00
|
|
|
"time"
|
|
|
|
|
2022-08-16 03:42:25 +02:00
|
|
|
"github.com/google/uuid"
|
2022-07-01 19:48:14 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
2022-07-01 21:17:19 +02:00
|
|
|
"github.com/scripthaus-dev/mshell/pkg/base"
|
2022-07-01 19:48:14 +02:00
|
|
|
"github.com/scripthaus-dev/mshell/pkg/packet"
|
2022-07-01 02:02:19 +02:00
|
|
|
"github.com/scripthaus-dev/sh2-server/pkg/scbase"
|
2022-07-01 19:48:14 +02:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2022-06-13 20:11:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const LineTypeCmd = "cmd"
|
|
|
|
const LineTypeText = "text"
|
2022-07-01 19:48:14 +02:00
|
|
|
const DBFileName = "sh2.db"
|
2022-07-01 02:02:19 +02:00
|
|
|
|
2022-07-01 21:17:19 +02:00
|
|
|
const DefaultSessionName = "default"
|
|
|
|
const DefaultWindowName = "default"
|
2022-08-24 11:14:16 +02:00
|
|
|
const LocalRemoteAlias = "local"
|
2022-07-13 06:51:17 +02:00
|
|
|
const DefaultScreenWindowName = "w1"
|
2022-07-01 21:17:19 +02:00
|
|
|
|
2022-07-01 23:07:13 +02:00
|
|
|
const DefaultCwd = "~"
|
|
|
|
|
2022-08-16 03:42:25 +02:00
|
|
|
const (
|
|
|
|
CmdStatusRunning = "running"
|
|
|
|
CmdStatusDetached = "detached"
|
|
|
|
CmdStatusError = "error"
|
|
|
|
CmdStatusDone = "done"
|
|
|
|
CmdStatusHangup = "hangup"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-08-16 21:08:26 +02:00
|
|
|
ShareModeLocal = "local"
|
|
|
|
ShareModePrivate = "private"
|
|
|
|
ShareModeView = "view"
|
|
|
|
ShareModeShared = "shared"
|
2022-08-16 03:42:25 +02:00
|
|
|
)
|
2022-07-08 01:29:14 +02:00
|
|
|
|
2022-08-21 21:31:29 +02:00
|
|
|
const (
|
|
|
|
ConnectModeStartup = "startup"
|
|
|
|
ConnectModeAuto = "auto"
|
|
|
|
ConnectModeManual = "manual"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
RemoteTypeSsh = "ssh"
|
|
|
|
)
|
|
|
|
|
2022-07-01 21:17:19 +02:00
|
|
|
var globalDBLock = &sync.Mutex{}
|
|
|
|
var globalDB *sqlx.DB
|
|
|
|
var globalDBErr error
|
|
|
|
|
2022-07-01 19:48:14 +02:00
|
|
|
func GetSessionDBName() string {
|
2022-07-01 02:02:19 +02:00
|
|
|
scHome := scbase.GetScHomeDir()
|
|
|
|
return path.Join(scHome, DBFileName)
|
|
|
|
}
|
2022-06-13 20:11:56 +02:00
|
|
|
|
2022-09-01 21:47:10 +02:00
|
|
|
func IsValidConnectMode(mode string) bool {
|
2022-09-14 02:11:36 +02:00
|
|
|
return mode == ConnectModeStartup || mode == ConnectModeAuto || mode == ConnectModeManual
|
2022-09-01 21:47:10 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 01:10:46 +02:00
|
|
|
func GetDB(ctx context.Context) (*sqlx.DB, error) {
|
|
|
|
if IsTxWrapContext(ctx) {
|
|
|
|
return nil, fmt.Errorf("cannot call GetDB from within a running transaction")
|
|
|
|
}
|
2022-07-01 21:17:19 +02:00
|
|
|
globalDBLock.Lock()
|
|
|
|
defer globalDBLock.Unlock()
|
|
|
|
if globalDB == nil && globalDBErr == nil {
|
2022-08-16 21:08:26 +02:00
|
|
|
dbName := GetSessionDBName()
|
|
|
|
globalDB, globalDBErr = sqlx.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc&_journal_mode=WAL&_busy_timeout=5000", dbName))
|
|
|
|
if globalDBErr != nil {
|
|
|
|
globalDBErr = fmt.Errorf("opening db[%s]: %w", dbName, globalDBErr)
|
|
|
|
}
|
2022-07-01 19:48:14 +02:00
|
|
|
}
|
2022-07-01 21:17:19 +02:00
|
|
|
return globalDB, globalDBErr
|
2022-07-01 19:48:14 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 01:21:19 +02:00
|
|
|
type ClientData struct {
|
2022-08-16 03:42:25 +02:00
|
|
|
UserId string `json:"userid"`
|
|
|
|
UserPrivateKeyBytes []byte `json:"-"`
|
|
|
|
UserPublicKeyBytes []byte `json:"-"`
|
|
|
|
UserPrivateKey *ecdsa.PrivateKey
|
|
|
|
UserPublicKey *ecdsa.PublicKey
|
2022-08-27 01:21:19 +02:00
|
|
|
ActiveSessionId string `json:"activesessionid"`
|
2022-08-16 03:42:25 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 06:57:23 +02:00
|
|
|
type SessionType struct {
|
2022-07-13 06:51:17 +02:00
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SessionIdx int64 `json:"sessionidx"`
|
|
|
|
ActiveScreenId string `json:"activescreenid"`
|
2022-08-24 22:21:54 +02:00
|
|
|
OwnerId string `json:"ownerid"`
|
2022-08-16 21:08:26 +02:00
|
|
|
ShareMode string `json:"sharemode"`
|
|
|
|
AccessKey string `json:"-"`
|
2022-07-13 06:51:17 +02:00
|
|
|
NotifyNum int64 `json:"notifynum"`
|
|
|
|
Screens []*ScreenType `json:"screens"`
|
|
|
|
Remotes []*RemoteInstance `json:"remotes"`
|
2022-07-15 03:39:40 +02:00
|
|
|
|
|
|
|
// only for updates
|
|
|
|
Remove bool `json:"remove,omitempty"`
|
|
|
|
Full bool `json:"full,omitempty"`
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 23:15:20 +02:00
|
|
|
type SessionStatsType struct {
|
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
NumScreens int `json:"numscreens"`
|
|
|
|
NumWindows int `json:"numwindows"`
|
|
|
|
NumLines int `json:"numlines"`
|
|
|
|
NumCmds int `json:"numcmds"`
|
|
|
|
DiskStats SessionDiskSizeType `json:"diskstats"`
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:27:16 +02:00
|
|
|
type WindowOptsType struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *WindowOptsType) Scan(val interface{}) error {
|
|
|
|
return quickScanJson(opts, val)
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:10:46 +02:00
|
|
|
func (opts WindowOptsType) Value() (driver.Value, error) {
|
2022-07-12 23:27:16 +02:00
|
|
|
return quickValueJson(opts)
|
2022-07-05 07:18:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 21:08:26 +02:00
|
|
|
type WindowShareOptsType struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *WindowShareOptsType) Scan(val interface{}) error {
|
|
|
|
return quickScanJson(opts, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts WindowShareOptsType) Value() (driver.Value, error) {
|
|
|
|
return quickValueJson(opts)
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:56:05 +02:00
|
|
|
var RemoteNameRe = regexp.MustCompile("^\\*?[a-zA-Z0-9_-]+$")
|
|
|
|
|
2022-08-24 11:14:16 +02:00
|
|
|
type RemotePtrType struct {
|
2022-08-24 22:21:54 +02:00
|
|
|
OwnerId string `json:"ownerid"`
|
|
|
|
RemoteId string `json:"remoteid"`
|
|
|
|
Name string `json:"name"`
|
2022-08-24 11:14:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r RemotePtrType) IsSessionScope() bool {
|
|
|
|
return strings.HasPrefix(r.Name, "*")
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:56:05 +02:00
|
|
|
func (r RemotePtrType) Validate() error {
|
|
|
|
if r.OwnerId != "" {
|
|
|
|
if _, err := uuid.Parse(r.OwnerId); err != nil {
|
|
|
|
return fmt.Errorf("invalid ownerid format: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.RemoteId != "" {
|
|
|
|
if _, err := uuid.Parse(r.RemoteId); err != nil {
|
|
|
|
return fmt.Errorf("invalid remoteid format: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.Name != "" {
|
|
|
|
ok := RemoteNameRe.MatchString(r.Name)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid remote name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:14:16 +02:00
|
|
|
func (r RemotePtrType) MakeFullRemoteRef() string {
|
|
|
|
if r.RemoteId == "" {
|
|
|
|
return ""
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
if r.OwnerId == "" && r.Name == "" {
|
2022-08-24 11:14:16 +02:00
|
|
|
return r.RemoteId
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
if r.OwnerId != "" && r.Name == "" {
|
|
|
|
return fmt.Sprintf("@%s:%s", r.OwnerId, r.RemoteId)
|
2022-08-24 11:14:16 +02:00
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
if r.OwnerId == "" && r.Name != "" {
|
2022-08-24 11:14:16 +02:00
|
|
|
return fmt.Sprintf("%s:%s", r.RemoteId, r.Name)
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
return fmt.Sprintf("@%s:%s:%s", r.OwnerId, r.RemoteId, r.Name)
|
2022-08-24 11:14:16 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 07:18:01 +02:00
|
|
|
type WindowType struct {
|
2022-08-24 22:21:54 +02:00
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
WindowId string `json:"windowid"`
|
|
|
|
CurRemote RemotePtrType `json:"curremote"`
|
|
|
|
WinOpts WindowOptsType `json:"winopts"`
|
|
|
|
OwnerId string `json:"ownerid"`
|
|
|
|
ShareMode string `json:"sharemode"`
|
|
|
|
ShareOpts WindowShareOptsType `json:"shareopts"`
|
|
|
|
Lines []*LineType `json:"lines"`
|
|
|
|
Cmds []*CmdType `json:"cmds"`
|
2022-07-15 03:39:40 +02:00
|
|
|
|
|
|
|
// only for updates
|
|
|
|
Remove bool `json:"remove,omitempty"`
|
2022-07-13 06:51:17 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 11:14:16 +02:00
|
|
|
func (w *WindowType) ToMap() map[string]interface{} {
|
|
|
|
rtn := make(map[string]interface{})
|
|
|
|
rtn["sessionid"] = w.SessionId
|
|
|
|
rtn["windowid"] = w.WindowId
|
2022-08-24 22:21:54 +02:00
|
|
|
rtn["curremoteownerid"] = w.CurRemote.OwnerId
|
2022-08-24 11:14:16 +02:00
|
|
|
rtn["curremoteid"] = w.CurRemote.RemoteId
|
|
|
|
rtn["curremotename"] = w.CurRemote.Name
|
|
|
|
rtn["winopts"] = quickJson(w.WinOpts)
|
2022-08-24 22:21:54 +02:00
|
|
|
rtn["ownerid"] = w.OwnerId
|
2022-08-24 11:14:16 +02:00
|
|
|
rtn["sharemode"] = w.ShareMode
|
|
|
|
rtn["shareopts"] = quickJson(w.ShareOpts)
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func WindowFromMap(m map[string]interface{}) *WindowType {
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var w WindowType
|
|
|
|
quickSetStr(&w.SessionId, m, "sessionid")
|
|
|
|
quickSetStr(&w.WindowId, m, "windowid")
|
2022-08-24 22:21:54 +02:00
|
|
|
quickSetStr(&w.CurRemote.OwnerId, m, "curremoteownerid")
|
2022-08-24 11:14:16 +02:00
|
|
|
quickSetStr(&w.CurRemote.RemoteId, m, "curremoteid")
|
|
|
|
quickSetStr(&w.CurRemote.Name, m, "curremotename")
|
|
|
|
quickSetJson(&w.WinOpts, m, "winopts")
|
2022-08-24 22:21:54 +02:00
|
|
|
quickSetStr(&w.OwnerId, m, "ownerid")
|
2022-08-24 11:14:16 +02:00
|
|
|
quickSetStr(&w.ShareMode, m, "sharemode")
|
|
|
|
quickSetJson(&w.ShareOpts, m, "shareopts")
|
|
|
|
return &w
|
|
|
|
}
|
|
|
|
|
2022-08-28 23:24:05 +02:00
|
|
|
func (h *HistoryItemType) ToMap() map[string]interface{} {
|
|
|
|
rtn := make(map[string]interface{})
|
|
|
|
rtn["historyid"] = h.HistoryId
|
|
|
|
rtn["ts"] = h.Ts
|
|
|
|
rtn["userid"] = h.UserId
|
|
|
|
rtn["sessionid"] = h.SessionId
|
|
|
|
rtn["screenid"] = h.ScreenId
|
|
|
|
rtn["windowid"] = h.WindowId
|
|
|
|
rtn["lineid"] = h.LineId
|
|
|
|
rtn["haderror"] = h.HadError
|
|
|
|
rtn["cmdid"] = h.CmdId
|
|
|
|
rtn["cmdstr"] = h.CmdStr
|
|
|
|
rtn["remoteownerid"] = h.Remote.OwnerId
|
|
|
|
rtn["remoteid"] = h.Remote.RemoteId
|
|
|
|
rtn["remotename"] = h.Remote.Name
|
|
|
|
rtn["ismetacmd"] = h.IsMetaCmd
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func HistoryItemFromMap(m map[string]interface{}) *HistoryItemType {
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var h HistoryItemType
|
|
|
|
quickSetStr(&h.HistoryId, m, "historyid")
|
|
|
|
quickSetInt64(&h.Ts, m, "ts")
|
|
|
|
quickSetStr(&h.UserId, m, "userid")
|
|
|
|
quickSetStr(&h.SessionId, m, "sessionid")
|
|
|
|
quickSetStr(&h.ScreenId, m, "screenid")
|
|
|
|
quickSetStr(&h.WindowId, m, "windowid")
|
|
|
|
quickSetStr(&h.LineId, m, "lineid")
|
|
|
|
quickSetBool(&h.HadError, m, "haderror")
|
|
|
|
quickSetStr(&h.CmdId, m, "cmdid")
|
|
|
|
quickSetStr(&h.CmdStr, m, "cmdstr")
|
|
|
|
quickSetStr(&h.Remote.OwnerId, m, "remoteownerid")
|
|
|
|
quickSetStr(&h.Remote.RemoteId, m, "remoteid")
|
|
|
|
quickSetStr(&h.Remote.Name, m, "remotename")
|
|
|
|
quickSetBool(&h.IsMetaCmd, m, "ismetacmd")
|
2022-08-30 04:18:02 +02:00
|
|
|
quickSetStr(&h.HistoryNum, m, "historynum")
|
2022-08-28 23:24:05 +02:00
|
|
|
return &h
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:51:17 +02:00
|
|
|
type ScreenOptsType struct {
|
2022-08-27 06:44:18 +02:00
|
|
|
TabColor string `json:"tabcolor,omitempty"`
|
2022-07-13 06:51:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *ScreenOptsType) Scan(val interface{}) error {
|
|
|
|
return quickScanJson(opts, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts ScreenOptsType) Value() (driver.Value, error) {
|
|
|
|
return quickValueJson(opts)
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ScreenType struct {
|
2022-07-13 06:51:17 +02:00
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
ScreenId string `json:"screenid"`
|
|
|
|
ScreenIdx int64 `json:"screenidx"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ActiveWindowId string `json:"activewindowid"`
|
2022-08-27 02:51:28 +02:00
|
|
|
ScreenOpts *ScreenOptsType `json:"screenopts"`
|
2022-08-24 22:21:54 +02:00
|
|
|
OwnerId string `json:"ownerid"`
|
2022-08-16 21:08:26 +02:00
|
|
|
ShareMode string `json:"sharemode"`
|
2022-07-13 06:51:17 +02:00
|
|
|
Windows []*ScreenWindowType `json:"windows"`
|
2022-07-15 03:39:40 +02:00
|
|
|
|
|
|
|
// only for updates
|
|
|
|
Remove bool `json:"remove,omitempty"`
|
|
|
|
Full bool `json:"full,omitempty"`
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:51:17 +02:00
|
|
|
const (
|
|
|
|
LayoutFull = "full"
|
|
|
|
)
|
|
|
|
|
2022-07-12 23:27:16 +02:00
|
|
|
type LayoutType struct {
|
2022-07-13 06:51:17 +02:00
|
|
|
Type string `json:"type"`
|
|
|
|
Parent string `json:"parent,omitempty"`
|
|
|
|
ZIndex int64 `json:"zindex,omitempty"`
|
|
|
|
Float bool `json:"float,omitempty"`
|
|
|
|
Top string `json:"top,omitempty"`
|
|
|
|
Bottom string `json:"bottom,omitempty"`
|
|
|
|
Left string `json:"left,omitempty"`
|
|
|
|
Right string `json:"right,omitempty"`
|
|
|
|
Width string `json:"width,omitempty"`
|
|
|
|
Height string `json:"height,omitempty"`
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LayoutType) Scan(val interface{}) error {
|
|
|
|
return quickScanJson(l, val)
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:10:46 +02:00
|
|
|
func (l LayoutType) Value() (driver.Value, error) {
|
2022-07-12 23:27:16 +02:00
|
|
|
return quickValueJson(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ScreenWindowType struct {
|
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
ScreenId string `json:"screenid"`
|
|
|
|
WindowId string `json:"windowid"`
|
2022-07-13 06:51:17 +02:00
|
|
|
Name string `json:"name"`
|
2022-07-12 23:27:16 +02:00
|
|
|
Layout LayoutType `json:"layout"`
|
2022-07-15 03:39:40 +02:00
|
|
|
|
|
|
|
// only for updates
|
|
|
|
Remove bool `json:"remove,omitempty"`
|
2022-07-12 22:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type HistoryItemType struct {
|
2022-08-28 23:24:05 +02:00
|
|
|
HistoryId string `json:"historyid"`
|
|
|
|
Ts int64 `json:"ts"`
|
|
|
|
UserId string `json:"userid"`
|
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
ScreenId string `json:"screenid"`
|
|
|
|
WindowId string `json:"windowid"`
|
|
|
|
LineId string `json:"lineid"`
|
|
|
|
HadError bool `json:"haderror"`
|
|
|
|
CmdId string `json:"cmdid"`
|
|
|
|
CmdStr string `json:"cmdstr"`
|
|
|
|
Remote RemotePtrType `json:"remote"`
|
|
|
|
IsMetaCmd bool `json:"ismetacmd"`
|
2022-08-11 21:07:41 +02:00
|
|
|
|
|
|
|
// only for updates
|
|
|
|
Remove bool `json:"remove"`
|
2022-08-28 23:24:05 +02:00
|
|
|
|
|
|
|
// transient (string because of different history orderings)
|
|
|
|
HistoryNum string `json:"historynum"`
|
2022-07-01 19:48:14 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 04:18:02 +02:00
|
|
|
type HistoryQueryOpts struct {
|
|
|
|
MaxItems int
|
|
|
|
FromTs int64
|
|
|
|
}
|
|
|
|
|
2022-07-06 01:54:49 +02:00
|
|
|
type RemoteState struct {
|
2022-08-23 01:26:44 +02:00
|
|
|
Cwd string `json:"cwd"`
|
|
|
|
Env0 []byte `json:"env0"` // "env -0" format
|
2022-07-06 01:54:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RemoteState) Scan(val interface{}) error {
|
2022-07-12 23:27:16 +02:00
|
|
|
return quickScanJson(s, val)
|
2022-07-06 01:54:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 01:10:46 +02:00
|
|
|
func (s RemoteState) Value() (driver.Value, error) {
|
2022-07-12 23:27:16 +02:00
|
|
|
return quickValueJson(s)
|
2022-07-06 01:54:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:10:37 +02:00
|
|
|
type TermOpts struct {
|
2022-08-19 22:23:00 +02:00
|
|
|
Rows int64 `json:"rows"`
|
|
|
|
Cols int64 `json:"cols"`
|
|
|
|
FlexRows bool `json:"flexrows,omitempty"`
|
|
|
|
MaxPtySize int64 `json:"maxptysize,omitempty"`
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *TermOpts) Scan(val interface{}) error {
|
2022-07-12 23:27:16 +02:00
|
|
|
return quickScanJson(opts, val)
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 01:10:46 +02:00
|
|
|
func (opts TermOpts) Value() (driver.Value, error) {
|
2022-07-12 23:27:16 +02:00
|
|
|
return quickValueJson(opts)
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 01:54:49 +02:00
|
|
|
type RemoteInstance struct {
|
2022-08-24 22:21:54 +02:00
|
|
|
RIId string `json:"riid"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
WindowId string `json:"windowid"`
|
|
|
|
RemoteOwnerId string `json:"remoteownerid"`
|
|
|
|
RemoteId string `json:"remoteid"`
|
|
|
|
State RemoteState `json:"state"`
|
2022-08-11 03:33:32 +02:00
|
|
|
|
|
|
|
// only for updates
|
|
|
|
Remove bool `json:"remove,omitempty"`
|
2022-06-21 06:57:23 +02:00
|
|
|
}
|
|
|
|
|
2022-06-13 20:11:56 +02:00
|
|
|
type LineType struct {
|
2022-06-17 00:51:41 +02:00
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
WindowId string `json:"windowid"`
|
2022-08-16 21:08:26 +02:00
|
|
|
LineId string `json:"lineid"`
|
2022-06-13 20:11:56 +02:00
|
|
|
Ts int64 `json:"ts"`
|
|
|
|
UserId string `json:"userid"`
|
|
|
|
LineType string `json:"linetype"`
|
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
CmdId string `json:"cmdid,omitempty"`
|
2022-08-23 22:14:14 +02:00
|
|
|
Ephemeral bool `json:"ephemeral,omitempty"`
|
2022-07-13 23:16:08 +02:00
|
|
|
Remove bool `json:"remove,omitempty"`
|
2022-07-01 19:48:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 22:26:46 +02:00
|
|
|
type SSHOpts struct {
|
2022-08-24 06:05:49 +02:00
|
|
|
Local bool `json:"local"`
|
2022-07-02 02:38:36 +02:00
|
|
|
SSHHost string `json:"sshhost"`
|
2022-07-07 22:26:46 +02:00
|
|
|
SSHOptsStr string `json:"sshopts"`
|
2022-07-02 02:38:36 +02:00
|
|
|
SSHIdentity string `json:"sshidentity"`
|
|
|
|
SSHUser string `json:"sshuser"`
|
2022-07-07 22:26:46 +02:00
|
|
|
}
|
2022-07-02 02:38:36 +02:00
|
|
|
|
2022-08-19 22:23:00 +02:00
|
|
|
type RemoteOptsType struct {
|
2022-08-24 11:14:16 +02:00
|
|
|
Color string `json:"color"`
|
2022-08-19 22:23:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *RemoteOptsType) Scan(val interface{}) error {
|
|
|
|
return quickScanJson(opts, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts RemoteOptsType) Value() (driver.Value, error) {
|
|
|
|
return quickValueJson(opts)
|
|
|
|
}
|
|
|
|
|
2022-07-07 22:26:46 +02:00
|
|
|
type RemoteType struct {
|
2022-08-17 00:08:28 +02:00
|
|
|
RemoteId string `json:"remoteid"`
|
|
|
|
PhysicalId string `json:"physicalid"`
|
|
|
|
RemoteType string `json:"remotetype"`
|
|
|
|
RemoteAlias string `json:"remotealias"`
|
|
|
|
RemoteCanonicalName string `json:"remotecanonicalname"`
|
|
|
|
RemoteSudo bool `json:"remotesudo"`
|
|
|
|
RemoteUser string `json:"remoteuser"`
|
|
|
|
RemoteHost string `json:"remotehost"`
|
2022-08-21 21:31:29 +02:00
|
|
|
ConnectMode string `json:"connectmode"`
|
2022-08-17 00:08:28 +02:00
|
|
|
InitPk *packet.InitPacketType `json:"inipk"`
|
|
|
|
SSHOpts *SSHOpts `json:"sshopts"`
|
2022-08-19 22:23:00 +02:00
|
|
|
RemoteOpts *RemoteOptsType `json:"remoteopts"`
|
2022-08-17 00:08:28 +02:00
|
|
|
LastConnectTs int64 `json:"lastconnectts"`
|
2022-09-14 02:11:36 +02:00
|
|
|
Archived bool `json:"archived"`
|
2022-09-14 21:06:55 +02:00
|
|
|
RemoteIdx int64 `json:"remoteidx"`
|
2022-08-17 00:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoteType) GetName() string {
|
|
|
|
if r.RemoteAlias != "" {
|
|
|
|
return r.RemoteAlias
|
2022-07-07 22:26:46 +02:00
|
|
|
}
|
2022-09-14 02:11:36 +02:00
|
|
|
return r.RemoteCanonicalName
|
2022-07-01 19:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CmdType struct {
|
2022-07-07 09:10:37 +02:00
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
CmdId string `json:"cmdid"`
|
2022-08-24 22:21:54 +02:00
|
|
|
Remote RemotePtrType `json:"remote"`
|
2022-07-07 09:10:37 +02:00
|
|
|
CmdStr string `json:"cmdstr"`
|
|
|
|
RemoteState RemoteState `json:"remotestate"`
|
|
|
|
TermOpts TermOpts `json:"termopts"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
StartPk *packet.CmdStartPacketType `json:"startpk"`
|
|
|
|
DonePk *packet.CmdDonePacketType `json:"donepk"`
|
2022-07-12 22:50:44 +02:00
|
|
|
UsedRows int64 `json:"usedrows"`
|
2022-07-07 09:10:37 +02:00
|
|
|
RunOut []packet.PacketType `json:"runout"`
|
2022-07-13 23:16:08 +02:00
|
|
|
Remove bool `json:"remove"`
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 22:26:46 +02:00
|
|
|
func (r *RemoteType) ToMap() map[string]interface{} {
|
|
|
|
rtn := make(map[string]interface{})
|
|
|
|
rtn["remoteid"] = r.RemoteId
|
2022-08-17 00:08:28 +02:00
|
|
|
rtn["physicalid"] = r.PhysicalId
|
2022-07-07 22:26:46 +02:00
|
|
|
rtn["remotetype"] = r.RemoteType
|
2022-08-17 00:08:28 +02:00
|
|
|
rtn["remotealias"] = r.RemoteAlias
|
|
|
|
rtn["remotecanonicalname"] = r.RemoteCanonicalName
|
|
|
|
rtn["remotesudo"] = r.RemoteSudo
|
|
|
|
rtn["remoteuser"] = r.RemoteUser
|
|
|
|
rtn["remotehost"] = r.RemoteHost
|
2022-08-21 21:31:29 +02:00
|
|
|
rtn["connectmode"] = r.ConnectMode
|
2022-07-07 22:26:46 +02:00
|
|
|
rtn["initpk"] = quickJson(r.InitPk)
|
|
|
|
rtn["sshopts"] = quickJson(r.SSHOpts)
|
2022-08-19 22:23:00 +02:00
|
|
|
rtn["remoteopts"] = quickJson(r.RemoteOpts)
|
2022-07-07 22:26:46 +02:00
|
|
|
rtn["lastconnectts"] = r.LastConnectTs
|
2022-09-14 02:11:36 +02:00
|
|
|
rtn["archived"] = r.Archived
|
2022-09-14 21:06:55 +02:00
|
|
|
rtn["remoteidx"] = r.RemoteIdx
|
2022-07-07 22:26:46 +02:00
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoteFromMap(m map[string]interface{}) *RemoteType {
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
2022-07-07 22:26:46 +02:00
|
|
|
var r RemoteType
|
|
|
|
quickSetStr(&r.RemoteId, m, "remoteid")
|
2022-08-17 00:08:28 +02:00
|
|
|
quickSetStr(&r.PhysicalId, m, "physicalid")
|
2022-07-07 22:26:46 +02:00
|
|
|
quickSetStr(&r.RemoteType, m, "remotetype")
|
2022-08-17 00:08:28 +02:00
|
|
|
quickSetStr(&r.RemoteAlias, m, "remotealias")
|
|
|
|
quickSetStr(&r.RemoteCanonicalName, m, "remotecanonicalname")
|
|
|
|
quickSetBool(&r.RemoteSudo, m, "remotesudo")
|
|
|
|
quickSetStr(&r.RemoteUser, m, "remoteuser")
|
|
|
|
quickSetStr(&r.RemoteHost, m, "remotehost")
|
2022-08-21 21:31:29 +02:00
|
|
|
quickSetStr(&r.ConnectMode, m, "connectmode")
|
2022-07-07 22:26:46 +02:00
|
|
|
quickSetJson(&r.InitPk, m, "initpk")
|
|
|
|
quickSetJson(&r.SSHOpts, m, "sshopts")
|
2022-08-19 22:23:00 +02:00
|
|
|
quickSetJson(&r.RemoteOpts, m, "remoteopts")
|
2022-07-07 22:26:46 +02:00
|
|
|
quickSetInt64(&r.LastConnectTs, m, "lastconnectts")
|
2022-09-14 02:11:36 +02:00
|
|
|
quickSetBool(&r.Archived, m, "archived")
|
2022-09-14 21:06:55 +02:00
|
|
|
quickSetInt64(&r.RemoteIdx, m, "remoteidx")
|
2022-07-07 22:26:46 +02:00
|
|
|
return &r
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd *CmdType) ToMap() map[string]interface{} {
|
|
|
|
rtn := make(map[string]interface{})
|
|
|
|
rtn["sessionid"] = cmd.SessionId
|
|
|
|
rtn["cmdid"] = cmd.CmdId
|
2022-08-24 22:21:54 +02:00
|
|
|
rtn["remoteownerid"] = cmd.Remote.OwnerId
|
|
|
|
rtn["remoteid"] = cmd.Remote.RemoteId
|
|
|
|
rtn["remotename"] = cmd.Remote.Name
|
2022-07-07 09:10:37 +02:00
|
|
|
rtn["cmdstr"] = cmd.CmdStr
|
|
|
|
rtn["remotestate"] = quickJson(cmd.RemoteState)
|
|
|
|
rtn["termopts"] = quickJson(cmd.TermOpts)
|
|
|
|
rtn["status"] = cmd.Status
|
|
|
|
rtn["startpk"] = quickJson(cmd.StartPk)
|
|
|
|
rtn["donepk"] = quickJson(cmd.DonePk)
|
|
|
|
rtn["runout"] = quickJson(cmd.RunOut)
|
2022-07-12 22:50:44 +02:00
|
|
|
rtn["usedrows"] = cmd.UsedRows
|
2022-07-07 09:10:37 +02:00
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmdFromMap(m map[string]interface{}) *CmdType {
|
2022-07-07 22:26:46 +02:00
|
|
|
if len(m) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-07 09:10:37 +02:00
|
|
|
var cmd CmdType
|
|
|
|
quickSetStr(&cmd.SessionId, m, "sessionid")
|
|
|
|
quickSetStr(&cmd.CmdId, m, "cmdid")
|
2022-08-24 22:21:54 +02:00
|
|
|
quickSetStr(&cmd.Remote.OwnerId, m, "remoteownerid")
|
|
|
|
quickSetStr(&cmd.Remote.RemoteId, m, "remoteid")
|
|
|
|
quickSetStr(&cmd.Remote.Name, m, "remotename")
|
2022-07-07 09:10:37 +02:00
|
|
|
quickSetStr(&cmd.CmdStr, m, "cmdstr")
|
|
|
|
quickSetJson(&cmd.RemoteState, m, "remotestate")
|
|
|
|
quickSetJson(&cmd.TermOpts, m, "termopts")
|
|
|
|
quickSetStr(&cmd.Status, m, "status")
|
|
|
|
quickSetJson(&cmd.StartPk, m, "startpk")
|
|
|
|
quickSetJson(&cmd.DonePk, m, "donepk")
|
|
|
|
quickSetJson(&cmd.RunOut, m, "runout")
|
2022-07-12 22:50:44 +02:00
|
|
|
quickSetInt64(&cmd.UsedRows, m, "usedrows")
|
2022-07-07 09:10:37 +02:00
|
|
|
return &cmd
|
2022-06-13 20:11:56 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 06:39:25 +02:00
|
|
|
func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId string) *LineType {
|
2022-06-13 20:11:56 +02:00
|
|
|
rtn := &LineType{}
|
2022-06-17 00:51:41 +02:00
|
|
|
rtn.SessionId = sessionId
|
|
|
|
rtn.WindowId = windowId
|
2022-08-16 21:08:26 +02:00
|
|
|
rtn.LineId = uuid.New().String()
|
2022-06-13 20:11:56 +02:00
|
|
|
rtn.Ts = time.Now().UnixMilli()
|
2022-07-05 19:51:47 +02:00
|
|
|
rtn.UserId = userId
|
2022-06-13 20:11:56 +02:00
|
|
|
rtn.LineType = LineTypeCmd
|
2022-07-08 06:39:25 +02:00
|
|
|
rtn.CmdId = cmdId
|
2022-06-13 20:11:56 +02:00
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2022-07-05 19:51:47 +02:00
|
|
|
func makeNewLineText(sessionId string, windowId string, userId string, text string) *LineType {
|
2022-06-13 20:11:56 +02:00
|
|
|
rtn := &LineType{}
|
2022-06-17 00:51:41 +02:00
|
|
|
rtn.SessionId = sessionId
|
|
|
|
rtn.WindowId = windowId
|
2022-08-16 21:08:26 +02:00
|
|
|
rtn.LineId = uuid.New().String()
|
2022-06-13 20:11:56 +02:00
|
|
|
rtn.Ts = time.Now().UnixMilli()
|
2022-07-05 19:51:47 +02:00
|
|
|
rtn.UserId = userId
|
2022-06-13 20:11:56 +02:00
|
|
|
rtn.LineType = LineTypeText
|
|
|
|
rtn.Text = text
|
|
|
|
return rtn
|
|
|
|
}
|
2022-06-21 06:57:23 +02:00
|
|
|
|
2022-07-05 19:51:47 +02:00
|
|
|
func AddCommentLine(ctx context.Context, sessionId string, windowId string, userId string, commentText string) (*LineType, error) {
|
|
|
|
rtnLine := makeNewLineText(sessionId, windowId, userId, commentText)
|
2022-07-08 06:39:25 +02:00
|
|
|
err := InsertLine(ctx, rtnLine, nil)
|
2022-07-05 19:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rtnLine, nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 06:39:25 +02:00
|
|
|
func AddCmdLine(ctx context.Context, sessionId string, windowId string, userId string, cmd *CmdType) (*LineType, error) {
|
|
|
|
rtnLine := makeNewLineCmd(sessionId, windowId, userId, cmd.CmdId)
|
|
|
|
err := InsertLine(ctx, rtnLine, cmd)
|
2022-07-05 19:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rtnLine, nil
|
|
|
|
}
|
|
|
|
|
2022-07-01 21:17:19 +02:00
|
|
|
func EnsureLocalRemote(ctx context.Context) error {
|
2022-08-17 21:24:09 +02:00
|
|
|
physicalId, err := base.GetRemoteId()
|
2022-07-01 21:17:19 +02:00
|
|
|
if err != nil {
|
2022-08-17 21:24:09 +02:00
|
|
|
return fmt.Errorf("getting local physical remoteid: %w", err)
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
2022-08-17 21:24:09 +02:00
|
|
|
remote, err := GetRemoteByPhysicalId(ctx, physicalId)
|
2022-07-01 21:17:19 +02:00
|
|
|
if err != nil {
|
2022-08-17 21:24:09 +02:00
|
|
|
return fmt.Errorf("getting remote[%s] from db: %w", physicalId, err)
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
|
|
|
if remote != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-17 00:08:28 +02:00
|
|
|
hostName, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting hostname: %w", err)
|
|
|
|
}
|
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting user: %w", err)
|
|
|
|
}
|
2022-07-01 21:17:19 +02:00
|
|
|
// create the local remote
|
|
|
|
localRemote := &RemoteType{
|
2022-08-17 21:24:09 +02:00
|
|
|
RemoteId: uuid.New().String(),
|
|
|
|
PhysicalId: physicalId,
|
2022-08-21 21:31:29 +02:00
|
|
|
RemoteType: RemoteTypeSsh,
|
2022-08-24 11:14:16 +02:00
|
|
|
RemoteAlias: LocalRemoteAlias,
|
2022-08-17 00:08:28 +02:00
|
|
|
RemoteCanonicalName: fmt.Sprintf("%s@%s", user.Username, hostName),
|
|
|
|
RemoteSudo: false,
|
|
|
|
RemoteUser: user.Username,
|
|
|
|
RemoteHost: hostName,
|
2022-08-21 21:31:29 +02:00
|
|
|
ConnectMode: ConnectModeStartup,
|
2022-08-24 06:05:49 +02:00
|
|
|
SSHOpts: &SSHOpts{Local: true},
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
2022-09-14 02:11:36 +02:00
|
|
|
err = UpsertRemote(ctx, localRemote)
|
2022-07-01 21:17:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-17 00:08:28 +02:00
|
|
|
log.Printf("[db] added remote '%s', id=%s\n", localRemote.GetName(), localRemote.RemoteId)
|
2022-07-01 23:07:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:24:09 +02:00
|
|
|
func AddTest01Remote(ctx context.Context) error {
|
|
|
|
remote, err := GetRemoteByAlias(ctx, "test01")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting remote[test01] from db: %w", err)
|
|
|
|
}
|
|
|
|
if remote != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
testRemote := &RemoteType{
|
|
|
|
RemoteId: uuid.New().String(),
|
2022-08-21 21:31:29 +02:00
|
|
|
RemoteType: RemoteTypeSsh,
|
2022-08-17 21:24:09 +02:00
|
|
|
RemoteAlias: "test01",
|
|
|
|
RemoteCanonicalName: "ubuntu@test01.ec2",
|
|
|
|
RemoteSudo: false,
|
|
|
|
RemoteUser: "ubuntu",
|
|
|
|
RemoteHost: "test01.ec2",
|
|
|
|
SSHOpts: &SSHOpts{
|
2022-08-24 11:14:16 +02:00
|
|
|
Local: false,
|
2022-08-17 21:24:09 +02:00
|
|
|
SSHHost: "test01.ec2",
|
|
|
|
SSHUser: "ubuntu",
|
|
|
|
SSHIdentity: "/Users/mike/aws/mfmt.pem",
|
|
|
|
},
|
2022-08-21 21:31:29 +02:00
|
|
|
ConnectMode: ConnectModeStartup,
|
2022-08-17 21:24:09 +02:00
|
|
|
}
|
2022-09-14 02:11:36 +02:00
|
|
|
err = UpsertRemote(ctx, testRemote)
|
2022-08-17 21:24:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[db] added remote '%s', id=%s\n", testRemote.GetName(), testRemote.RemoteId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-19 22:23:00 +02:00
|
|
|
func AddTest02Remote(ctx context.Context) error {
|
|
|
|
remote, err := GetRemoteByAlias(ctx, "test2")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting remote[test01] from db: %w", err)
|
|
|
|
}
|
|
|
|
if remote != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
testRemote := &RemoteType{
|
|
|
|
RemoteId: uuid.New().String(),
|
2022-08-21 21:31:29 +02:00
|
|
|
RemoteType: RemoteTypeSsh,
|
2022-08-19 22:23:00 +02:00
|
|
|
RemoteAlias: "test2",
|
|
|
|
RemoteCanonicalName: "test2@test01.ec2",
|
|
|
|
RemoteSudo: false,
|
|
|
|
RemoteUser: "test2",
|
|
|
|
RemoteHost: "test01.ec2",
|
|
|
|
SSHOpts: &SSHOpts{
|
2022-08-24 11:14:16 +02:00
|
|
|
Local: false,
|
2022-08-19 22:23:00 +02:00
|
|
|
SSHHost: "test01.ec2",
|
|
|
|
SSHUser: "test2",
|
|
|
|
},
|
2022-08-21 21:31:29 +02:00
|
|
|
ConnectMode: ConnectModeStartup,
|
2022-08-19 22:23:00 +02:00
|
|
|
}
|
2022-09-14 02:11:36 +02:00
|
|
|
err = UpsertRemote(ctx, testRemote)
|
2022-08-19 22:23:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[db] added remote '%s', id=%s\n", testRemote.GetName(), testRemote.RemoteId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-01 23:45:33 +02:00
|
|
|
func EnsureDefaultSession(ctx context.Context) (*SessionType, error) {
|
2022-07-01 23:07:13 +02:00
|
|
|
session, err := GetSessionByName(ctx, DefaultSessionName)
|
|
|
|
if err != nil {
|
2022-07-01 23:45:33 +02:00
|
|
|
return nil, err
|
2022-07-01 23:07:13 +02:00
|
|
|
}
|
|
|
|
if session != nil {
|
2022-07-01 23:45:33 +02:00
|
|
|
return session, nil
|
2022-07-01 23:07:13 +02:00
|
|
|
}
|
2022-08-09 01:21:46 +02:00
|
|
|
_, err = InsertSessionWithName(ctx, DefaultSessionName, true)
|
2022-07-01 23:07:13 +02:00
|
|
|
if err != nil {
|
2022-07-01 23:45:33 +02:00
|
|
|
return nil, err
|
2022-07-01 23:07:13 +02:00
|
|
|
}
|
2022-07-01 23:45:33 +02:00
|
|
|
return GetSessionByName(ctx, DefaultSessionName)
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
2022-08-16 03:42:25 +02:00
|
|
|
|
2022-08-27 01:21:19 +02:00
|
|
|
func createClientData(tx *TxWrap) error {
|
2022-08-16 03:42:25 +02:00
|
|
|
userId := uuid.New().String()
|
|
|
|
curve := elliptic.P384()
|
|
|
|
pkey, err := ecdsa.GenerateKey(curve, rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("generating P-834 key: %w", err)
|
|
|
|
}
|
|
|
|
pkBytes, err := x509.MarshalECPrivateKey(pkey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshaling (pkcs8) private key bytes: %w", err)
|
|
|
|
}
|
|
|
|
pubBytes, err := x509.MarshalPKIXPublicKey(&pkey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshaling (pkix) public key bytes: %w", err)
|
|
|
|
}
|
2022-08-27 01:21:19 +02:00
|
|
|
query := `INSERT INTO client (userid, activesessionid, userpublickeybytes, userprivatekeybytes) VALUES (?, '', ?, ?)`
|
2022-08-16 03:42:25 +02:00
|
|
|
tx.ExecWrap(query, userId, pubBytes, pkBytes)
|
|
|
|
fmt.Printf("create new userid[%s] with public/private keypair\n", userId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-27 01:21:19 +02:00
|
|
|
func EnsureClientData(ctx context.Context) (*ClientData, error) {
|
|
|
|
var rtn ClientData
|
2022-08-16 03:42:25 +02:00
|
|
|
err := WithTx(ctx, func(tx *TxWrap) error {
|
|
|
|
query := `SELECT count(*) FROM client`
|
|
|
|
count := tx.GetInt(query)
|
|
|
|
if count > 1 {
|
|
|
|
return fmt.Errorf("invalid client database, multiple (%d) rows in client table", count)
|
|
|
|
}
|
|
|
|
if count == 0 {
|
2022-08-27 01:21:19 +02:00
|
|
|
createErr := createClientData(tx)
|
2022-08-16 03:42:25 +02:00
|
|
|
if createErr != nil {
|
|
|
|
return createErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
found := tx.GetWrap(&rtn, "SELECT * FROM client")
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("invalid client data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rtn.UserId == "" {
|
|
|
|
return nil, fmt.Errorf("invalid client data (no userid)")
|
|
|
|
}
|
|
|
|
if len(rtn.UserPrivateKeyBytes) == 0 || len(rtn.UserPublicKeyBytes) == 0 {
|
|
|
|
return nil, fmt.Errorf("invalid client data (no public/private keypair)")
|
|
|
|
}
|
|
|
|
rtn.UserPrivateKey, err = x509.ParseECPrivateKey(rtn.UserPrivateKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid client data, cannot parse private key: %w", err)
|
|
|
|
}
|
|
|
|
pubKey, err := x509.ParsePKIXPublicKey(rtn.UserPublicKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid client data, cannot parse public key: %w", err)
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
rtn.UserPublicKey, ok = pubKey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid client data, wrong public key type: %T", pubKey)
|
|
|
|
}
|
|
|
|
return &rtn, nil
|
|
|
|
}
|