|
6 | 6 | "io"
|
7 | 7 | "log"
|
8 | 8 | "net"
|
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "os" |
9 | 12 |
|
10 | 13 | "golang.org/x/crypto/ssh"
|
11 | 14 | )
|
@@ -40,9 +43,46 @@ func (t *Tunnel) Create() {
|
40 | 43 | return nil
|
41 | 44 | },
|
42 | 45 | }
|
| 46 | + hostport := "labstack.me:22" |
| 47 | + var client *ssh.Client |
43 | 48 |
|
44 | 49 | // Connect
|
45 |
| - client, err := ssh.Dial("tcp", "labstack.me:22", config) |
| 50 | + proxy := os.Getenv("http_proxy") |
| 51 | + if proxy != "" { |
| 52 | + proxyUrl, err := url.Parse(proxy) |
| 53 | + if err != nil { |
| 54 | + log.Fatalf("cannot open new session: %v\n", err) |
| 55 | + } |
| 56 | + tcp, err := net.Dial("tcp", proxyUrl.Host) |
| 57 | + if err != nil { |
| 58 | + log.Fatalf("cannot open new session: %v\n", err) |
| 59 | + } |
| 60 | + connReq := &http.Request{ |
| 61 | + Method: "CONNECT", |
| 62 | + URL: &url.URL{Path: hostport}, |
| 63 | + Host: hostport, |
| 64 | + Header: make(http.Header), |
| 65 | + } |
| 66 | + if proxyUrl.User != nil { |
| 67 | + if p, ok := proxyUrl.User.Password(); ok { |
| 68 | + connReq.SetBasicAuth(proxyUrl.User.Username(), p) |
| 69 | + } |
| 70 | + } |
| 71 | + connReq.Write(tcp) |
| 72 | + resp, err := http.ReadResponse(bufio.NewReader(tcp), connReq) |
| 73 | + if err != nil { |
| 74 | + log.Fatalf("cannot open new session: %v\n", err) |
| 75 | + } |
| 76 | + defer resp.Body.Close() |
| 77 | + |
| 78 | + c, chans, reqs, err := ssh.NewClientConn(tcp, hostport, config) |
| 79 | + if err != nil { |
| 80 | + log.Fatalf("cannot open new session: %v\n", err) |
| 81 | + } |
| 82 | + client = ssh.NewClient(c, chans, reqs) |
| 83 | + } else { |
| 84 | + client, err = ssh.Dial("tcp", hostport, config) |
| 85 | + } |
46 | 86 | if err != nil {
|
47 | 87 | log.Fatalf("Failed to connect %v\n", err)
|
48 | 88 | }
|
@@ -88,6 +128,7 @@ func (t *Tunnel) Create() {
|
88 | 128 | out, err := net.Dial("tcp", fmt.Sprintf("%s:%d", t.TargetHost, t.TargetPort))
|
89 | 129 | if err != nil {
|
90 | 130 | log.Printf("%v\n", err)
|
| 131 | + return |
91 | 132 | }
|
92 | 133 | defer out.Close()
|
93 | 134 |
|
|
0 commit comments