Advertisement
Guest User

Untitled

a guest
Aug 7th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.75 KB | None | 0 0
  1. // A small SSH daemon providing client login attempts
  2.  
  3. package main
  4.  
  5. import (
  6.     "fmt"
  7.     "io/ioutil"
  8.     "log"
  9.     "net"
  10.     "time"
  11.     "golang.org/x/crypto/ssh"
  12. )
  13.  
  14. func main() {
  15.     config := &ssh.ServerConfig{
  16.         PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
  17.             fmt.Println(
  18.                 "Time:", time.Now().Format(time.RFC822Z),
  19.                 "Client:", c.RemoteAddr().String(),
  20.                 "User:", c.User(),
  21.                 "Password:", string(pass),
  22.             )
  23.             return nil, fmt.Errorf("password rejected for %q", c.User())
  24.         },
  25.     }
  26.  
  27.     privateBytes, err := ioutil.ReadFile("id_rsa")
  28.     if err != nil {
  29.         log.Fatal("Failed to load private key (./id_rsa)")
  30.     }
  31.  
  32.     private, err := ssh.ParsePrivateKey(privateBytes)
  33.     if err != nil {
  34.         log.Fatal("Failed to parse private key")
  35.     }
  36.  
  37.     config.AddHostKey(private)
  38.  
  39.     listener, err := net.Listen("tcp", "0.0.0.0:22")
  40.     if err != nil {
  41.         log.Fatalf("Failed to listen on 22 (%s)", err)
  42.     }
  43.  
  44.     log.Print("Listening on 22...")
  45.     for {
  46.         tcpConn, err := listener.Accept()
  47.         if err != nil {
  48.             log.Printf("Failed to accept incoming connection (%s)", err)
  49.             continue
  50.         }
  51.         _,  chans, reqs, err := ssh.NewServerConn(tcpConn, config)
  52.         if err != nil {
  53.             log.Printf("Failed to handshake (%s)", err)
  54.             continue
  55.         }
  56.  
  57.         go ssh.DiscardRequests(reqs)
  58.         go handleChannels(chans)
  59.     }
  60. }
  61.  
  62. func handleChannels(chans <-chan ssh.NewChannel) {
  63.     for newChannel := range chans {
  64.         go handleChannel(newChannel)
  65.     }
  66. }
  67.  
  68. func handleChannel(newChannel ssh.NewChannel) {
  69.     return
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement