Auto-save IRC networks

This commit is contained in:
Simon Ser 2020-03-12 21:28:09 +01:00
parent 0ef08dfbb5
commit 85f28daf2d
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 48 additions and 7 deletions

View File

@ -14,6 +14,8 @@ A user-friendly IRC bouncer.
go run ./cmd/jouncectl create-user jounce go run ./cmd/jouncectl create-user jounce
go run ./cmd/jounce go run ./cmd/jounce
Then connect with username `jounce@chat.freenode.net` and join `#jounce`.
## Contributing ## Contributing
Send patches on the [mailing list], report bugs on the [issue tracker]. Send patches on the [mailing list], report bugs on the [issue tracker].

View File

@ -1,10 +1,12 @@
package jounce package jounce
import ( import (
"crypto/tls"
"fmt" "fmt"
"io" "io"
"net" "net"
"strings" "strings"
"time"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"gopkg.in/irc.v3" "gopkg.in/irc.v3"
@ -336,6 +338,15 @@ func (dc *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
return nil return nil
} }
func sanityCheckServer(addr string) error {
dialer := net.Dialer{Timeout: 30 * time.Second}
conn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
if err != nil {
return err
}
return conn.Close()
}
func (dc *downstreamConn) register() error { func (dc *downstreamConn) register() error {
username := strings.TrimPrefix(dc.username, "~") username := strings.TrimPrefix(dc.username, "~")
var networkName string var networkName string
@ -365,13 +376,25 @@ func (dc *downstreamConn) register() error {
if networkName != "" { if networkName != "" {
network = u.getNetwork(networkName) network = u.getNetwork(networkName)
if network == nil { if network == nil {
dc.logger.Printf("failed registration: unknown network %q", networkName) addr := networkName
dc.SendMessage(&irc.Message{ if !strings.ContainsRune(addr, ':') {
Prefix: dc.srv.prefix(), addr = addr + ":6697"
Command: irc.ERR_PASSWDMISMATCH, }
Params: []string{"*", fmt.Sprintf("Unknown network %q", networkName)},
}) dc.logger.Printf("trying to connect to new upstream server %q", addr)
return nil if err := sanityCheckServer(addr); err != nil {
dc.logger.Printf("failed to connect to %q: %v", addr, err)
return ircError{&irc.Message{
Command: irc.ERR_PASSWDMISMATCH,
Params: []string{"*", fmt.Sprintf("Failed to connect to %q", networkName)},
}}
}
dc.logger.Printf("auto-adding network %q", networkName)
network, err = u.createNetwork(networkName, dc.nick)
if err != nil {
return err
}
} }
} }

View File

@ -163,6 +163,22 @@ func (u *user) run() {
u.lock.Unlock() u.lock.Unlock()
} }
func (u *user) createNetwork(addr, nick string) (*network, error) {
network := newNetwork(u, &Network{
Addr: addr,
Nick: nick,
})
err := u.srv.db.StoreNetwork(u.Username, &network.Network)
if err != nil {
return nil, err
}
u.lock.Lock()
u.networks = append(u.networks, network)
u.lock.Unlock()
go network.run()
return network, nil
}
type Server struct { type Server struct {
Hostname string Hostname string
Logger Logger Logger Logger