switch to gcfg for conf file

- add some validation for config file
- add comments explaining config
- remove TLS listener since most clients can't use it anyway
- remove unused nick generation function
This commit is contained in:
Jeremy Latt 2014-03-01 14:34:51 -08:00
parent 542744d52a
commit 83d021fcb7
5 changed files with 76 additions and 147 deletions

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"
[operator "root"]
password = "JDJhJDA0JEhkcm10UlNFRkRXb25iOHZuSDVLZXVBWlpyY0xyNkQ4dlBVc1VMWVk1LlFjWFpQbGxZNUtl"
[debug]
net = true
client = false
channel = false
server = false

View File

@ -22,9 +22,9 @@ func genPasswd(passwd string) {
} }
func initDB(config *irc.Config) { func initDB(config *irc.Config) {
os.Remove(config.Database()) os.Remove(config.Server.Database)
db, err := sql.Open("sqlite3", config.Database()) db, err := sql.Open("sqlite3", config.Server.Database)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -64,10 +64,10 @@ func main() {
} }
// 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

@ -1,18 +1,22 @@
package irc package irc
import ( import (
"code.google.com/p/gcfg"
"encoding/base64" "encoding/base64"
"encoding/json" "errors"
"log" "log"
"os"
"path/filepath" "path/filepath"
) )
func decodePassword(password string) []byte { type PassConfig struct {
if password == "" { Password string
}
func (conf *PassConfig) PasswordBytes() []byte {
if conf.Password == "" {
return nil return nil
} }
bytes, err := base64.StdEncoding.DecodeString(password) bytes, err := base64.StdEncoding.DecodeString(conf.Password)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -20,72 +24,56 @@ func decodePassword(password string) []byte {
} }
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) return
err = decoder.Decode(config) }
if err != nil { if config.Server.Database == "" {
err = errors.New("server.database missing")
return
}
if len(config.Server.Listen) == 0 {
err = errors.New("server.listen missing")
return return
} }
config.directory = filepath.Dir(filename) // make
config.MOTD = filepath.Join(config.directory, config.MOTD) dir := filepath.Dir(filename)
for _, lconf := range config.Listeners { if config.Server.MOTD != "" {
if lconf.Net == "" { config.Server.MOTD = filepath.Join(dir, config.Server.MOTD)
lconf.Net = "tcp"
}
} }
config.Server.Database = filepath.Join(dir, config.Server.Database)
return return
} }

View File

@ -2,10 +2,7 @@ package irc
import ( import (
"bufio" "bufio"
"crypto/rand"
"crypto/tls"
"database/sql" "database/sql"
"encoding/binary"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"log" "log"
@ -36,7 +33,7 @@ type Server struct {
} }
func NewServer(config *Config) *Server { func NewServer(config *Config) *Server {
db, err := sql.Open("sqlite3", config.Database()) db, err := sql.Open("sqlite3", config.Server.Database)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -48,11 +45,11 @@ func NewServer(config *Config) *Server {
ctime: time.Now(), ctime: time.Now(),
db: db, db: db,
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),
timeout: make(chan *Client, 16), timeout: make(chan *Client, 16),
} }
@ -61,8 +58,8 @@ func NewServer(config *Config) *Server {
server.loadChannels() server.loadChannels()
for _, listenerConf := range config.Listeners { for _, addr := range config.Server.Listen {
go server.listen(listenerConf) go server.listen(addr)
} }
return server return server
@ -169,33 +166,18 @@ func (server *Server) InitPhase() Phase {
return Authorization return Authorization
} }
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 {
@ -214,24 +196,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
// //