本文整理汇总了Golang中golang.org/x/crypto/ssh.Channel类的典型用法代码示例。如果您正苦于以下问题:Golang Channel类的具体用法?Golang Channel怎么用?Golang Channel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Channel类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
}
}
}
开发者ID:Freeaqingme,项目名称:SshReverseProxy,代码行数:31,代码来源:daemon.go
示例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
}
开发者ID:robinmonjo,项目名称:dockpack,代码行数:34,代码来源:server.go
示例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)
}
}()
}
开发者ID:gdevillele,项目名称:grpc-go,代码行数:34,代码来源:ssh_server.go
示例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)
}
}
}
开发者ID:benjaminharnett,项目名称:diego-ssh,代码行数:26,代码来源:proxy.go
示例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)
}
}
开发者ID:pgrew,项目名称:kappa,代码行数:34,代码来源:handler.go
示例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
}
开发者ID:Lanzafame,项目名称:butter,代码行数:33,代码来源:handler.go
示例7: pipe
func pipe(dst, src ssh.Channel) {
_, err := io.Copy(dst, src)
if err != nil {
fmt.Println(err.Error())
}
dst.CloseWrite()
}
开发者ID:Freeaqingme,项目名称:SshReverseProxy,代码行数:8,代码来源:daemon.go
示例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
}
开发者ID:aledbf,项目名称:builder,代码行数:17,代码来源:server.go
示例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
}
开发者ID:subsilent,项目名称:kappa,代码行数:54,代码来源:terminal.go
示例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()
}
开发者ID:blacklabeldata,项目名称:sshh,代码行数:53,代码来源:terminal.go
示例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
}
开发者ID:Lanzafame,项目名称:butter,代码行数:13,代码来源:git.go
示例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
}
开发者ID:helgi,项目名称:builder,代码行数:16,代码来源:git.go
示例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)
}
}
}
}
开发者ID:pgrew,项目名称:kappa,代码行数:14,代码来源:handler.go
示例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
}
开发者ID:olegfedoseev,项目名称:golang-meetup-demo,代码行数:15,代码来源:ssh.go
示例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)
}
开发者ID:CoolCloud,项目名称:docker-bastion,代码行数:46,代码来源:sshd.go
示例16: startTerminal
func (s *shellHandler) startTerminal(parentTomb tomb.Tomb, sshConn *ssh.ServerConn, channel ssh.Channel) error {
defer channel.Close()
prompt := ">>> "
term := terminal.NewTerminal(channel, prompt)
// // Try to make the terminal raw
// oldState, err := terminal.MakeRaw(0)
// if err != nil {
// logger.Warn("Error making terminal raw: ", err.Error())
// }
// defer terminal.Restore(0, oldState)
// Get username
username, ok := sshConn.Permissions.Extensions["username"]
if !ok {
username = "user"
}
// Write ascii text
term.Write([]byte(fmt.Sprintf("\r\n Nice job, %s! You are connected!\r\n", username)))
defer term.Write([]byte(fmt.Sprintf("\r\nGoodbye, %s!\r\n", username)))
// Start REPL
for {
select {
case <-parentTomb.Dying():
return nil
default:
s.logger.Info("Reading line...")
input, err := term.ReadLine()
if err != nil {
fmt.Errorf("Readline() error")
return err
}
// Process line
line := strings.TrimSpace(input)
if len(line) > 0 {
// Log input and handle exit requests
if line == "exit" || line == "quit" {
s.logger.Info("Closing connection")
return nil
}
// Echo input
channel.Write(term.Escape.Green)
channel.Write([]byte(line + "\r\n"))
channel.Write(term.Escape.Reset)
}
}
}
return nil
}
开发者ID:blacklabeldata,项目名称:sshh,代码行数:56,代码来源:terminal.go
示例17: Reply
//Reply handles the operation between a req and channel
func Reply(req *ssh.Request, dest ssh.Channel, c *ConnInsight) {
dx, err := dest.SendRequest(req.Type, req.WantReply, req.Payload)
checkError(err, fmt.Sprintf("Request %s processed", req.Type))
if req.WantReply {
req.Reply(dx, nil)
}
meta := map[string]interface{}{
"type": "reply",
"name": req.Type,
"payload": req.Payload,
}
c.Aux().Emit(meta)
}
开发者ID:influx6,项目名称:proxies,代码行数:19,代码来源:ssh.go
示例18: handleRequests
func (s *sshServer) handleRequests(channel ssh.Channel, in <-chan *ssh.Request) error {
env := make(map[string]string)
for req := range in {
switch req.Type {
default:
log.Printf("unrecognized ssh request type=%q payload=%s wantreply=%t", req.Type, req.Payload, req.WantReply)
req.Reply(false, nil) // unhandled; tell them so
case "env":
var e envReq
if err := ssh.Unmarshal(req.Payload, &e); err != nil {
req.Reply(false, nil)
return err
}
req.Reply(true, nil)
env[string(e.Key)] = string(e.Val)
case "exec":
var e execReq
if err := ssh.Unmarshal(req.Payload, &e); err != nil {
req.Reply(false, nil)
return err
}
req.Reply(true, nil)
var cmdbuf bytes.Buffer
for k, v := range env {
cmdbuf.WriteString(k)
cmdbuf.WriteByte('=')
cmdbuf.WriteString(v)
cmdbuf.WriteByte(' ')
}
cmdbuf.Write(e.Command)
log.Printf("Running command %q", cmdbuf.String())
cmd := &packer.RemoteCmd{Command: cmdbuf.String()}
cmd.Stdout = channel
cmd.Stderr = channel.Stderr()
var rc int
if err := s.comm.Start(cmd); err != nil {
rc = 255 // TODO: What is a better choice here?
} else {
cmd.Wait()
rc = cmd.ExitStatus
}
channel.CloseWrite()
channel.SendRequest("exit-status", false, []byte{0, 0, 0, byte(rc)})
channel.Close()
}
}
return nil
}
开发者ID:jszwedko,项目名称:packer-provisioner-tunnel,代码行数:54,代码来源:ssh.go
示例19: forwardRequest
func forwardRequest(req *ssh.Request, channel ssh.Channel) error {
if string(req.Type) != "subsystem" && string(req.Type) != "exit-status" {
req.Reply(false, nil)
if req.Type == "env" {
return nil
}
return fmt.Errorf("Ignoring unsupported request type: %s", string(req.Type))
}
reply, err := channel.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
return err
}
if req.WantReply {
req.Reply(reply, nil)
}
return nil
}
开发者ID:Freeaqingme,项目名称:SshReverseProxy,代码行数:20,代码来源:daemon.go
示例20: handleChannel
func handleChannel(ch ssh.Channel) {
term := terminal.NewTerminal(ch, "> ")
serverTerm := &ssh.ServerTerminal{
Term: term,
Channel: ch,
}
ch.Accept()
defer ch.Close()
for {
line, err := serverTerm.ReadLine()
if err == io.EOF {
return
}
if err != nil {
log.Println("handleChannel readLine err:", err)
continue
}
fmt.Println(line)
}
}
开发者ID:larmog,项目名称:honeypot.go,代码行数:20,代码来源:honeypot.go
注:本文中的golang.org/x/crypto/ssh.Channel类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论