doulangchao8934
2015-10-23 18:25
浏览 95
已采纳

有没有办法在Go中进行基于动态套接字的端口转发,例如SSH中的-d开关?

I used to use a batch script to create an SSH tunnel that would be usable as a socks5 proxy. Today, I thought I'd implement it in Go, both to learn the language and to remove the need for me to constantly run a batch script file whenever the connection drops.

Right now, how I do this is to use plink. The command to do this with plink is:

plink -N -C -D 8888 -pw password username@example.com

And here is my Go code:

package main

import (
    "os"
    "os/exec"
    "log"
)

func runPlink() {
    command := exec.Command("plink.exe", "-N", "-C", "-D", "8888", "-pw", "password", "username@example.com")

    if output, err := command.CombinedOutput(); err != nil {
        log.Println(string(output), err.Error())
        runPlink()
    }
}

func main() {
    if _, err := os.Stat("plink.exe"); os.IsNotExist(err) {
        log.Fatalln("Cannot find plink.exe. Please copy it next to this application.")
    }

    runPlink()
}

I would like to make this application self-contained, so that it doesn't rely on the existence of plink.exe to work.

Is there a way to achieve this in Go?

图片转代码服务由CSDN问答提供 功能建议

我曾经使用批处理脚本来创建可以用作socks5代理的SSH隧道。 今天,我以为我会在Go中实现它,既学习语言,也不再需要我在连接断开时不断运行批处理脚本文件。

现在, 我要做的是使用 plink 。 使用plink进行此操作的命令是:

  plink -N -C -D 8888 -pw password username@example.com 
   \  n 
 

这是我的Go代码:

 包main 
 
import(
“ os” 
“ os / exec” 
“ log  “ 
)
 
func runPlink(){
命令:= exec.Command(” plink.exe“,”-N“,”-C“,”-D“,” 8888“,”-pw“  ,“ password”,“ username@example.com”)
 
如果输出,则err:= command.CombinedOutput();  err!= nil {
 log.Println(string(output),err.Error())
 runPlink()
} 
} 
 
func main(){
如果_,err:= os  .Stat( “plink.exe”);  os.IsNotExist(err){
 log.Fatalln(“找不到plink.exe。请将其复制到此应用程序旁边。”)
} 
 
 runPlink()
} 
  <  / pre> 
 
 

我想使此应用程序自包含,以便它不依赖 plink.exe 的存在来工作。 \ n

在Go中有实现此目的的方法吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doubi2014 2015-10-23 19:47
    已采纳

    This might not be ideal, but you could pretty easily use a combination of https://github.com/jteeuwen/go-bindata and https://github.com/getlantern/byteexec - essentially you would embed the plink executable inside of your own executable, then load it and run it with something like:

    func runPlink() {
        programBytes, err := Asset("plink.exe")
        be, err := byteexec.New(programBytes)
        if err != nil {
            log.Fatalf("Uh oh: %s", err)
        }
        cmd := be.Command("-N", "-C", "-D", "8888", "-pw", "password", "username@example.com")
        if output, err := cmd.CombinedOutput(); err != nil {
            log.Println(string(output), err.Error())
            runPlink()
        }
    }
    
    点赞 评论

相关推荐 更多相似问题