Skip to content

Commit

Permalink
Add TLS Support for IRC communication (evan-buss#90)
Browse files Browse the repository at this point in the history
* Fix length comparison so zero doesn't appear. (evan-buss#74)

* feat: Add IRC TLS support

Co-authored-by: Evan Buss <evan.buss28@gmail.com>
  • Loading branch information
Kab1r and evan-buss authored Jul 17, 2022
1 parent be85fa8 commit f384706
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
Log bool // True if IRC messages should be logged
Dir string
Server string
EnableTLS bool
SearchBot string
Version string
irc *irc.Conn
Expand Down
3 changes: 2 additions & 1 deletion cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func instantiate(config *Config) {
fmt.Printf("Connecting to %s.", config.Server)
conn := irc.New(config.UserName, config.Version)
config.irc = conn
err := core.Join(conn, config.Server)
err := core.Join(conn, config.Server, config.EnableTLS)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%sConnected to %s.\n", clearLine, config.Server)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/openbooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var desktopConfig server.Config
func init() {
desktopCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug mode.")
desktopCmd.PersistentFlags().StringVarP(&globalFlags.UserName, "name", "n", generateUserName(), "Use a name that isn't randomly generated. One word only.")
desktopCmd.PersistentFlags().StringVarP(&globalFlags.Server, "server", "s", "irc.irchighway.net", "IRC server to connect to.")
desktopCmd.PersistentFlags().StringVarP(&globalFlags.Server, "server", "s", "irc.irchighway.net:6697", "IRC server to connect to.")
desktopCmd.Flags().BoolVar(&serverConfig.EnableTLS, "tls", true, "Connect to server using TLS.")
desktopCmd.PersistentFlags().BoolVarP(&globalFlags.Log, "log", "l", false, "Save raw IRC logs for each client connection.")
desktopCmd.PersistentFlags().StringVar(&globalFlags.SearchBot, "searchbot", "search", "The IRC bot that handles search queries. Try 'searchook' if 'search' is down.")

Expand Down
4 changes: 2 additions & 2 deletions core/irchighway.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// Specific irc.irchighway.net commands

// Join connects to the irc.irchighway.net server and joins the #ebooks channel
func Join(irc *irc.Conn, url string) error {
err := irc.Connect(url)
func Join(irc *irc.Conn, address string, enableTLS bool) error {
err := irc.Connect(address, enableTLS)
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions irc/irc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package irc

import (
"crypto/tls"
"net"
)

Expand All @@ -24,8 +25,15 @@ func New(username, realname string) *Conn {
}

// Connect connects to the given server at port 6667
func (i *Conn) Connect(address string) error {
conn, err := net.Dial("tcp", address+":6667")
func (i *Conn) Connect(address string, enableTLS bool) error {
var conn net.Conn
var err error
if enableTLS {
conn, err = tls.Dial("tcp", address, &tls.Config{InsecureSkipVerify: true})
} else {
conn, err = net.Dial("tcp", address)
}

if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion server/app/src/pages/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default function SearchPage() {
setShowErrors(false);
}, [activeItem]);

const hasErrors = () =>
activeItem?.errors?.length && activeItem?.errors?.length > 0;
const errorMode = () => showErrors && activeItem;
const validInput = () => {
if (errorMode()) {
Expand Down Expand Up @@ -106,7 +108,7 @@ export default function SearchPage() {
{errorMode() ? "Download" : "Search"}
</Button>
</form>
{activeItem?.errors?.length && (
{hasErrors() && (
<Button
appearance={errorMode() ? "primary" : "minimal"}
onClick={() => setShowErrors((show) => !show)}
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Config struct {
DownloadDir string
Basepath string
Server string
EnableTLS bool
SearchTimeout time.Duration
SearchBot string
DisableBrowserDownloads bool
Expand Down
2 changes: 1 addition & 1 deletion server/websocket_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (server *server) routeMessage(message Request, c *Client) {

// handle ConnectionRequests and either connect to the server or do nothing
func (c *Client) startIrcConnection(server *server) {
err := core.Join(c.irc, server.config.Server)
err := core.Join(c.irc, server.config.Server, server.config.EnableTLS)
if err != nil {
c.log.Println(err)
c.send <- newErrorResponse("Unable to connect to IRC server.")
Expand Down

0 comments on commit f384706

Please sign in to comment.