Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support TLS client certificates #29

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions ircdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Sending Escapes:
Options:
--tls Connect using TLS.
--tls-noverify Don't verify the provided TLS certificates.
--tls-cert=<file> A file containing a TLS client cert & key, to use for TLS connections.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one more thing, could you name this --client-cert? We are also considering a server cert argument in #9.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--client-cert is long enough that it throws out the alignment of flag name vs description, btw

--listen=<address> Listen on an address like ":7778", pass through traffic.
--hide=<messages> Comma-separated list of commands/numerics to not print.
--origin=<url> URL to send as the Origin header for a WebSocket connection
Expand Down Expand Up @@ -152,11 +153,20 @@ func parseConnectionConfig(arguments map[string]any) (config lib.ConnectionConfi
config.Origin = originString.(string)
}

config.TLSConfig = &tls.Config{}
if tlsNoverify {
config.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
config.TLSConfig.InsecureSkipVerify = true
}
if tlsCert := arguments["--tls-cert"]; tlsCert != nil {
tlsCert, tErr := tls.LoadX509KeyPair(tlsCert.(string), tlsCert.(string))

if tErr != nil {
err = fmt.Errorf("Cannot load TLS cert/key: %w", tErr)
return
}
config.TLSConfig.Certificates = []tls.Certificate{tlsCert}
}

return
}

Expand Down
2 changes: 1 addition & 1 deletion lib/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewConnection(config ConnectionConfig) (IRCSocket, error) {
return nil, err
}
} else {
socket, err = NewIRCWebSocket(config.WebsocketURL, config.Origin, config.TLSConfig)
socket, err = NewIRCWebSocket(config.WebsocketURL, config.Origin, config.TLS, config.TLSConfig)
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions lib/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type IRCWebSocket struct {
websocket *websocket.Conn
}

func NewIRCWebSocket(wsUrl, origin string, tlsConfig *tls.Config) (IRCSocket, error) {
func NewIRCWebSocket(wsUrl, origin string, useTLS bool, tlsConfig *tls.Config) (IRCSocket, error) {
var headers http.Header
if origin != "" {
headers = make(http.Header)
Expand All @@ -36,8 +36,11 @@ func NewIRCWebSocket(wsUrl, origin string, tlsConfig *tls.Config) (IRCSocket, er

dialer := websocket.Dialer{
Subprotocols: []string{"text.ircv3.net", "binary.ircv3.net"},
TLSClientConfig: tlsConfig,
}
if useTLS {
jesopo marked this conversation as resolved.
Show resolved Hide resolved
dialer.TLSClientConfig = tlsConfig
}

ws, resp, err := dialer.Dial(wsUrl, headers)
if err != nil {
explanation := "no HTTP response"
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ var (
errNoWSSupport = errors.New("websocket support disabled at compile time")
)

func NewIRCWebSocket(wsUrl, origin string, tlsConfig *tls.Config) (IRCSocket, error) {
func NewIRCWebSocket(wsUrl, origin string, useTLS bool, tlsConfig *tls.Config) (IRCSocket, error) {
return nil, errNoWSSupport
}