Merge pull request #9 from jlatt/gcfg

switch from JSON to gcfg config files
This commit is contained in:
Jeremy Latt
2014-03-05 23:02:22 -08:00
6 changed files with 97 additions and 159 deletions
+37 -57
View File
@@ -1,14 +1,17 @@
package irc
import (
"encoding/json"
"code.google.com/p/gcfg"
"errors"
"log"
"os"
"path/filepath"
)
func decodePassword(password string) []byte {
bytes, err := DecodePassword(password)
type PassConfig struct {
Password string
}
func (conf *PassConfig) PasswordBytes() []byte {
bytes, err := DecodePassword(conf.Password)
if err != nil {
log.Fatal(err)
}
@@ -16,72 +19,49 @@ func decodePassword(password string) []byte {
}
type Config struct {
Debug map[string]bool
Listeners []ListenerConfig
MOTD string
Name string
Operators []OperatorConfig
Password string
directory string
Server struct {
PassConfig
Database string
Listen []string
MOTD string
Name string
}
Operator map[string]*PassConfig
Debug struct {
Net bool
Client bool
Channel bool
Server bool
}
}
func (conf *Config) Database() string {
return filepath.Join(conf.directory, "ergonomadic.db")
}
func (conf *Config) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
func (conf *Config) OperatorsMap() map[string][]byte {
func (conf *Config) Operators() map[string][]byte {
operators := make(map[string][]byte)
for _, opConf := range conf.Operators {
operators[opConf.Name] = opConf.PasswordBytes()
for name, opConf := range conf.Operator {
operators[name] = opConf.PasswordBytes()
}
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) {
config = &Config{}
file, err := os.Open(filename)
err = gcfg.ReadFileInto(config, filename)
if err != nil {
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
if config.Server.Name == "" {
err = errors.New("server.name missing")
return
}
config.directory = filepath.Dir(filename)
config.MOTD = filepath.Join(config.directory, config.MOTD)
for _, lconf := range config.Listeners {
if lconf.Net == "" {
lconf.Net = "tcp"
}
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
}
+10 -46
View File
@@ -2,10 +2,7 @@ package irc
import (
"bufio"
"crypto/rand"
"crypto/tls"
"database/sql"
"encoding/binary"
"fmt"
"log"
"net"
@@ -41,21 +38,21 @@ func NewServer(config *Config) *Server {
clients: make(ClientNameMap),
commands: make(chan Command, 16),
ctime: time.Now(),
db: OpenDB(config.Database()),
db: OpenDB(config.Server.Database),
idle: make(chan *Client, 16),
motdFile: config.MOTD,
name: config.Name,
motdFile: config.Server.MOTD,
name: config.Server.Name,
newConns: make(chan net.Conn, 16),
operators: config.OperatorsMap(),
password: config.PasswordBytes(),
operators: config.Operators(),
password: config.Server.PasswordBytes(),
signals: make(chan os.Signal, 1),
timeout: make(chan *Client, 16),
}
server.loadChannels()
for _, listenerConf := range config.Listeners {
go server.listen(listenerConf)
for _, addr := range config.Server.Listen {
go server.listen(addr)
}
signal.Notify(server.signals, syscall.SIGINT, syscall.SIGHUP,
@@ -171,33 +168,18 @@ func (server *Server) InitPhase() Phase {
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
//
func (s *Server) listen(config ListenerConfig) {
listener, err := newListener(config)
func (s *Server) listen(addr string) {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal(s, "listen error: ", err)
}
if DEBUG_SERVER {
log.Printf("%s listening on %s", s, config.Address)
log.Printf("%s listening on %s", s, addr)
}
for {
@@ -216,24 +198,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
//