mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-09-24 11:51:16 -04:00
Split into sub-packages
This commit is contained in:
parent
838f58e648
commit
531010747e
2
Makefile
2
Makefile
@ -6,7 +6,7 @@ SRCS = %.go
|
|||||||
|
|
||||||
all: $(BINARY)
|
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
|
go build -ldflags "-X main.buildCommit=`git describe --long --tags --dirty --always`" ./cmd/ssh-chat
|
||||||
|
|
||||||
deps:
|
deps:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package sshchat
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -6,7 +6,9 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/shazow/ssh-chat/log"
|
||||||
"github.com/shazow/ssh-chat/sshd"
|
"github.com/shazow/ssh-chat/sshd"
|
||||||
|
"github.com/shazow/ssh-chat/utils"
|
||||||
"golang.org/x/crypto/ssh"
|
"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.
|
// Auth stores lookups for bans, whitelists, and ops. It implements the sshd.Auth interface.
|
||||||
type Auth struct {
|
type Auth struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
bannedAddr *Set
|
bannedAddr *utils.Set
|
||||||
banned *Set
|
banned *utils.Set
|
||||||
whitelist *Set
|
whitelist *utils.Set
|
||||||
ops *Set
|
ops *utils.Set
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAuth creates a new empty Auth.
|
// NewAuth creates a new empty Auth.
|
||||||
func NewAuth() *Auth {
|
func NewAuth() *Auth {
|
||||||
return &Auth{
|
return &Auth{
|
||||||
bannedAddr: NewSet(),
|
bannedAddr: utils.NewSet(),
|
||||||
banned: NewSet(),
|
banned: utils.NewSet(),
|
||||||
whitelist: NewSet(),
|
whitelist: utils.NewSet(),
|
||||||
ops: NewSet(),
|
ops: utils.NewSet(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +95,7 @@ func (a *Auth) Op(key ssh.PublicKey, d time.Duration) {
|
|||||||
} else {
|
} else {
|
||||||
a.ops.Add(authkey)
|
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.
|
// IsOp checks if a public key is an op.
|
||||||
@ -116,7 +118,7 @@ func (a *Auth) Whitelist(key ssh.PublicKey, d time.Duration) {
|
|||||||
} else {
|
} else {
|
||||||
a.whitelist.Add(authkey)
|
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.
|
// Ban will set a public key as banned.
|
||||||
@ -134,7 +136,7 @@ func (a *Auth) BanFingerprint(authkey string, d time.Duration) {
|
|||||||
} else {
|
} else {
|
||||||
a.banned.Add(authkey)
|
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.
|
// Ban will set an IP address as banned.
|
||||||
@ -145,5 +147,5 @@ func (a *Auth) BanAddr(addr net.Addr, d time.Duration) {
|
|||||||
} else {
|
} else {
|
||||||
a.bannedAddr.Add(key)
|
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)
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package sshchat
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
@ -10,14 +10,13 @@ import (
|
|||||||
"os/user"
|
"os/user"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alexcesaro/log"
|
|
||||||
"github.com/alexcesaro/log/golog"
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
|
|
||||||
"github.com/shazow/ssh-chat"
|
"github.com/shazow/ssh-chat/auth"
|
||||||
"github.com/shazow/ssh-chat/chat"
|
|
||||||
"github.com/shazow/ssh-chat/chat/message"
|
"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"
|
"github.com/shazow/ssh-chat/sshd"
|
||||||
)
|
)
|
||||||
import _ "net/http/pprof"
|
import _ "net/http/pprof"
|
||||||
@ -34,12 +33,6 @@ type Options struct {
|
|||||||
Pprof int `long:"pprof" description:"Enable pprof http server for profiling."`
|
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{}) {
|
func fail(code int, format string, args ...interface{}) {
|
||||||
fmt.Fprintf(os.Stderr, format, args...)
|
fmt.Fprintf(os.Stderr, format, args...)
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
@ -63,20 +56,8 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Figure out the log level
|
|
||||||
numVerbose := len(options.Verbose)
|
numVerbose := len(options.Verbose)
|
||||||
if numVerbose > len(logLevels) {
|
log.Init(numVerbose)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
privateKeyPath := options.Identity
|
privateKeyPath := options.Identity
|
||||||
if strings.HasPrefix(privateKeyPath, "~/") {
|
if strings.HasPrefix(privateKeyPath, "~/") {
|
||||||
@ -96,7 +77,7 @@ func main() {
|
|||||||
fail(3, "Failed to parse key: %v\n", err)
|
fail(3, "Failed to parse key: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := sshchat.NewAuth()
|
auth := auth.NewAuth()
|
||||||
config := sshd.MakeAuth(auth)
|
config := sshd.MakeAuth(auth)
|
||||||
config.AddHostKey(signer)
|
config.AddHostKey(signer)
|
||||||
|
|
||||||
@ -109,7 +90,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Printf("Listening for connections on %v\n", s.Addr().String())
|
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])
|
host.SetTheme(message.Themes[0])
|
||||||
|
|
||||||
err = fromFile(options.Admin, func(line []byte) error {
|
err = fromFile(options.Admin, func(line []byte) error {
|
||||||
|
11
godoc.go
11
godoc.go
@ -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
|
|
@ -1,4 +1,4 @@
|
|||||||
package sshchat
|
package host
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -8,8 +8,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shazow/rateio"
|
"github.com/shazow/rateio"
|
||||||
|
"github.com/shazow/ssh-chat/auth"
|
||||||
"github.com/shazow/ssh-chat/chat"
|
"github.com/shazow/ssh-chat/chat"
|
||||||
"github.com/shazow/ssh-chat/chat/message"
|
"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"
|
"github.com/shazow/ssh-chat/sshd"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,7 +37,7 @@ type Host struct {
|
|||||||
commands chat.Commands
|
commands chat.Commands
|
||||||
|
|
||||||
motd string
|
motd string
|
||||||
auth *Auth
|
auth *auth.Auth
|
||||||
count int
|
count int
|
||||||
|
|
||||||
// Default theme
|
// Default theme
|
||||||
@ -42,7 +45,7 @@ type Host struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewHost creates a Host on top of an existing listener.
|
// 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()
|
room := chat.NewRoom()
|
||||||
h := Host{
|
h := Host{
|
||||||
Room: room,
|
Room: room,
|
||||||
@ -80,7 +83,7 @@ func (h Host) isOp(conn sshd.Connection) bool {
|
|||||||
|
|
||||||
// Connect a specific Terminal to this host and its room.
|
// Connect a specific Terminal to this host and its room.
|
||||||
func (h *Host) Connect(term *sshd.Terminal) {
|
func (h *Host) Connect(term *sshd.Terminal) {
|
||||||
id := NewIdentity(term.Conn)
|
id := identity.NewIdentity(term.Conn)
|
||||||
user := message.NewUserScreen(id, term)
|
user := message.NewUserScreen(id, term)
|
||||||
user.Config.Theme = &h.theme
|
user.Config.Theme = &h.theme
|
||||||
go func() {
|
go func() {
|
||||||
@ -102,7 +105,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
|
|||||||
member, err = h.Join(user)
|
member, err = h.Join(user)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Failed to join: %s", err)
|
log.Logger.Errorf("Failed to join: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +125,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
|
|||||||
// Closed
|
// Closed
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
logger.Errorf("Terminal reading error: %s", err)
|
log.Logger.Errorf("Terminal reading error: %s", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +162,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
|
|||||||
|
|
||||||
err = h.Leave(user)
|
err = h.Leave(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Failed to leave: %s", err)
|
log.Logger.Errorf("Failed to leave: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -313,7 +316,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
|
|||||||
return errors.New("user not found")
|
return errors.New("user not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
id := target.Identifier.(*Identity)
|
id := target.Identifier.(*identity.Identity)
|
||||||
room.Send(message.NewSystemMsg(id.Whois(), msg.From()))
|
room.Send(message.NewSystemMsg(id.Whois(), msg.From()))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -392,7 +395,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
|
|||||||
until, _ = time.ParseDuration(args[1])
|
until, _ = time.ParseDuration(args[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
id := target.Identifier.(*Identity)
|
id := target.Identifier.(*identity.Identity)
|
||||||
h.auth.Ban(id.PublicKey(), until)
|
h.auth.Ban(id.PublicKey(), until)
|
||||||
h.auth.BanAddr(id.RemoteAddr(), until)
|
h.auth.BanAddr(id.RemoteAddr(), until)
|
||||||
|
|
||||||
@ -400,7 +403,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
|
|||||||
room.Send(message.NewAnnounceMsg(body))
|
room.Send(message.NewAnnounceMsg(body))
|
||||||
target.Close()
|
target.Close()
|
||||||
|
|
||||||
logger.Debugf("Banned: \n-> %s", id.Whois())
|
log.Logger.Debugf("Banned: \n-> %s", id.Whois())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@ -458,7 +461,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
|
|||||||
return errors.New("user not found")
|
return errors.New("user not found")
|
||||||
}
|
}
|
||||||
member.Op = true
|
member.Op = true
|
||||||
id := member.Identifier.(*Identity)
|
id := member.Identifier.(*identity.Identity)
|
||||||
h.auth.Op(id.PublicKey(), until)
|
h.auth.Op(id.PublicKey(), until)
|
||||||
|
|
||||||
body := fmt.Sprintf("Made op by %s.", msg.From().Name())
|
body := fmt.Sprintf("Made op by %s.", msg.From().Name())
|
@ -1,4 +1,4 @@
|
|||||||
package sshchat
|
package host
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
@ -1,4 +1,4 @@
|
|||||||
package sshchat
|
package identity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
41
log/logger.go
Normal file
41
log/logger.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
20
logger.go
20
logger.go
@ -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))
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package sshchat
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
@ -1,4 +1,4 @@
|
|||||||
package sshchat
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
Loading…
x
Reference in New Issue
Block a user