3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-05-02 20:57:31 +02:00

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
commit fa5b1e6cf7
6 changed files with 97 additions and 159 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

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"github.com/jlatt/ergonomadic/irc" "github.com/jlatt/ergonomadic/irc"
"log" "log"
"os"
"path/filepath"
) )
func main() { func main() {
@ -26,18 +28,22 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = os.Chdir(filepath.Dir(*conf))
if err != nil {
log.Fatal(err)
}
if *initdb { if *initdb {
irc.InitDB(config.Database()) irc.InitDB(config.Server.Database)
log.Println("database initialized: " + config.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

@ -1,14 +1,17 @@
package irc package irc
import ( import (
"encoding/json" "code.google.com/p/gcfg"
"errors"
"log" "log"
"os"
"path/filepath"
) )
func decodePassword(password string) []byte { type PassConfig struct {
bytes, err := DecodePassword(password) Password string
}
func (conf *PassConfig) PasswordBytes() []byte {
bytes, err := DecodePassword(conf.Password)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -16,72 +19,49 @@ 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)
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

@ -2,10 +2,7 @@ package irc
import ( import (
"bufio" "bufio"
"crypto/rand"
"crypto/tls"
"database/sql" "database/sql"
"encoding/binary"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -41,21 +38,21 @@ func NewServer(config *Config) *Server {
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: make(chan Command, 16), commands: make(chan Command, 16),
ctime: time.Now(), ctime: time.Now(),
db: OpenDB(config.Database()), 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),
timeout: make(chan *Client, 16), timeout: make(chan *Client, 16),
} }
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, signal.Notify(server.signals, syscall.SIGINT, syscall.SIGHUP,
@ -171,33 +168,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 {
@ -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 // server functionality
// //