本文整理汇总了Golang中golang.org/x/crypto/ssh.Channel类的典型用法代码示例。如果您正苦于以下问题:Golang Channel类的具体用法?Golang Channel怎么用?Golang Channel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Channel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: pipeRequests
func pipeRequests(psChannel, sChannel ssh.Channel, psRequests, sRequests <-chan *ssh.Request) {
defer func() {
return
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
defer sChannel.Close()
defer psChannel.Close()
for {
select {
case lRequest, ok := <-psRequests:
if !ok {
return
}
if err := forwardRequest(lRequest, sChannel); err != nil {
fmt.Println("Error: " + err.Error())
continue
}
case rRequest, ok := <-sRequests:
if !ok {
return
}
if err := forwardRequest(rRequest, psChannel); err != nil {
fmt.Println("Error: " + err.Error())
continue
}
}
}
}
示例2: attachCmd
func attachCmd(cmd *exec.Cmd, ch ssh.Channel) (*sync.WaitGroup, error) {
var wg sync.WaitGroup
wg.Add(3)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
go func() {
defer wg.Done()
io.Copy(stdin, ch)
}()
go func() {
defer wg.Done()
io.Copy(ch.Stderr(), stderr)
}()
go func() {
defer wg.Done()
io.Copy(ch, stdout)
}()
return &wg, nil
}
示例3: handleChannel
func handleChannel(s *Stream, ch ssh.Channel, reqs <-chan *ssh.Request, handle func(*Stream)) {
// handle requests receive for this Channel
go func(in <-chan *ssh.Request) {
for req := range in {
logrus.Debugln("AdminTool -> Request of type:", req.Type, "len:", len(req.Type))
logrus.Debugln("AdminTool -> Request payload:", string(req.Payload), "len:", len(req.Payload))
if req.WantReply {
req.Reply(false, nil)
}
}
logrus.Debugln("AdminTool -> End of request GO chan")
}(reqs)
// read data from channel
go func() {
for {
buffer := make([]byte, 64)
n, err := ch.Read(buffer)
if err != nil {
if err.Error() == "EOF" {
handleData(s, []byte{}, true)
// all data received: handle Stream message
handle(s)
break
} else {
logrus.Fatalln("failed to read channel : " + err.Error())
}
}
handleData(s, buffer[:n], false)
}
}()
}
示例4: ProxyRequests
func ProxyRequests(logger lager.Logger, channelType string, reqs <-chan *ssh.Request, channel ssh.Channel) {
logger = logger.Session("proxy-requests", lager.Data{
"channel-type": channelType,
})
logger.Info("started")
defer logger.Info("completed")
defer channel.Close()
for req := range reqs {
logger.Info("request", lager.Data{
"type": req.Type,
"wantReply": req.WantReply,
"payload": req.Payload,
})
success, err := channel.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
logger.Error("send-request-failed", err)
continue
}
if req.WantReply {
req.Reply(success, nil)
}
}
}
示例5: handleSessionRequests
func handleSessionRequests(logger log.Logger, channel ssh.Channel, requests <-chan *ssh.Request, system datamodel.System, user datamodel.User) {
defer channel.Close()
// Sessions have out-of-band requests such as "shell",
// "pty-req" and "env". Here we handle only the
// "shell" request.
for req := range requests {
ok := false
switch req.Type {
case "shell":
ok = true
if len(req.Payload) > 0 {
fmt.Println(string(req.Payload))
// We don't accept any
// commands, only the
// default shell.
ok = false
}
case "pty-req":
// Responding 'ok' here will let the client
// know we have a pty ready for input
ok = true
go startTerminal(logger, channel, system, user)
default:
// fmt.Println("default req: ", req)
}
req.Reply(ok, nil)
}
}
示例6: Request
func (handle *handle) Request(ch ssh.Channel, req *ssh.Request) (bool, error) {
config.Log.Debug("handle %s", req.Type)
switch req.Type {
case "pty-req":
fallthrough
case "shell":
return true, ShellDisabled
case "env":
// we store these off??
return false, nil
case "exec":
// it is prefixed with the length so we strip it off
command := string(req.Payload[4:])
config.Log.Debug("handle cmd %s", command)
// find the correct handler and run it
config.Log.Debug("looking for command in %d", len(repo.Commands()))
for _, cmd := range repo.Commands() {
if cmd.Match(command) {
config.Log.Debug("found match", command)
code, err := cmd.Run(command, ch)
exitStatusBuffer := make([]byte, 4)
binary.PutUvarint(exitStatusBuffer, uint64(code))
config.Log.Debug("cmd finished", code, err)
// purposefully ignoring the possible error
ch.SendRequest("exit-status", false, exitStatusBuffer)
return true, err
}
}
return true, UnknownCommand
}
return true, UnkownRequest
}
示例7: pipe
func pipe(dst, src ssh.Channel) {
_, err := io.Copy(dst, src)
if err != nil {
fmt.Println(err.Error())
}
dst.CloseWrite()
}
示例8: Ping
// Ping handles a simple test SSH exec.
//
// Returns the string PONG and exit status 0.
//
// Params:
// - channel (ssh.Channel): The channel to respond on.
// - request (*ssh.Request): The request.
//
func Ping(channel ssh.Channel, req *ssh.Request) error {
log.Info("PING")
if _, err := channel.Write([]byte("pong")); err != nil {
log.Err("Failed to write to channel: %s", err)
}
sendExitStatus(0, channel)
req.Reply(true, nil)
return nil
}
示例9: Handle
func (s *shellHandler) Handle(parentTomb tomb.Tomb, sshConn *ssh.ServerConn, channel ssh.Channel, requests <-chan *ssh.Request) error {
defer channel.Close()
users, err := s.system.Users()
if err != nil {
return err
}
user, err := users.Get(sshConn.Permissions.Extensions["username"])
if err != nil {
return err
}
// Create tomb for terminal goroutines
var t tomb.Tomb
// Sessions have out-of-band requests such as "shell",
// "pty-req" and "env". Here we handle only the
// "shell" request.
for {
select {
case <-parentTomb.Dying():
t.Kill(nil)
return t.Wait()
case req := <-requests:
ok := false
switch req.Type {
case "shell":
ok = true
if len(req.Payload) > 0 {
fmt.Println(string(req.Payload))
// We don't accept any
// commands, only the
// default shell.
ok = false
}
case "pty-req":
// Responding 'ok' here will let the client
// know we have a pty ready for input
ok = true
go s.startTerminal(t, channel, s.system, user)
default:
// fmt.Println("default req: ", req)
}
req.Reply(ok, nil)
}
}
return nil
}
示例10: Handle
func (s *shellHandler) Handle(parentTomb tomb.Tomb, sshConn *ssh.ServerConn, channel ssh.Channel, requests <-chan *ssh.Request) error {
defer channel.Close()
s.logger.Info("WooHoo!!! Inside Handler!")
// Create tomb for terminal goroutines
var t tomb.Tomb
// Sessions have out-of-band requests such as "shell",
// "pty-req" and "env". Here we handle only the
// "shell" request.
t.Go(func() error {
OUTER:
for {
select {
case <-parentTomb.Dying():
t.Kill(nil)
break OUTER
case req := <-requests:
if req == nil {
break OUTER
}
ok := false
switch req.Type {
case "shell":
ok = true
if len(req.Payload) > 0 {
// fmt.Println(string(req.Payload))
// We don't accept any
// commands, only the
// default shell.
ok = false
}
case "pty-req":
// Responding 'ok' here will let the client
// know we have a pty ready for input
ok = true
t.Go(func() error {
return s.startTerminal(t, sshConn, channel)
})
}
req.Reply(ok, nil)
}
}
return nil
})
return t.Wait()
}
示例11: Run
func (push Push) Run(command string, ch ssh.Channel) (uint64, error) {
//TODO make "master" be dynamic
code, err := gitShell(ch, ch.Stderr(), command)
if err == nil {
newCommit := getCommit("master")
stream := ch.Stderr()
err = deploy.Run(stream, newCommit)
if err != nil {
return 1, err
}
}
return code, err
}
示例12: plumbCommand
// plumbCommand connects the exec in/output and the channel in/output.
//
// The sidechannel is for sending errors to logs.
func plumbCommand(cmd *exec.Cmd, channel ssh.Channel, sidechannel io.Writer) *sync.WaitGroup {
var wg sync.WaitGroup
inpipe, _ := cmd.StdinPipe()
go func() {
io.Copy(inpipe, channel)
inpipe.Close()
}()
cmd.Stdout = channel
cmd.Stderr = channel.Stderr()
return &wg
}
示例13: handleChannelRequests
func handleChannelRequests(logger log.Logger, channel ssh.Channel, requests <-chan *ssh.Request, system datamodel.System, user datamodel.User) {
defer channel.Close()
for req := range requests {
if req.Type == "skl" {
logger.Info("SKL request", "request", string(req.Payload))
req.Reply(true, nil)
} else {
if req.WantReply {
req.Reply(false, nil)
}
}
}
}
示例14: sendCmdResult
func sendCmdResult(channel ssh.Channel, result []byte, statusCode uint32) error {
if _, err := channel.Write(result); err != nil {
return fmt.Errorf("failed to write to ssh-channel: %v", err)
}
status := struct {
Status uint32
}{
statusCode,
}
_, err := channel.SendRequest("exit-status", false, ssh.Marshal(&status))
if err != nil {
return fmt.Errorf("failed to SendRequest: %v", err)
}
return nil
}
示例15: handleChannel
func (sshd *SSHD) handleChannel(channel ssh.Channel, requests <-chan *ssh.Request, container string) {
cmd := exec.Command("docker", "exec", "-i", "-t", container, "/bin/bash")
closeChannel := func() {
channel.Close()
_, err := cmd.Process.Wait()
if err != nil {
log.Errorf("failed to exit docker exec (%s)", err)
}
}
fp, err := pty.Start(cmd)
if err != nil {
log.Error("pty.Start: ", err)
closeChannel()
return
}
go func() {
for req := range requests {
log.Debugf("new request: %s", req.Type)
switch req.Type {
case "shell":
if len(req.Payload) == 0 {
req.Reply(true, nil)
}
case "pty-req":
termLen := req.Payload[3]
w, h := sshd.parseDims(req.Payload[termLen+4:])
sshd.setWinsize(fp.Fd(), w, h)
req.Reply(true, nil)
case "window-change":
w, h := sshd.parseDims(req.Payload)
sshd.setWinsize(fp.Fd(), w, h)
case "env":
}
}
}()
var once sync.Once
cp := func(dst io.Writer, src io.Reader) {
io.Copy(dst, src)
once.Do(closeChannel)
}
go cp(channel, fp)
go cp(fp, channel)
}