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