-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
## v1.0.0 / 2021-06-25 | ||
## v1.0.2 / 2021-06-25 | ||
|
||
- Golang SSH 隧道 | ||
- Golang SSH 隧道 | ||
- 增加扩展包方式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/crypto/ssh" | ||
"io" | ||
"net" | ||
"time" | ||
) | ||
|
||
// 转发 | ||
func sForward(serverAddr string, remoteAddr string, localConn net.Conn, config *ssh.ClientConfig) { | ||
// 设置sshClientConn | ||
sshClientConn, err := ssh.Dial("tcp", serverAddr, config) | ||
if err != nil { | ||
fmt.Printf("ssh.Dial failed: %s", err) | ||
} | ||
|
||
// 设置Connection | ||
sshConn, err := sshClientConn.Dial("tcp", remoteAddr) | ||
|
||
// 将localConn.Reader复制到sshConn.Writer | ||
go func() { | ||
_, err = io.Copy(sshConn, localConn) | ||
if err != nil { | ||
fmt.Printf("io.Copy failed: %v", err) | ||
} | ||
}() | ||
|
||
// 将sshConn.Reader复制到localConn.Writer | ||
go func() { | ||
_, err = io.Copy(localConn, sshConn) | ||
if err != nil { | ||
fmt.Printf("io.Copy failed: %v", err) | ||
} | ||
}() | ||
} | ||
func Tunnel(username string, password string, serverAddr string, remoteAddr string, localAddr string) { | ||
// 设置SSH配置 | ||
fmt.Printf("%s,服务器:%s;远程:%s;本地:%s\n", "设置SSH配置", serverAddr, remoteAddr, localAddr) | ||
config := &ssh.ClientConfig{ | ||
User: username, | ||
Auth: []ssh.AuthMethod{ | ||
ssh.Password(password), | ||
}, | ||
Timeout: 30 * time.Second, | ||
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { | ||
return nil | ||
}, | ||
} | ||
|
||
// 设置本地监听器 | ||
localListener, err := net.Listen("tcp", localAddr) | ||
if err != nil { | ||
fmt.Printf("net.Listen failed: %v\n", err) | ||
} | ||
|
||
for { | ||
// 设置本地 | ||
localConn, err := localListener.Accept() | ||
if err != nil { | ||
fmt.Printf("localListener.Accept failed: %v\n", err) | ||
} | ||
go sForward(serverAddr, remoteAddr, localConn, config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestName(t *testing.T) { | ||
Tunnel("root", "", ":22", ":3306", "localhost:13306") | ||
} |