Merge remote-tracking branch 'origin/master' into cap-protocol

Conflicts:
	irc/server.go
This commit is contained in:
Jeremy Latt 2014-03-06 10:34:50 -08:00
commit d54f530d13
12 changed files with 194 additions and 222 deletions

View File

@ -6,12 +6,11 @@ and issues are welcome.
## Some Features ## Some Features
- follows the RFC where possible - follows the RFC where possible
- JSON-based configuration - gcfg gitconfig-style configuration
- server password - server password (PASS command)
- channels with many standard modes - channels with most standard modes
- IRC operators - IRC operators (OPER command)
- TLS support (but better to use stunnel with proxy protocol) - haproxy [PROXY protocol](http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt) header for hostname setting
- haproxy PROXY protocol header for hostname setting
- passwords stored in bcrypt format - passwords stored in bcrypt format
- channels that persist between restarts (+P) - channels that persist between restarts (+P)
@ -23,27 +22,39 @@ I wanted to learn Go.
"Ergonomadic" is an anagram of "Go IRC Daemon". "Ergonomadic" is an anagram of "Go IRC Daemon".
## What about SSL/TLS support?
Go has a not-yet-verified-as-safe TLS 1.2 implementation. Sadly, many
popular IRC clients will negotiate nothing newer than SSLv2. If you
want to use SSL to protect traffic, I recommend using
[stunnel](https://www.stunnel.org/index.html) version 4.56 with
haproxy's
[PROXY protocol](http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt). This
will allow the server to get the client's original addresses for
hostname lookups.
## Installation ## Installation
```sh ```sh
go get go get
go install go install
ergonomadic -conf '/path/to/config.json' -initdb ergonomadic -conf ergonomadic.conf -initdb
``` ```
## Configuration ## Configuration
See the example `config.json`. Passwords are base64-encoded bcrypted See the example `ergonomadic.conf`. Passwords are base64-encoded
byte strings. You can generate them with the `genpasswd` subcommand. bcrypted byte strings. You can generate them with the `genpasswd`
subcommand.
```sh ```sh
ergonomadic -genpasswd 'hunter21!' ergonomadic -genpasswd 'hunter2!'
``` ```
## Running the Server ## Running the Server
```sh ```sh
ergonomadic -conf '/path/to/config.json' ergonomadic -conf ergonomadic.conf
``` ```
## Helpful Documentation ## Helpful Documentation

View File

@ -1,39 +0,0 @@
// Ergonomadic IRC Server Config
// -----------------------------
// Passwords are generated by `ergonomadic -genpasswd "$plaintext"`.
// Comments are not allowed in the actual config file.
{
// `name` is usually a hostname.
"name": "irc.example.com",
// The path to the MOTD is relative to this file's directory.
"motd": "motd.txt",
// PASS command password
"password": "JDJhJDA0JHBBenUyV3Z5UU5iWUpiYmlNMlNLZC5VRDZDM21HUzFVbmxLUUI3NTVTLkZJOERLdUFaUWNt",
// `listeners` are places to bind and listen for
// connections. http://golang.org/pkg/net/#Dial demonstrates valid
// values for `net` and `address`. `net` is optional and defaults
// to `tcp`.
"listeners": [ {
"address": "localhost:7777"
}, {
"net": "tcp6",
"address": "[::1]:7777"
} ],
// Operators for the OPER command
"operators": [ {
"name": "root",
"password": "JDJhJDA0JHBBenUyV3Z5UU5iWUpiYmlNMlNLZC5VRDZDM21HUzFVbmxLUUI3NTVTLkZJOERLdUFaUWNt"
} ],
// Global debug flags. `net` generates a lot of output.
"debug": {
"net": true,
"client": false,
"channel": false,
"server": false
}
}

16
ergonomadic.conf Normal file
View File

@ -0,0 +1,16 @@
[server]
name = "irc.example.com" ; required, usually a hostname
database = "ergonomadic.db" ; path relative to this file
listen = "localhost:6667" ; see `net.Listen` for examples
listen = "[::1]:6667" ; multiple `listen`s are allowed.
motd = "motd.txt" ; path relative to this file
password = "JDJhJDA0JHJzVFFlNXdOUXNhLmtkSGRUQVVEVHVYWXRKUmdNQ3FKVTRrczRSMTlSWGRPZHRSMVRzQmtt" ; 'test'
[operator "root"]
password = "JDJhJDA0JEhkcm10UlNFRkRXb25iOHZuSDVLZXVBWlpyY0xyNkQ4dlBVc1VMWVk1LlFjWFpQbGxZNUtl" ; 'toor'
[debug]
net = true
client = false
channel = false
server = false

View File

@ -1,73 +1,49 @@
package main package main
import ( import (
"code.google.com/p/go.crypto/bcrypt"
"database/sql"
"encoding/base64"
"flag" "flag"
"fmt" "fmt"
"github.com/jlatt/ergonomadic/irc" "github.com/jlatt/ergonomadic/irc"
_ "github.com/mattn/go-sqlite3"
"log" "log"
"os" "os"
"path/filepath"
) )
func genPasswd(passwd string) {
crypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
log.Fatal(err)
}
encoded := base64.StdEncoding.EncodeToString(crypted)
fmt.Println(encoded)
}
func initDB(config *irc.Config) {
os.Remove(config.Database())
db, err := sql.Open("sqlite3", config.Database())
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`
CREATE TABLE channel (
name TEXT NOT NULL UNIQUE,
flags TEXT NOT NULL,
key TEXT NOT NULL,
topic TEXT NOT NULL,
user_limit INTEGER DEFAULT 0)`)
if err != nil {
log.Fatal(err)
}
}
func main() { func main() {
conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file") conf := flag.String("conf", "ergonomadic.conf", "ergonomadic config file")
initdb := flag.Bool("initdb", false, "initialize database") initdb := flag.Bool("initdb", false, "initialize database")
passwd := flag.String("genpasswd", "", "bcrypt a password") passwd := flag.String("genpasswd", "", "bcrypt a password")
flag.Parse() flag.Parse()
if *passwd != "" { if *passwd != "" {
genPasswd(*passwd) encoded, err := irc.GenerateEncodedPassword(*passwd)
if err != nil {
log.Fatal("encoding error: ", err)
}
fmt.Println(encoded)
return return
} }
config, err := irc.LoadConfig(*conf) config, err := irc.LoadConfig(*conf)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("error loading config: ", err)
}
err = os.Chdir(filepath.Dir(*conf))
if err != nil {
log.Fatal("chdir error: ", err)
} }
if *initdb { if *initdb {
initDB(config) irc.InitDB(config.Server.Database)
log.Println("database initialized: " + config.Server.Database)
return return
} }
// TODO move to data structures // TODO move to data structures
irc.DEBUG_NET = config.Debug["net"] irc.DEBUG_NET = config.Debug.Net
irc.DEBUG_CLIENT = config.Debug["client"] irc.DEBUG_CLIENT = config.Debug.Client
irc.DEBUG_CHANNEL = config.Debug["channel"] irc.DEBUG_CHANNEL = config.Debug.Channel
irc.DEBUG_SERVER = config.Debug["server"] irc.DEBUG_SERVER = config.Debug.Server
irc.NewServer(config).Run() irc.NewServer(config).Run()
} }

