ginuerzh/gost · error

ssh: wrong connection type

Error message

ssh: wrong connection type

What it means

Returned by sshDirectForwardConnector.ConnectContext when the supplied conn is not the expected *sshNopConn wrapper (an ugly but deliberate type check), meaning the connection cannot be used to open a direct-tcpip SSH channel. Additionally, UDP network values are rejected outright with a '%s unsupported' error from the same switch. Both are caller misuse of the connector.

Source

Thrown at ssh.go:84

func (c *sshDirectForwardConnector) Connect(conn net.Conn, raddr string, options ...ConnectOption) (net.Conn, error) {
	return c.ConnectContext(context.Background(), conn, "tcp", raddr, options...)
}

func (c *sshDirectForwardConnector) ConnectContext(ctx context.Context, conn net.Conn, network, raddr string, options ...ConnectOption) (net.Conn, error) {
	switch network {
	case "udp", "udp4", "udp6":
		return nil, fmt.Errorf("%s unsupported", network)
	}

	opts := &ConnectOptions{}
	for _, option := range options {
		option(opts)
	}

	cc, ok := conn.(*sshNopConn) // TODO: this is an ugly type assertion, need to find a better solution.
	if !ok {
		return nil, errors.New("ssh: wrong connection type")
	}

	timeout := opts.Timeout
	if timeout <= 0 {
		timeout = ConnectTimeout
	}

	cc.session.conn.SetDeadline(time.Now().Add(timeout))
	defer cc.session.conn.SetDeadline(time.Time{})

	conn, err := cc.session.client.Dial("tcp", raddr)
	if err != nil {
		log.Logf("[ssh-tcp] %s -> %s : %s", cc.session.addr, raddr, err)
		return nil, err
	}
	return conn, nil
}

View on GitHub (pinned to a33fdbf4c9)

Solutions

  1. Pass the *sshNopConn produced by the SSH forward transporter into the connector
  2. Use a tcp-family network value; SSH direct forwarding does not carry UDP
  3. For UDP over SSH, use a connector that tunnels UDP inside an SSH channel instead
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at ssh.go:84 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ginuerzh/gost@a33fdbf4c9 (2026-09-02). Data as JSON: /api/errors/764db02aa13de51e. Report an issue: GitHub.