doutan5798 2016-06-24 00:19 采纳率: 100%
浏览 104
已采纳

带有GO的SSH反向隧道

I am trying to write a server / client that can help get around Firewalls / Nat Issues.

I noticed SSH has built into support for doing this already. (http://rustyrazorblade.com/2010/03/ssh-reverse-tunnel-to-access-box-behind-firewall/)

I tried a few different SSH examples and none seem to be working. I found one project that says it implemented the Remote Port Fowarding -> https://godoc.org/dev.justinjudd.org/justin/easyssh

The Server says it is Listening for connections but I am unable to SSH from Server Machine To Client Machine. (ssh localhost 8080 on remote machine should forward to client machine.

Client ->

package main

import (
  "log"

  "dev.justinjudd.org/justin/easyssh"
  "golang.org/x/crypto/ssh"
)

func main() {
  config := &ssh.ClientConfig{
    User: "test",
    Auth: []ssh.AuthMethod{
      ssh.Password("test"),
    },
  }


  conn, err := easyssh.Dial("tcp", "*SSH-SERVER*:22", config)
  if err != nil {
    log.Fatalf("unable to connect: %s", err)
  }
  defer conn.Close()

  err = conn.RemoteForward("0.0.0.0:8080", "127.0.0.1:22")
  if err != nil {
    log.Fatalf("unable to forward local port: %s", err)
  }

}

Server ->

package main

import (
  "fmt"
  "io/ioutil"
  "log"

  "dev.justinjudd.org/justin/easyssh"

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

func main() {

  privateBytes, err := ioutil.ReadFile("id_rsa")
  if err != nil {
    log.Fatal("Failed to load private key (./id_rsa)")
  }

  private, err := ssh.ParsePrivateKey(privateBytes)
  if err != nil {
    log.Fatal("Failed to parse private key")
  }

  config := &ssh.ServerConfig{
    PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
      if c.User() == "test" && string(pass) == "test" {
        log.Printf("User logged in: %s", c.User())
        return nil, nil
      }
      return nil, fmt.Errorf("password rejected for %s", c.User())
    },
  }
  config.AddHostKey(private)


  easyssh.HandleChannel(easyssh.SessionRequest, easyssh.SessionHandler())
  easyssh.HandleChannel(easyssh.DirectForwardRequest, easyssh.DirectPortForwardHandler())
  easyssh.HandleRequestFunc(easyssh.RemoteForwardRequest, easyssh.TCPIPForwardRequest)

  easyssh.ListenAndServe(":22", config, nil)
}
  • 写回答

2条回答 默认 最新

  • doupao6011 2016-06-29 15:30
    关注

    I'm not sure that you understand how SSH tunneling works looking on your code.

    You need to have SSH connectivity to the remote (server). Then you setup SSH tunnel so local TCP:8080 port will be forwarded to the remote server TCP:8080 port using SSH connection. Actual 8080 port can be closed via firewall.

    Can you connect from your client to your server with SSH?

    You need to check localhost:8080 port and you need to be sure that your server 8080 port is listened by some application too.

    Take a look here for some examples and theory.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)