net

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocalIPAddr

func GetLocalIPAddr() (string, error)

GetLocalIPAddr 读取本机 IP 地址。

Types

type RichSSHClient added in v1.0.5

type RichSSHClient struct {
	Host           string
	Port           int
	User           string
	Password       string
	PrivateKeyFile string
	PrivateKey     []byte // PEM bytes; if both Password and PrivateKey provided, use PrivateKey
	JumpSSHHost    string
	JumpSSHPort    int
	ProxyURL       string

	EnablePTY bool // default false
	// contains filtered or unexported fields
}

func NewRichSSHClient added in v1.0.5

func NewRichSSHClient(host string, port int, user string, opts ...RichSSHClientOption) *RichSSHClient

NewRichSSHClient builds client with defaults and options.

func (*RichSSHClient) Close added in v1.0.5

func (c *RichSSHClient) Close()

Close closes everything.

func (*RichSSHClient) Connect added in v1.0.5

func (c *RichSSHClient) Connect(ctx context.Context) error

Connect establishes SSH connection (handles jump/proxy).

func (*RichSSHClient) DownloadFile added in v1.0.5

func (c *RichSSHClient) DownloadFile(ctx context.Context, remotePath, localPath string, progressWriter io.Writer) error

DownloadFile downloads remotePath -> localPath. If progressWriter != nil, it will be written with bytes transferred.

func (*RichSSHClient) Run added in v1.0.5

Run executes a command and returns output and exit code.

func (*RichSSHClient) RunStream added in v1.0.5

func (c *RichSSHClient) RunStream(ctx context.Context, cmd string, outWriter, errWriter io.Writer) error

RunStream runs command, piping stdout/stderr to the provided writers in real time. outWriter and errWriter can be nil. PTY respects EnablePTY.

func (*RichSSHClient) UploadFile added in v1.0.5

func (c *RichSSHClient) UploadFile(ctx context.Context, localPath, remotePath string, progressWriter io.Writer) error

UploadFile uploads localPath -> remotePath. If progressWriter != nil, it will be written with bytes transferred.

type RichSSHClientOption added in v1.0.5

type RichSSHClientOption func(c *RichSSHClient)

func WithDialTimeout added in v1.0.5

func WithDialTimeout(d time.Duration) RichSSHClientOption

func WithJumpHost added in v1.0.5

func WithJumpHost(host string, port int) RichSSHClientOption

func WithPTY added in v1.0.5

func WithPTY(enable bool) RichSSHClientOption

func WithPassword added in v1.0.5

func WithPassword(pw string) RichSSHClientOption

func WithPrivateKeyFile added in v1.0.5

func WithPrivateKeyFile(path string) RichSSHClientOption

func WithPrivateKeyPEM added in v1.0.5

func WithPrivateKeyPEM(pem []byte) RichSSHClientOption

func WithProxyURL added in v1.0.5

func WithProxyURL(proxyURL string) RichSSHClientOption

type RichSSHClientResponse added in v1.0.5

type RichSSHClientResponse struct {
	ExitCode int
	Stdout   []byte
	Stderr   []byte
}

type SSHClient

type SSHClient struct {
	// RSA 私钥证书路径或全文内容
	PrivateKey string
	// 主机 Domain/IP 地址
	Host string
	// SSH 端口
	Port int
	// SSH 登录用户名
	User string
	// SSH 登录密码
	Password string
	// 静默方式: 不输出 Stdout 信息
	Quiet bool
	// 是否已连接?
	Connected bool
	// SSH 客户端实例
	Client *ssh.Client
	// 代理服务器地址 (支持 http,https,socks5,socks5h, 例如: http://127.0.0.1:3128, socks5://127.0.0.1:1080)
	Proxy string
	// 超时时间 (默认不超时)
	Timeout time.Duration
	// 开启 TTY 终端模式
	TTY bool
	// 缓冲区大小 (默认: 8192 bytes)
	ChunkSize uint16
	// SSH 隧道
	Tunnel *SSHTunnel
	// Logger 接口对象
	Logger glog.Logger
}

SSHClient SSH 客户端。

func NewSSHClient

func NewSSHClient(host string, port int, user string, privateKey string, quiet bool, opts ...SSHClientOption) *SSHClient

func (*SSHClient) Close

func (s *SSHClient) Close() error

func (*SSHClient) Connect

func (s *SSHClient) Connect() error

func (*SSHClient) Download

func (s *SSHClient) Download(src, dst string) error

func (*SSHClient) Run

func (s *SSHClient) Run(command string) (int, error)

func (*SSHClient) RunWithWriter

func (s *SSHClient) RunWithWriter(command string, w io.Writer) (int, error)

func (*SSHClient) Upload

func (s *SSHClient) Upload(src, dst string) error

type SSHClientOption

type SSHClientOption func(*SSHClient)

func SSHOptionWithChunkSize

func SSHOptionWithChunkSize(chunkSize uint16) SSHClientOption

func SSHOptionWithLogger

func SSHOptionWithLogger(logger glog.Logger) SSHClientOption

func SSHOptionWithPassword

func SSHOptionWithPassword(password string) SSHClientOption

func SSHOptionWithProxy

func SSHOptionWithProxy(proxyURL string) SSHClientOption

SSHOptionWithProxy 支持 http/https/socks5/socks5h 代理协议。

func SSHOptionWithTimeout

func SSHOptionWithTimeout(timeout time.Duration) SSHClientOption

func SSHOptionWithTunnel

func SSHOptionWithTunnel(tunnel *SSHTunnel) SSHClientOption

type SSHTunnel

type SSHTunnel struct {
	// 本地
	Local *SSHTunnelEndpoint
	// 隧道主机
	Server *SSHTunnelEndpoint
	// 目标主机
	Remote *SSHTunnelEndpoint
	// SSH 客户端配置
	Config *ssh.ClientConfig
	// contains filtered or unexported fields
}

func (*SSHTunnel) Start

func (tunnel *SSHTunnel) Start(opened chan bool)

func (*SSHTunnel) Stop

func (tunnel *SSHTunnel) Stop()

type SSHTunnelEndpoint

type SSHTunnelEndpoint struct {
	Host string
	Port int
}

func (*SSHTunnelEndpoint) String

func (endpoint *SSHTunnelEndpoint) String() string

Jump to

Keyboard shortcuts

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