Skip to content

Commit

Permalink
Added basic TLS support
Browse files Browse the repository at this point in the history
Using -key and -cert CLI optins.

Signed-off-by: Andrea Barberio <[email protected]>
  • Loading branch information
insomniacslk committed May 5, 2020
1 parent e38b1e9 commit d90d589
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -32,6 +33,8 @@ var (
logLevel = flag.String("L", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
flagSlackDebug = flag.Bool("D", false, "Enable debug logging of the Slack API")
flagPagination = flag.Int("P", 0, "Pagination value for API calls. If 0 or unspecified, use the recommended default (currently 200). Larger values can help on large Slack teams")
flagKey = flag.String("key", "", "TLS key for HTTPS server. Requires -cert")
flagCert = flag.String("cert", "", "TLS certificate for HTTPS server. Requires -key")
)

var log = logger.GetLogger("main")
Expand Down Expand Up @@ -81,6 +84,21 @@ func main() {
log.Fatalf("Missing or invalid download directory: %s", *fileDownloadLocation)
}
}
doTLS := false
if *flagKey != "" && *flagCert != "" {
doTLS = true
}
var tlsConfig *tls.Config
if doTLS {
if *flagKey == "" || *flagCert == "" {
log.Fatalf("-key and -cert must be specified together")
}
cert, err := tls.LoadX509KeyPair(*flagCert, *flagKey)
if err != nil {
log.Fatalf("Failed to load TLS key/cert: %v", err)
}
tlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
}
server := Server{
LocalAddr: &localAddr,
Name: sName,
Expand All @@ -89,6 +107,7 @@ func main() {
FileProxyPrefix: *fileProxyPrefix,
SlackDebug: *flagSlackDebug,
Pagination: *flagPagination,
TLSConfig: tlsConfig,
}
if err := server.Start(); err != nil {
log.Fatal(err)
Expand Down
12 changes: 9 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net"
Expand All @@ -14,22 +15,27 @@ import (
type Server struct {
Name string
LocalAddr net.Addr
Listener *net.TCPListener
Listener net.Listener
SlackAPIKey string
SlackDebug bool
ChunkSize int
FileDownloadLocation string
FileProxyPrefix string
Pagination int
TLSConfig *tls.Config
}

// Start runs the IRC server
func (s Server) Start() error {
listener, err := net.Listen("tcp", s.LocalAddr.String())
var err error
if s.TLSConfig != nil {
s.Listener, err = tls.Listen("tcp", s.LocalAddr.String(), s.TLSConfig)
} else {
s.Listener, err = net.Listen("tcp", s.LocalAddr.String())
}
if err != nil {
return err
}
s.Listener = listener.(*net.TCPListener)
defer s.Listener.Close()
log.Infof("Listening on %v", s.LocalAddr)
for {
Expand Down

0 comments on commit d90d589

Please sign in to comment.