diff --git a/Makefile b/Makefile index 735c767..eb3a016 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ SRCS = %.go all: $(BINARY) -$(BINARY): deps **/**/*.go **/*.go *.go +$(BINARY): deps **/**/*.go **/*.go go build -ldflags "-X main.buildCommit=`git describe --long --tags --dirty --always`" ./cmd/ssh-chat deps: diff --git a/auth.go b/auth/auth.go similarity index 84% rename from auth.go rename to auth/auth.go index 4d27ca4..0f6a8f8 100644 --- a/auth.go +++ b/auth/auth.go @@ -1,4 +1,4 @@ -package sshchat +package auth import ( "errors" @@ -6,7 +6,9 @@ import ( "sync" "time" + "github.com/shazow/ssh-chat/log" "github.com/shazow/ssh-chat/sshd" + "github.com/shazow/ssh-chat/utils" "golang.org/x/crypto/ssh" ) @@ -37,19 +39,19 @@ func newAuthAddr(addr net.Addr) string { // Auth stores lookups for bans, whitelists, and ops. It implements the sshd.Auth interface. type Auth struct { sync.RWMutex - bannedAddr *Set - banned *Set - whitelist *Set - ops *Set + bannedAddr *utils.Set + banned *utils.Set + whitelist *utils.Set + ops *utils.Set } // NewAuth creates a new empty Auth. func NewAuth() *Auth { return &Auth{ - bannedAddr: NewSet(), - banned: NewSet(), - whitelist: NewSet(), - ops: NewSet(), + bannedAddr: utils.NewSet(), + banned: utils.NewSet(), + whitelist: utils.NewSet(), + ops: utils.NewSet(), } } @@ -93,7 +95,7 @@ func (a *Auth) Op(key ssh.PublicKey, d time.Duration) { } else { a.ops.Add(authkey) } - logger.Debugf("Added to ops: %s (for %s)", authkey, d) + log.Logger.Debugf("Added to ops: %s (for %s)", authkey, d) } // IsOp checks if a public key is an op. @@ -116,7 +118,7 @@ func (a *Auth) Whitelist(key ssh.PublicKey, d time.Duration) { } else { a.whitelist.Add(authkey) } - logger.Debugf("Added to whitelist: %s (for %s)", authkey, d) + log.Logger.Debugf("Added to whitelist: %s (for %s)", authkey, d) } // Ban will set a public key as banned. @@ -134,7 +136,7 @@ func (a *Auth) BanFingerprint(authkey string, d time.Duration) { } else { a.banned.Add(authkey) } - logger.Debugf("Added to banned: %s (for %s)", authkey, d) + log.Logger.Debugf("Added to banned: %s (for %s)", authkey, d) } // Ban will set an IP address as banned. @@ -145,5 +147,5 @@ func (a *Auth) BanAddr(addr net.Addr, d time.Duration) { } else { a.bannedAddr.Add(key) } - logger.Debugf("Added to bannedAddr: %s (for %s)", key, d) + log.Logger.Debugf("Added to bannedAddr: %s (for %s)", key, d) } diff --git a/auth_test.go b/auth/auth_test.go similarity index 98% rename from auth_test.go rename to auth/auth_test.go index 4692557..f885083 100644 --- a/auth_test.go +++ b/auth/auth_test.go @@ -1,4 +1,4 @@ -package sshchat +package auth import ( "crypto/rand" diff --git a/cmd/ssh-chat/cmd.go b/cmd/ssh-chat/cmd.go index a4e02c3..c0187b4 100644 --- a/cmd/ssh-chat/cmd.go +++ b/cmd/ssh-chat/cmd.go @@ -10,14 +10,13 @@ import ( "os/user" "strings" - "github.com/alexcesaro/log" - "github.com/alexcesaro/log/golog" "github.com/jessevdk/go-flags" "golang.org/x/crypto/ssh" - "github.com/shazow/ssh-chat" - "github.com/shazow/ssh-chat/chat" + "github.com/shazow/ssh-chat/auth" "github.com/shazow/ssh-chat/chat/message" + "github.com/shazow/ssh-chat/host" + "github.com/shazow/ssh-chat/log" "github.com/shazow/ssh-chat/sshd" ) import _ "net/http/pprof" @@ -34,12 +33,6 @@ type Options struct { Pprof int `long:"pprof" description:"Enable pprof http server for profiling."` } -var logLevels = []log.Level{ - log.Warning, - log.Info, - log.Debug, -} - func fail(code int, format string, args ...interface{}) { fmt.Fprintf(os.Stderr, format, args...) os.Exit(code) @@ -63,20 +56,8 @@ func main() { }() } - // Figure out the log level numVerbose := len(options.Verbose) - if numVerbose > len(logLevels) { - numVerbose = len(logLevels) - 1 - } - - logLevel := logLevels[numVerbose] - sshchat.SetLogger(golog.New(os.Stderr, logLevel)) - - if logLevel == log.Debug { - // Enable logging from submodules - chat.SetLogger(os.Stderr) - sshd.SetLogger(os.Stderr) - } + log.Init(numVerbose) privateKeyPath := options.Identity if strings.HasPrefix(privateKeyPath, "~/") { @@ -96,7 +77,7 @@ func main() { fail(3, "Failed to parse key: %v\n", err) } - auth := sshchat.NewAuth() + auth := auth.NewAuth() config := sshd.MakeAuth(auth) config.AddHostKey(signer) @@ -109,7 +90,7 @@ func main() { fmt.Printf("Listening for connections on %v\n", s.Addr().String()) - host := sshchat.NewHost(s, auth) + host := host.NewHost(s, auth) host.SetTheme(message.Themes[0]) err = fromFile(options.Admin, func(line []byte) error { diff --git a/godoc.go b/godoc.go deleted file mode 100644 index 8b4842b..0000000 --- a/godoc.go +++ /dev/null @@ -1,11 +0,0 @@ -/* -sshchat package is an implementation of an ssh server which serves a chat room -instead of a shell. - -sshd subdirectory contains the ssh-related pieces which know nothing about chat. - -chat subdirectory contains the chat-related pieces which know nothing about ssh. - -The Host type is the glue between the sshd and chat pieces. -*/ -package sshchat diff --git a/host.go b/host/host.go similarity index 94% rename from host.go rename to host/host.go index ca17f70..bfcc557 100644 --- a/host.go +++ b/host/host.go @@ -1,4 +1,4 @@ -package sshchat +package host import ( "errors" @@ -8,8 +8,11 @@ import ( "time" "github.com/shazow/rateio" + "github.com/shazow/ssh-chat/auth" "github.com/shazow/ssh-chat/chat" "github.com/shazow/ssh-chat/chat/message" + "github.com/shazow/ssh-chat/identity" + "github.com/shazow/ssh-chat/log" "github.com/shazow/ssh-chat/sshd" ) @@ -34,7 +37,7 @@ type Host struct { commands chat.Commands motd string - auth *Auth + auth *auth.Auth count int // Default theme @@ -42,7 +45,7 @@ type Host struct { } // NewHost creates a Host on top of an existing listener. -func NewHost(listener *sshd.SSHListener, auth *Auth) *Host { +func NewHost(listener *sshd.SSHListener, auth *auth.Auth) *Host { room := chat.NewRoom() h := Host{ Room: room, @@ -80,7 +83,7 @@ func (h Host) isOp(conn sshd.Connection) bool { // Connect a specific Terminal to this host and its room. func (h *Host) Connect(term *sshd.Terminal) { - id := NewIdentity(term.Conn) + id := identity.NewIdentity(term.Conn) user := message.NewUserScreen(id, term) user.Config.Theme = &h.theme go func() { @@ -102,7 +105,7 @@ func (h *Host) Connect(term *sshd.Terminal) { member, err = h.Join(user) } if err != nil { - logger.Errorf("Failed to join: %s", err) + log.Logger.Errorf("Failed to join: %s", err) return } @@ -122,7 +125,7 @@ func (h *Host) Connect(term *sshd.Terminal) { // Closed break } else if err != nil { - logger.Errorf("Terminal reading error: %s", err) + log.Logger.Errorf("Terminal reading error: %s", err) break } @@ -159,7 +162,7 @@ func (h *Host) Connect(term *sshd.Terminal) { err = h.Leave(user) if err != nil { - logger.Errorf("Failed to leave: %s", err) + log.Logger.Errorf("Failed to leave: %s", err) return } } @@ -313,7 +316,7 @@ func (h *Host) InitCommands(c *chat.Commands) { return errors.New("user not found") } - id := target.Identifier.(*Identity) + id := target.Identifier.(*identity.Identity) room.Send(message.NewSystemMsg(id.Whois(), msg.From())) return nil @@ -392,7 +395,7 @@ func (h *Host) InitCommands(c *chat.Commands) { until, _ = time.ParseDuration(args[1]) } - id := target.Identifier.(*Identity) + id := target.Identifier.(*identity.Identity) h.auth.Ban(id.PublicKey(), until) h.auth.BanAddr(id.RemoteAddr(), until) @@ -400,7 +403,7 @@ func (h *Host) InitCommands(c *chat.Commands) { room.Send(message.NewAnnounceMsg(body)) target.Close() - logger.Debugf("Banned: \n-> %s", id.Whois()) + log.Logger.Debugf("Banned: \n-> %s", id.Whois()) return nil }, @@ -458,7 +461,7 @@ func (h *Host) InitCommands(c *chat.Commands) { return errors.New("user not found") } member.Op = true - id := member.Identifier.(*Identity) + id := member.Identifier.(*identity.Identity) h.auth.Op(id.PublicKey(), until) body := fmt.Sprintf("Made op by %s.", msg.From().Name()) diff --git a/host_test.go b/host/host_test.go similarity index 99% rename from host_test.go rename to host/host_test.go index f804f28..22aeec9 100644 --- a/host_test.go +++ b/host/host_test.go @@ -1,4 +1,4 @@ -package sshchat +package host import ( "bufio" diff --git a/identity.go b/identity/identity.go similarity index 98% rename from identity.go rename to identity/identity.go index 21988e3..5a19586 100644 --- a/identity.go +++ b/identity/identity.go @@ -1,4 +1,4 @@ -package sshchat +package identity import ( "fmt" diff --git a/log/logger.go b/log/logger.go new file mode 100644 index 0000000..6e6422a --- /dev/null +++ b/log/logger.go @@ -0,0 +1,41 @@ +package log + +import ( + "os" + + "github.com/alexcesaro/log" + "github.com/alexcesaro/log/golog" + "github.com/shazow/ssh-chat/chat" + "github.com/shazow/ssh-chat/sshd" +) + +var logLevels = []log.Level{ + log.Warning, + log.Info, + log.Debug, +} + +// Logger Global Logger +var Logger *golog.Logger + +// SetLogger Set the global logger +func SetLogger(l *golog.Logger) { + Logger = l +} + +// Init Initialize the global logger +func Init(numVerbose int) { + // Figure out the log level + if numVerbose > len(logLevels) { + numVerbose = len(logLevels) - 1 + } + + logLevel := logLevels[numVerbose] + SetLogger(golog.New(os.Stderr, logLevel)) + + if logLevel == log.Debug { + // Enable logging from submodules + chat.SetLogger(os.Stderr) + sshd.SetLogger(os.Stderr) + } +} diff --git a/logger.go b/logger.go deleted file mode 100644 index d1c64ac..0000000 --- a/logger.go +++ /dev/null @@ -1,20 +0,0 @@ -package sshchat - -import ( - "bytes" - - "github.com/alexcesaro/log" - "github.com/alexcesaro/log/golog" -) - -var logger *golog.Logger - -func SetLogger(l *golog.Logger) { - logger = l -} - -func init() { - // Set a default null logger - var b bytes.Buffer - SetLogger(golog.New(&b, log.Debug)) -} diff --git a/set.go b/utils/set.go similarity index 98% rename from set.go rename to utils/set.go index f0d607c..4649186 100644 --- a/set.go +++ b/utils/set.go @@ -1,4 +1,4 @@ -package sshchat +package utils import ( "sync" diff --git a/set_test.go b/utils/set_test.go similarity index 98% rename from set_test.go rename to utils/set_test.go index 1d7fbef..8316357 100644 --- a/set_test.go +++ b/utils/set_test.go @@ -1,4 +1,4 @@ -package sshchat +package utils import ( "testing"