You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.3 KiB

10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Prototype, git client looks like do not recognize req.Reply.
  5. package ssh
  6. import (
  7. "fmt"
  8. "io/ioutil"
  9. "net"
  10. "os"
  11. "os/exec"
  12. "strings"
  13. "code.google.com/p/go.crypto/ssh"
  14. "github.com/Unknwon/com"
  15. "github.com/gogits/gogs/modules/log"
  16. )
  17. func handleServerConn(keyId string, chans <-chan ssh.NewChannel) {
  18. for newChan := range chans {
  19. if newChan.ChannelType() != "session" {
  20. newChan.Reject(ssh.UnknownChannelType, "unknown channel type")
  21. continue
  22. }
  23. channel, requests, err := newChan.Accept()
  24. if err != nil {
  25. log.Error(3, "Could not accept channel: %v", err)
  26. continue
  27. }
  28. go func(in <-chan *ssh.Request) {
  29. defer channel.Close()
  30. for req := range in {
  31. ok, payload := false, strings.TrimLeft(string(req.Payload), "\x00")
  32. fmt.Println("Request:", req.Type, req.WantReply, payload)
  33. switch req.Type {
  34. case "env":
  35. args := strings.Split(strings.Replace(payload, "\x00", "", -1), "\v")
  36. if len(args) != 2 {
  37. break
  38. }
  39. args[0] = strings.TrimLeft(args[0], "\x04")
  40. _, _, err := com.ExecCmdBytes("env", args[0]+"="+args[1])
  41. if err != nil {
  42. log.Error(3, "env: %v", err)
  43. channel.Stderr().Write([]byte(err.Error()))
  44. break
  45. }
  46. ok = true
  47. case "exec":
  48. os.Setenv("SSH_ORIGINAL_COMMAND", strings.TrimLeft(payload, "'("))
  49. log.Info("Payload: %v", strings.TrimLeft(payload, "'("))
  50. cmd := exec.Command("/Users/jiahuachen/Applications/Go/src/github.com/gogits/gogs-ng/gogs-ng", "serv", "key-"+keyId)
  51. cmd.Stdout = channel
  52. cmd.Stdin = channel
  53. cmd.Stderr = channel.Stderr()
  54. if err := cmd.Run(); err != nil {
  55. log.Error(3, "exec: %v", err)
  56. } else {
  57. ok = true
  58. }
  59. }
  60. fmt.Println("Done:", ok)
  61. req.Reply(ok, nil) // BUG: Git on Mac seems not know this reply and hang?
  62. }
  63. fmt.Println("Done!!!")
  64. }(requests)
  65. }
  66. }
  67. func listen(config *ssh.ServerConfig, port string) {
  68. listener, err := net.Listen("tcp", "0.0.0.0:"+port)
  69. if err != nil {
  70. panic(err)
  71. }
  72. for {
  73. // Once a ServerConfig has been configured, connections can be accepted.
  74. conn, err := listener.Accept()
  75. if err != nil {
  76. log.Error(3, "Fail to accept incoming connection: %v", err)
  77. continue
  78. }
  79. // Before use, a handshake must be performed on the incoming net.Conn.
  80. sConn, chans, reqs, err := ssh.NewServerConn(conn, config)
  81. if err != nil {
  82. log.Error(3, "Fail to handshake: %v", err)
  83. continue
  84. }
  85. // The incoming Request channel must be serviced.
  86. go ssh.DiscardRequests(reqs)
  87. go handleServerConn(sConn.Permissions.Extensions["key-id"], chans)
  88. }
  89. }
  90. // Listen starts a SSH server listens on given port.
  91. func Listen(port string) {
  92. config := &ssh.ServerConfig{
  93. PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
  94. // keyCache[string(ssh.MarshalAuthorizedKey(key))] = 2
  95. return &ssh.Permissions{Extensions: map[string]string{"key-id": "2"}}, nil
  96. },
  97. }
  98. privateBytes, err := ioutil.ReadFile("/Users/jiahuachen/.ssh/id_rsa")
  99. if err != nil {
  100. panic("failed to load private key")
  101. }
  102. private, err := ssh.ParsePrivateKey(privateBytes)
  103. if err != nil {
  104. panic("failed to parse private key")
  105. }
  106. config.AddHostKey(private)
  107. go listen(config, port)
  108. }