dongqian5384 2016-03-19 12:58
浏览 352
已采纳

如何将远程ssh服务器与socks代理连接?

package main

import (
    "errors"
    "fmt"
    "net"

    "golang.org/x/crypto/ssh"
)

var (
    socks    string = "127.0.0.1:8000"
    server   string = "192.168.1.1:2222"
    cmd      string = ""
    login    string = "root"
    password string = ""
)

func main() {
    c, err := netConn(socks)
    if err != nil {
        fmt.Println(err)
    }
    conf := &ssh.ClientConfig{
        User: login,
        Auth: []ssh.AuthMethod{
            ssh.Password(password),
        },
    }
    client, err := Dialer(conf, c, server)
    if err != nil {
        fmt.Println(err)
    }
    session, err := client.NewSession()
    defer session.Close()
    if err != nil {
        fmt.Println(err)
    }
    session.Run(cmd)
}

func netConn(addr string) (net.Conn, error) {
    conn, err := net.Dial("tcp", addr)
    if err != nil {
        return nil, errors.New(fmt.Sprintf("Unable to connect to %v", err))
    }
    return conn, nil
}

func Dialer(conf *ssh.ClientConfig, c net.Conn, host string) (*ssh.Client, error) {
    conn, chans, reqs, err := ssh.NewClientConn(c, host, conf)
    if err != nil {
        return nil, err
    }
    return ssh.NewClient(conn, chans, reqs), nil
}

Client panic

ssh: handshake failed: EOF

panic: runtime error: invalid memory address or nil pointer dereference

[signal 0xb code=0x1 addr=0x0 pc=0x80f3e2d]

goroutine 1 [running]:

panic(0x8201b60, 0x18522030)

/usr/lib/go/src/runtime/panic.go:464 +0x326

golang.org/x/crypto/ssh.(*Client).NewSession(0x0, 0x1, 0x0, 0x0)

/home/user/go/src/golang.org/x/crypto/ssh/client.go:129 +0xcd

main.main()

/home/user/go/src/ss/i.go:34 +0x276

And socks server in log

[ERR] socks: Unsupported SOCKS version: [83]

How i can make it posible? Thanks.

  • 写回答

2条回答 默认 最新

  • doujianjian2060 2018-01-15 15:55
    关注

    You can use the x/net/proxy package as follows:

    package main
    
    import (
        "log"
    
        "golang.org/x/crypto/ssh"
        "golang.org/x/net/proxy"
    )
    
    func main() {
    
        sshConfig := &ssh.ClientConfig{
            User: "user",
            // Auth: .... fill out with keys etc as normal
        }
    
        client, err := proxiedSSHClient("127.0.0.1:8000", "127.0.0.1:22", sshConfig)
        if err != nil {
            log.Fatal(err)
        }
    
        // get a session etc...
    }
    
    func proxiedSSHClient(proxyAddress, sshServerAddress string, sshConfig *ssh.ClientConfig) (*ssh.Client, error) {
        dialer, err := proxy.SOCKS5("tcp", proxyAddress, nil, proxy.Direct)
        if err != nil {
            return nil, err
        }
    
        conn, err := dialer.Dial("tcp", sshServerAddress)
        if err != nil {
            return nil, err
        }
    
        c, chans, reqs, err := ssh.NewClientConn(conn, sshServerAddress, sshConfig)
        if err != nil {
            return nil, err
        }
    
        return ssh.NewClient(c, chans, reqs), nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#目标检测#的问题:(qq收集表到时间才能填写,填写的份数有上限)
  • ¥50 ZYNQ7020双核FLAHS烧写的问题
  • ¥20 ue 5 中想要实现第一人称人物左右行走摆动的效果,摄像头只向右摆动一次(关键词-结点)
  • ¥15 钢岔管添加弹性约束模拟围岩作用
  • ¥15 AD9164瞬时带宽1.8G,怎么计算出来?
  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题