Compare commits

..

No commits in common. "87c8dfcbea0e6e467051e71cc798a589362676e4" and "99e47fcb17be66da221166d210f0b227668c1a86" have entirely different histories.

16 changed files with 356 additions and 1989 deletions

View File

@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
@ -52,49 +51,6 @@ func (a *App) startup(ctx context.Context) {
wailsruntime.EventsEmit(ctx, "files-dropped", paths)
}
})
go a.autoSyncLoop()
}
func (a *App) autoSyncLoop() {
const checkInterval = 60 * time.Second
ticker := time.NewTicker(checkInterval)
defer ticker.Stop()
log.Printf("[autosync] started, vault=%s", a.vault)
for {
select {
case <-ticker.C:
serverURL := ""
cfg, err := config.Load(a.vault)
if err == nil {
serverURL = cfg.Sync.ServerURL
}
// Fall back to SQLite sync_state if config doesn't have it.
if serverURL == "" {
sURL, _, _, _, _ := a.sync.GetState()
serverURL = sURL
}
if serverURL == "" {
log.Printf("[autosync] no server URL")
continue
}
if cfg != nil && cfg.Sync.SyncInterval <= 0 {
log.Printf("[autosync] interval=%d, skipping", cfg.Sync.SyncInterval)
continue
}
deviceToken := config.LoadDeviceToken(a.vault)
if deviceToken == "" {
log.Printf("[autosync] no device token")
continue
}
log.Printf("[autosync] running SyncNow...")
if _, err := a.SyncNow(); err != nil {
log.Printf("[autosync] SyncNow error: %v", err)
}
case <-a.ctx.Done():
log.Printf("[autosync] stopped")
return
}
}
}
// ============================================================
@ -888,10 +844,6 @@ type SyncStatusDTO struct {
Configured bool `json:"configured"`
ServerURL string `json:"serverUrl"`
DeviceID string `json:"deviceId"`
DeviceName string `json:"deviceName"`
Connected bool `json:"connected"`
Revoked bool `json:"revoked"`
TokenStored bool `json:"tokenStored"`
UnpushedOps int `json:"unpushedOps"`
LastSyncAt string `json:"lastSyncAt"`
SyncInterval int `json:"syncInterval"`
@ -902,120 +854,65 @@ func (a *App) SyncStatus() (*SyncStatusDTO, error) {
if err != nil {
return &SyncStatusDTO{}, nil
}
unpushed, _ := a.sync.GetUnpushedOps()
cfg, _ := config.Load(a.vault)
deviceToken := config.LoadDeviceToken(a.vault)
dto := &SyncStatusDTO{
Configured: serverURL != "" && (apiKey != "" || deviceToken != ""),
Configured: serverURL != "" && apiKey != "",
ServerURL: serverURL,
UnpushedOps: len(unpushed),
LastSyncAt: lastSyncAt,
UnpushedOps: 0,
TokenStored: deviceToken != "",
}
if cfg != nil {
dto.DeviceID = cfg.Sync.DeviceID
dto.SyncInterval = cfg.Sync.SyncInterval
}
unpushed, _ := a.sync.GetUnpushedOps()
dto.UnpushedOps = len(unpushed)
if deviceToken != "" {
client := syncsvc.NewClient(serverURL, "", "", a.vault)
client.DeviceToken = deviceToken
if cfg != nil {
client.DeviceID = cfg.Sync.DeviceID
}
if info, err := client.GetMe(); err == nil {
dto.DeviceName = info.DeviceName
dto.DeviceID = info.DeviceID
dto.Connected = true
if info.RevokedAt != "" {
dto.Revoked = true
dto.Connected = false
}
}
}
return dto, nil
}
func (a *App) SyncConfigure(serverURL, username, password string) error {
// Register device on server with user credentials.
hostname, _ := os.Hostname()
if hostname == "" {
hostname = "unknown"
}
client := syncsvc.NewClient(serverURL, "", "", a.vault)
deviceID, deviceToken, err := client.PairDevice(serverURL, username, password, hostname, "verstak-gui/v2")
deviceID, apiKey, err := client.RegisterDeviceWithAuth(hostname, username, password)
if err != nil {
return fmt.Errorf("pair: %w", err)
return fmt.Errorf("register: %w", err)
}
// Save token to separate file with 0600 perms.
if err := config.SaveDeviceToken(a.vault, deviceToken); err != nil {
return fmt.Errorf("save token: %w", err)
}
if err := a.sync.SetState(serverURL, ""); err != nil {
if err := a.sync.SetState(serverURL, apiKey); err != nil {
return err
}
// Persist to vault config.
cfg, err := config.Load(a.vault)
if err != nil {
cfg = &config.Config{}
return err
}
cfg.Sync.ServerURL = serverURL
cfg.Sync.APIKey = apiKey
cfg.Sync.DeviceID = deviceID
cfg.Sync.APIKey = ""
return config.Save(a.vault, cfg)
}
func (a *App) SyncDisconnect() error {
deviceToken := config.LoadDeviceToken(a.vault)
cfg, err := config.Load(a.vault)
if err != nil {
cfg = &config.Config{}
}
// Revoke token on server if we have one.
if deviceToken != "" {
client := syncsvc.NewClient(cfg.Sync.ServerURL, "", "", a.vault)
client.DeviceToken = deviceToken
_ = client.RevokeCurrent()
}
config.RemoveDeviceToken(a.vault)
cfg.Sync.ServerURL = ""
cfg.Sync.DeviceID = ""
cfg.Sync.APIKey = ""
if err := config.Save(a.vault, cfg); err != nil {
return err
}
return a.sync.SetState("", "")
}
func (a *App) SyncTestConnection(serverURL, username, password string) error {
client := syncsvc.NewClient(serverURL, "", "", a.vault)
_, _, err := client.PairDevice(serverURL, username, password, "test-connection", "verstak-gui/v2")
_, _, err := client.RegisterDeviceWithAuth("test-connection", username, password)
return err
}
func (a *App) SyncSetInterval(minutes int) error {
cfg, err := config.Load(a.vault)
if err != nil {
cfg = &config.Config{}
}
// If config lost the server URL, restore from sync_state.
if cfg.Sync.ServerURL == "" {
sURL, _, _, _, _ := a.sync.GetState()
if sURL != "" {
cfg.Sync.ServerURL = sURL
}
}
if cfg.Sync.DeviceID == "" {
cfg.Sync.DeviceID = a.sync.GetDeviceID()
return err
}
cfg.Sync.SyncInterval = minutes
return config.Save(a.vault, cfg)
}
func (a *App) SyncNow() (map[string]interface{}, error) {
serverURL, apiKey, lastPullSeq, _, err := a.sync.GetState()
deviceToken := config.LoadDeviceToken(a.vault)
if err != nil || serverURL == "" || (apiKey == "" && deviceToken == "") {
serverURL, apiKey, lastRev, _, err := a.sync.GetState()
if err != nil || serverURL == "" || apiKey == "" {
return nil, fmt.Errorf("sync not configured")
}
@ -1025,7 +922,6 @@ func (a *App) SyncNow() (map[string]interface{}, error) {
}
client := syncsvc.NewClient(serverURL, apiKey, deviceID, a.vault)
client.DeviceToken = deviceToken
// Push unpushed ops.
unpushed, err := a.sync.GetUnpushedOps()
@ -1044,33 +940,15 @@ func (a *App) SyncNow() (map[string]interface{}, error) {
}
// Pull remote ops.
pullResult, err := client.Pull(lastPullSeq)
pullResult, err := client.Pull(lastRev)
if err != nil {
return nil, fmt.Errorf("pull: %w", err)
}
if len(pullResult.Ops) > 0 {
// Apply pulled ops locally (record as remote ops, mark applied).
for _, op := range pullResult.Ops {
_ = a.sync.RecordRemoteOp(op)
}
opIDs := make([]string, len(pullResult.Ops))
for i, op := range pullResult.Ops {
opIDs[i] = op.OpID
}
_ = a.sync.MarkApplied(opIDs)
}
// Update sync state.
if pullResult.ServerSequence > lastPullSeq {
_ = a.sync.SetLastPullSeq(pullResult.ServerSequence)
}
_ = a.sync.SetLastSyncAt(time.Now().UTC().Format(time.RFC3339))
return map[string]interface{}{
"pushed": len(pushResult.Accepted),
"pulled": len(pullResult.Ops),
"serverSequence": pullResult.ServerSequence,
"serverRevision": pullResult.ServerRevision,
}, nil
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -16,8 +16,8 @@
background: #13131f;
}
</style>
<script type="module" crossorigin src="/assets/main-CvznySlT.js"></script>
<link rel="stylesheet" crossorigin href="/assets/main-Bkv7FuGB.css">
<script type="module" crossorigin src="/assets/main-DZkGJWBF.js"></script>
<link rel="stylesheet" crossorigin href="/assets/main-BnVt-oqm.css">
</head>
<body>
<div id="app"></div>

File diff suppressed because it is too large Load Diff

View File

@ -704,12 +704,12 @@ func runSyncPull(args []string) {
syncSvc := syncsvc.NewService(db, deviceID)
client := syncsvc.NewClient(cfg.Sync.ServerURL, cfg.Sync.APIKey, deviceID, abs)
_, _, lastSeq, _, err := syncSvc.GetState()
_, _, lastRev, _, err := syncSvc.GetState()
if err != nil {
lastSeq = 0
lastRev = 0
}
result, err := client.Pull(lastSeq)
result, err := client.Pull(lastRev)
if err != nil {
fmt.Fprintf(os.Stderr, "Pull failed: %v\n", err)
os.Exit(1)
@ -725,7 +725,7 @@ func runSyncPull(args []string) {
syncSvc.MarkApplied(opIDs)
}
fmt.Printf("Pulled %d ops (server seq: %d)\n", len(result.Ops), result.ServerSequence)
fmt.Printf("Pulled %d ops (server rev: %d)\n", len(result.Ops), result.ServerRevision)
}
func runSyncStatus(args []string) {

View File

@ -60,9 +60,8 @@ sudo ./verstak-server/install.sh \
### Что там есть
- **Дашборд** — статистика: количество устройств, количество операций, список API-ключей, форма настройки SMTP.
- **Дашборд** — статистика: количество устройств, количество операций, список API-ключей.
- **Управление API-ключами** — просмотр, создание и удаление ключей устройств.
- **Настройка SMTP** — сервер, порт, логин, пароль, email отправителя, URL сервера (для ссылок в письмах). SMTP нужен для отправки писем подтверждения email и сброса пароля.
Сессия живёт 24 часа. После перезапуска сервера все сессии сбрасываются (хранятся в памяти).
@ -70,28 +69,7 @@ sudo ./verstak-server/install.sh \
Можно запустить сервер с другими `--admin-user`/`--admin-pass` — добавится второй администратор. Повторный запуск с тем же именем меняет пароль.
## 4. Регистрация пользователей
### Как зарегистрироваться
Откройте в браузере: `http://<сервер>:47732/register`
Форма принимает:
- **Логин** — латинские буквы и цифры
- **Email** — для подтверждения и сброса пароля
- **Пароль** — минимум 8 символов, латинские буквы и цифры
После отправки формы на email приходит письмо с ссылкой подтверждения (если SMTP настроен). Если SMTP не настроен, подтверждение происходит автоматически, и можно сразу логиниться.
### Как войти
`http://<сервер>:47732/login` — форма логина принимает логин или email.
### Личный кабинет
После входа — `/dashboard` — список устройств пользователя с полными API-ключами, кнопками копирования и удаления, форма добавления нового устройства.
## 5. API-ключи (устройства)
## 4. API-ключи (устройства)
### Что такое API-ключ
@ -99,27 +77,21 @@ API-ключ — это токен, который клиент (Верстак
### Как создать
#### Через веб-интерфейс пользователя
1. Зайти в `/login`, войти.
2. В `/dashboard` ввести имя устройства и нажать "Connect".
3. Скопировать сгенерированный ключ.
#### Через админ-панель
1. Зайти в `/admin/login`.
Через админ-панель:
1. Зайти в `/admin/dashboard`.
2. В разделе "API Keys" ввести имя устройства и нажать "Create".
3. Скопировать сгенерированный ключ.
#### Через API (требует логин+пароль пользователя)
Через API (требует логин и пароль администратора):
```bash
curl -X POST http://localhost:47732/api/v1/device/register \
-H "Content-Type: application/json" \
-d '{"name":"мой-ноутбук","username":"ivan","password":"пароль-пользователя"}'
-d '{"name":"мой-ноутбук","username":"admin","password":"пароль-админа"}'
```
Ответ: `{"device_id":"...","api_key":"..."}`
**Важно:** не выставляйте сервер в интернет без HTTPS (через reverse proxy). До создания полноценной системы пользователей регистрация устройств требует учётных данных администратора.
### Как использовать
Ключ передаётся в заголовке `Authorization: Bearer <ключ>`:
@ -130,11 +102,11 @@ curl -X POST http://localhost:47732/api/v1/sync/push \
-d '{"device_id":"b10c5d8e3f2a","ops":[...]}'
```
В GUI Верстак достаточно указать URL сервера, логин и пароль — устройство зарегистрируется автоматически.
В клиенте Верстак достаточно вбить URL сервера и API-ключ в настройках (GUI Settings или `config.yml`).
### Один ключ на все устройства или отдельный на каждое?
**На каждое устройство — отдельный ключ.** Так вы сможете отозвать доступ конкретному устройству (удалить ключ в личном кабинете), не затронув остальные.
**На каждое устройство — отдельный ключ.** Так вы сможете отозвать доступ конкретному устройству (удалить ключ в админ-панели), не затронув остальные.
Технически один и тот же ключ можно использовать на нескольких устройствах, но:
- его нельзя будет удалить, не отключив все устройства разом;
@ -143,7 +115,7 @@ curl -X POST http://localhost:47732/api/v1/sync/push \
**Рекомендация:** создавайте отдельный ключ для каждого клиента (ПК, ноутбук, телефон).
## 6. Настройка клиента
## 5. Настройка клиента
### CLI
```bash
@ -161,35 +133,25 @@ sync:
auto_sync: false
```
API-ключ и device_id можно получить, зарегистрировав устройство через API:
```bash
curl -s http://сервер:47732/api/v1/device/register \
-H "Content-Type: application/json" \
-d '{"name":"мой-пк","username":"ivan","password":"мой-пароль"}' | jq .
```
### GUI
В графическом интерфейсе нажмите на иконку шестерёнки в левом нижнем углу → откроется окно настроек синхронизации. Укажите URL сервера, логин и пароль, нажмите "Test Connection" для проверки, затем "Сохранить". Кнопка "Синхронизировать" (или пункт меню в сайдбаре) запускает push + pull.
В графическом интерфейсе нажмите на иконку шестерёнки в левом нижнем углу → откроется окно настроек синхронизации. Укажите URL сервера и API-ключ, нажмите "Сохранить". Кнопка "Синхронизировать" запускает push + pull.
## 7. Безопасность
## 6. Безопасность
Сервер **не поддерживает HTTPS**. В production используйте reverse proxy (nginx, Caddy) для терминирования TLS.
Что стоит учесть:
- Регистрация устройств требует аутентификации пользователя.
- Подтверждение email обязательно, если настроен SMTP (иначе подтверждение автоматическое).
- Нет logout'а для админа — сессия живёт 24 часа или до перезапуска сервера.
- Регистрация устройств открыта — любой, кто достучался до сервера, может создать ключ.
- Нет logout'а — сессия живёт 24 часа или до перезапуска сервера.
- Нет rate limiting'а — возможен перебор пароля.
- Пароли хранятся в bcrypt — база данных не должна быть общедоступной.
- SMTP-пароль хранится в открытом виде в конфиге сервера.
- Пароль хранится в bcrypt — база данных не должна быть общедоступной.
Рекомендации для production:
- Закрыть порт сервера фаерволом (только доверенные IP).
- Использовать VPN (WireGuard/OpenVPN) для доступа между устройствами.
- Поставить nginx/Caddy перед сервером с HTTPS.
- Настроить SMTP для полноценного подтверждения email и сброса пароля.
- Или поставить nginx/Caddy перед сервером с HTTPS и базовой аутентификацией на `/api/v1/device/register`.
## 8. Полный API
## 7. Полный API
### Открытые endpoint'ы
@ -206,22 +168,11 @@ curl -s http://сервер:47732/api/v1/device/register \
| POST | `/api/v1/blobs/` | Загрузить blob (multipart) |
| GET | `/api/v1/blobs/{sha256}` | Скачать blob |
### Требуют логин+пароль (body JSON)
### Требуют логин+пароль администратора
| Метод | Путь | Описание |
|---|---|---|
| POST | `/api/v1/auth/register` | Регистрация (username, email, password) |
| GET | `/api/v1/auth/confirm?token=` | Подтверждение email |
| POST | `/api/v1/auth/login` | Вход, возвращает Bearer-токен |
| POST | `/api/v1/auth/forgot` | Запрос сброса пароля (email) |
| POST | `/api/v1/auth/reset` | Сброс пароля (token, password) |
| POST | `/api/v1/device/register` | Регистрация устройства (name, username, password) |
### Требуют сессию пользователя (Bearer token или cookie)
| Метод | Путь | Описание |
|---|---|---|
| GET | `/api/v1/user/devices` | Список устройств пользователя |
| POST | `/api/v1/device/register` | Регистрация устройства (body: name + username + password) |
### Требуют сессию админа (cookie)
@ -232,13 +183,3 @@ curl -s http://сервер:47732/api/v1/device/register \
| GET | `/admin/api/keys` | Список API-ключей (JSON) |
| POST | `/admin/api/keys` | Создать ключ |
| DELETE | `/admin/api/keys/{id}` | Удалить ключ |
| POST | `/admin/api/smtp` | Сохранить SMTP-конфигурацию |
### Пользовательский веб-интерфейс (cookie)
| Метод | Путь | Описание |
|---|---|---|
| GET/POST | `/register` | Форма регистрации |
| GET/POST | `/login` | Форма входа |
| GET | `/dashboard` | Личный кабинет (устройства) |
| GET | `/logout` | Выход |

View File

@ -925,22 +925,7 @@
if (syncInterval > 0) {
await wailsCall('SyncSetInterval', syncInterval)
}
syncPassword = ''
syncUsername = ''
await loadSyncStatus()
showSettings = false
} catch (e) {
syncResult = 'err: ' + String(e)
}
syncLoading = false
}
async function saveSyncInterval() {
syncLoading = true
syncResult = ''
try {
await wailsCall('SyncSetInterval', syncInterval)
syncResult = 'интервал сохранён'
syncResult = 'ok'
await loadSyncStatus()
} catch (e) {
syncResult = 'err: ' + String(e)
@ -960,25 +945,12 @@
syncLoading = false
}
async function disconnectSync() {
syncLoading = true
syncResult = ''
try {
await wailsCall('SyncDisconnect')
syncResult = 'disconnected'
await loadSyncStatus()
} catch (e) {
syncResult = 'err: ' + String(e)
}
syncLoading = false
}
async function runSyncNow() {
syncLoading = true
syncResult = ''
try {
const r = await wailsCall('SyncNow')
syncResult = 'pushed ' + r.pushed + ', pulled ' + r.pulled + ' (seq ' + r.serverSequence + ')'
syncResult = 'pushed ' + r.pushed + ', pulled ' + r.pulled + ' (rev ' + r.serverRevision + ')'
await loadSyncStatus()
} catch (e) {
syncResult = 'err: ' + String(e)
@ -1552,73 +1524,36 @@
<h3>Настройки синхронизации</h3>
{#if syncStatus}
<div class="sync-status">
<div class="sync-row">
<span class="sync-label">Статус</span>
<span class="sync-value">
{#if syncStatus.revoked}
<span style="color:#ff6b6b">Отозвано</span>
{:else if syncStatus.connected}
<span style="color:#34d399">Подключено</span>
{:else if syncStatus.configured}
<span style="color:#f59e0b">Не подключено</span>
{:else}
<span style="color:#666">Отключена</span>
{/if}
</span>
</div>
{#if syncStatus.serverUrl}
<div class="sync-row"><span class="sync-label">Сервер</span><span class="sync-value mono">{syncStatus.serverUrl}</span></div>
{/if}
{#if syncStatus.deviceName}
<div class="sync-row"><span class="sync-label">Устройство</span><span class="sync-value">{syncStatus.deviceName}</span></div>
{/if}
{#if syncStatus.deviceId && !syncStatus.deviceName}
<div class="sync-row"><span class="sync-label">ID устройства</span><span class="sync-value mono">{syncStatus.deviceId}</span></div>
{/if}
<div class="sync-row"><span class="sync-label">Статус</span><span class="sync-value">{syncStatus.configured ? 'Включена' : 'Отключена'}</span></div>
<div class="sync-row"><span class="sync-label">Сервер</span><span class="sync-value mono">{syncStatus.serverUrl || '—'}</span></div>
<div class="sync-row"><span class="sync-label">Устройство</span><span class="sync-value mono">{syncStatus.deviceId || '—'}</span></div>
<div class="sync-row"><span class="sync-label">Неотправлено</span><span class="sync-value">{syncStatus.unpushedOps}</span></div>
{#if syncStatus.lastSyncAt}
<div class="sync-row"><span class="sync-label">Последняя синх.</span><span class="sync-value">{syncStatus.lastSyncAt}</span></div>
{/if}
<div class="sync-row"><span class="sync-label">Последняя синх.</span><span class="sync-value">{syncStatus.lastSyncAt || '—'}</span></div>
</div>
{/if}
{#if syncStatus?.configured}
<div class="sync-connected-actions">
<button class="btn" on:click={runSyncNow} disabled={syncLoading}>Синхронизировать</button>
<button class="btn btn-danger" on:click={disconnectSync} disabled={syncLoading}>Отключиться</button>
</div>
{:else}
<div class="form-group">
<label>URL сервера</label>
<input type="text" placeholder="https://example.com:47732" bind:value={syncServerUrl} />
</div>
<div class="form-group">
<label>Логин</label>
<input type="text" placeholder="username" bind:value={syncUsername} />
</div>
<div class="form-group">
<label>Пароль</label>
<input type="password" placeholder="password" bind:value={syncPassword} />
</div>
<div class="modal-actions" style="margin-top:12px">
<button class="btn" on:click={testConnection} disabled={syncLoading || !syncServerUrl}>Проверить</button>
<button class="btn btn-primary" on:click={saveSyncConfig} disabled={syncLoading}>Подключиться</button>
</div>
{/if}
<div style="margin-top:16px;padding-top:16px;border-top:1px solid #2a2a3c">
<div class="form-group">
<label>Автосинхронизация (мин, 0 = отключено)</label>
<input type="number" placeholder="0" bind:value={syncInterval} min="0" />
</div>
<button class="btn" on:click={saveSyncInterval} disabled={syncLoading}>Сохранить интервал</button>
<div class="form-group">
<label>URL сервера</label>
<input type="text" placeholder="https://example.com:47732" bind:value={syncServerUrl} />
</div>
<div class="form-group">
<label>Логин</label>
<input type="text" placeholder="username" bind:value={syncUsername} />
</div>
<div class="form-group">
<label>Пароль</label>
<input type="password" placeholder="password" bind:value={syncPassword} />
</div>
<div class="form-group">
<label>Автосинхронизация (мин)</label>
<input type="number" placeholder="0 = отключено" bind:value={syncInterval} min="0" />
</div>
{#if syncResult}
<div class="sync-result" style="margin-top:8px">{syncResult}</div>
<div class="sync-result">{syncResult}</div>
{/if}
<div class="modal-actions" style="margin-top:12px">
<div class="modal-actions">
<button class="btn" on:click={testConnection} disabled={syncLoading || !syncServerUrl}>Проверить</button>
<button class="btn btn-primary" on:click={saveSyncConfig} disabled={syncLoading}>Подключиться</button>
<button class="btn" on:click={runSyncNow} disabled={syncLoading || !syncStatus?.configured}>Синхронизировать</button>
<button class="btn" on:click={closeSettings}>Закрыть</button>
</div>
</div>
@ -1651,8 +1586,8 @@
.header { padding: 12px 24px; border-bottom: 1px solid #2a2a3c; display: flex; align-items: center; flex-shrink: 0; min-height: 48px; }
.header-left { display: flex; align-items: center; gap: 8px; flex: 1; }
.header-right { display: flex; align-items: center; gap: 8px; }
.header-sync-btn { background: #1e1e38; border: 1px solid #6366f1; border-radius: 8px; padding: 6px 12px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; color: #c0c0f0; font-family: inherit; font-size: 13px; position: relative; }
.header-sync-btn:hover { background: #2a2a50; color: #e4e4ef; border-color: #818cf8; }
.header-sync-btn { background: #1a1a28; border: 1px solid #2a2a3c; border-radius: 8px; padding: 6px 12px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; color: #b0b0c0; font-family: inherit; font-size: 13px; position: relative; }
.header-sync-btn:hover { background: #222233; color: #e4e4ef; border-color: #6366f1; }
.header-sync-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.sync-badge { background: #6366f1; color: #fff; font-size: 10px; border-radius: 50%; width: 16px; height: 16px; display: inline-flex; align-items: center; justify-content: center; position: absolute; top: -6px; right: -6px; }
.crumb { font-size: 14px; font-weight: 500; }
@ -1843,8 +1778,8 @@
.activity-feed-time { font-size: 11px; color: #555; }
/* Sync */
.sidebar-sync-btn { background: #1e1e38; border: 1px solid #6366f1; border-radius: 8px; padding: 8px 12px; cursor: pointer; width: 100%; display: flex; align-items: center; gap: 8px; color: #c0c0f0; font-family: inherit; font-size: 13px; }
.sidebar-sync-btn:hover { background: #2a2a50; color: #e4e4ef; border-color: #818cf8; }
.sidebar-sync-btn { background: #1a1a28; border: 1px solid #2a2a3c; border-radius: 8px; padding: 8px 12px; cursor: pointer; width: 100%; display: flex; align-items: center; gap: 8px; color: #888; font-family: inherit; font-size: 13px; }
.sidebar-sync-btn:hover { background: #222233; color: #e4e4ef; border-color: #6366f1; }
.sidebar-sync-label { flex: 1; text-align: left; }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: #4a4a4a; flex-shrink: 0; }
.sync-dot.active { background: #4ade80; box-shadow: 0 0 6px rgba(74,222,128,0.5); }
@ -1855,5 +1790,4 @@
.sync-value { color: #e4e4ef; }
.sync-value.mono { font-family: 'SF Mono', 'Fira Code', monospace; font-size: 12px; }
.sync-result { font-size: 12px; color: #6366f1; padding: 4px 0; }
.sync-connected-actions { display: flex; gap: 8px; margin-bottom: 16px; }
</style>

2
go.mod
View File

@ -5,7 +5,6 @@ go 1.25.0
require (
github.com/mattn/go-sqlite3 v1.14.44
github.com/wailsapp/wails/v2 v2.12.0
golang.org/x/crypto v0.33.0
gopkg.in/yaml.v3 v3.0.1
)
@ -34,6 +33,7 @@ require (
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/wailsapp/go-webview2 v1.0.22 // indirect
github.com/wailsapp/mimetype v1.4.1 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect

View File

@ -10,29 +10,29 @@ import (
// Config lives at .verstak/config.yml inside the vault.
type Config struct {
Engine EngineConfig `yaml:"engine"`
Sync SyncConfig `yaml:"sync"`
Browser BrowserConfig `yaml:"browser"`
Engine EngineConfig `yaml:"engine"`
Sync SyncConfig `yaml:"sync"`
Browser BrowserConfig `yaml:"browser"`
}
type EngineConfig struct {
Version int `yaml:"version"`
VaultID string `yaml:"vault_id"`
CreatedAt string `yaml:"created_at"`
VaultRoot string `yaml:"vault_root"`
Version int `yaml:"version"`
VaultID string `yaml:"vault_id"`
CreatedAt string `yaml:"created_at"`
VaultRoot string `yaml:"vault_root"`
}
type SyncConfig struct {
ServerURL string `yaml:"server_url"`
APIKey string `yaml:"api_key"`
DeviceID string `yaml:"device_id"`
AutoSync bool `yaml:"auto_sync"`
SyncInterval int `yaml:"sync_interval"`
ServerURL string `yaml:"server_url"`
APIKey string `yaml:"api_key"`
DeviceID string `yaml:"device_id"`
AutoSync bool `yaml:"auto_sync"`
SyncInterval int `yaml:"sync_interval"`
}
type BrowserConfig struct {
Enabled bool `yaml:"enabled"`
LocalPort int `yaml:"local_port"`
Enabled bool `yaml:"enabled"`
LocalPort int `yaml:"local_port"`
}
// Load reads .verstak/config.yml from the vault root.
@ -67,41 +67,3 @@ func Save(vaultRoot string, cfg *Config) error {
func MetaDir(vaultRoot string) string {
return filepath.Join(vaultRoot, ".verstak")
}
// DeviceTokenPath returns the path to the device_token file.
func DeviceTokenPath(vaultRoot string) string {
return filepath.Join(vaultRoot, ".verstak", "device_token.json")
}
// SaveDeviceToken writes the device token to a separate file with 0600 perms.
func SaveDeviceToken(vaultRoot, token string) error {
path := DeviceTokenPath(vaultRoot)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o750); err != nil {
return err
}
data := fmt.Sprintf(`{"device_token":%q}`, token)
return os.WriteFile(path, []byte(data), 0o600)
}
// LoadDeviceToken reads the device token from the separate file.
func LoadDeviceToken(vaultRoot string) string {
path := DeviceTokenPath(vaultRoot)
data, err := os.ReadFile(path)
if err != nil {
return ""
}
var v struct {
DeviceToken string `yaml:"device_token"`
}
if err := yaml.Unmarshal(data, &v); err != nil {
return ""
}
return v.DeviceToken
}
// RemoveDeviceToken deletes the device token file.
func RemoveDeviceToken(vaultRoot string) error {
path := DeviceTokenPath(vaultRoot)
return os.Remove(path)
}

View File

@ -1,6 +0,0 @@
package storage
// migration011 — add last_pull_seq to sync_state.
const migration011 = `
ALTER TABLE sync_state ADD COLUMN last_pull_seq INTEGER NOT NULL DEFAULT 0;
`

View File

@ -57,17 +57,16 @@ CREATE TABLE IF NOT EXISTS _schema_ver (
`
var migrationFiles = map[int]string{
1: migration001,
2: migration002,
3: migration003,
4: migration004,
5: migration005,
6: migration006,
1: migration001,
2: migration002,
3: migration003,
4: migration004,
5: migration005,
6: migration006,
// 7: migration007 (FTS5) — created lazily by search.Rebuild()
8: migration008,
9: migration009,
8: migration008,
9: migration009,
10: migration010,
11: migration011,
}
func (db *DB) runInitialSchema() error {

View File

@ -14,12 +14,11 @@ import (
// Client communicates with the Verstak Sync Server.
type Client struct {
ServerURL string
APIKey string // legacy API key
DeviceToken string // new device token
DeviceID string
VaultRoot string
HTTP *http.Client
ServerURL string
APIKey string
DeviceID string
VaultRoot string
HTTP *http.Client
}
// NewClient creates a sync client.
@ -33,56 +32,6 @@ func NewClient(serverURL, apiKey, deviceID, vaultRoot string) *Client {
}
}
// PairDevice calls POST /api/client/pair and returns device_id and device_token.
func (c *Client) PairDevice(serverURL, username, password, deviceName, clientVersion string) (deviceID, deviceToken string, err error) {
body := map[string]string{
"login": username,
"password": password,
"device_name": deviceName,
"client_version": clientVersion,
}
var resp struct {
DeviceID string `json:"device_id"`
DeviceToken string `json:"device_token"`
}
savedURL := c.ServerURL
c.ServerURL = serverURL
err = c.post("/api/client/pair", body, &resp)
c.ServerURL = savedURL
if err != nil {
return "", "", err
}
return resp.DeviceID, resp.DeviceToken, nil
}
// GetMe calls GET /api/client/me and returns device info.
type DeviceInfo struct {
DeviceID string `json:"device_id"`
UserID string `json:"user_id"`
Username string `json:"username"`
DeviceName string `json:"device_name"`
ClientVersion string `json:"client_version"`
LastSeen string `json:"last_seen"`
RevokedAt string `json:"revoked_at"`
CreatedAt string `json:"created_at"`
}
func (c *Client) GetMe() (*DeviceInfo, error) {
var resp DeviceInfo
if err := c.get("/api/client/me", &resp); err != nil {
return nil, err
}
return &resp, nil
}
// RevokeCurrent calls POST /api/client/revoke-current.
func (c *Client) RevokeCurrent() error {
var resp struct {
Status string `json:"status"`
}
return c.post("/api/client/revoke-current", nil, &resp)
}
// RegisterDevice calls POST /api/v1/device/register and returns the API key.
func (c *Client) RegisterDevice(name string) (apiKey string, err error) {
body := map[string]string{"name": name}
@ -132,28 +81,24 @@ func (c *Client) Login(username, password string) (token string, err error) {
// PushRequest is the payload for POST /sync/push.
type PushRequest struct {
DeviceID string `json:"device_id"`
IdempotencyKey string `json:"idempotency_key,omitempty"`
Ops []PushOp `json:"ops"`
DeviceID string `json:"device_id"`
Ops []PushOp `json:"ops"`
}
// PushOp is a single operation in a push request.
type PushOp struct {
OpID string `json:"op_id"`
EntityType string `json:"entity_type"`
EntityID string `json:"entity_id"`
OpType string `json:"op_type"`
PayloadJSON string `json:"payload_json"`
ClientSequence int `json:"client_sequence"`
LastSeenServerSeq int `json:"last_seen_server_seq"`
CreatedAt string `json:"created_at"`
OpID string `json:"op_id"`
EntityType string `json:"entity_type"`
EntityID string `json:"entity_id"`
OpType string `json:"op_type"`
PayloadJSON string `json:"payload_json"`
CreatedAt string `json:"created_at"`
}
// PushResponse is the response from POST /sync/push.
type PushResponse struct {
Accepted []string `json:"accepted"`
Count int `json:"count"`
Conflicts []map[string]interface{} `json:"conflicts"`
Accepted []string `json:"accepted"`
Count int `json:"count"`
}
// Push sends local operations to the server.
@ -179,18 +124,18 @@ func (c *Client) Push(ops []Op) (*PushResponse, error) {
// PullRequest is the payload for POST /sync/pull.
type PullRequest struct {
SinceSequence int `json:"since_sequence"`
SinceRevision int `json:"since_revision"`
}
// PullResponse is the response from POST /sync/pull.
type PullResponse struct {
ServerSequence int `json:"server_sequence"`
Ops []Op `json:"ops"`
ServerRevision int `json:"server_revision"`
Ops []Op `json:"ops"`
}
// Pull fetches remote operations since a given sequence.
func (c *Client) Pull(sinceSequence int) (*PullResponse, error) {
req := PullRequest{SinceSequence: sinceSequence}
// Pull fetches remote operations since a given revision.
func (c *Client) Pull(sinceRevision int) (*PullResponse, error) {
req := PullRequest{SinceRevision: sinceRevision}
var resp PullResponse
if err := c.post("/api/v1/sync/pull", req, &resp); err != nil {
return nil, err
@ -221,7 +166,7 @@ func (c *Client) UploadBlob(localPath string) (sha256 string, err error) {
return "", err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+c.bearerToken())
req.Header.Set("Authorization", "Bearer "+c.APIKey)
resp, err := c.HTTP.Do(req)
if err != nil {
@ -245,7 +190,7 @@ func (c *Client) DownloadBlob(sha256, destPath string) error {
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.bearerToken())
req.Header.Set("Authorization", "Bearer "+c.APIKey)
resp, err := c.HTTP.Do(req)
if err != nil {
@ -268,50 +213,17 @@ func (c *Client) DownloadBlob(sha256, destPath string) error {
// --- internal ---
func (c *Client) bearerToken() string {
if c.DeviceToken != "" {
return c.DeviceToken
}
return c.APIKey
}
func (c *Client) post(path string, body, result interface{}) error {
var b bytes.Buffer
if body != nil {
if err := json.NewEncoder(&b).Encode(body); err != nil {
return err
}
if err := json.NewEncoder(&b).Encode(body); err != nil {
return err
}
req, err := http.NewRequest("POST", c.ServerURL+path, &b)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.bearerToken())
resp, err := c.HTTP.Do(req)
if err != nil {
return fmt.Errorf("http: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
data, _ := io.ReadAll(resp.Body)
return fmt.Errorf("server %d: %s", resp.StatusCode, string(data))
}
if result != nil {
return json.NewDecoder(resp.Body).Decode(result)
}
return nil
}
func (c *Client) get(path string, result interface{}) error {
req, err := http.NewRequest("GET", c.ServerURL+path, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.bearerToken())
req.Header.Set("Authorization", "Bearer "+c.APIKey)
resp, err := c.HTTP.Do(req)
if err != nil {

View File

@ -30,16 +30,15 @@ const (
// Op represents a sync operation.
type Op struct {
ID string `json:"id"`
OpID string `json:"op_id"`
ServerSequence int `json:"server_sequence,omitempty"`
DeviceID string `json:"device_id,omitempty"`
EntityType string `json:"entity_type"`
EntityID string `json:"entity_id"`
OpType string `json:"op_type"`
PayloadJSON string `json:"payload_json"`
CreatedAt string `json:"created_at"`
PushedAt *string `json:"pushed_at,omitempty"`
ID string `json:"id"`
OpID string `json:"op_id"`
DeviceID string `json:"device_id,omitempty"`
EntityType string `json:"entity_type"`
EntityID string `json:"entity_id"`
OpType string `json:"op_type"`
PayloadJSON string `json:"payload_json"`
CreatedAt string `json:"created_at"`
PushedAt *string `json:"pushed_at,omitempty"`
}
// Service records and manages sync operations.
@ -75,17 +74,6 @@ func (s *Service) RecordOp(entityType, entityID, opType string, payload interfac
return err
}
// RecordRemoteOp writes a remote op to the local sync_ops table (already applied server-side).
func (s *Service) RecordRemoteOp(op Op) error {
now := time.Now().UTC().Format(time.RFC3339)
_, err := s.db.Exec(
`INSERT OR IGNORE INTO sync_ops (id, op_id, device_id, entity_type, entity_id, op_type, payload_json, created_at, pushed_at, applied_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
op.OpID+"-remote", op.OpID, op.DeviceID, op.EntityType, op.EntityID, op.OpType, op.PayloadJSON, op.CreatedAt, now, now,
)
return err
}
// GetUnpushedOps returns ops that have not been pushed yet.
func (s *Service) GetUnpushedOps() ([]Op, error) {
rows, err := s.db.Query(
@ -123,10 +111,10 @@ func (s *Service) MarkApplied(opIDs []string) error {
}
// GetState returns the current sync state.
func (s *Service) GetState() (serverURL, apiKey string, lastPullSeq int, lastSyncAt string, err error) {
func (s *Service) GetState() (serverURL, apiKey string, lastPushRev int, lastSyncAt string, err error) {
err = s.db.QueryRow(
`SELECT server_url, api_key, last_pull_seq, COALESCE(last_sync_at,'') FROM sync_state WHERE device_id=?`,
s.deviceID).Scan(&serverURL, &apiKey, &lastPullSeq, &lastSyncAt)
`SELECT server_url, api_key, last_push_rev, COALESCE(last_sync_at,'') FROM sync_state WHERE device_id=?`,
s.deviceID).Scan(&serverURL, &apiKey, &lastPushRev, &lastSyncAt)
if err == sql.ErrNoRows {
return "", "", 0, "", nil
}
@ -136,33 +124,14 @@ func (s *Service) GetState() (serverURL, apiKey string, lastPullSeq int, lastSyn
// SetState saves sync connection state.
func (s *Service) SetState(serverURL, apiKey string) error {
_, err := s.db.Exec(
`INSERT INTO sync_state (device_id, server_url, api_key, last_pull_seq, last_sync_at)
`INSERT INTO sync_state (device_id, server_url, api_key, last_push_rev, last_sync_at)
VALUES (?, ?, ?, 0, '')
ON CONFLICT(device_id) DO UPDATE SET
server_url=excluded.server_url,
api_key=excluded.api_key`,
ON CONFLICT(device_id) DO UPDATE SET server_url=excluded.server_url, api_key=excluded.api_key`,
s.deviceID, serverURL, apiKey,
)
return err
}
// SetLastPullSeq updates the last pulled server sequence.
func (s *Service) SetLastPullSeq(seq int) error {
_, err := s.db.Exec("UPDATE sync_state SET last_pull_seq=? WHERE device_id=?", seq, s.deviceID)
return err
}
// GetDeviceID returns the device ID used by this service.
func (s *Service) GetDeviceID() string {
return s.deviceID
}
// SetLastSyncAt updates the last sync timestamp.
func (s *Service) SetLastSyncAt(t string) error {
_, err := s.db.Exec("UPDATE sync_state SET last_sync_at=? WHERE device_id=?", t, s.deviceID)
return err
}
// --- helpers ---
func scanOps(rows *sql.Rows) ([]Op, error) {