2020-02-06 15:18:19 +00:00
|
|
|
package jounce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
2020-02-06 15:39:09 +00:00
|
|
|
const (
|
2020-02-06 16:04:49 +00:00
|
|
|
rpl_localusers = "265"
|
2020-02-06 15:39:09 +00:00
|
|
|
rpl_globalusers = "266"
|
|
|
|
)
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
type upstreamConn struct {
|
2020-02-06 16:04:49 +00:00
|
|
|
upstream *Upstream
|
|
|
|
net net.Conn
|
|
|
|
irc *irc.Conn
|
|
|
|
srv *Server
|
2020-02-06 15:39:09 +00:00
|
|
|
registered bool
|
2020-02-06 16:04:49 +00:00
|
|
|
|
|
|
|
serverName string
|
|
|
|
availableUserModes string
|
|
|
|
availableChannelModes string
|
|
|
|
channelModesWithParam string
|
2020-02-06 15:18:19 +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},
|
|
|
|
})
|
2020-02-06 15:39:09 +00:00
|
|
|
case irc.RPL_WELCOME:
|
|
|
|
c.registered = true
|
2020-02-06 16:04:49 +00:00
|
|
|
c.srv.Logger.Printf("Connection to %q registered", c.upstream.Addr)
|
|
|
|
case irc.RPL_MYINFO:
|
|
|
|
if len(msg.Params) < 5 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
c.serverName = msg.Params[1]
|
|
|
|
c.availableUserModes = msg.Params[3]
|
|
|
|
c.availableChannelModes = msg.Params[4]
|
|
|
|
if len(msg.Params) > 5 {
|
|
|
|
c.channelModesWithParam = msg.Params[5]
|
|
|
|
}
|
|
|
|
case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
2020-02-06 15:39:09 +00:00
|
|
|
// Ignore
|
|
|
|
case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
|
|
|
|
// Ignore
|
|
|
|
case irc.RPL_MOTDSTART, irc.RPL_MOTD, irc.RPL_ENDOFMOTD:
|
|
|
|
// Ignore
|
|
|
|
case rpl_localusers, rpl_globalusers:
|
|
|
|
// Ignore
|
|
|
|
case irc.RPL_STATSVLINE, irc.RPL_STATSPING, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
|
|
|
|
// Ignore
|
2020-02-06 15:18:19 +00:00
|
|
|
default:
|
|
|
|
c.srv.Logger.Printf("Unhandled upstream message: %v", msg)
|
|
|
|
}
|
2020-02-06 15:39:09 +00:00
|
|
|
return nil
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func connect(s *Server, 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)
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:04:49 +00:00
|
|
|
c := upstreamConn{
|
|
|
|
upstream: upstream,
|
|
|
|
net: netConn,
|
|
|
|
irc: irc.NewConn(netConn),
|
|
|
|
srv: s,
|
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.handleMessage(msg); err != nil {
|
2020-02-06 16:04:49 +00:00
|
|
|
c.srv.Logger.Printf("Failed to handle message %q from %q: %v", msg, upstream.Addr, err)
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return netConn.Close()
|
|
|
|
}
|