Split into sub-packages

This commit is contained in:
James Mills 2016-07-09 13:14:53 -07:00
parent 838f58e648
commit 531010747e
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
12 changed files with 82 additions and 86 deletions

View File

@ -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:

View File

@ -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)
}

View File

@ -1,4 +1,4 @@
package sshchat
package auth
import (
"crypto/rand"

View File

@ -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 {

View File

@ -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

View File

@ -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())

View File

@ -1,4 +1,4 @@
package sshchat
package host
import (
"bufio"

View File

@ -1,4 +1,4 @@
package sshchat
package identity
import (
"fmt"

41
log/logger.go Normal file
View File

@ -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)
}
}

View File

@ -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))
}

View File

@ -1,4 +1,4 @@
package sshchat
package utils
import (
"sync"

View File

@ -1,4 +1,4 @@
package sshchat
package utils
import (
"testing"