Skip to content

Commit

Permalink
feat: using our fork (#101)
Browse files Browse the repository at this point in the history
* Revert "fix: using our ssh fork (#95)"

This reverts commit a14cf6d.

* Revert "refactor: type aliasing ssh types (#94)"

This reverts commit f7f0c95.

* feat: using our fork

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 22, 2022
1 parent d2f4099 commit d65a162
Show file tree
Hide file tree
Showing 38 changed files with 253 additions and 317 deletions.
5 changes: 3 additions & 2 deletions accesscontrol/accesscontrol.go
Expand Up @@ -4,14 +4,15 @@ package accesscontrol
import (
"fmt"

"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
)

// Middleware will exit 1 connections trying to execute commands that are not allowed.
// If no allowed commands are provided, no commands will be allowed.
func Middleware(cmds ...string) wish.Middleware {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
if len(s.Command()) == 0 {
sh(s)
return
Expand Down
10 changes: 5 additions & 5 deletions accesscontrol/accesscontrol_test.go
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"testing"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish/accesscontrol"
"github.com/charmbracelet/wish/testsession"
"golang.org/x/crypto/ssh"
gossh "golang.org/x/crypto/ssh"
)

const out = "hello world"
Expand Down Expand Up @@ -77,10 +77,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB, allowedCmds ...string) *ssh.Session {
func setup(tb testing.TB, allowedCmds ...string) *gossh.Session {
tb.Helper()
return testsession.New(tb, &wish.Server{
Handler: accesscontrol.Middleware(allowedCmds...)(func(s wish.Session) {
return testsession.New(tb, &ssh.Server{
Handler: accesscontrol.Middleware(allowedCmds...)(func(s ssh.Session) {
s.Write([]byte(out))
}),
}, nil)
Expand Down
5 changes: 3 additions & 2 deletions activeterm/activeterm.go
Expand Up @@ -4,13 +4,14 @@ package activeterm
import (
"fmt"

"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
)

// Middleware will exit 1 connections trying with no active terminals.
func Middleware() wish.Middleware {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
_, _, active := s.Pty()
if !active {
fmt.Fprintln(s, "Requires an active PTY")
Expand Down
10 changes: 5 additions & 5 deletions activeterm/activeterm_test.go
Expand Up @@ -3,10 +3,10 @@ package activeterm_test
import (
"testing"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish/activeterm"
"github.com/charmbracelet/wish/testsession"
"golang.org/x/crypto/ssh"
gossh "golang.org/x/crypto/ssh"
)

func TestMiddleware(t *testing.T) {
Expand All @@ -21,10 +21,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB) *ssh.Session {
func setup(tb testing.TB) *gossh.Session {
tb.Helper()
return testsession.New(tb, &wish.Server{
Handler: activeterm.Middleware()(func(s wish.Session) {
return testsession.New(tb, &ssh.Server{
Handler: activeterm.Middleware()(func(s ssh.Session) {
s.Write([]byte("hello"))
}),
}, nil)
Expand Down
11 changes: 6 additions & 5 deletions bubbletea/tea.go
Expand Up @@ -6,6 +6,7 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/muesli/termenv"
)
Expand All @@ -20,7 +21,7 @@ type BubbleTeaHandler = Handler // nolint: revive
// Handler is the function Bubble Tea apps implement to hook into the
// SSH Middleware. This will create a new tea.Program for every connection and
// start it with the tea.ProgramOptions returned.
type Handler func(wish.Session) (tea.Model, []tea.ProgramOption)
type Handler func(ssh.Session) (tea.Model, []tea.ProgramOption)

// ProgramHandler is the function Bubble Tea apps implement to hook into the SSH
// Middleware. This should return a new tea.Program. This handler is different
Expand All @@ -29,7 +30,7 @@ type Handler func(wish.Session) (tea.Model, []tea.ProgramOption)
//
// Make sure to set the tea.WithInput and tea.WithOutput to the ssh.Session
// otherwise the program will not function properly.
type ProgramHandler func(wish.Session) *tea.Program
type ProgramHandler func(ssh.Session) *tea.Program

// Middleware takes a Handler and hooks the input and output for the
// ssh.Session into the tea.Program. It also captures window resize events and
Expand All @@ -44,7 +45,7 @@ func Middleware(bth Handler) wish.Middleware {
// by an SSH client's terminal cannot be detected by the server but this will
// allow for manually setting the color profile on all SSH connections.
func MiddlewareWithColorProfile(bth Handler, cp termenv.Profile) wish.Middleware {
h := func(s wish.Session) *tea.Program {
h := func(s ssh.Session) *tea.Program {
m, opts := bth(s)
if m == nil {
return nil
Expand All @@ -63,9 +64,9 @@ func MiddlewareWithColorProfile(bth Handler, cp termenv.Profile) wish.Middleware
// Make sure to set the tea.WithInput and tea.WithOutput to the ssh.Session
// otherwise the program will not function properly.
func MiddlewareWithProgramHandler(bth ProgramHandler, cp termenv.Profile) wish.Middleware {
return func(sh wish.Handler) wish.Handler {
return func(sh ssh.Handler) ssh.Handler {
lipgloss.SetColorProfile(cp)
return func(s wish.Session) {
return func(s ssh.Session) {
p := bth(s)
if p != nil {
_, windowChanges, _ := s.Pty()
Expand Down
9 changes: 6 additions & 3 deletions comment/comment.go
@@ -1,11 +1,14 @@
package comment

import "github.com/charmbracelet/wish"
import (
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
)

// Middleware prints a comment at the end of the session.
func Middleware(comment string) wish.Middleware {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
sh(s)
wish.Println(s, comment)
}
Expand Down
10 changes: 5 additions & 5 deletions comment/comment_test.go
Expand Up @@ -3,9 +3,9 @@ package comment
import (
"testing"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish/testsession"
"golang.org/x/crypto/ssh"
gossh "golang.org/x/crypto/ssh"
)

func TestMiddleware(t *testing.T) {
Expand All @@ -18,10 +18,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB) *ssh.Session {
func setup(tb testing.TB) *gossh.Session {
tb.Helper()
return testsession.New(tb, &wish.Server{
Handler: Middleware("test")(func(s wish.Session) {}),
return testsession.New(tb, &ssh.Server{
Handler: Middleware("test")(func(s ssh.Session) {}),
}, nil)
}

Expand Down
5 changes: 3 additions & 2 deletions elapsed/elapsed.go
Expand Up @@ -3,6 +3,7 @@ package timer
import (
"time"

"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
)

Expand All @@ -11,8 +12,8 @@ import (
//
// This must be called as the last middleware in the chain.
func MiddlewareWithFormat(format string) wish.Middleware {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
now := time.Now()
sh(s)
wish.Printf(s, format, time.Since(now))
Expand Down
10 changes: 5 additions & 5 deletions elapsed/elapsed_test.go
Expand Up @@ -4,9 +4,9 @@ import (
"testing"
"time"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish/testsession"
"golang.org/x/crypto/ssh"
gossh "golang.org/x/crypto/ssh"
)

var waitDuration = time.Second
Expand All @@ -23,10 +23,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB) *ssh.Session {
func setup(tb testing.TB) *gossh.Session {
tb.Helper()
return testsession.New(tb, &wish.Server{
Handler: MiddlewareWithFormat("%v")(func(s wish.Session) {
return testsession.New(tb, &ssh.Server{
Handler: MiddlewareWithFormat("%v")(func(s ssh.Session) {
time.Sleep(waitDuration)
}),
}, nil)
Expand Down
3 changes: 2 additions & 1 deletion examples/bubbletea/main.go
Expand Up @@ -13,6 +13,7 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
Expand Down Expand Up @@ -58,7 +59,7 @@ func main() {
// handles the incoming ssh.Session. Here we just grab the terminal info and
// pass it to the new model. You can also return tea.ProgramOptions (such as
// tea.WithAltScreen) on a session by session basis.
func teaHandler(s wish.Session) (tea.Model, []tea.ProgramOption) {
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
pty, _, active := s.Pty()
if !active {
wish.Fatalln(s, "no active terminal, skipping")
Expand Down
3 changes: 2 additions & 1 deletion examples/bubbleteaprogram/main.go
Expand Up @@ -13,6 +13,7 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
Expand Down Expand Up @@ -68,7 +69,7 @@ func myCustomBubbleteaMiddleware() wish.Middleware {
}()
return p
}
teaHandler := func(s wish.Session) *tea.Program {
teaHandler := func(s ssh.Session) *tea.Program {
pty, _, active := s.Pty()
if !active {
wish.Fatalln(s, "no active terminal, skipping")
Expand Down
5 changes: 3 additions & 2 deletions examples/cobra/main.go
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"
"time"

"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/logging"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -47,8 +48,8 @@ func main() {
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithMiddleware(
func(h wish.Handler) wish.Handler {
return func(s wish.Session) {
func(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
rootCmd := cmd()
rootCmd.SetArgs(s.Command())
rootCmd.SetIn(s)
Expand Down
19 changes: 10 additions & 9 deletions examples/git/main.go
Expand Up @@ -13,6 +13,7 @@ import (
"syscall"
"time"

"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
gm "github.com/charmbracelet/wish/git"
lm "github.com/charmbracelet/wish/logging"
Expand All @@ -28,23 +29,23 @@ type app struct {
access gm.AccessLevel
}

func (a app) AuthRepo(repo string, pk wish.PublicKey) gm.AccessLevel {
func (a app) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {
return a.access
}

func (a app) Push(repo string, pk wish.PublicKey) {
func (a app) Push(repo string, pk ssh.PublicKey) {
log.Printf("pushed %s", repo)
}

func (a app) Fetch(repo string, pk wish.PublicKey) {
func (a app) Fetch(repo string, pk ssh.PublicKey) {
log.Printf("fetch %s", repo)
}

func passHandler(ctx wish.Context, password string) bool {
func passHandler(ctx ssh.Context, password string) bool {
return false
}

func pkHandler(ctx wish.Context, key wish.PublicKey) bool {
func pkHandler(ctx ssh.Context, key ssh.PublicKey) bool {
return true
}

Expand All @@ -53,8 +54,8 @@ func main() {
a := app{gm.ReadWriteAccess}

s, err := wish.NewServer(
wish.WithPublicKeyAuth(pkHandler),
wish.WithPasswordAuth(passHandler),
ssh.PublicKeyAuth(pkHandler),
ssh.PasswordAuth(passHandler),
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
wish.WithHostKeyPath(".ssh/git_server_ed25519"),
wish.WithMiddleware(
Expand Down Expand Up @@ -88,8 +89,8 @@ func main() {
// Normally we would use a Bubble Tea program for the TUI but for simplicity,
// we'll just write a list of the pushed repos to the terminal and exit the ssh
// session.
func gitListMiddleware(h wish.Handler) wish.Handler {
return func(s wish.Session) {
func gitListMiddleware(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
// Git will have a command included so only run this if there are no
// commands passed to ssh.
if len(s.Command()) == 0 {
Expand Down
12 changes: 6 additions & 6 deletions examples/go.mod
Expand Up @@ -4,10 +4,10 @@ go 1.18

require (
github.com/charmbracelet/bubbletea v0.23.1
github.com/charmbracelet/ssh v0.0.0-20221117183211-483d43d97103
github.com/charmbracelet/wish v0.5.0
github.com/muesli/termenv v0.13.0
github.com/spf13/cobra v1.5.0
golang.org/x/crypto v0.3.0
)

require (
Expand All @@ -19,7 +19,6 @@ require (
github.com/caarlos0/sshmarshal v0.1.0 // indirect
github.com/charmbracelet/keygen v0.3.0 // indirect
github.com/charmbracelet/lipgloss v0.6.0 // indirect
github.com/charmbracelet/ssh v0.0.0-20221117183211-483d43d97103 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
Expand All @@ -41,10 +40,11 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

Expand Down

0 comments on commit d65a162

Please sign in to comment.