Compare commits
12 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 68911b3af4 | |||
| b229cfe264 | |||
| b3ba967353 | |||
| 4acdfeaa87 | |||
| 9991f887b0 | |||
| 088024ad9c | |||
| 3643430798 | |||
| 383d14e155 | |||
| 30d6cd26d6 | |||
| 71f752c46e | |||
| 5abee4c3bb | |||
| 164c562f9f |
10 changed files with 353 additions and 30 deletions
|
|
@ -10,18 +10,24 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container:
|
container:
|
||||||
image: golang:1.27
|
image: golang:1.27
|
||||||
|
volumes:
|
||||||
|
- /opt/runner/keys:/keys:ro
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install packaging tools
|
- name: Install packaging tools
|
||||||
run: |
|
run: |
|
||||||
apt-get update
|
apt-get update
|
||||||
apt-get install -y dpkg-dev git curl
|
apt-get install -y dpkg-dev git curl openssh-client
|
||||||
|
|
||||||
|
- name: Setup SSH
|
||||||
|
run: |
|
||||||
|
test -r /keys/forgejo_runner || { echo "Deploy key /keys/forgejo_runner chybi"; exit 1; }
|
||||||
|
install -m 600 /keys/forgejo_runner /tmp/forgejo_runner
|
||||||
|
echo "GIT_SSH_COMMAND=ssh -p 554 -i /tmp/forgejo_runner -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR" >> "$GITHUB_ENV"
|
||||||
|
|
||||||
- name: Checkout source
|
- name: Checkout source
|
||||||
env:
|
|
||||||
FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
|
|
||||||
run: |
|
run: |
|
||||||
git clone "https://token:${FORGEJO_TOKEN}@git.janvete.com/seth/Caddyview.git" src
|
git clone ssh://git@git-ssh.janvete.com:554/seth/Caddyview.git src
|
||||||
cd src
|
cd src
|
||||||
git checkout "${{ github.ref_name }}"
|
git checkout "${{ github.ref_name }}"
|
||||||
|
|
||||||
|
|
@ -74,17 +80,26 @@ jobs:
|
||||||
VERSION=${{ github.ref_name }}
|
VERSION=${{ github.ref_name }}
|
||||||
API="${FORGEJO_URL}/api/v1/repos/seth/Caddyview"
|
API="${FORGEJO_URL}/api/v1/repos/seth/Caddyview"
|
||||||
|
|
||||||
|
# Try to create release; if it already exists, fetch the existing one.
|
||||||
RELEASE_JSON=$(curl -sS -H "Authorization: token ${FORGEJO_TOKEN}" \
|
RELEASE_JSON=$(curl -sS -H "Authorization: token ${FORGEJO_TOKEN}" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-X POST \
|
-X POST \
|
||||||
-d "{\"tag_name\":\"${VERSION}\",\"name\":\"${VERSION}\",\"draft\":false,\"prerelease\":false}" \
|
-d "{\"tag_name\":\"${VERSION}\",\"name\":\"${VERSION}\",\"draft\":false,\"prerelease\":false}" \
|
||||||
"${API}/releases")
|
"${API}/releases")
|
||||||
|
|
||||||
echo "Release response: ${RELEASE_JSON}"
|
echo "Create release response: ${RELEASE_JSON}"
|
||||||
RELEASE_ID=$(echo "${RELEASE_JSON}" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
RELEASE_ID=$(echo "${RELEASE_JSON}" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||||
|
|
||||||
if [ -z "${RELEASE_ID}" ]; then
|
if [ -z "${RELEASE_ID}" ]; then
|
||||||
echo "Failed to create release"
|
echo "Release may already exist, fetching existing release..."
|
||||||
|
EXISTING=$(curl -sS -H "Authorization: token ${FORGEJO_TOKEN}" \
|
||||||
|
"${API}/releases/tags/${VERSION}")
|
||||||
|
echo "Existing release response: ${EXISTING}"
|
||||||
|
RELEASE_ID=$(echo "${EXISTING}" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${RELEASE_ID}" ]; then
|
||||||
|
echo "Failed to create or fetch release"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
@ -104,8 +119,6 @@ jobs:
|
||||||
|
|
||||||
- name: Update Homebrew tap
|
- name: Update Homebrew tap
|
||||||
working-directory: src
|
working-directory: src
|
||||||
env:
|
|
||||||
FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
|
|
||||||
run: |
|
run: |
|
||||||
VERSION=${{ github.ref_name }}
|
VERSION=${{ github.ref_name }}
|
||||||
VER=${VERSION#v}
|
VER=${VERSION#v}
|
||||||
|
|
@ -113,7 +126,7 @@ jobs:
|
||||||
SHA_AMD64=$(sha256sum dist/caddyview-${VERSION}-darwin-amd64.tar.gz | awk '{print $1}')
|
SHA_AMD64=$(sha256sum dist/caddyview-${VERSION}-darwin-amd64.tar.gz | awk '{print $1}')
|
||||||
SHA_LINUX=$(sha256sum dist/caddyview-${VERSION}-linux-amd64.tar.gz | awk '{print $1}')
|
SHA_LINUX=$(sha256sum dist/caddyview-${VERSION}-linux-amd64.tar.gz | awk '{print $1}')
|
||||||
|
|
||||||
git clone "https://token:${FORGEJO_TOKEN}@git.janvete.com/seth/homebrew-tools.git" tap
|
git clone ssh://git@git-ssh.janvete.com:554/seth/homebrew-tools.git tap
|
||||||
|
|
||||||
cat > tap/Formula/caddyview.rb << FORMULA
|
cat > tap/Formula/caddyview.rb << FORMULA
|
||||||
class Caddyview < Formula
|
class Caddyview < Formula
|
||||||
|
|
@ -158,16 +171,18 @@ jobs:
|
||||||
|
|
||||||
- name: Update APT repository
|
- name: Update APT repository
|
||||||
working-directory: src
|
working-directory: src
|
||||||
env:
|
|
||||||
FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
|
|
||||||
run: |
|
run: |
|
||||||
VERSION=${{ github.ref_name }}
|
VERSION=${{ github.ref_name }}
|
||||||
VER=${VERSION#v}
|
VER=${VERSION#v}
|
||||||
|
|
||||||
git clone "https://token:${FORGEJO_TOKEN}@git.janvete.com/seth/apt.git" apt-repo
|
git clone ssh://git@git-ssh.janvete.com:554/seth/apt.git apt-repo
|
||||||
cp "dist/caddyview_${VER}_amd64.deb" apt-repo/
|
cp "dist/caddyview_${VER}_amd64.deb" apt-repo/
|
||||||
|
|
||||||
cd apt-repo
|
cd apt-repo
|
||||||
|
# repo muze byt uplne prazdny (bez commitu a bez branche)
|
||||||
|
if ! git rev-parse --verify HEAD >/dev/null 2>&1; then
|
||||||
|
git checkout -b main
|
||||||
|
fi
|
||||||
dpkg-scanpackages . > Packages
|
dpkg-scanpackages . > Packages
|
||||||
gzip -k -f Packages
|
gzip -k -f Packages
|
||||||
|
|
||||||
|
|
@ -196,4 +211,4 @@ jobs:
|
||||||
git config user.name "forgejo-actions[bot]"
|
git config user.name "forgejo-actions[bot]"
|
||||||
git add -A
|
git add -A
|
||||||
git commit -m "chore: add caddyview ${VERSION}"
|
git commit -m "chore: add caddyview ${VERSION}"
|
||||||
git push
|
git push -u origin HEAD
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/janvete/caddyview/internal/sshclient"
|
"git.janvete.com/seth/caddyview/internal/sshclient"
|
||||||
"github.com/janvete/caddyview/internal/tui"
|
"git.janvete.com/seth/caddyview/internal/tui"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
5
go.mod
5
go.mod
|
|
@ -1,4 +1,4 @@
|
||||||
module github.com/janvete/caddyview
|
module git.janvete.com/seth/caddyview
|
||||||
|
|
||||||
go 1.26.4
|
go 1.26.4
|
||||||
|
|
||||||
|
|
@ -7,6 +7,7 @@ require (
|
||||||
github.com/charmbracelet/lipgloss v1.1.0
|
github.com/charmbracelet/lipgloss v1.1.0
|
||||||
github.com/spf13/cobra v1.10.2
|
github.com/spf13/cobra v1.10.2
|
||||||
golang.org/x/crypto v0.53.0
|
golang.org/x/crypto v0.53.0
|
||||||
|
golang.org/x/term v0.47.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
@ -27,6 +28,6 @@ require (
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
github.com/spf13/pflag v1.0.9 // indirect
|
github.com/spf13/pflag v1.0.9 // indirect
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
golang.org/x/sys v0.46.0 // indirect
|
golang.org/x/sys v0.49.0 // indirect
|
||||||
golang.org/x/text v0.38.0 // indirect
|
golang.org/x/text v0.38.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
8
go.sum
8
go.sum
|
|
@ -48,10 +48,10 @@ golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZ
|
||||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
|
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||||
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc=
|
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
|
||||||
golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y=
|
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=
|
||||||
golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE=
|
golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE=
|
||||||
golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4=
|
golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|
|
||||||
|
|
@ -304,13 +304,14 @@ func ParseAggregatedOutput(raw string) (*AggregatedHistory, error) {
|
||||||
if len(parts) < 6 {
|
if len(parts) < 6 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ts, err := strconv.ParseInt(parts[0], 10, 64)
|
// Caddy logs ts as a float (unix seconds with fractional part).
|
||||||
|
tsFloat, err := strconv.ParseFloat(parts[0], 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
status, _ := strconv.Atoi(parts[1])
|
status, _ := strconv.Atoi(parts[1])
|
||||||
h.Errors = append(h.Errors, ErrorEntry{
|
h.Errors = append(h.Errors, ErrorEntry{
|
||||||
Time: time.Unix(ts, 0),
|
Time: time.Unix(int64(tsFloat), 0),
|
||||||
Status: status,
|
Status: status,
|
||||||
Method: parts[2],
|
Method: parts[2],
|
||||||
URI: parts[3],
|
URI: parts[3],
|
||||||
|
|
|
||||||
95
internal/sshclient/bcryptpbkdf.go
Normal file
95
internal/sshclient/bcryptpbkdf.go
Normal file
|
|
@ -0,0 +1,95 @@
|
||||||
|
// Copyright 2014 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found at https://go.dev/LICENSE.
|
||||||
|
//
|
||||||
|
// Adapted from golang.org/x/crypto/ssh/internal/bcrypt_pbkdf, which is not
|
||||||
|
// importable (internal package). Implements bcrypt_pbkdf(3) from OpenBSD.
|
||||||
|
//
|
||||||
|
// See https://flak.tedunangst.com/post/bcrypt-pbkdf and
|
||||||
|
// https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libutil/bcrypt_pbkdf.c.
|
||||||
|
package sshclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha512"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/blowfish"
|
||||||
|
)
|
||||||
|
|
||||||
|
const bcryptBlockSize = 32
|
||||||
|
|
||||||
|
// bcryptPBKDF derives a key from the password, salt and rounds count,
|
||||||
|
// returning a []byte of length keyLen that can be used as cryptographic key.
|
||||||
|
func bcryptPBKDF(password, salt []byte, rounds, keyLen int) ([]byte, error) {
|
||||||
|
if rounds < 1 {
|
||||||
|
return nil, errors.New("bcrypt_pbkdf: number of rounds is too small")
|
||||||
|
}
|
||||||
|
if len(password) == 0 {
|
||||||
|
return nil, errors.New("bcrypt_pbkdf: empty password")
|
||||||
|
}
|
||||||
|
if len(salt) == 0 || len(salt) > 1<<20 {
|
||||||
|
return nil, errors.New("bcrypt_pbkdf: bad salt length")
|
||||||
|
}
|
||||||
|
if keyLen > 1024 {
|
||||||
|
return nil, errors.New("bcrypt_pbkdf: keyLen is too large")
|
||||||
|
}
|
||||||
|
|
||||||
|
numBlocks := (keyLen + bcryptBlockSize - 1) / bcryptBlockSize
|
||||||
|
key := make([]byte, numBlocks*bcryptBlockSize)
|
||||||
|
|
||||||
|
h := sha512.New()
|
||||||
|
h.Write(password)
|
||||||
|
shapass := h.Sum(nil)
|
||||||
|
|
||||||
|
shasalt := make([]byte, 0, sha512.Size)
|
||||||
|
cnt, tmp := make([]byte, 4), make([]byte, bcryptBlockSize)
|
||||||
|
for block := 1; block <= numBlocks; block++ {
|
||||||
|
h.Reset()
|
||||||
|
h.Write(salt)
|
||||||
|
cnt[0] = byte(block >> 24)
|
||||||
|
cnt[1] = byte(block >> 16)
|
||||||
|
cnt[2] = byte(block >> 8)
|
||||||
|
cnt[3] = byte(block)
|
||||||
|
h.Write(cnt)
|
||||||
|
bcryptHash(tmp, shapass, h.Sum(shasalt))
|
||||||
|
|
||||||
|
out := make([]byte, bcryptBlockSize)
|
||||||
|
copy(out, tmp)
|
||||||
|
for i := 2; i <= rounds; i++ {
|
||||||
|
h.Reset()
|
||||||
|
h.Write(tmp)
|
||||||
|
bcryptHash(tmp, shapass, h.Sum(shasalt))
|
||||||
|
for j := 0; j < len(out); j++ {
|
||||||
|
out[j] ^= tmp[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, v := range out {
|
||||||
|
key[i*numBlocks+(block-1)] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return key[:keyLen], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var bcryptMagic = []byte("OxychromaticBlowfishSwatDynamite")
|
||||||
|
|
||||||
|
func bcryptHash(out, shapass, shasalt []byte) {
|
||||||
|
c, err := blowfish.NewSaltedCipher(shapass, shasalt)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for i := 0; i < 64; i++ {
|
||||||
|
blowfish.ExpandKey(shasalt, c)
|
||||||
|
blowfish.ExpandKey(shapass, c)
|
||||||
|
}
|
||||||
|
copy(out, bcryptMagic)
|
||||||
|
for i := 0; i < 32; i += 8 {
|
||||||
|
for j := 0; j < 64; j++ {
|
||||||
|
c.Encrypt(out[i:i+8], out[i:i+8])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Swap bytes due to different endianness.
|
||||||
|
for i := 0; i < 32; i += 4 {
|
||||||
|
out[i+3], out[i+2], out[i+1], out[i] = out[i], out[i+1], out[i+2], out[i+3]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,15 +2,17 @@ package sshclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/janvete/caddyview/internal/parser"
|
"git.janvete.com/seth/caddyview/internal/parser"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"golang.org/x/crypto/ssh/agent"
|
"golang.org/x/crypto/ssh/agent"
|
||||||
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
|
@ -149,6 +151,7 @@ func buildAuthMethods(keyPath string) ([]ssh.AuthMethod, error) {
|
||||||
candidates = append(candidates, filepath.Join(home, ".ssh", name))
|
candidates = append(candidates, filepath.Join(home, ".ssh", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var problems []string
|
||||||
for _, p := range candidates {
|
for _, p := range candidates {
|
||||||
data, err := os.ReadFile(p)
|
data, err := os.ReadFile(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -156,17 +159,58 @@ func buildAuthMethods(keyPath string) ([]ssh.AuthMethod, error) {
|
||||||
}
|
}
|
||||||
signer, err := ssh.ParsePrivateKey(data)
|
signer, err := ssh.ParsePrivateKey(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var passErr *ssh.PassphraseMissingError
|
||||||
|
if errors.As(err, &passErr) {
|
||||||
|
signer, err = unlockWithPassphrase(p, data)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
problems = append(problems, fmt.Sprintf("%s (%v)", p, err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
methods = append(methods, ssh.PublicKeys(signer))
|
methods = append(methods, ssh.PublicKeys(signer))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(methods) == 0 {
|
if len(methods) == 0 {
|
||||||
|
if len(problems) > 0 {
|
||||||
|
return nil, fmt.Errorf("key files found but could not be loaded: %s", strings.Join(problems, "; "))
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("no SSH auth method available — no agent (SSH_AUTH_SOCK) and no key files found in %v", candidates)
|
return nil, fmt.Errorf("no SSH auth method available — no agent (SSH_AUTH_SOCK) and no key files found in %v", candidates)
|
||||||
}
|
}
|
||||||
return methods, nil
|
return methods, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unlockWithPassphrase prompts for the passphrase of an encrypted private key.
|
||||||
|
// It runs before the TUI starts, so stdin is still a normal terminal.
|
||||||
|
func unlockWithPassphrase(path string, data []byte) (ssh.Signer, error) {
|
||||||
|
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
||||||
|
return nil, fmt.Errorf("key is passphrase-protected and stdin is not a terminal")
|
||||||
|
}
|
||||||
|
for attempt := 0; attempt < 3; attempt++ {
|
||||||
|
fmt.Fprintf(os.Stderr, "Enter passphrase for %s: ", path)
|
||||||
|
pass, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
|
fmt.Fprintln(os.Stderr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
signer, err := parsePrivateKeyWithPassphrase(data, pass)
|
||||||
|
if err == nil {
|
||||||
|
return signer, nil
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "caddyview: cannot decrypt key: %v\n", err)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("could not decrypt key in 3 attempts")
|
||||||
|
}
|
||||||
|
|
||||||
|
// parsePrivateKeyWithPassphrase handles ciphers beyond what x/crypto/ssh
|
||||||
|
// supports (aes256-gcm@openssh.com, e.g. 1Password key exports).
|
||||||
|
func parsePrivateKeyWithPassphrase(data, pass []byte) (ssh.Signer, error) {
|
||||||
|
if isGCMEncryptedKey(data) {
|
||||||
|
return parseGCMKey(data, pass)
|
||||||
|
}
|
||||||
|
return ssh.ParsePrivateKeyWithPassphrase(data, pass)
|
||||||
|
}
|
||||||
|
|
||||||
// agentAuth returns an auth method backed by the SSH agent if SSH_AUTH_SOCK is set.
|
// agentAuth returns an auth method backed by the SSH agent if SSH_AUTH_SOCK is set.
|
||||||
func agentAuth() ssh.AuthMethod {
|
func agentAuth() ssh.AuthMethod {
|
||||||
sock := os.Getenv("SSH_AUTH_SOCK")
|
sock := os.Getenv("SSH_AUTH_SOCK")
|
||||||
|
|
|
||||||
122
internal/sshclient/gcmkey.go
Normal file
122
internal/sshclient/gcmkey.go
Normal file
|
|
@ -0,0 +1,122 @@
|
||||||
|
package sshclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
// aes256-gcm@openssh.com is a cipher OpenSSH (and 1Password key exports) may
|
||||||
|
// use for private key files. golang.org/x/crypto/ssh only supports aes256-ctr
|
||||||
|
// and aes256-cbc, so these keys are decrypted here and re-marshalled as
|
||||||
|
// unencrypted openssh-key-v1 blobs that ssh.ParsePrivateKey accepts.
|
||||||
|
const (
|
||||||
|
opensshKeyMagic = "openssh-key-v1\x00"
|
||||||
|
opensshGCMCipher = "aes256-gcm@openssh.com"
|
||||||
|
gcmTagSize = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
type opensshPrivateKeyHeader struct {
|
||||||
|
CipherName string
|
||||||
|
KdfName string
|
||||||
|
KdfOpts string
|
||||||
|
NumKeys uint32
|
||||||
|
PubKey []byte
|
||||||
|
PrivKeyBlock []byte
|
||||||
|
// For AEAD ciphers (aes256-gcm@openssh.com) OpenSSH stores the 16-byte
|
||||||
|
// auth tag as raw bytes after the encrypted block, not inside it.
|
||||||
|
Rest []byte `ssh:"rest"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// isGCMEncryptedKey reports whether data is an OpenSSH private key encrypted
|
||||||
|
// with aes256-gcm@openssh.com.
|
||||||
|
func isGCMEncryptedKey(data []byte) bool {
|
||||||
|
block, _ := pem.Decode(data)
|
||||||
|
if block == nil || block.Type != "OPENSSH PRIVATE KEY" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(block.Bytes) < len(opensshKeyMagic) || string(block.Bytes[:len(opensshKeyMagic)]) != opensshKeyMagic {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var header struct {
|
||||||
|
CipherName string
|
||||||
|
Rest []byte `ssh:"rest"`
|
||||||
|
}
|
||||||
|
if err := ssh.Unmarshal(block.Bytes[len(opensshKeyMagic):], &header); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return header.CipherName == opensshGCMCipher
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseGCMKey decrypts an aes256-gcm@openssh.com encrypted OpenSSH private
|
||||||
|
// key with passphrase and returns the signer.
|
||||||
|
func parseGCMKey(data, passphrase []byte) (ssh.Signer, error) {
|
||||||
|
block, _ := pem.Decode(data)
|
||||||
|
if block == nil || len(block.Bytes) < len(opensshKeyMagic) {
|
||||||
|
return nil, errors.New("invalid OpenSSH private key PEM")
|
||||||
|
}
|
||||||
|
var w opensshPrivateKeyHeader
|
||||||
|
if err := ssh.Unmarshal(block.Bytes[len(opensshKeyMagic):], &w); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if w.NumKeys != 1 {
|
||||||
|
return nil, errors.New("multi-key files are not supported")
|
||||||
|
}
|
||||||
|
if w.KdfName != "bcrypt" {
|
||||||
|
return nil, fmt.Errorf("unknown KDF %q, only supports %q", w.KdfName, "bcrypt")
|
||||||
|
}
|
||||||
|
var kdfOpts struct {
|
||||||
|
Salt string
|
||||||
|
Rounds uint32
|
||||||
|
}
|
||||||
|
if err := ssh.Unmarshal([]byte(w.KdfOpts), &kdfOpts); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Same cap as x/crypto/ssh so a hostile file cannot tie up the caller.
|
||||||
|
const maxRounds = 1 << 11
|
||||||
|
if kdfOpts.Rounds > maxRounds {
|
||||||
|
return nil, fmt.Errorf("bcrypt KDF rounds %d exceed maximum %d", kdfOpts.Rounds, maxRounds)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AES-256-GCM: 32-byte key + 12-byte nonce derived by bcrypt_pbkdf.
|
||||||
|
// The 16-byte auth tag follows the encrypted block as raw bytes;
|
||||||
|
// gcm.Open expects it appended to the ciphertext.
|
||||||
|
if len(w.Rest) != gcmTagSize {
|
||||||
|
return nil, fmt.Errorf("unexpected GCM tag length %d", len(w.Rest))
|
||||||
|
}
|
||||||
|
k, err := bcryptPBKDF(passphrase, []byte(kdfOpts.Salt), int(kdfOpts.Rounds), 32+12)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
aesCipher, err := aes.NewCipher(k[:32])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
gcm, err := cipher.NewGCM(aesCipher)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
sealed := make([]byte, 0, len(w.PrivKeyBlock)+gcmTagSize)
|
||||||
|
sealed = append(sealed, w.PrivKeyBlock...)
|
||||||
|
sealed = append(sealed, w.Rest...)
|
||||||
|
plain, err := gcm.Open(nil, k[32:], sealed, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-marshal as an unencrypted key (tag dropped). The plaintext keeps
|
||||||
|
// its original OpenSSH padding, which ssh.ParsePrivateKey validates.
|
||||||
|
w.CipherName = "none"
|
||||||
|
w.KdfName = "none"
|
||||||
|
w.KdfOpts = ""
|
||||||
|
w.PrivKeyBlock = plain
|
||||||
|
w.Rest = nil
|
||||||
|
raw := make([]byte, 0, len(opensshKeyMagic)+len(plain)+128)
|
||||||
|
raw = append(raw, opensshKeyMagic...)
|
||||||
|
raw = append(raw, ssh.Marshal(&w)...)
|
||||||
|
return ssh.ParsePrivateKey(pem.EncodeToMemory(&pem.Block{Type: "OPENSSH PRIVATE KEY", Bytes: raw}))
|
||||||
|
}
|
||||||
|
|
@ -9,8 +9,8 @@ import (
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/janvete/caddyview/internal/parser"
|
"git.janvete.com/seth/caddyview/internal/parser"
|
||||||
"github.com/janvete/caddyview/internal/sshclient"
|
"git.janvete.com/seth/caddyview/internal/sshclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
// -- tabs ---------------------------------------------------------------------
|
// -- tabs ---------------------------------------------------------------------
|
||||||
|
|
@ -132,6 +132,8 @@ type model struct {
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
err error
|
err error
|
||||||
|
|
||||||
|
errorsScrollOffset int
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialModel(client *sshclient.Client, logPath string) model {
|
func initialModel(client *sshclient.Client, logPath string) model {
|
||||||
|
|
@ -233,6 +235,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
m.width = msg.Width
|
m.width = msg.Width
|
||||||
m.height = msg.Height
|
m.height = msg.Height
|
||||||
|
|
||||||
|
case tea.MouseMsg:
|
||||||
|
if m.activeTab == tabErrors {
|
||||||
|
switch msg.Button {
|
||||||
|
case tea.MouseButtonWheelUp:
|
||||||
|
if m.errorsScrollOffset > 0 {
|
||||||
|
m.errorsScrollOffset--
|
||||||
|
}
|
||||||
|
case tea.MouseButtonWheelDown:
|
||||||
|
m.errorsScrollOffset++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "q", "ctrl+c":
|
case "q", "ctrl+c":
|
||||||
|
|
@ -240,14 +254,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case "1":
|
case "1":
|
||||||
m.activeTab = tabLive
|
m.activeTab = tabLive
|
||||||
|
m.errorsScrollOffset = 0
|
||||||
case "2":
|
case "2":
|
||||||
m.activeTab = tabDay
|
m.activeTab = tabDay
|
||||||
|
m.errorsScrollOffset = 0
|
||||||
return m, m.ensureHistory(tabDay)
|
return m, m.ensureHistory(tabDay)
|
||||||
case "3":
|
case "3":
|
||||||
m.activeTab = tabWeek
|
m.activeTab = tabWeek
|
||||||
|
m.errorsScrollOffset = 0
|
||||||
return m, m.ensureHistory(tabWeek)
|
return m, m.ensureHistory(tabWeek)
|
||||||
case "4":
|
case "4":
|
||||||
m.activeTab = tabErrors
|
m.activeTab = tabErrors
|
||||||
|
m.errorsScrollOffset = 0
|
||||||
return m, m.ensureHistory(tabDay)
|
return m, m.ensureHistory(tabDay)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -290,6 +308,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
h.stats = msg.agg.ToStats()
|
h.stats = msg.agg.ToStats()
|
||||||
h.buckets = msg.agg.ToBuckets(tabBucketSize(msg.tab))
|
h.buckets = msg.agg.ToBuckets(tabBucketSize(msg.tab))
|
||||||
h.errors = msg.agg.RecentErrors(100)
|
h.errors = msg.agg.RecentErrors(100)
|
||||||
|
m.errorsScrollOffset = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
case errMsg:
|
case errMsg:
|
||||||
|
|
@ -506,8 +525,34 @@ func (m model) renderErrors() string {
|
||||||
ipW, "REMOTE IP",
|
ipW, "REMOTE IP",
|
||||||
"URI",
|
"URI",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Fixed-height overhead: header + tab bar + title/header rows + footer/borders.
|
||||||
|
const overhead = 10
|
||||||
|
maxRows := m.height - overhead
|
||||||
|
if maxRows < 3 {
|
||||||
|
maxRows = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clamp scroll offset.
|
||||||
|
if m.errorsScrollOffset > len(errs)-maxRows {
|
||||||
|
if len(errs) > maxRows {
|
||||||
|
m.errorsScrollOffset = len(errs) - maxRows
|
||||||
|
} else {
|
||||||
|
m.errorsScrollOffset = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.errorsScrollOffset < 0 {
|
||||||
|
m.errorsScrollOffset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
end := m.errorsScrollOffset + maxRows
|
||||||
|
if end > len(errs) {
|
||||||
|
end = len(errs)
|
||||||
|
}
|
||||||
|
visible := errs[m.errorsScrollOffset:end]
|
||||||
|
|
||||||
rows := []string{styleHeader.Render(title), "", styleHeader.Render(header)}
|
rows := []string{styleHeader.Render(title), "", styleHeader.Render(header)}
|
||||||
for _, e := range errs {
|
for _, e := range visible {
|
||||||
statusStr := fmt.Sprintf("%d", e.Status)
|
statusStr := fmt.Sprintf("%d", e.Status)
|
||||||
var statusStyled string
|
var statusStyled string
|
||||||
if e.Status >= 500 {
|
if e.Status >= 500 {
|
||||||
|
|
@ -732,7 +777,7 @@ func bucketSizeLabel(d time.Duration) string {
|
||||||
// -- entry point --------------------------------------------------------------
|
// -- entry point --------------------------------------------------------------
|
||||||
|
|
||||||
func Run(client *sshclient.Client, logPath string) error {
|
func Run(client *sshclient.Client, logPath string) error {
|
||||||
p := tea.NewProgram(initialModel(client, logPath), tea.WithAltScreen())
|
p := tea.NewProgram(initialModel(client, logPath), tea.WithAltScreen(), tea.WithMouseCellMotion())
|
||||||
_, err := p.Run()
|
_, err := p.Run()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -1,6 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/janvete/caddyview/cmd"
|
import "git.janvete.com/seth/caddyview/cmd"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue