logo
0
0
Login
修复agent forwarding不工作的问题: 实现SSH agent forwarding功能,支持在exec命令和shell会话中转发SSH agent连接

Simple SSH Server

This is a simple SSH server implementation using the gliderlabs/ssh library with timeout functionality.

Project Structure

. ├── main.go ├── main_test.go ├── go.mod ├── go.sum └── README.md

Getting Started

Prerequisites

  • Go 1.25 or higher

Building

go build -o solo

Running

# Run with default settings (0.0.0.0:8822) ./solo # Run with custom address ./solo -addr "127.0.0.1:2222" # Run with custom timeouts ./solo -max-timeout=60s -idle-timeout=20s # Run with agent forwarding disabled ./solo -enable-agent=false # Run with PTY terminal support disabled ./solo -enable-pty=false # Run with a custom authorized keys file ./solo -publickey-file="/path/to/authorized_keys" # Run in daemon mode (background) ./solo -daemon # Run with debug mode ./solo -debug # Use environment variables SOLO_ADDR="127.0.0.1:2222" SOLO_MAX_TIMEOUT=60s SOLO_IDLE_TIMEOUT=20s SOLO_ENABLE_AGENT=false SOLO_ENABLE_PTY=false ./solo

Configuration

By default, the server looks for an authorized keys file at $HOME/.config/solo/authorized_keys. If this file doesn't exist, the server will create the directory structure automatically. You can override this location using the -publickey-file flag or the SOLO_PUBLICKEY_FILE environment variable.

Testing

go test -v

Usage

Once the server is running, you can connect to it using any SSH client:

Shell Session

ssh username@localhost -p 8822

This will start an interactive shell session. If PTY is enabled (default), you'll get a fully interactive terminal.

Exec Command

ssh username@localhost -p 8822 "ls -la"

This will execute the specified command and return the output. The server properly handles exit codes for commands.

SFTP

If SFTP is enabled (default), you can also connect using SFTP:

sftp -P 8822 username@localhost

Timeout Settings

  • max-timeout: Maximum duration for a connection (default: 3000s)
  • idle-timeout: Timeout for idle connections (default: 300s)

When a connection exceeds these limits, it will be automatically closed by the server.

Agent Forwarding

  • enable-agent: Enable SSH agent forwarding (default: true)

When enabled, the server will forward SSH agent connections if requested by the client. This allows clients to use their local SSH keys for authentication with other servers.

To test agent forwarding, you can use the SSH client with the -A flag:

ssh -A -p 8822 username@localhost

Once connected, you can verify that agent forwarding is working by checking if the SSH_AUTH_SOCK environment variable is set:

echo $SSH_AUTH_SOCK

You can also use ssh-add -l to list the keys loaded in your agent:

ssh-add -l

Exec/Shell Support

The server now fully supports both exec commands and shell sessions:

  • Exec commands: Run one-off commands like ssh user@host "ls -la"
  • Shell sessions: Interactive shell sessions with full environment support
  • Proper exit code handling for all commands
  • Full environment variable support inherited from the SSH session

Daemon Mode

  • daemon: Run the server in the background as a daemon (default: false)

When enabled, the server will fork itself to run in the background, redirecting all standard input/output to /dev/null. The parent process will print the PID of the daemon and exit.

PTY Terminal Support

  • enable-pty: Enable PTY terminal support (default: true)

When enabled, the server will provide a PTY terminal when requested by the client. This allows clients to run interactive commands like bash, top, vim, etc. When a PTY is requested, the server launches bash in a pseudo-terminal for a full interactive shell experience.

License

MIT