View File

@ -464,9 +464,8 @@ func (channel *Channel) Invite(invitee *Client, inviter *Client) {
return return
} }
// TODO Modify channel masks
inviter.RplInviting(invitee, channel.name) inviter.RplInviting(invitee, channel.name)
invitee.Reply(RplInviteMsg(inviter, channel.name)) invitee.Reply(RplInviteMsg(inviter, invitee, channel.name))
if invitee.flags[Away] { if invitee.flags[Away] {
inviter.RplAway(invitee) inviter.RplAway(invitee)
} }

View File

@ -1,7 +1,6 @@
package irc package irc
import ( import (
"code.google.com/p/go.crypto/bcrypt"
"code.google.com/p/go.text/unicode/norm" "code.google.com/p/go.text/unicode/norm"
"errors" "errors"
"fmt" "fmt"
@ -214,7 +213,7 @@ func (cmd *PassCommand) CheckPassword() {
if cmd.hash == nil { if cmd.hash == nil {
return return
} }
cmd.err = bcrypt.CompareHashAndPassword(cmd.hash, cmd.password) cmd.err = ComparePassword(cmd.hash, cmd.password)
} }
func NewPassCommand(args []string) (editableCommand, error) { func NewPassCommand(args []string) (editableCommand, error) {

View File

@ -1,91 +1,67 @@
package irc package irc
import ( import (
"encoding/base64" "code.google.com/p/gcfg"
"encoding/json" "errors"
"log" "log"
"os"
"path/filepath"
) )
func decodePassword(password string) []byte { type PassConfig struct {
if password == "" { Password string
return nil }
}
bytes, err := base64.StdEncoding.DecodeString(password) func (conf *PassConfig) PasswordBytes() []byte {
bytes, err := DecodePassword(conf.Password)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("decode password error: ", err)
} }
return bytes return bytes
} }
type Config struct { type Config struct {
Debug map[string]bool Server struct {
Listeners []ListenerConfig PassConfig
MOTD string Database string
Name string Listen []string
Operators []OperatorConfig MOTD string
Password string Name string
directory string }
Operator map[string]*PassConfig
Debug struct {
Net bool
Client bool
Channel bool
Server bool
}
} }
func (conf *Config) Database() string { func (conf *Config) Operators() map[string][]byte {
return filepath.Join(conf.directory, "ergonomadic.db")
}
func (conf *Config) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
func (conf *Config) OperatorsMap() map[string][]byte {
operators := make(map[string][]byte) operators := make(map[string][]byte)
for _, opConf := range conf.Operators { for name, opConf := range conf.Operator {
operators[opConf.Name] = opConf.PasswordBytes() operators[name] = opConf.PasswordBytes()
} }
return operators return operators
} }
type OperatorConfig struct {
Name string
Password string
}
func (conf *OperatorConfig) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
type ListenerConfig struct {
Net string
Address string
Key string
Certificate string
}
func (config *ListenerConfig) IsTLS() bool {
return (config.Key != "") && (config.Certificate != "")
}
func LoadConfig(filename string) (config *Config, err error) { func LoadConfig(filename string) (config *Config, err error) {
config = &Config{} config = &Config{}
err = gcfg.ReadFileInto(config, filename)
file, err := os.Open(filename)
if err != nil { if err != nil {
return return
} }
defer file.Close() if config.Server.Name == "" {
err = errors.New("server.name missing")
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
return return
} }
if config.Server.Database == "" {
config.directory = filepath.Dir(filename) err = errors.New("server.database missing")
config.MOTD = filepath.Join(config.directory, config.MOTD) return
for _, lconf := range config.Listeners { }
if lconf.Net == "" { if len(config.Server.Listen) == 0 {
lconf.Net = "tcp" err = errors.New("server.listen missing")
} return
} }
return return
} }

