Browse Source

Read anonircd.conf, add SSL support

master
Trevor Slocum 6 years ago
parent
commit
d3c9c9f4fc
  1. 4
      .gitignore
  2. 17
      anonircd.go
  3. 65
      server.go

4
.gitignore vendored

@ -1,3 +1,5 @@
.idea
bin
anonircd
anonircd.conf
bin
data

17
anonircd.go

@ -25,7 +25,10 @@ import (
"math/rand"
"time"
"github.com/BurntSushi/toml"
irc "gopkg.in/sorcix/irc.v2"
"log"
"os"
)
var anonymous = irc.Prefix{"Anonymous", "Anon", "IRC"}
@ -43,6 +46,11 @@ const umodes = "a"
const cmodes = "it"
const cmodesarg = "kl"
type Config struct {
SSLCert string
SSLKey string
}
func randomIdentifier() string {
b := make([]byte, 10)
for i := range b {
@ -54,6 +62,13 @@ func randomIdentifier() string {
func main() {
rand.Seed(time.Now().UTC().UnixNano())
server := Server{time.Now().Unix(), make(map[string]*Client), make(map[string]*Channel), new(sync.RWMutex)}
var config Config
if _, err := os.Stat("anonircd.conf"); err == nil {
if _, err := toml.DecodeFile("anonircd.conf", &config); err != nil {
log.Fatalf("Failed to read anonircd.conf: %v", err)
}
}
server := Server{&config, time.Now().Unix(), make(map[string]*Client), make(map[string]*Channel), new(sync.RWMutex)}
server.listen()
}

65
server.go

@ -11,9 +11,11 @@ import (
irc "gopkg.in/sorcix/irc.v2"
"math/rand"
"crypto/tls"
)
type Server struct {
config *Config
created int64
clients map[string]*Client
channels map[string]*Channel
@ -276,7 +278,7 @@ func (s *Server) handleRead(c *Client) {
c.writebuffer <- &irc.Message{&anonirc, irc.RPL_WELCOME, []string{"Welcome to AnonIRC " + c.getPrefix().String()}}
c.writebuffer <- &irc.Message{&anonirc, irc.RPL_YOURHOST, []string{"Your host is AnonIRC, running version AnonIRCd"}}
c.writebuffer <- &irc.Message{&anonirc, irc.RPL_CREATED, []string{fmt.Sprintf("This server was created %s", time.Unix(s.created, 0).UTC())}}
c.writebuffer <- &irc.Message{&anonirc, strings.Join([]string{irc.RPL_MYINFO, c.nick, "AnonIRC AnonIRCd ", umodes, cmodes, cmodesarg}, " "), []string{}}
c.writebuffer <- &irc.Message{&anonirc, strings.Join([]string{irc.RPL_MYINFO, c.nick, "AnonIRC AnonIRCd", umodes, cmodes, cmodesarg}, " "), []string{}}
motdsplit := strings.Split(motd, "\n")
for i, motdmsg := range motdsplit {
@ -351,9 +353,10 @@ func (s *Server) handleWrite(c *Client) {
}
func (s *Server) handleConnection(conn net.Conn) {
client := Client{randomIdentifier(), "*", "", "", conn, []string{}, make(chan *irc.Message), irc.NewDecoder(conn), irc.NewEncoder(conn), false, new(sync.RWMutex)}
defer conn.Close()
client := Client{randomIdentifier(), "*", "", "", conn, []string{}, make(chan *irc.Message), irc.NewDecoder(conn), irc.NewEncoder(conn), false, new(sync.RWMutex)}
s.Lock()
s.clients[client.identifier] = &client
s.Unlock()
@ -361,26 +364,42 @@ func (s *Server) handleConnection(conn net.Conn) {
go s.handleWrite(&client)
s.handleRead(&client)
}
func (s *Server) pingClients() {
for _, c := range s.clients {
ping := fmt.Sprintf("anonirc%d%d", int32(time.Now().Unix()), rand.Intn(1000))
c.pings = append(c.pings, ping)
c.writebuffer <- &irc.Message{&anonirc, irc.PING, []string{ping}}
func (s *Server) listenPlain() {
listen, err := net.Listen("tcp", ":6667")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
defer listen.Close()
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
go s.handleConnection(conn)
}
time.Sleep(15 * time.Second)
}
func (s *Server) listen() {
listener, err := net.Listen("tcp", ":6667")
func (s *Server) listenSSL() {
if s.config.SSLCert == "" {
return // SSL is disabled
}
cert, err := tls.LoadX509KeyPair(s.config.SSLCert, s.config.SSLKey)
if err != nil {
log.Fatalf("Failed to listen: %v", err)
log.Fatalf("Failed to load SSL certificate: %v", err)
}
defer listener.Close()
go s.pingClients()
listen, err := tls.Listen("tcp", ":6697", &tls.Config{Certificates:[]tls.Certificate{cert}})
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
defer listen.Close()
for {
conn, err := listener.Accept()
conn, err := listen.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
@ -388,3 +407,21 @@ func (s *Server) listen() {
go s.handleConnection(conn)
}
}
func (s *Server) pingClients() {
for {
for _, c := range s.clients {
ping := fmt.Sprintf("anonirc%d%d", int32(time.Now().Unix()), rand.Intn(1000))
c.pings = append(c.pings, ping)
c.writebuffer <- &irc.Message{&anonirc, irc.PING, []string{ping}}
}
time.Sleep(15 * time.Second)
}
}
func (s *Server) listen() {
go s.listenPlain()
go s.listenSSL()
s.pingClients()
}

Loading…
Cancel
Save