2020-02-04 09:46:22 +00:00
|
|
|
package jounce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-07 10:36:42 +00:00
|
|
|
"log"
|
2020-02-04 09:46:22 +00:00
|
|
|
"net"
|
2020-02-06 20:30:44 +00:00
|
|
|
"sync"
|
2020-02-04 09:46:22 +00:00
|
|
|
|
|
|
|
"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-06 19:25:37 +00:00
|
|
|
type prefixLogger struct {
|
|
|
|
logger Logger
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Logger = (*prefixLogger)(nil)
|
|
|
|
|
|
|
|
func (l *prefixLogger) Print(v ...interface{}) {
|
|
|
|
v = append([]interface{}{l.prefix}, v...)
|
|
|
|
l.logger.Print(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *prefixLogger) Printf(format string, v ...interface{}) {
|
|
|
|
v = append([]interface{}{l.prefix}, v...)
|
|
|
|
l.logger.Printf("%v"+format, v...)
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:36:42 +00:00
|
|
|
type user struct {
|
|
|
|
username string
|
|
|
|
srv *Server
|
|
|
|
|
2020-02-07 10:56:36 +00:00
|
|
|
lock sync.Mutex
|
|
|
|
upstreamConns []*upstreamConn
|
|
|
|
downstreamConns []*downstreamConn
|
2020-02-07 10:36:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 10:46:44 +00:00
|
|
|
func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
|
|
|
u.lock.Lock()
|
|
|
|
for _, uc := range u.upstreamConns {
|
2020-02-07 10:58:50 +00:00
|
|
|
if !uc.registered || uc.closed {
|
2020-02-07 10:46:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
f(uc)
|
|
|
|
}
|
|
|
|
u.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:56:36 +00:00
|
|
|
func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
|
|
|
u.lock.Lock()
|
|
|
|
for _, dc := range u.downstreamConns {
|
|
|
|
f(dc)
|
|
|
|
}
|
|
|
|
u.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:03:07 +00:00
|
|
|
type Upstream struct {
|
|
|
|
Addr string
|
|
|
|
Nick string
|
|
|
|
Username string
|
|
|
|
Realname string
|
2020-02-06 18:22:04 +00:00
|
|
|
Channels []string
|
2020-02-06 15:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Hostname string
|
|
|
|
Logger Logger
|
|
|
|
Upstreams []Upstream // TODO: per-user
|
2020-02-06 20:11:35 +00:00
|
|
|
|
2020-02-06 20:30:44 +00:00
|
|
|
lock sync.Mutex
|
2020-02-07 10:36:42 +00:00
|
|
|
users map[string]*user
|
2020-02-06 20:11:35 +00:00
|
|
|
downstreamConns []*downstreamConn
|
2020-02-07 10:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer() *Server {
|
|
|
|
return &Server{
|
|
|
|
Logger: log.New(log.Writer(), "", log.LstdFlags),
|
|
|
|
users: make(map[string]*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-06 15:03:07 +00:00
|
|
|
func (s *Server) Run() {
|
2020-02-07 10:36:42 +00:00
|
|
|
// TODO: multi-user
|
|
|
|
u := &user{username: "jounce", srv: s}
|
|
|
|
|
|
|
|
s.lock.Lock()
|
|
|
|
s.users[u.username] = u
|
|
|
|
s.lock.Unlock()
|
|
|
|
|
2020-02-06 15:03:07 +00:00
|
|
|
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() {
|
2020-02-07 10:36:42 +00:00
|
|
|
conn, err := connectToUpstream(u, upstream)
|
2020-02-06 20:20:22 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Logger.Printf("failed to connect to upstream server %q: %v", upstream.Addr, err)
|
|
|
|
return
|
|
|
|
}
|
2020-02-06 21:25:32 +00:00
|
|
|
|
2020-02-07 10:36:42 +00:00
|
|
|
u.lock.Lock()
|
|
|
|
u.upstreamConns = append(u.upstreamConns, conn)
|
|
|
|
u.lock.Unlock()
|
2020-02-06 21:25:32 +00:00
|
|
|
|
2020-02-06 20:20:22 +00:00
|
|
|
if err := conn.readMessages(); err != nil {
|
|
|
|
conn.logger.Printf("failed to handle messages: %v", err)
|
2020-02-06 15:13:29 +00:00
|
|
|
}
|
2020-02-06 21:25:32 +00:00
|
|
|
|
2020-02-07 10:36:42 +00:00
|
|
|
u.lock.Lock()
|
|
|
|
for i, c := range u.upstreamConns {
|
2020-02-06 21:25:32 +00:00
|
|
|
if c == conn {
|
2020-02-07 10:36:42 +00:00
|
|
|
u.upstreamConns = append(u.upstreamConns[:i], u.upstreamConns[i+1:]...)
|
2020-02-06 21:25:32 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 10:36:42 +00:00
|
|
|
u.lock.Unlock()
|
2020-02-06 15:13:29 +00:00
|
|
|
}()
|
2020-02-06 15:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:39:56 +00:00
|
|
|
func (s *Server) getUser(name string) *user {
|
|
|
|
s.lock.Lock()
|
|
|
|
u := s.users[name]
|
|
|
|
s.lock.Unlock()
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
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-06 20:11:35 +00:00
|
|
|
netConn, err := ln.Accept()
|
2020-02-04 09:46:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to accept connection: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:11:35 +00:00
|
|
|
conn := newDownstreamConn(s, netConn)
|
2020-02-04 09:46:22 +00:00
|
|
|
go func() {
|
2020-02-06 20:30:44 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
s.downstreamConns = append(s.downstreamConns, conn)
|
|
|
|
s.lock.Unlock()
|
2020-02-06 21:25:32 +00:00
|
|
|
|
2020-02-06 20:11:35 +00:00
|
|
|
if err := conn.readMessages(); err != nil {
|
2020-02-06 20:20:22 +00:00
|
|
|
conn.logger.Printf("failed to handle messages: %v", err)
|
2020-02-04 09:46:22 +00:00
|
|
|
}
|
2020-02-06 21:25:32 +00:00
|
|
|
|
2020-02-06 20:30:44 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
for i, c := range s.downstreamConns {
|
|
|
|
if c == conn {
|
|
|
|
s.downstreamConns = append(s.downstreamConns[:i], s.downstreamConns[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.lock.Unlock()
|
2020-02-04 09:46:22 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|