Skip to content

Instantly share code, notes, and snippets.

@jonasschneider
Created February 17, 2013 00:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasschneider/4969501 to your computer and use it in GitHub Desktop.
Save jonasschneider/4969501 to your computer and use it in GitHub Desktop.
Go SSH server attempt (non-functional)
package main;
import(
"code.google.com/p/go.crypto/ssh"
"io/ioutil"
"fmt"
"io"
"os/exec"
"os"
)
func main() {
config := &ssh.ServerConfig{
PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool {
// accept any user/pw
return true
},
}
pemBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
panic("Failed to load private key")
}
if err = config.SetRSAPrivateKey(pemBytes); err != nil {
panic("Failed to parse private key")
}
// listen
listener, err := ssh.Listen("tcp", "0.0.0.0:2022", config)
if err != nil {
panic("failed to listen for connection")
}
// accept a typ connection
sConn, err := listener.Accept()
if err != nil {
panic("failed to accept incoming connection")
}
// do the ssh dance
if err := sConn.Handshake(); err != nil {
panic("failed to handshake")
}
// Accept a single ssh channel
channel, err := sConn.Accept()
if err != nil {
if err == io.EOF { fmt.Println("main connection EOF"); } else {
fmt.Println(err)
panic("error from Accept")
}
}
fmt.Println("Accepted a channel of type", channel.ChannelType())
if channel.ChannelType() != "session" {
channel.Reject(ssh.UnknownChannelType, "unknown channel type")
}
err = channel.Accept() // accept the channel
if err != nil {
fmt.Println(err)
panic("error from ChanAccept")
}
fmt.Println("dumping extra data, len:", len(channel.ExtraData()))
os.Stdout.Write(channel.ExtraData())
done := make(chan bool)
go func() {
defer channel.Close()
//cmd := exec.Command("git-shell")
cmd := exec.Command("cat")
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
}
stdin, err := cmd.StdinPipe()
if err != nil {
fmt.Println(err)
}
cmd.Start()
chan_done := make(chan bool)
go func() {
io.Copy(channel, stdout)
fmt.Printf("sender done")
chan_done <- true
}()
go func() {
io.Copy(stdin, channel)
fmt.Printf("receiver done")
chan_done <- true
}()
<- chan_done
<- chan_done
done <- true
}()
<- done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment