Skip to content

Commit

Permalink
Use logging.Logger with the appropriate level instead of debug func
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 15, 2019
1 parent 837c95d commit b55c431
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
6 changes: 6 additions & 0 deletions cmd/apps/therealssh/therealssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"flag"
"log"

"github.com/sirupsen/logrus"
"github.com/skycoin/skycoin/src/util/logging"

homedir "github.com/mitchellh/go-homedir"

ssh "github.com/skycoin/skywire/internal/therealssh"
Expand All @@ -32,6 +35,9 @@ func main() {
}

ssh.Debug = *debug
if !ssh.Debug {
logging.SetLevel(logrus.InfoLevel)
}

auth, err := ssh.NewFileAuthorizer(path)
if err != nil {
Expand Down
28 changes: 12 additions & 16 deletions internal/therealssh/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"sync"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/kr/pty"
"github.com/skycoin/dmsg/cipher"

Expand All @@ -27,6 +29,8 @@ var Debug = false

// SSHChannel defines communication channel parameters.
type SSHChannel struct {
log *logging.Logger

RemoteID uint32
RemoteAddr *app.Addr

Expand All @@ -46,8 +50,8 @@ type SSHChannel struct {

// OpenChannel constructs new SSHChannel with empty Session.
func OpenChannel(remoteID uint32, remoteAddr *app.Addr, conn net.Conn) *SSHChannel {
return &SSHChannel{RemoteID: remoteID, conn: conn, RemoteAddr: remoteAddr, msgCh: make(chan []byte),
dataCh: make(chan []byte), done: make(chan struct{})}
return &SSHChannel{log: logging.MustGetLogger("ssh_channel"), RemoteID: remoteID, conn: conn,
RemoteAddr: remoteAddr, msgCh: make(chan []byte), dataCh: make(chan []byte), done: make(chan struct{})}
}

// OpenClientChannel constructs new client SSHChannel with empty Session.
Expand Down Expand Up @@ -80,7 +84,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)
sshCh.log.Debugf("sending request %x", requestType)
req := append([]byte{byte(requestType)}, payload...)

if err := sshCh.Send(CmdChannelRequest, req); err != nil {
Expand All @@ -99,7 +103,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])
sshCh.log.Debugf("new request %x", data[0])
switch RequestType(data[0]) {
case RequestPTY:
var u *user.User
Expand Down Expand Up @@ -150,7 +154,7 @@ func (sshCh *SSHChannel) SocketPath() string {
// ServeSocket starts socket handling loop.
func (sshCh *SSHChannel) ServeSocket() error {
os.Remove(sshCh.SocketPath())
debug("waiting for new socket connections on: %s", sshCh.SocketPath())
sshCh.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)
Expand All @@ -164,7 +168,7 @@ func (sshCh *SSHChannel) ServeSocket() error {
return fmt.Errorf("failed to accept connection: %s", err)
}

debug("got new socket connection")
sshCh.log.Debugln("got new socket connection")
defer func() {
conn.Close()
sshCh.closeListener() //nolint:errcheck
Expand All @@ -191,7 +195,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)
sshCh.log.Debugf("starting new session for %s with %#v", user.Username, sz)
sshCh.session, err = OpenSession(user, sz)
if err != nil {
sshCh.session = nil
Expand All @@ -218,7 +222,7 @@ func (sshCh *SSHChannel) Start(command string) error {
}
}()

debug("starting new pty process %s", command)
sshCh.log.Debugf("starting new pty process %s", command)
return sshCh.session.Start(command)
}

Expand Down Expand Up @@ -321,14 +325,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)
Expand Down
21 changes: 12 additions & 9 deletions internal/therealssh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/kr/pty"
"github.com/skycoin/dmsg/cipher"

Expand All @@ -28,13 +30,14 @@ type Dialer interface {
// Client proxies CLI's requests to a remote server. Control messages
// are sent via RPC interface. PTY data is exchanged via unix socket.
type Client struct {
log *logging.Logger
dialer Dialer
chans *chanList
}

// NewClient construct new RPC listener and Client from a given RPC address and app dialer.
func NewClient(rpcAddr string, d Dialer) (net.Listener, *Client, error) {
client := &Client{chans: newChanList(), dialer: d}
client := &Client{log: logging.MustGetLogger("therealssh_client"), chans: newChanList(), dialer: d}
rpcClient := &RPCClient{client}
if err := rpc.Register(rpcClient); err != nil {
return nil, nil, fmt.Errorf("RPC register failure: %s", err)
Expand Down Expand Up @@ -63,7 +66,7 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSH
}

sshCh = OpenClientChannel(0, remotePK, conn)
debug("sending channel open command")
c.log.Debugln("sending channel open command")
localID = c.chans.add(sshCh)
req := appendU32([]byte{byte(CmdChannelOpen)}, localID)
if _, err := conn.Write(req); err != nil {
Expand All @@ -77,9 +80,9 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSH
}
}()

debug("waiting for channel open response")
c.log.Debugln("waiting for channel open response")
data := <-sshCh.msgCh
debug("got channel open response")
c.log.Debugln("got channel open response")
if data[0] == ResponseFail {
cErr = fmt.Errorf("failed to open channel: %s", string(data[1:]))
return
Expand Down Expand Up @@ -127,7 +130,7 @@ func (c *Client) serveConn(conn net.Conn) error {
}

data := payload[5:]
debug("got new command: %x", payload[0])
c.log.Debugf("got new command: %x", payload[0])
switch CommandType(payload[0]) {
case CmdChannelOpenResponse, CmdChannelResponse:
sshCh.msgCh <- data
Expand Down Expand Up @@ -169,13 +172,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")
rpc.c.log.Debugln("requesting SSH channel")
localID, channel, err := rpc.c.OpenChannel(args.RemotePK)
if err != nil {
return err
}

debug("requesting PTY session")
rpc.c.log.Debugln("requesting PTY session")
if _, err := channel.Request(RequestPTY, args.ToBinary()); err != nil {
return fmt.Errorf("PTY request failure: %s", err)
}
Expand All @@ -191,7 +194,7 @@ func (rpc *RPCClient) Exec(args *ExecArgs, socketPath *string) error {
return errors.New("unknown channel")
}

debug("requesting shell process")
rpc.c.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)
Expand All @@ -204,7 +207,7 @@ func (rpc *RPCClient) Exec(args *ExecArgs, socketPath *string) error {

waitCh := make(chan bool)
go func() {
debug("starting socket listener")
rpc.c.log.Debugln("starting socket listener")
waitCh <- true
if err := sshCh.ServeSocket(); err != nil {
log.Println("Session failure:", err)
Expand Down
6 changes: 4 additions & 2 deletions internal/therealssh/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"testing"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/dmsg/cipher"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -15,7 +17,7 @@ import (
func TestClientOpenChannel(t *testing.T) {
pk, _ := cipher.GenerateKeyPair()
conn, dialer := newPipeDialer()
c := &Client{dialer, newChanList()}
c := &Client{logging.MustGetLogger("therealssh_client"), dialer, newChanList()}

type data struct {
ch *SSHChannel
Expand Down Expand Up @@ -46,7 +48,7 @@ func TestClientOpenChannel(t *testing.T) {

func TestClientHandleResponse(t *testing.T) {
pk, _ := cipher.GenerateKeyPair()
c := &Client{nil, newChanList()}
c := &Client{logging.MustGetLogger("therealssh_client"), nil, newChanList()}
in, out := net.Pipe()
errCh := make(chan error)
go func() {
Expand Down
13 changes: 8 additions & 5 deletions internal/therealssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"net"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/dmsg/cipher"

"github.com/skycoin/skywire/pkg/app"
Expand Down Expand Up @@ -56,18 +58,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 *app.Addr, remoteID uint32, conn net.Conn) error {
debug("opening new channel")
s.log.Debugln("opening new channel")
channel := OpenChannel(remoteID, remoteAddr, conn)
var res []byte

Expand All @@ -77,14 +80,14 @@ func (s *Server) OpenChannel(remoteAddr *app.Addr, remoteID uint32, conn net.Con
res = appendU32([]byte{ResponseConfirm}, s.chans.add(channel))
}

debug("sending response")
s.log.Debugln("sending response")
if err := channel.Send(CmdChannelOpenResponse, res); err != nil {
channel.Close()
return fmt.Errorf("channel response failure: %s", err)
}

go func() {
debug("listening for channel requests")
s.log.Debugln("listening for channel requests")
if err := channel.Serve(); err != nil {
log.Println("channel failure:", err)
}
Expand Down Expand Up @@ -157,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)
Expand Down

0 comments on commit b55c431

Please sign in to comment.