soju/server.go

314 lines
6.1 KiB
Go
Raw Normal View History

2020-02-04 09:46:22 +00:00
package jounce
import (
2020-02-06 15:03:07 +00:00
"crypto/tls"
2020-02-04 09:46:22 +00:00
"fmt"
2020-02-04 10:25:53 +00:00
"io"
2020-02-04 09:46:22 +00:00
"log"
"net"
"gopkg.in/irc.v3"
)
2020-02-06 14:50:46 +00:00
type Logger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
}
2020-02-04 11:19:18 +00:00
type ircError struct {
Message *irc.Message
}
func newUnknownCommandError(cmd string) ircError {
return ircError{&irc.Message{
Command: irc.ERR_UNKNOWNCOMMAND,
Params: []string{
"*",
cmd,
"Unknown command",
},
}}
}
func newNeedMoreParamsError(cmd string) ircError {
return ircError{&irc.Message{
Command: irc.ERR_NEEDMOREPARAMS,
Params: []string{
"*",
cmd,
"Not enough parameters",
},
}}
}
func (err ircError) Error() string {
return err.Message.String()
}
2020-02-06 14:22:26 +00:00
type downstreamConn struct {
2020-02-04 11:19:18 +00:00
net net.Conn
irc *irc.Conn
2020-02-04 17:56:07 +00:00
srv *Server
2020-02-04 11:19:18 +00:00
registered bool
2020-02-04 17:56:07 +00:00
closed bool
2020-02-04 11:19:18 +00:00
nick string
username string
realname string
}
2020-02-06 14:22:26 +00:00
func (c *downstreamConn) Close() error {
2020-02-04 17:56:07 +00:00
if err := c.net.Close(); err != nil {
return err
}
c.closed = true
return nil
}
2020-02-06 14:22:26 +00:00
func (c *downstreamConn) WriteMessage(msg *irc.Message) error {
2020-02-04 17:56:07 +00:00
msg.Prefix = c.srv.prefix()
return c.irc.WriteMessage(msg)
}
2020-02-06 14:22:26 +00:00
func (c *downstreamConn) handleMessage(msg *irc.Message) error {
2020-02-06 11:18:37 +00:00
switch msg.Command {
case "PING":
// TODO: handle params
return c.WriteMessage(&irc.Message{
Command: "PONG",
2020-02-06 15:03:07 +00:00
Params: []string{c.srv.Hostname},
2020-02-06 11:18:37 +00:00
})
default:
if c.registered {
return c.handleMessageRegistered(msg)
} else {
return c.handleMessageUnregistered(msg)
}
}
}
2020-02-06 14:22:26 +00:00
func (c *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
2020-02-04 11:19:18 +00:00
switch msg.Command {
case "NICK":
if len(msg.Params) != 1 {
return newNeedMoreParamsError(msg.Command)
}
c.nick = msg.Params[0]
case "USER":
if len(msg.Params) != 4 {
return newNeedMoreParamsError(msg.Command)
}
c.username = "~" + msg.Params[0]
c.realname = msg.Params[3]
2020-02-04 17:56:07 +00:00
case "QUIT":
return c.Close()
2020-02-04 11:19:18 +00:00
default:
return newUnknownCommandError(msg.Command)
}
2020-02-04 17:56:07 +00:00
if c.username != "" && c.nick != "" {
return c.register()
}
return nil
}
2020-02-06 14:22:26 +00:00
func (c *downstreamConn) register() error {
2020-02-04 17:56:07 +00:00
c.registered = true
err := c.WriteMessage(&irc.Message{
Command: irc.RPL_WELCOME,
Params: []string{c.nick, "Welcome to jounce, " + c.nick},
})
if err != nil {
return err
}
err = c.WriteMessage(&irc.Message{
Command: irc.RPL_YOURHOST,
Params: []string{c.nick, "Your host is " + c.srv.Hostname},
})
if err != nil {
return err
}
err = c.WriteMessage(&irc.Message{
Command: irc.RPL_CREATED,
Params: []string{c.nick, "This server was created <datetime>"}, // TODO
})
if err != nil {
return err
}
err = c.WriteMessage(&irc.Message{
Command: irc.RPL_MYINFO,
Params: []string{c.nick, c.srv.Hostname, "unknown", "", ""},
})
if err != nil {
return err
}
2020-02-06 11:08:54 +00:00
err = c.WriteMessage(&irc.Message{
Command: irc.ERR_NOMOTD,
2020-02-06 15:03:07 +00:00
Params: []string{c.nick, "No MOTD"},
2020-02-06 11:08:54 +00:00
})
if err != nil {
return err
}
2020-02-04 11:19:18 +00:00
return nil
}
2020-02-06 14:22:26 +00:00
func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
2020-02-04 11:19:18 +00:00
switch msg.Command {
case "NICK", "USER":
return ircError{&irc.Message{
Command: irc.ERR_ALREADYREGISTERED,
Params: []string{
c.nick,
"You may not reregister",
},
}}
2020-02-04 17:56:07 +00:00
case "QUIT":
return c.Close()
2020-02-04 11:19:18 +00:00
default:
return newUnknownCommandError(msg.Command)
}
2020-02-04 10:25:53 +00:00
}
2020-02-06 15:03:07 +00:00
type upstreamConn struct {
net net.Conn
irc *irc.Conn
srv *Server
}
2020-02-06 15:11:28 +00:00
func (c *upstreamConn) handleMessage(msg *irc.Message) error {
switch msg.Command {
case "PING":
// TODO: handle params
return c.irc.WriteMessage(&irc.Message{
Command: "PONG",
Params: []string{c.srv.Hostname},
})
default:
c.srv.Logger.Printf("Unhandled upstream message: %v", msg)
return nil
}
}
2020-02-06 15:03:07 +00:00
type Upstream struct {
Addr string
Nick string
Username string
Realname string
}
type Server struct {
Hostname string
Logger Logger
Upstreams []Upstream // TODO: per-user
2020-02-04 17:56:07 +00:00
}
2020-02-04 09:46:22 +00:00
2020-02-04 17:56:07 +00:00
func (s *Server) prefix() *irc.Prefix {
return &irc.Prefix{Name: s.Hostname}
}
2020-02-04 10:25:53 +00:00
2020-02-04 17:56:07 +00:00
func (s *Server) handleConn(netConn net.Conn) error {
2020-02-06 14:50:46 +00:00
s.Logger.Printf("Handling connection from %v", netConn.RemoteAddr())
2020-02-06 14:22:26 +00:00
c := downstreamConn{net: netConn, irc: irc.NewConn(netConn), srv: s}
2020-02-04 17:56:07 +00:00
defer c.Close()
2020-02-04 09:46:22 +00:00
for {
2020-02-04 11:19:18 +00:00
msg, err := c.irc.ReadMessage()
2020-02-04 10:25:53 +00:00
if err == io.EOF {
break
} else if err != nil {
2020-02-04 11:19:18 +00:00
return fmt.Errorf("failed to read IRC command: %v", err)
2020-02-04 09:46:22 +00:00
}
2020-02-06 15:03:07 +00:00
s.Logger.Printf("Downstream message: %v", msg)
2020-02-04 10:25:53 +00:00
2020-02-06 11:18:37 +00:00
err = c.handleMessage(msg)
2020-02-04 11:19:18 +00:00
if ircErr, ok := err.(ircError); ok {
2020-02-04 17:56:07 +00:00
ircErr.Message.Prefix = s.prefix()
if err := c.WriteMessage(ircErr.Message); err != nil {
2020-02-04 11:19:18 +00:00
return fmt.Errorf("failed to write IRC reply: %v", err)
}
} else if err != nil {
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
2020-02-04 10:25:53 +00:00
}
2020-02-04 17:56:07 +00:00
if c.closed {
return nil
}
2020-02-04 09:46:22 +00:00
}
2020-02-04 10:25:53 +00:00
2020-02-04 17:56:07 +00:00
return c.Close()
2020-02-04 09:46:22 +00:00
}
2020-02-06 15:03:07 +00:00
func (s *Server) connect(upstream *Upstream) error {
s.Logger.Printf("Connecting to %v", upstream.Addr)
netConn, err := tls.Dial("tcp", upstream.Addr, nil)
if err != nil {
return fmt.Errorf("failed to dial %q: %v", upstream.Addr, err)
}
c := upstreamConn{net: netConn, irc: irc.NewConn(netConn), srv: s}
defer netConn.Close()
err = c.irc.WriteMessage(&irc.Message{
Command: "NICK",
Params: []string{upstream.Nick},
})
if err != nil {
return err
}
err = c.irc.WriteMessage(&irc.Message{
Command: "USER",
Params: []string{upstream.Username, "0", "*", upstream.Realname},
})
if err != nil {
return err
}
for {
msg, err := c.irc.ReadMessage()
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("failed to read IRC command: %v", err)
}
2020-02-06 15:11:28 +00:00
if err := c.handleMessage(msg); err != nil {
return err
}
2020-02-06 15:03:07 +00:00
}
return netConn.Close()
}
func (s *Server) Run() {
for i := range s.Upstreams {
2020-02-06 15:13:29 +00:00
upstream := &s.Upstreams[i]
2020-02-06 15:03:07 +00:00
// TODO: retry connecting
2020-02-06 15:13:29 +00:00
go func() {
if err := s.connect(upstream); err != nil {
s.Logger.Printf("Failed to connect to upstream server %q: %v", upstream.Addr, err)
}
}()
2020-02-06 15:03:07 +00:00
}
}
2020-02-04 10:25:53 +00:00
func (s *Server) Serve(ln net.Listener) error {
2020-02-04 09:46:22 +00:00
for {
2020-02-04 10:25:53 +00:00
c, err := ln.Accept()
2020-02-04 09:46:22 +00:00
if err != nil {
return fmt.Errorf("failed to accept connection: %v", err)
}
go func() {
2020-02-04 10:25:53 +00:00
if err := s.handleConn(c); err != nil {
2020-02-04 09:46:22 +00:00
log.Printf("error handling connection: %v", err)
}
}()
}
}