2023-11-19 13:06:38 +03:00
|
|
|
package sshtun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Neur0toxine/sshpoke/pkg/proto/ssh"
|
|
|
|
"github.com/function61/gokit/app/backoff"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SessionCallback func(*ssh.Session)
|
|
|
|
|
|
|
|
var NoopSessionCallback SessionCallback = func(*ssh.Session) {}
|
|
|
|
|
|
|
|
type Tunnel struct {
|
|
|
|
user string
|
|
|
|
address Endpoint
|
|
|
|
authMethods []ssh.AuthMethod
|
|
|
|
log *zap.SugaredLogger
|
2023-11-19 17:03:12 +03:00
|
|
|
tunConfig TunnelConfig
|
2023-11-19 13:06:38 +03:00
|
|
|
connected atomic.Bool
|
|
|
|
}
|
|
|
|
|
2023-11-19 17:03:12 +03:00
|
|
|
type TunnelConfig struct {
|
|
|
|
Forward Forward
|
2023-11-20 22:44:14 +03:00
|
|
|
HostKeyCallback ssh.HostKeyCallback
|
2023-11-21 20:02:07 +03:00
|
|
|
Shell *BoolOrStr
|
2023-11-20 22:23:58 +03:00
|
|
|
ClientVersion string
|
2023-11-19 13:06:38 +03:00
|
|
|
FakeRemoteHost bool
|
|
|
|
KeepAliveInterval uint
|
|
|
|
KeepAliveMax uint
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:23:58 +03:00
|
|
|
type BoolOrStr string
|
|
|
|
|
2023-11-21 20:02:07 +03:00
|
|
|
func (b *BoolOrStr) IsBool() bool {
|
|
|
|
if b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
v := *b
|
|
|
|
return v == "true" || v == "false" || v == "1" || v == "0" || v == ""
|
2023-11-20 22:23:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 20:02:07 +03:00
|
|
|
func (b *BoolOrStr) Falsy() bool {
|
|
|
|
if b == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
v := *b
|
|
|
|
return v == "" || v == "0" || v == "false"
|
2023-11-20 22:23:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 20:02:07 +03:00
|
|
|
func (b *BoolOrStr) String() string {
|
|
|
|
if b == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(*b)
|
2023-11-20 22:23:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 17:03:12 +03:00
|
|
|
func New(address, user string, auth []ssh.AuthMethod, sc TunnelConfig, log *zap.SugaredLogger) *Tunnel {
|
2023-11-19 13:06:38 +03:00
|
|
|
return &Tunnel{
|
|
|
|
address: AddrToEndpoint(address),
|
|
|
|
user: user,
|
|
|
|
authMethods: auth,
|
2023-11-19 17:03:12 +03:00
|
|
|
tunConfig: sc,
|
2023-11-19 13:06:38 +03:00
|
|
|
log: log.With(zap.String("sshServer", address)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 18:55:04 +03:00
|
|
|
func (t *Tunnel) Connect(ctx context.Context,
|
|
|
|
hsLineCb func(msg string), bannerCb ssh.BannerCallback, sessionCb SessionCallback) {
|
2023-11-19 13:06:38 +03:00
|
|
|
if t.connected.Load() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer t.connected.Store(false)
|
|
|
|
backoffTime := backoff.ExponentialWithCappedMax(100*time.Millisecond, 5*time.Second)
|
|
|
|
for {
|
|
|
|
t.connected.Store(true)
|
2023-11-21 18:55:04 +03:00
|
|
|
err := t.connect(ctx, hsLineCb, bannerCb, sessionCb)
|
2023-11-19 13:06:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.log.Error("connect error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(backoffTime())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect once to the SSH server. if the connection breaks, we return error and the caller
|
|
|
|
// will try to re-connect
|
2023-11-21 18:55:04 +03:00
|
|
|
func (t *Tunnel) connect(ctx context.Context,
|
|
|
|
hsLineCb func(string), bannerCb ssh.BannerCallback, sessionCb SessionCallback) error {
|
2023-11-19 13:06:38 +03:00
|
|
|
t.log.Debug("connecting")
|
|
|
|
sshConfig := &ssh.ClientConfig{
|
2023-11-21 18:55:04 +03:00
|
|
|
Config: ssh.Config{
|
|
|
|
HandshakePacketReader: newHandshakePerLineReader(hsLineCb),
|
|
|
|
},
|
2023-11-19 13:06:38 +03:00
|
|
|
User: t.user,
|
|
|
|
Auth: t.authMethods,
|
2023-11-20 22:44:14 +03:00
|
|
|
HostKeyCallback: t.tunConfig.HostKeyCallback,
|
2023-11-19 13:06:38 +03:00
|
|
|
BannerCallback: bannerCb,
|
2023-11-20 22:23:58 +03:00
|
|
|
ClientVersion: t.tunConfig.ClientVersion,
|
2023-11-19 13:06:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var sshClient *ssh.Client
|
|
|
|
var errConnect error
|
|
|
|
|
|
|
|
sshClient, errConnect = dialSSH(ctx, t.address.String(), sshConfig)
|
|
|
|
if errConnect != nil {
|
|
|
|
return errConnect
|
|
|
|
}
|
|
|
|
|
|
|
|
defer sshClient.Close()
|
|
|
|
defer t.log.Debug("disconnecting")
|
|
|
|
|
|
|
|
t.log.Debug("connected")
|
|
|
|
listenerStopped := make(chan error)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
sess, err := sshClient.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
t.log.Errorf("session error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
if sessionCb == nil {
|
|
|
|
sessionCb = func(*ssh.Session) {}
|
|
|
|
}
|
|
|
|
|
2023-11-21 20:02:07 +03:00
|
|
|
if t.tunConfig.Shell == nil || !t.tunConfig.Shell.Falsy() {
|
|
|
|
if t.tunConfig.Shell == nil || t.tunConfig.Shell.IsBool() {
|
2023-11-20 22:23:58 +03:00
|
|
|
if err := sess.Shell(); err != nil {
|
|
|
|
t.log.Warnf("failed to start empty shell: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := sess.Start(t.tunConfig.Shell.String()); err != nil {
|
|
|
|
t.log.Warnf("failed to start shell '%s': %s", t.tunConfig.Shell, err.Error())
|
|
|
|
}
|
2023-11-19 14:12:39 +03:00
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_ = sess.Wait()
|
2023-11-24 22:01:53 +03:00
|
|
|
t.log.Debug("main session has been terminated")
|
2023-11-19 14:12:39 +03:00
|
|
|
}()
|
|
|
|
}
|
2023-11-19 13:06:38 +03:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
sessionCb(sess)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
reverseErr := make(chan error)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
reverseErr <- t.reverseForwardOnePort(sshClient, listenerStopped)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go t.keepAlive(ctx, sshClient, &wg)
|
|
|
|
|
|
|
|
if err := <-reverseErr; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case listenerFirstErr := <-listenerStopped:
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return listenerFirstErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialSSH(ctx context.Context, addr string, sshConfig *ssh.ClientConfig) (*ssh.Client, error) {
|
|
|
|
dialer := net.Dialer{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := dialer.DialContext(ctx, "tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
clConn, newChan, reqChan, err := ssh.NewClientConn(conn, addr, sshConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssh.NewClient(clConn, newChan, reqChan), nil
|
|
|
|
}
|