View File

@ -19,11 +19,11 @@ var (
// regexps // regexps
ChannelNameExpr = regexp.MustCompile(`^[&!#+][\pL\pN]{1,63}$`) ChannelNameExpr = regexp.MustCompile(`^[&!#+][\pL\pN]{1,63}$`)
NicknameExpr = regexp.MustCompile( NicknameExpr = regexp.MustCompile(
"^[\\pL\\[\\]{}^`][\\pL\\pN\\[\\]{}^`]{1,31}$") "^[\\pL\\[\\]{}^`_][\\pL\\pN\\[\\]{}^`_|]{1,31}$")
) )
const ( const (
SEM_VER = "ergonomadic-1.2.11" SEM_VER = "ergonomadic-1.2.13"
CRLF = "\r\n" CRLF = "\r\n"
MAX_REPLY_LEN = 512 - len(CRLF) MAX_REPLY_LEN = 512 - len(CRLF)

32
irc/database.go Normal file
View File

@ -0,0 +1,32 @@
package irc
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
func InitDB(path string) {
os.Remove(path)
db := OpenDB(path)
defer db.Close()
_, err := db.Exec(`
CREATE TABLE channel (
name TEXT NOT NULL UNIQUE,
flags TEXT NOT NULL,
key TEXT NOT NULL,
topic TEXT NOT NULL,
user_limit INTEGER DEFAULT 0)`)
if err != nil {
log.Fatal("initdb error: ", err)
}
}
func OpenDB(path string) *sql.DB {
db, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatal("open db error: ", err)
}
return db
}

37
irc/password.go Normal file
View File

@ -0,0 +1,37 @@
package irc
import (
"code.google.com/p/go.crypto/bcrypt"
"encoding/base64"
"errors"
)
var (
EmptyPasswordError = errors.New("empty password")
)
func GenerateEncodedPassword(passwd string) (encoded string, err error) {
if passwd == "" {
err = EmptyPasswordError
return
}
bcrypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
return
}
encoded = base64.StdEncoding.EncodeToString(bcrypted)
return
}
func DecodePassword(encoded string) (decoded []byte, err error) {
if encoded == "" {
err = EmptyPasswordError
return
}
decoded, err = base64.StdEncoding.DecodeString(encoded)
return
}
func ComparePassword(hash, password []byte) error {
return bcrypt.CompareHashAndPassword(hash, password)
}

View File

