forwarder

package module
v0.0.0-...-3256fd0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

Forward unix domain socket file over SSH

Origin code from daytona

This is a simple implementation of a forward UDF over SSH. It is used to forward a UDF server to a remote machine.

package main

import (
	"context"

	"github.com/sirupsen/logrus"

	"oosshagent/pkg/forwarder"
)

func main() {
	myForwarder := forwarder.NewUnixRemote("/var/folders/dt/wqkv0wf13nl6n8jbf98jggjh0000gn/T/ssh-px37G3xkyelq/agent.4973", "192.168.1.250", "/tmp/my_socks")
	myForwarder.SetTunneledConnState(func(tun *forwarder.ForwardConfig, state *forwarder.TunneledConnState) {
		logrus.Infof("%v", state)
	})

	myForwarder.SetKeyFile("/Users/danhexon/.ssh/id_ed25519")
	myForwarder.SetUser("ihexon")
	myForwarder.SetPort(22)

	// We set a callback to know when the tunnel is ready
	myForwarder.SetConnState(func(tun *forwarder.ForwardConfig, state forwarder.ConnState) {
		switch state {
		case forwarder.StateStarting:
			logrus.Infoln("STATE is Starting")
		case forwarder.StateStarted:
			logrus.Infoln("STATE is Started")
		case forwarder.StateStopped:
			logrus.Infoln("STATE is Stopped")
		}
	})

	if err := myForwarder.Start(context.Background()); err != nil {
		logrus.Infof("SSH tunnel error: %v", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnState

type ConnState int
const (
	// StateStopped represents a stopped tunnel. A call to Start will make the state to transition to StateStarting.
	StateStopped ConnState = iota

	// StateStarting represents a tunnel initializing and preparing to listen for connections.
	// A successful initialization will make the state to transition to StateStarted, otherwise it will transition to StateStopped.
	StateStarting

	// StateStarted represents a tunnel ready to accept connections.
	// A call to stop or an error will make the state to transition to StateStopped.
	StateStarted
)

type Endpoint

type Endpoint struct {
	// contains filtered or unexported fields
}

func NewServerEndpoint

func NewServerEndpoint(host string, port int) *Endpoint

func NewUnixEndpoint

func NewUnixEndpoint(socket string) *Endpoint

func (*Endpoint) String

func (e *Endpoint) String() string

func (*Endpoint) Type

func (e *Endpoint) Type() string

type ForwardConfig

type ForwardConfig struct {
	User   string
	Server *Endpoint

	Local  *Endpoint
	Remote *Endpoint

	SSHClient *ssh.Client
	SSHConfig *ssh.ClientConfig
	// contains filtered or unexported fields
}

func NewUnix

func NewUnix(localUnixSocket string, server string, remoteUnixSocket string) *ForwardConfig

NewUnix does the same as New but using unix sockets.

func NewUnixRemote

func NewUnixRemote(localUnixSocket string, server string, remoteUnixSocket string) *ForwardConfig

NewUnixRemote does the same as NewRemote but using unix sockets.

func (*ForwardConfig) CleanTargetSocketFile

func (tun *ForwardConfig) CleanTargetSocketFile() error

CleanTargetSocketFile delete the target socket file before forward

func (*ForwardConfig) GetAliveConnCount

func (tun *ForwardConfig) GetAliveConnCount() int

func (*ForwardConfig) SetConnState

func (tun *ForwardConfig) SetConnState(connStateFun func(*ForwardConfig, ConnState))

SetConnState specifies an optional callback function that is called when a SSH tunnel changes state. See the ConnState type and associated constants for details.

func (*ForwardConfig) SetKeyFile

func (tun *ForwardConfig) SetKeyFile(file string) *ForwardConfig

SetKeyFile changes the authentication to key-based and uses the specified file. Leaving the file empty defaults to the default linux private key locations: `~/.ssh/id_rsa`, `~/.ssh/id_dsa`, `~/.ssh/id_ecdsa`, `~/.ssh/id_ecdsa_sk`, `~/.ssh/id_ed25519` and `~/.ssh/id_ed25519_sk`.

func (*ForwardConfig) SetPort

func (tun *ForwardConfig) SetPort(port int) *ForwardConfig

SetPort changes the port where the SSH connection will be made.

func (*ForwardConfig) SetTunneledConnState

func (tun *ForwardConfig) SetTunneledConnState(tunneledConnStateFun func(*ForwardConfig, *TunneledConnState))

func (*ForwardConfig) SetUser

func (tun *ForwardConfig) SetUser(user string) *ForwardConfig

SetUser changes the user used to make the SSH connection.

func (*ForwardConfig) Start

func (tun *ForwardConfig) Start(ctx context.Context) error

func (*ForwardConfig) Stop

func (tun *ForwardConfig) Stop()

Stop closes all connections and makes Start exit `gracefully`.

type ForwardType

type ForwardType int
const (
	Remote ForwardType = iota
)

type TunneledConnState

type TunneledConnState struct {
	// From is the address initating the connection.
	From string
	// Info holds a message with info on the state of the connection (useful for debug purposes).
	Info string
	// Error holds an error on the connection or nil if the connection is successful.
	Error error
	// Ready indicates if the connection is established.
	Ready bool
	// Closed indicates if the connection is closed.
	Closed bool
}

func (*TunneledConnState) String

func (s *TunneledConnState) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL