From 53b73d71f30e640a6b8548262f9e412a192d5cbe Mon Sep 17 00:00:00 2001 From: "J.W.F. (he/him)" <4721034+jwflory@users.noreply.github.com> Date: Thu, 29 Oct 2020 18:35:02 -0400 Subject: [PATCH] irc: Add support for multiple IP addresses (closes #324) (#356) --- docs/user/config-file-glossary.rst | 5 ++++ env.example | 3 ++ internal/config.go | 1 + internal/handlers/irc/irc.go | 5 ++++ internal/handlers/irc/irc_test.go | 44 ++++++++++++++++-------------- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/docs/user/config-file-glossary.rst b/docs/user/config-file-glossary.rst index 1cb1ef2..05587b3 100644 --- a/docs/user/config-file-glossary.rst +++ b/docs/user/config-file-glossary.rst @@ -11,7 +11,12 @@ This glossary is intended for advanced users. IRC settings ************ +Host connection settings +======================== +``IRC_HOST_IP=""`` + Specify IP address to use for IRC connection. + Useful for hosts with multiple IP addresses. Server connection settings ============================== diff --git a/env.example b/env.example index cd190dd..b599555 100644 --- a/env.example +++ b/env.example @@ -8,6 +8,9 @@ # # ############################################################################### +#####----- Host connection settings -----##### +IRC_HOST_IP="" + #####----- IRC server connection settings -----##### IRC_SERVER=chat.freenode.net diff --git a/internal/config.go b/internal/config.go index cde4091..37bd3e3 100644 --- a/internal/config.go +++ b/internal/config.go @@ -15,6 +15,7 @@ const defaultPath = ".env" // IRCSettings includes settings related to the IRC bot/message relaying type IRCSettings struct { + BindAddress string `env:"IRC_HOST_IP" envDefault:""` Server string `env:"IRC_SERVER,required"` ServerPass string `env:"IRC_SERVER_PASSWORD" envDefault:""` Port int `env:"IRC_PORT" envDefault:"6667" validate:"min=0,max=65535"` diff --git a/internal/handlers/irc/irc.go b/internal/handlers/irc/irc.go index af6d645..ccbccce 100644 --- a/internal/handlers/irc/irc.go +++ b/internal/handlers/irc/irc.go @@ -34,6 +34,11 @@ func NewClient(settings *internal.IRCSettings, telegramSettings *internal.Telegr SSL: settings.UseSSL, }) + // Bind an IP address for IRC connection + if settings.BindAddress != "" { + client.Config.Bind = settings.BindAddress + } + // IRC server authentication if settings.ServerPass != "" { client.Config.ServerPass = settings.ServerPass diff --git a/internal/handlers/irc/irc_test.go b/internal/handlers/irc/irc_test.go index a62c248..b641794 100644 --- a/internal/handlers/irc/irc_test.go +++ b/internal/handlers/irc/irc_test.go @@ -12,7 +12,7 @@ import ( func TestNewClientBasic(t *testing.T) { ircSettings := &internal.IRCSettings{ Server: "irc.batcave.intl", - Port: 1337, + Port: 1337, BotIdent: "alfred", BotName: "Alfred Pennyworth", BotNick: "alfred-p", @@ -22,13 +22,13 @@ func TestNewClientBasic(t *testing.T) { } client := NewClient(ircSettings, nil, logger) - expectedPing, _:= time.ParseDuration("20s") + expectedPing, _ := time.ParseDuration("20s") expectedConfig := girc.Config{ Server: "irc.batcave.intl", - Port: 1337, - Nick: "alfred-p", - Name: "Alfred Pennyworth", - User: "alfred", + Port: 1337, + Nick: "alfred-p", + Name: "Alfred Pennyworth", + User: "alfred", PingDelay: expectedPing, } assert.Equal(t, client.Settings, ircSettings, "Client settings should be properly set") @@ -37,13 +37,14 @@ func TestNewClientBasic(t *testing.T) { func TestNewClientFull(t *testing.T) { ircSettings := &internal.IRCSettings{ - Server: "irc.batcave.intl", - ServerPass: "BatmanNeverDies!", - Port: 1337, - BotIdent: "alfred", - BotName: "Alfred Pennyworth", - BotNick: "alfred-p", - NickServUser: "irc_moderators", + BindAddress: "129.21.13.37", + Server: "irc.batcave.intl", + ServerPass: "BatmanNeverDies!", + Port: 1337, + BotIdent: "alfred", + BotName: "Alfred Pennyworth", + BotNick: "alfred-p", + NickServUser: "irc_moderators", NickServPassword: "ProtectGotham", } logger := internal.Debug{ @@ -52,14 +53,15 @@ func TestNewClientFull(t *testing.T) { client := NewClient(ircSettings, nil, logger) expectedPing, _ := time.ParseDuration("20s") expectedConfig := girc.Config{ - Server: "irc.batcave.intl", - ServerPass: "BatmanNeverDies!", - Port: 1337, - Nick: "alfred-p", - Name: "Alfred Pennyworth", - User: "alfred", - PingDelay: expectedPing, - SASL: &girc.SASLPlain{ + Bind: "129.21.13.37", + Server: "irc.batcave.intl", + ServerPass: "BatmanNeverDies!", + Port: 1337, + Nick: "alfred-p", + Name: "Alfred Pennyworth", + User: "alfred", + PingDelay: expectedPing, + SASL: &girc.SASLPlain{ User: "irc_moderators", Pass: "ProtectGotham", },