本文整理汇总了Golang中golang.org/x/crypto/ssh.Marshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Marshal函数的具体用法?Golang Marshal怎么用?Golang Marshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Marshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RequestPty
func (sc *SshSession) RequestPty(term string, h int, w int, termmodes ssh.TerminalModes) (err error) {
if sc.session != nil {
return sc.session.RequestPty(term, h, w, termmodes)
} else {
var tm []byte
for k, v := range termmodes {
kv := struct {
Key byte
Val uint32
}{k, v}
tm = append(tm, ssh.Marshal(&kv)...)
}
tm = append(tm, tty_OP_END)
req := ptyRequestMsg{
Term: term,
Columns: uint32(w),
Rows: uint32(h),
Width: uint32(w * 8),
Height: uint32(h * 8),
Modelist: string(tm),
}
ok, err := sc.ch.SendRequest("pty-req", true, ssh.Marshal(&req))
if err == nil && !ok {
err = errors.New("ssh: pty-req failed")
}
return err
}
}
示例2: insertCert
func (c *client) insertCert(s interface{}, cert *ssh.Certificate, comment string, constraints []byte) error {
var req []byte
switch k := s.(type) {
case *rsa.PrivateKey:
if len(k.Primes) != 2 {
return fmt.Errorf("agent: unsupported RSA key with %d primes", len(k.Primes))
}
k.Precompute()
req = ssh.Marshal(rsaCertMsg{
Type: cert.Type(),
CertBytes: cert.Marshal(),
D: k.D,
Iqmp: k.Precomputed.Qinv,
P: k.Primes[0],
Q: k.Primes[1],
Comments: comment,
Constraints: constraints,
})
case *dsa.PrivateKey:
req = ssh.Marshal(dsaCertMsg{
Type: cert.Type(),
CertBytes: cert.Marshal(),
X: k.X,
Comments: comment,
})
case *ecdsa.PrivateKey:
req = ssh.Marshal(ecdsaCertMsg{
Type: cert.Type(),
CertBytes: cert.Marshal(),
D: k.D,
Comments: comment,
})
default:
return fmt.Errorf("agent: unsupported key type %T", s)
}
// if constraints are present then the message type needs to be changed.
if len(constraints) != 0 {
req[0] = agentAddIdConstrained
}
signer, err := ssh.NewSignerFromKey(s)
if err != nil {
return err
}
if bytes.Compare(cert.Key.Marshal(), signer.PublicKey().Marshal()) != 0 {
return errors.New("agent: signer and cert have different public key")
}
resp, err := c.call(req)
if err != nil {
return err
}
if _, ok := resp.(*successAgentMsg); ok {
return nil
}
return errors.New("agent: failure")
}
示例3: insertKey
// Insert adds a private key to the agent.
func (c *client) insertKey(s interface{}, comment string, constraints []byte) error {
var req []byte
switch k := s.(type) {
case *rsa.PrivateKey:
if len(k.Primes) != 2 {
return fmt.Errorf("agent: unsupported RSA key with %d primes", len(k.Primes))
}
k.Precompute()
req = ssh.Marshal(rsaKeyMsg{
Type: ssh.KeyAlgoRSA,
N: k.N,
E: big.NewInt(int64(k.E)),
D: k.D,
Iqmp: k.Precomputed.Qinv,
P: k.Primes[0],
Q: k.Primes[1],
Comments: comment,
Constraints: constraints,
})
case *dsa.PrivateKey:
req = ssh.Marshal(dsaKeyMsg{
Type: ssh.KeyAlgoDSA,
P: k.P,
Q: k.Q,
G: k.G,
Y: k.Y,
X: k.X,
Comments: comment,
Constraints: constraints,
})
case *ecdsa.PrivateKey:
nistID := fmt.Sprintf("nistp%d", k.Params().BitSize)
req = ssh.Marshal(ecdsaKeyMsg{
Type: "ecdsa-sha2-" + nistID,
Curve: nistID,
KeyBytes: elliptic.Marshal(k.Curve, k.X, k.Y),
D: k.D,
Comments: comment,
Constraints: constraints,
})
default:
return fmt.Errorf("agent: unsupported key type %T", s)
}
// if constraints are present then the message type needs to be changed.
if len(constraints) != 0 {
req[0] = agentAddIdConstrained
}
resp, err := c.call(req)
if err != nil {
return err
}
if _, ok := resp.(*successAgentMsg); ok {
return nil
}
return errors.New("agent: failure")
}
示例4: sendExitMessage
func (sess *session) sendExitMessage(err error) {
logger := sess.logger.Session("send-exit-message")
logger.Info("started")
defer logger.Info("finished")
if err != nil {
logger.Error("building-exit-message-from-error", err)
}
if err == nil {
_, sendErr := sess.channel.SendRequest("exit-status", false, ssh.Marshal(exitStatusMsg{}))
if sendErr != nil {
logger.Error("send-exit-status-failed", sendErr)
}
return
}
exitError, ok := err.(*exec.ExitError)
if !ok {
exitMessage := exitStatusMsg{Status: 255}
_, sendErr := sess.channel.SendRequest("exit-status", false, ssh.Marshal(exitMessage))
if sendErr != nil {
logger.Error("send-exit-status-failed", sendErr)
}
return
}
waitStatus, ok := exitError.Sys().(syscall.WaitStatus)
if !ok {
exitMessage := exitStatusMsg{Status: 255}
_, sendErr := sess.channel.SendRequest("exit-status", false, ssh.Marshal(exitMessage))
if sendErr != nil {
logger.Error("send-exit-status-failed", sendErr)
}
return
}
if waitStatus.Signaled() {
exitMessage := exitSignalMsg{
Signal: string(signals.SSHSignals[waitStatus.Signal()]),
CoreDumped: waitStatus.CoreDump(),
}
_, sendErr := sess.channel.SendRequest("exit-signal", false, ssh.Marshal(exitMessage))
if sendErr != nil {
logger.Error("send-exit-status-failed", sendErr)
}
return
}
exitMessage := exitStatusMsg{Status: uint32(waitStatus.ExitStatus())}
_, sendErr := sess.channel.SendRequest("exit-status", false, ssh.Marshal(exitMessage))
if sendErr != nil {
logger.Error("send-exit-status-failed", sendErr)
}
}
示例5: resize
func (ss *SecureShell) resize(resized <-chan os.Signal, session SecureSession, terminalFd uintptr, initialWidth, initialHeight int) {
type resizeMessage struct {
Width uint32
Height uint32
PixelWidth uint32
PixelHeight uint32
}
var previousWidth, previousHeight int
previousWidth = initialWidth
previousHeight = initialHeight
for _ = range resized {
width, height := ss.Term.GetWinsize(terminalFd)
if width == previousWidth && height == previousHeight {
continue
}
message := resizeMessage{
Width: uint32(width),
Height: uint32(height),
}
session.SendRequest("window-change", false, ssh.Marshal(message))
previousWidth = width
previousHeight = height
}
}
示例6: ReadAt
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
req := fxpReadMsg{Handle: f.handle}
fxpCh, err := f.sftp.chans.newChan()
if err != nil {
return
}
defer f.sftp.chans.remove(fxpCh.id)
req.SetID(fxpCh.id)
for len(b) > 0 {
req.Offset = uint64(n) + uint64(off)
req.Length = uint32(min(uint64(len(b)), maxDataBytes))
if err = f.sftp.writePacket(ssh.Marshal(req)); err != nil {
return
}
resp := fxpCh.waitForResponse()
switch msg := resp.(type) {
case *fxpStatusResp:
if msg.Status == eof {
err = io.EOF
return
}
err = msg
return
case *fxpDataResp:
n += copy(b, msg.Data)
b = b[len(msg.Data):]
default:
panic("unexpected message type returned from server")
}
}
return
}
示例7: handleSSHChannel
func handleSSHChannel(newChan ssh.NewChannel, session *Session) {
ch, reqs, err := newChan.Accept()
if err != nil {
log.Println("handle channel failed:", err)
return
}
exitCh := make(chan int)
go func() {
status := struct{ Status uint32 }{uint32(<-exitCh)}
_, err = ch.SendRequest("exit-status", false, ssh.Marshal(&status))
assert(err)
ch.Close()
}()
for req := range reqs {
go func(req *ssh.Request) {
if req.WantReply {
req.Reply(true, nil)
}
switch req.Type {
case "exec":
var payload = struct{ Value string }{}
ssh.Unmarshal(req.Payload, &payload)
line := strings.Trim(payload.Value, "\n")
var args []string
if line != "" {
args = strings.Split(line, " ")
}
RunCmd(args, ch, ch, ch.Stderr(), exitCh, session)
}
}(req)
}
}
示例8: resize
func (c *secureShell) resize(resized <-chan os.Signal, session SecureSession, terminalFd uintptr) {
type resizeMessage struct {
Width uint32
Height uint32
PixelWidth uint32
PixelHeight uint32
}
var previousWidth, previousHeight int
for range resized {
width, height := c.getWindowDimensions(terminalFd)
if width == previousWidth && height == previousHeight {
continue
}
message := resizeMessage{
Width: uint32(width),
Height: uint32(height),
}
_, _ = session.SendRequest("window-change", false, ssh.Marshal(message))
previousWidth = width
previousHeight = height
}
}
示例9: WriteAt
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
req := fxpWriteMsg{Handle: f.handle}
fxpCh, err := f.sftp.chans.newChan()
if err != nil {
return
}
defer f.sftp.chans.remove(fxpCh.id)
req.SetID(fxpCh.id)
for len(b) > 0 {
req.Offset = uint64(n) + uint64(off)
l := min(uint64(len(b)), maxDataBytes)
req.Data = b[:l]
if err = f.sftp.writePacket(ssh.Marshal(req)); err != nil {
return
}
resp := fxpCh.waitForResponse()
switch msg := resp.(type) {
case *fxpStatusResp:
if msg.Status != OK {
err = msg
return
}
n += int(l)
b = b[l:]
default:
panic("unexpected message type returned from server")
}
}
return
}
示例10: forwardLocalConn
func forwardLocalConn(logger lager.Logger, localConn net.Conn, conn *ssh.ServerConn, forwardIP string, forwardPort uint32) {
defer localConn.Close()
var req forwardTCPIPChannelRequest
req.ForwardIP = forwardIP
req.ForwardPort = forwardPort
host, port, err := net.SplitHostPort(localConn.RemoteAddr().String())
if err != nil {
logger.Error("failed-to-split-host-port", err)
return
}
req.OriginIP = host
_, err = fmt.Sscanf(port, "%d", &req.OriginPort)
if err != nil {
logger.Error("failed-to-parse-port", err)
return
}
channel, reqs, err := conn.OpenChannel("forwarded-tcpip", ssh.Marshal(req))
if err != nil {
logger.Error("failed-to-open-channel", err)
return
}
defer channel.Close()
go func() {
for r := range reqs {
logger.Info("ignoring-request", lager.Data{
"type": r.Type,
})
r.Reply(false, nil)
}
}()
wg := new(sync.WaitGroup)
pipe := func(to io.WriteCloser, from io.ReadCloser) {
// if either end breaks, close both ends to ensure they're both unblocked,
// otherwise io.Copy can block forever if e.g. reading after write end has
// gone away
defer to.Close()
defer from.Close()
defer wg.Done()
io.Copy(to, from)
}
wg.Add(1)
go pipe(localConn, channel)
wg.Add(1)
go pipe(channel, localConn)
wg.Wait()
}
示例11: marshalKey
func marshalKey(k *Key) []byte {
var record struct {
Blob []byte
Comment string
}
record.Blob = k.Marshal()
record.Comment = k.Comment
return ssh.Marshal(&record)
}
示例12: Marshal
func (ek *Ed25519PublicKey) Marshal() []byte {
wirekey := struct {
Name string
Pub []byte
}{
ek.Type(),
(*ek.bytes)[:],
}
return ssh.Marshal(&wirekey)
}
示例13: init
// init starts the SFTP protocol by negotiating the protocol version to use and
// starts the response handler in a goroutine.
func (s *Client) init() error {
msg := fxpInitMsg{
Version: 3,
}
if err := s.writePacket(ssh.Marshal(msg)); err != nil {
return err
}
packet, err := s.readOnePacket()
if err != nil {
return err
}
resp, err := decodeClient(packet)
if err != nil {
return err
}
switch resp := resp.(type) {
case *fxpVersionMsg:
if resp.Version != 3 {
return errors.New("only version 3 of Client protocol supported")
}
default:
return errors.New("invalid packet received during initialization")
}
vers := resp.(*fxpVersionMsg)
s.exts = make(map[string]extension)
if len(vers.Ext) > 0 {
exts := vers.Ext
for len(exts) > 0 {
ew := extensionWire{}
if err := ssh.Unmarshal(exts, &ew); err != nil {
return err
}
if len(exts) < 2 {
break
}
exts = ew.Rest
e := extension{
Name: ew.Name,
Data: ew.Data,
}
// OpenSSH's sftp-server implementation specifies that
// the data portion of an extension is an ASCII-encoded
// version number. This is not part of the SFTP
// specification, however.
if n, err := strconv.Atoi(ew.Data); err == nil {
e.version = n
}
s.exts[e.Name] = e
}
}
go s.mainLoop()
return nil
}
示例14: sendRequest
// sendRequests sends a request to the server and returns a new channel on
// which the response will be sent.
func (s *Client) sendRequest(req ider) (*fxpChan, error) {
fxpCh, err := s.chans.newChan()
if err != nil {
return nil, err
}
req.SetID(fxpCh.id)
if err := s.writePacket(ssh.Marshal(req)); err != nil {
return nil, err
}
return fxpCh, nil
}
示例15: SendWindowSize
func (inst *instance) SendWindowSize() {
if w, h, err := inst.terminal.GetSize(); err != nil {
color.Redln("Error getting term size:", err)
} else {
msg := ssh.Marshal(&winchMsg{uint32(w), uint32(h), 0, 0})
_, err = inst.session.SendRequest("window-change", false, msg)
if err != nil {
color.Redln("Error sending winch:", err)
}
}
}