diff --git a/cmd/apps/therealssh-client/therealssh-client.go b/cmd/apps/therealssh-client/therealssh-client.go index 95e2be4da..741797d1b 100644 --- a/cmd/apps/therealssh-client/therealssh-client.go +++ b/cmd/apps/therealssh-client/therealssh-client.go @@ -8,6 +8,9 @@ import ( "log" "net/http" + "github.com/sirupsen/logrus" + "github.com/skycoin/skycoin/src/util/logging" + ssh "github.com/skycoin/skywire/internal/therealssh" "github.com/skycoin/skywire/pkg/app" ) @@ -29,6 +32,9 @@ func main() { }() ssh.Debug = *debug + if !ssh.Debug { + logging.SetLevel(logrus.InfoLevel) + } rpc, client, err := ssh.NewClient(*rpcAddr, sshApp) if err != nil { diff --git a/cmd/apps/therealssh/therealssh.go b/cmd/apps/therealssh/therealssh.go index 6456d392f..ce4c84ec1 100644 --- a/cmd/apps/therealssh/therealssh.go +++ b/cmd/apps/therealssh/therealssh.go @@ -8,6 +8,8 @@ import ( "log" "github.com/mitchellh/go-homedir" + "github.com/sirupsen/logrus" + "github.com/skycoin/skycoin/src/util/logging" ssh "github.com/skycoin/skywire/internal/therealssh" "github.com/skycoin/skywire/pkg/app" @@ -36,6 +38,9 @@ func main() { } ssh.Debug = *debug + if !ssh.Debug { + logging.SetLevel(logrus.InfoLevel) + } auth, err := ssh.NewFileAuthorizer(path) if err != nil { diff --git a/internal/therealssh/channel.go b/internal/therealssh/channel.go index d2b92f4ec..5e66e40fe 100644 --- a/internal/therealssh/channel.go +++ b/internal/therealssh/channel.go @@ -79,7 +79,7 @@ func (sshCh *SSHChannel) Write(p []byte) (n int, err error) { // Request sends request message and waits for response. func (sshCh *SSHChannel) Request(requestType RequestType, payload []byte) ([]byte, error) { - debug("sending request %x", requestType) + log.Debugf("sending request %x", requestType) req := append([]byte{byte(requestType)}, payload...) if err := sshCh.Send(CmdChannelRequest, req); err != nil { @@ -98,7 +98,7 @@ func (sshCh *SSHChannel) Request(requestType RequestType, payload []byte) ([]byt func (sshCh *SSHChannel) Serve() error { for data := range sshCh.msgCh { var err error - debug("new request %x", data[0]) + log.Debugf("new request %x", data[0]) switch RequestType(data[0]) { case RequestPTY: var u *user.User @@ -152,7 +152,7 @@ func (sshCh *SSHChannel) ServeSocket() error { log.WithError(err).Warn("Failed to remove SSH channel socket file") } - debug("waiting for new socket connections on: %s", sshCh.SocketPath()) + log.Debugf("waiting for new socket connections on: %s", sshCh.SocketPath()) l, err := net.ListenUnix("unix", &net.UnixAddr{Name: sshCh.SocketPath(), Net: "unix"}) if err != nil { return fmt.Errorf("failed to open unix socket: %s", err) @@ -166,7 +166,7 @@ func (sshCh *SSHChannel) ServeSocket() error { return fmt.Errorf("failed to accept connection: %s", err) } - debug("got new socket connection") + log.Debugln("got new socket connection") defer func() { if err := conn.Close(); err != nil { log.WithError(err).Warn("Failed to close connection") @@ -181,7 +181,7 @@ func (sshCh *SSHChannel) ServeSocket() error { go func() { if _, err := io.Copy(sshCh, conn); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { - log.Println("failed to write to server:", err) + log.Errorf("failed to write to server:", err) return } }() @@ -199,7 +199,7 @@ func (sshCh *SSHChannel) OpenPTY(user *user.User, sz *pty.Winsize) (err error) { return errors.New("session is already started") } - debug("starting new session for %s with %#v", user.Username, sz) + log.Debugf("starting new session for %s with %#v", user.Username, sz) sshCh.session, err = OpenSession(user, sz) if err != nil { sshCh.session = nil @@ -226,7 +226,7 @@ func (sshCh *SSHChannel) Start(command string) error { } }() - debug("starting new pty process %s", command) + log.Debugf("starting new pty process %s", command) return sshCh.session.Start(command) } @@ -333,14 +333,6 @@ func (sshCh *SSHChannel) closeListener() error { return sshCh.listener.Close() } -func debug(format string, v ...interface{}) { - if !Debug { - return - } - - log.Printf(format, v...) -} - func appendU32(buf []byte, n uint32) []byte { uintBuf := make([]byte, 4) binary.BigEndian.PutUint32(uintBuf[0:], n) diff --git a/internal/therealssh/client.go b/internal/therealssh/client.go index 39ec48ce3..8abeb805d 100644 --- a/internal/therealssh/client.go +++ b/internal/therealssh/client.go @@ -57,7 +57,7 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSH } sshCh = OpenClientChannel(0, remotePK, conn) - debug("sending channel open command") + log.Debugln("sending channel open command") localID = c.chans.add(sshCh) req := appendU32([]byte{byte(CmdChannelOpen)}, localID) if _, err := conn.Write(req); err != nil { @@ -71,9 +71,9 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSH } }() - debug("waiting for channel open response") + log.Debugln("waiting for channel open response") data := <-sshCh.msgCh - debug("got channel open response") + log.Debugln("got channel open response") if data[0] == ResponseFail { cErr = fmt.Errorf("failed to open channel: %s", string(data[1:])) return @@ -121,7 +121,7 @@ func (c *Client) serveConn(conn net.Conn) error { } data := payload[5:] - debug("got new command: %x", payload[0]) + log.Debugf("got new command: %x", payload[0]) switch CommandType(payload[0]) { case CmdChannelOpenResponse, CmdChannelResponse: sshCh.msgCh <- data @@ -165,13 +165,13 @@ type RPCClient struct { // RequestPTY defines RPC request for a new PTY session. func (rpc *RPCClient) RequestPTY(args *RequestPTYArgs, channelID *uint32) error { - debug("requesting SSH channel") + log.Debugln("requesting SSH channel") localID, channel, err := rpc.c.OpenChannel(args.RemotePK) if err != nil { return err } - debug("requesting PTY session") + log.Debugln("requesting PTY session") if _, err := channel.Request(RequestPTY, args.ToBinary()); err != nil { return fmt.Errorf("PTY request failure: %s", err) } @@ -187,7 +187,7 @@ func (rpc *RPCClient) Exec(args *ExecArgs, socketPath *string) error { return errors.New("unknown channel") } - debug("requesting shell process") + log.Debugln("requesting shell process") if args.CommandWithArgs == nil { if _, err := sshCh.Request(RequestShell, nil); err != nil { return fmt.Errorf("shell request failure: %s", err) @@ -200,7 +200,7 @@ func (rpc *RPCClient) Exec(args *ExecArgs, socketPath *string) error { waitCh := make(chan bool) go func() { - debug("starting socket listener") + log.Debugln("starting socket listener") waitCh <- true if err := sshCh.ServeSocket(); err != nil { log.Error("Session failure:", err) diff --git a/internal/therealssh/server.go b/internal/therealssh/server.go index 1a57fea04..9c78169c3 100644 --- a/internal/therealssh/server.go +++ b/internal/therealssh/server.go @@ -8,6 +8,7 @@ import ( "net" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" "github.com/skycoin/skywire/pkg/routing" ) @@ -55,18 +56,19 @@ var responseUnauthorized = append([]byte{ResponseFail}, []byte("unauthorized").. // Server handles remote PTY data exchange. type Server struct { + log *logging.Logger auth Authorizer chans *chanList } // NewServer constructs new Server. func NewServer(auth Authorizer) *Server { - return &Server{auth, newChanList()} + return &Server{logging.MustGetLogger("therealssh_server"), auth, newChanList()} } // OpenChannel opens new client channel. func (s *Server) OpenChannel(remoteAddr routing.Addr, remoteID uint32, conn net.Conn) error { - debug("opening new channel") + log.Debugln("opening new channel") channel := OpenChannel(remoteID, remoteAddr, conn) var res []byte @@ -76,7 +78,7 @@ func (s *Server) OpenChannel(remoteAddr routing.Addr, remoteID uint32, conn net. res = appendU32([]byte{ResponseConfirm}, s.chans.add(channel)) } - debug("sending response") + s.log.Debugln("sending response") if err := channel.Send(CmdChannelOpenResponse, res); err != nil { if err := channel.Close(); err != nil { log.WithError(err).Warn("Failed to close channel") @@ -85,7 +87,7 @@ func (s *Server) OpenChannel(remoteAddr routing.Addr, remoteID uint32, conn net. } go func() { - debug("listening for channel requests") + s.log.Debugln("listening for channel requests") if err := channel.Serve(); err != nil { log.Error("channel failure:", err) } @@ -158,7 +160,7 @@ func (s *Server) Serve(conn net.Conn) error { payloadID := binary.BigEndian.Uint32(payload[1:]) data := payload[5:] - debug("got new command: %x", payload[0]) + s.log.Debugf("got new command: %x", payload[0]) switch CommandType(payload[0]) { case CmdChannelOpen: err = s.OpenChannel(raddr, payloadID, conn)