|
| 1 | +package ssh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "net" |
| 8 | + |
| 9 | + "github.com/c2stack/c2g/c2" |
| 10 | + |
| 11 | + gssh "golang.org/x/crypto/ssh" |
| 12 | + "golang.org/x/crypto/ssh/terminal" |
| 13 | +) |
| 14 | + |
| 15 | +type Service struct { |
| 16 | + config *gssh.ServerConfig |
| 17 | + authorizedKeys map[string]struct{} |
| 18 | + stop func() |
| 19 | + options Options |
| 20 | +} |
| 21 | + |
| 22 | +type Options struct { |
| 23 | + Address string |
| 24 | + HostKeyFiles []string |
| 25 | + AuthorizedKeysFile string |
| 26 | +} |
| 27 | + |
| 28 | +func NewService() *Service { |
| 29 | + s := &Service{} |
| 30 | + return s |
| 31 | +} |
| 32 | + |
| 33 | +func (self *Service) Options() Options { |
| 34 | + return self.options |
| 35 | +} |
| 36 | + |
| 37 | +func (self *Service) Apply(options Options) error { |
| 38 | + if self.stop != nil { |
| 39 | + self.stop() |
| 40 | + } |
| 41 | + self.config = &gssh.ServerConfig{ |
| 42 | + PasswordCallback: self.passwordAuth, |
| 43 | + PublicKeyCallback: self.keyAuth, |
| 44 | + } |
| 45 | + for _, k := range options.HostKeyFiles { |
| 46 | + privateBytes, err := ioutil.ReadFile(k) |
| 47 | + if err != nil { |
| 48 | + return c2.NewErr(fmt.Sprintf("Failed to load private key %s : %v", k, err)) |
| 49 | + } |
| 50 | + private, err := gssh.ParsePrivateKey(privateBytes) |
| 51 | + if err != nil { |
| 52 | + return c2.NewErr(fmt.Sprintf("Failed to parse private key %s: %v", k, err)) |
| 53 | + } |
| 54 | + self.config.AddHostKey(private) |
| 55 | + } |
| 56 | + |
| 57 | + authBytes, err := ioutil.ReadFile(options.AuthorizedKeysFile) |
| 58 | + if err != nil { |
| 59 | + return c2.NewErr(fmt.Sprintf("Failed to read authorized file %s : %v", options.AuthorizedKeysFile, err)) |
| 60 | + } |
| 61 | + self.authorizedKeys = make(map[string]struct{}) |
| 62 | + for len(authBytes) > 0 { |
| 63 | + pubKey, _, _, rest, err := gssh.ParseAuthorizedKey(authBytes) |
| 64 | + if err != nil { |
| 65 | + log.Fatal(err) |
| 66 | + } |
| 67 | + self.authorizedKeys[string(pubKey.Marshal())] = struct{}{} |
| 68 | + authBytes = rest |
| 69 | + } |
| 70 | + |
| 71 | + self.options = options |
| 72 | + go self.start() |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (self *Service) passwordAuth(c gssh.ConnMetadata, pass []byte) (*gssh.Permissions, error) { |
| 77 | + return nil, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (self *Service) keyAuth(c gssh.ConnMetadata, pubKey gssh.PublicKey) (*gssh.Permissions, error) { |
| 81 | + return nil, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (self *Service) start() error { |
| 85 | + l, err := net.Listen("tcp", self.options.Address) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + c, err := l.Accept() |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + _, chans, reqs, err := gssh.NewServerConn(c, self.config) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + go gssh.DiscardRequests(reqs) |
| 101 | + |
| 102 | + for newChannel := range chans { |
| 103 | + fmt.Println("new channel") |
| 104 | + if newChannel.ChannelType() != "session" { |
| 105 | + newChannel.Reject(gssh.UnknownChannelType, "unknown channel type") |
| 106 | + } |
| 107 | + ch, req, err := newChannel.Accept() |
| 108 | + if err != nil { |
| 109 | + log.Fatalf("Could not accept channel: %v", err) |
| 110 | + } |
| 111 | + go func(in <-chan *gssh.Request) { |
| 112 | + for req := range in { |
| 113 | + req.Reply(req.Type == "shell", nil) |
| 114 | + } |
| 115 | + }(req) |
| 116 | + term := terminal.NewTerminal(ch, "> ") |
| 117 | + go func() { |
| 118 | + defer ch.Close() |
| 119 | + for { |
| 120 | + line, err := term.ReadLine() |
| 121 | + if err != nil { |
| 122 | + break |
| 123 | + } |
| 124 | + fmt.Println(line) |
| 125 | + } |
| 126 | + }() |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
0 commit comments