3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-24 21:09:30 +01:00

TLS support

This commit is contained in:
Jeremy Latt 2014-02-09 19:41:00 -08:00
parent e625f62baa
commit db0f494604
4 changed files with 38 additions and 9 deletions

4
.gitignore vendored
View File

@ -1,4 +1,2 @@
pkg
bin
ergonomadic.db
ergonomadic.json ergonomadic.json
*.pem

View File

@ -7,7 +7,7 @@ import (
type Config struct { type Config struct {
Name string Name string
Listen string Listeners []ListenerConfig
Password string Password string
Operators []OperatorConfig Operators []OperatorConfig
Debug map[string]bool Debug map[string]bool
@ -18,6 +18,16 @@ type OperatorConfig struct {
Password string Password string
} }
type ListenerConfig struct {
Address string
Key string
Certificate string
}
func (config *ListenerConfig) IsTLS() bool {
return (config.Key != "") && (config.Certificate != "")
}
func LoadConfig() (config *Config, err error) { func LoadConfig() (config *Config, err error) {
config = &Config{} config = &Config{}

View File

@ -15,7 +15,7 @@ var (
) )
const ( const (
VERSION = "ergonomadic-1" VERSION = "1.0.0"
CRLF = "\r\n" CRLF = "\r\n"
MAX_REPLY_LEN = 512 - len(CRLF) MAX_REPLY_LEN = 512 - len(CRLF)

View File

@ -2,6 +2,7 @@ package irc
import ( import (
"crypto/rand" "crypto/rand"
"crypto/tls"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"log" "log"
@ -37,7 +38,11 @@ func NewServer(config *Config) *Server {
} }
go server.receiveCommands(commands) go server.receiveCommands(commands)
go server.listen(config.Listen)
for _, listenerConf := range config.Listeners {
go server.listen(listenerConf)
}
return server return server
} }
@ -63,14 +68,30 @@ func (server *Server) receiveCommands(commands <-chan Command) {
} }
} }
func (s *Server) listen(addr string) { func newListener(config ListenerConfig) (net.Listener, error) {
listener, err := net.Listen("tcp", addr) 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,
MinVersion: tls.VersionTLS12,
})
}
return net.Listen("tcp", config.Address)
}
func (s *Server) listen(config ListenerConfig) {
listener, err := newListener(config)
if err != nil { if err != nil {
log.Fatal("Server.Listen: ", err) log.Fatal("Server.Listen: ", err)
} }
s.hostname = LookupHostname(listener.Addr()) s.hostname = LookupHostname(listener.Addr())
log.Print("Server.Listen: listening on ", addr) log.Print("Server.Listen: listening on ", config.Address)
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()