Skip to content

Commit

Permalink
Support for dial non-TLS connection
Browse files Browse the repository at this point in the history
  • Loading branch information
waybackarchiver committed Feb 21, 2024
1 parent d0f20ad commit 479789b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
46 changes: 46 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,52 @@ func TestIRC(t *testing.T) {
tt.call(t, opts, tt.want)
})
}

testsBool := []struct {
name string
envs map[string]bool
call func(*testing.T, *Options, bool)
want bool
}{
{
name: "default irc secure",
envs: map[string]bool{
"WAYBACK_IRC_SECURE": true,
},
call: func(t *testing.T, opts *Options, want bool) {
called := opts.IRCSecure()
if called != want {
t.Errorf(`Unexpected get the irc secure, got %v instead of %v`, called, want)
}
},
want: defIRCSecure,
},
{
name: "specified irc secure",
envs: map[string]bool{
"WAYBACK_IRC_SECURE": false,
},
call: func(t *testing.T, opts *Options, want bool) {
called := opts.IRCSecure()
if called != want {
t.Errorf(`Unexpected get the irc secure, got %v instead of %v`, called, want)
}
},
want: false,
},
}
for _, tt := range testsBool {
t.Run(tt.name, func(t *testing.T) {
for key, val := range tt.envs {
t.Setenv(key, strconv.FormatBool(val))
}
opts, err := NewParser().ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing environment variables failed: %v`, err)
}
tt.call(t, opts, tt.want)
})
}
}

func TestMatrix(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
defIRCPassword = ""
defIRCChannel = ""
defIRCServer = "irc.libera.chat:6697"
defIRCSecure = true

defXMPPUsername = ""
defXMPPPassword = ""
Expand Down Expand Up @@ -229,6 +230,7 @@ type irc struct {
password string
channel string
server string
secure bool
}

type onion struct {
Expand Down Expand Up @@ -337,6 +339,7 @@ func NewOptions() *Options {
password: defIRCPassword,
channel: defIRCChannel,
server: defIRCServer,
secure: defIRCSecure,
},
onion: &onion{
localPort: defOnionLocalPort,
Expand Down Expand Up @@ -629,6 +632,11 @@ func (o *Options) IRCServer() string {
return o.irc.server
}

// IRCSecure returns whether use TLS for IRC connection.
func (o *Options) IRCSecure() bool {
return o.irc.secure
}

// PublishToIRCChannel returns whether publish results to IRC channel.
func (o *Options) PublishToIRCChannel() bool {
return o.irc.nick != "" && o.irc.channel != ""
Expand Down
2 changes: 2 additions & 0 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func (p *Parser) parseLines(lines []string) (err error) {
p.opts.irc.channel = parseString(val, defIRCChannel)
case "WAYBACK_IRC_SERVER":
p.opts.irc.server = parseString(val, defIRCServer)
case "WAYBACK_IRC_SECURE":
p.opts.irc.secure = parseBool(val, defIRCSecure)
case "WAYBACK_MATRIX_HOMESERVER":
p.opts.matrix.homeserver = parseString(val, defMatrixHomeserver)
case "WAYBACK_MATRIX_USERID":
Expand Down
4 changes: 3 additions & 1 deletion service/relaychat/relaychat.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func (i *IRC) Serve() error {
if err != nil {
return errors.Wrap(err, "failed to establish connection")
}
conn = tls.Client(conn, &tls.Config{MinVersion: tls.VersionTLS12, ServerName: srv.Hostname()})
if i.opts.IRCSecure() {
conn = tls.Client(conn, &tls.Config{MinVersion: tls.VersionTLS12, ServerName: srv.Hostname()})
}
i.conn = irc.NewClient(conn, config)
logger.Info("Serving IRC server: %s, nick: %s", i.opts.IRCServer(), i.conn.CurrentNick())

Check warning on line 129 in service/relaychat/relaychat.go

View check run for this annotation

Codecov / codecov/patch

service/relaychat/relaychat.go#L120-L129

Added lines #L120 - L129 were not covered by tests

Expand Down

0 comments on commit 479789b

Please sign in to comment.