@ -128,8 +128,8 @@ func RplError(message string) string {
return NewStringReply(nil, ERROR, ":%s", message) return NewStringReply(nil, ERROR, ":%s", message)
} }
func RplInviteMsg(inviter *Client, channel string) string { func RplInviteMsg(inviter *Client, invitee *Client, channel string) string {
return NewStringReply(inviter, INVITE, channel) return NewStringReply(inviter, INVITE, "%s :%s", invitee.Nick(), channel)
} }
func RplKick(channel *Channel, client *Client, target *Client, comment string) string { func RplKick(channel *Channel, client *Client, target *Client, comment string) string {

View File

@ -2,12 +2,8 @@ package irc
import ( import (
"bufio" "bufio"
"crypto/rand"
"crypto/tls"
"database/sql" "database/sql"
"encoding/binary"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3"
"log" "log"
"net" "net"
"os" "os"
@ -16,6 +12,7 @@ import (
"runtime/debug" "runtime/debug"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"syscall"
"time" "time"
) )
@ -35,34 +32,30 @@ type Server struct {
} }
func NewServer(config *Config) *Server { func NewServer(config *Config) *Server {
db, err := sql.Open("sqlite3", config.Database())
if err != nil {
log.Fatal(err)
}
server := &Server{ server := &Server{
channels: make(ChannelNameMap), channels: make(ChannelNameMap),
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: make(chan Command, 16), commands: make(chan Command, 16),
ctime: time.Now(), ctime: time.Now(),
db: db, db: OpenDB(config.Server.Database),
idle: make(chan *Client, 16), idle: make(chan *Client, 16),
motdFile: config.MOTD, motdFile: config.Server.MOTD,
name: config.Name, name: config.Server.Name,
newConns: make(chan net.Conn, 16), newConns: make(chan net.Conn, 16),
operators: config.OperatorsMap(), operators: config.Operators(),
password: config.PasswordBytes(), password: config.Server.PasswordBytes(),
signals: make(chan os.Signal, 1), signals: make(chan os.Signal, 1),
} }
signal.Notify(server.signals, os.Interrupt, os.Kill)
server.loadChannels() server.loadChannels()
for _, listenerConf := range config.Listeners { for _, addr := range config.Server.Listen {
go server.listen(listenerConf) go server.listen(addr)
} }
signal.Notify(server.signals, syscall.SIGINT, syscall.SIGHUP,
syscall.SIGTERM, syscall.SIGQUIT)
return server return server
} }
@ -71,7 +64,7 @@ func (server *Server) loadChannels() {
SELECT name, flags, key, topic, user_limit SELECT name, flags, key, topic, user_limit
FROM channel`) FROM channel`)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("error loading channels: ", err)
} }
for rows.Next() { for rows.Next() {
var name, flags, key, topic string var name, flags, key, topic string
@ -128,14 +121,20 @@ func (server *Server) processCommand(cmd Command) {
} }
} }
func (server *Server) Shutdown() {
server.db.Close()
for _, client := range server.clients {
client.Reply(RplNotice(server, client, "shutting down"))
}
}
func (server *Server) Run() { func (server *Server) Run() {
done := false done := false
for !done { for !done {
select { select {
case <-server.signals: case <-server.signals:
server.db.Close() server.Shutdown()
done = true done = true
continue
case conn := <-server.newConns: case conn := <-server.newConns:
NewClient(server, conn) NewClient(server, conn)
@ -149,33 +148,18 @@ func (server *Server) Run() {
} }
} }
func newListener(config ListenerConfig) (net.Listener, error) {
if config.IsTLS() {
certificate, err := tls.LoadX509KeyPair(config.Certificate, config.Key)
if err != nil {
return nil, err
}
return tls.Listen("tcp", config.Address, &tls.Config{
Certificates: []tls.Certificate{certificate},
PreferServerCipherSuites: true,
})
}
return net.Listen("tcp", config.Address)
}
// //
// listen goroutine // listen goroutine
// //
func (s *Server) listen(config ListenerConfig) { func (s *Server) listen(addr string) {
listener, err := newListener(config) listener, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
log.Fatal(s, "listen error: ", err) log.Fatal(s, "listen error: ", err)
} }
if DEBUG_SERVER { if DEBUG_SERVER {
log.Printf("%s listening on %s", s, config.Address) log.Printf("%s listening on %s", s, addr)
} }
for { for {
@ -194,24 +178,6 @@ func (s *Server) listen(config ListenerConfig) {
} }
} }
func (s *Server) GenerateGuestNick() string {
bytes := make([]byte, 8)
for {
_, err := rand.Read(bytes)
if err != nil {
panic(err)
}
randInt, n := binary.Uvarint(bytes)
if n <= 0 {
continue // TODO handle error
}
nick := fmt.Sprintf("guest%d", randInt)
if s.clients.Get(nick) == nil {
return nick
}
}
}
// //
// server functionality // server functionality
// //
@ -880,9 +846,8 @@ func (msg *InviteCommand) HandleServer(server *Server) {
channel := server.channels.Get(msg.channel) channel := server.channels.Get(msg.channel)
if channel == nil { if channel == nil {
name := strings.ToLower(msg.channel) client.RplInviting(target, msg.channel)
client.RplInviting(target, name) target.Reply(RplInviteMsg(client, target, msg.channel))
target.Reply(RplInviteMsg(client, name))
return return
} }