大家好,我希望了解项目https://github.com/Freeaqingme/SSHalama/一个 SFTP 代理。
我偶然发现了这段代码
go s.pipeRequests(psChannel, sChannel, psRequests, sRequests)
time.Sleep(50 * time.Millisecond)
go s.pipe(sChannel, psChannel)
go s.pipe(psChannel, Channel)
在这里,time.Sleep(50 * time.Millisecond)非常关键的忽略 SFTP 会话建立但没有出现 PTTY。
完整的代码如下所示。
func (s *Server) handleChannel(newChannel ssh.NewChannel, rClient *ssh.Client) {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type: "+newChannel.ChannelType())
return
}
psChannel, psRequests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
}
sChannel, sRequests, err := rClient.OpenChannel(newChannel.ChannelType(), nil)
if err != nil {
panic("Failed to create session: " + err.Error())
}
go s.pipeRequests(psChannel, sChannel, psRequests, sRequests)
time.Sleep(50 * time.Millisecond)
go s.pipe(sChannel, psChannel)
go s.pipe(psChannel, sChannel)
}
func (s *Server) pipe(dst, src ssh.Channel) {
_, err := io.Copy(dst, src)
if err != nil {
fmt.Println(err.Error())
}
dst.CloseWrite()
}
我想了解为什么sleep ( time.Sleep(50 * time.Millisecond)) 对于 SFTP 会话的工作和 PTTY 出现如此重要。
蝴蝶不菲
相关分类