Last active
April 4, 2016 15:50
-
-
Save snarlysodboxer/8661645 to your computer and use it in GitHub Desktop.
pile of go ssh port forward attempt
This file contains hidden or 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
package main | |
import ( | |
"code.google.com/p/go.crypto/ssh" | |
//"net/http" | |
"io/ioutil" | |
"net" | |
) | |
func main() { | |
config := &ssh.ClientConfig{ | |
User: "root", | |
Auth: []ssh.ClientAuth{ | |
ssh.ClientAuthPassword(ssh.PasswordAuth("password")), | |
}, | |
} | |
conn := Dial(config) | |
defer conn.Close() | |
sshListener, err := conn.Listen("tcp", "192.168.1.100:22") | |
if err != nil { | |
t.Fatalf("conn.Listen failed: %v", err) | |
} | |
go func() { | |
sshConn, err := sshListener.Accept() | |
if err != nil { | |
t.Fatalf("listen.Accept failed: %v", err) | |
} | |
_, err = io.Copy(sshConn, sshConn) | |
if err != nil && err != io.EOF { | |
t.Fatalf("ssh client copy: %v", err) | |
} | |
sshConn.Close() | |
}() | |
forwardedAddr := sshListener.Addr().String() | |
tcpConn, err := net.Dial("tcp", forwardedAddr) | |
if err != nil { | |
t.Fatalf("TCP dial failed: %v", err) | |
} | |
readChan := make(chan []byte) | |
go func() { | |
data, _ := ioutil.ReadAll(tcpConn) | |
readChan <- data | |
}() | |
// | |
// // Invent some data. | |
// data := make([]byte, 100*1000) | |
// for i := range data { | |
// data[i] = byte(i % 255) | |
// } | |
// | |
// var sent []byte | |
// for len(sent) < 1000*1000 { | |
// // Send random sized chunks | |
// m := rand.Intn(len(data)) | |
// n, err := tcpConn.Write(data[:m]) | |
// if err != nil { | |
// break | |
// } | |
// sent = append(sent, data[:n]...) | |
// } | |
// if err := tcpConn.(*net.TCPConn).CloseWrite(); err != nil { | |
// t.Errorf("tcpConn.CloseWrite: %v", err) | |
// } | |
// | |
// read := <-readChan | |
// | |
// if len(sent) != len(read) { | |
// t.Fatalf("got %d bytes, want %d", len(read), len(sent)) | |
// } | |
// if bytes.Compare(sent, read) != 0 { | |
// t.Fatalf("read back data does not match") | |
// } | |
// | |
if err := sshListener.Close(); err != nil { | |
t.Fatalf("sshListener.Close: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment