Skip to content

Commit

Permalink
Added support for SSL #51 (#53)
Browse files Browse the repository at this point in the history
* Added support for SSL #51

* Added TLS support for Prefork
  • Loading branch information
muhammednagy authored Jun 24, 2020
1 parent f60cb78 commit 2213f9c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
15 changes: 15 additions & 0 deletions gearbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ type Settings struct {

// The maximum amount of time to wait for the next request when keep-alive is enabled
IdleTimeout time.Duration // default unlimited

// Enable TLS or not
TLSEnabled bool // default false

// The path of the TLS certificate
TLSCertPath string // default ""

// The path of the TLS key
TLSKeyPath string // default ""
}

// Route struct which holds each route info
Expand Down Expand Up @@ -256,6 +265,9 @@ func (gb *gearbox) Start(address string) error {
pf.Reuseport = true
pf.Network = "tcp4"

if gb.settings.TLSEnabled {
return pf.ListenAndServeTLS(address, gb.settings.TLSCertPath, gb.settings.TLSKeyPath)
}
return pf.ListenAndServe(address)
}

Expand All @@ -269,6 +281,9 @@ func (gb *gearbox) Start(address string) error {
printStartupMessage(address)
}

if gb.settings.TLSEnabled {
return gb.httpServer.ServeTLS(ln, gb.settings.TLSCertPath, gb.settings.TLSKeyPath)
}
return gb.httpServer.Serve(ln)
}

Expand Down
35 changes: 35 additions & 0 deletions gearbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gearbox
import (
"bufio"
"bytes"
"crypto/tls"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -236,6 +237,40 @@ func TestStart(t *testing.T) {
gb.Start(":3000")
}

// TestStart tests start service method
func TestStartWithTLS(t *testing.T) {
gb := New(&Settings{
DisableStartupMessage: true,
TLSKeyPath: "ssl-cert-snakeoil.key",
TLSCertPath: "ssl-cert-snakeoil.crt",
TLSEnabled: true,
})
// use a channel to hand off the error ( if any )
errs := make(chan error, 1)

go func() {
time.Sleep(1000 * time.Millisecond)
_, err := tls.DialWithDialer(&net.Dialer{
Timeout: time.Second * 10,
},
"tcp",
"localhost:3000",
&tls.Config{
InsecureSkipVerify: true,
})
errs <- err
gb.Stop()
}()

gb.Start(":3000")

// wait for an error
err := <-errs
if err != nil {
t.Fatalf("StartWithSSL failed to connect with TLS error: %s", err)
}
}

// TestStartInvalidListener tests start with invalid listener
func TestStartInvalidListener(t *testing.T) {
gb := New()
Expand Down
22 changes: 22 additions & 0 deletions ssl-cert-snakeoil.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDjjCCAnYCCQD7pt94/+oaVTANBgkqhkiG9w0BAQsFADCBiDELMAkGA1UEBhMC
RUUxEDAOBgNVBAgMB1RhbGxpbm4xEDAOBgNVBAcMB1RhbGxpbm4xEDAOBgNVBAoM
B0dlYXJib3gxEDAOBgNVBAsMB0dlYXJib3gxEjAQBgNVBAMMCWxvY2FsaG9zdDEd
MBsGCSqGSIb3DQEJARYObWVAbXVobmFneS5jb20wHhcNMjAwNjI0MTgzODE2WhcN
MjEwNjI0MTgzODE2WjCBiDELMAkGA1UEBhMCRUUxEDAOBgNVBAgMB1RhbGxpbm4x
EDAOBgNVBAcMB1RhbGxpbm4xEDAOBgNVBAoMB0dlYXJib3gxEDAOBgNVBAsMB0dl
YXJib3gxEjAQBgNVBAMMCWxvY2FsaG9zdDEdMBsGCSqGSIb3DQEJARYObWVAbXVo
bmFneS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDIlbxHqZnm
mCOd7mW5XbqIxfd85rIoUnGgeTn+PoahjkV3A5uP9RCpRFig3TveR8gZM5argNMe
eFqgppcEMSMWAQAaWGm/ieL94Xz3WuOBZg5ACrtpiHuThyVFvKpUp/2QwGNBeMI+
1fgxCruylTJoNWPj2U/uPwsVuxxk/bpc9W+oWFXGc6tUYYv/XmmEW9WYrHZUZkR9
bsu6iwcQUJo6poMNopqGPjEgTheLJn/QEx0Muk6ESxLJyLSbhIn9cXsoDsgaTp8l
LG5nrWa1mwdYP48dQO5eqc8+HWX8ngfGzFf7RBZdMVw/oKOrImm70+jnwT1PFvDD
JZsW0rlaOY8ZAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAD3llJQnRiDKc4OqtnTr
Hwt4jqazuysovHkpH2z++CiGWCRzLpKSuJDbhCfZzFeq3+h0gen2tVCuWaFVJDtI
g81ANcagSIW9rxEMCrbn0cRsIWjpSCyE/+4alUh7f89bZJMQPR3IZBSs5aRHUVcY
wIIOSWG882gN/a8R/atMYUWEmwSvWw43UWa/hlDYP6LuqhlTABa+GVMJdqA7q5F+
oT8Em7KaEl8VszLKJQqu+fFJvBwkVj+qfp/8ziXxQ+DCtDLVdBcNnc7G9Zqj9xSR
4xIje+189L/gI/uiEqxpPFwPwGUK4vsho4A2KTgoDMuz+wL8KkC7R5R3QvVtBZaz
aa8=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions ssl-cert-snakeoil.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAyJW8R6mZ5pgjne5luV26iMX3fOayKFJxoHk5/j6GoY5FdwOb
j/UQqURYoN073kfIGTOWq4DTHnhaoKaXBDEjFgEAGlhpv4ni/eF891rjgWYOQAq7
aYh7k4clRbyqVKf9kMBjQXjCPtX4MQq7spUyaDVj49lP7j8LFbscZP26XPVvqFhV
xnOrVGGL/15phFvVmKx2VGZEfW7LuosHEFCaOqaDDaKahj4xIE4XiyZ/0BMdDLpO
hEsSyci0m4SJ/XF7KA7IGk6fJSxuZ61mtZsHWD+PHUDuXqnPPh1l/J4HxsxX+0QW
XTFcP6CjqyJpu9Po58E9TxbwwyWbFtK5WjmPGQIDAQABAoIBAQCdgzf+7mg0dHRD
qNmo3rmEfjECnAZ7M4KXCZtI08DVTOuvsd2PBlq3ETyn1hMVUjkH0UMYGmmvG8PP
NAKTACMVPGPgTF1+U8E0rMV9WJofGw1FrSfOj6a7Cv1x7d2z7NdpvCo1V6bxwQ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grPZ5SV+xlA=
-----END RSA PRIVATE KEY-----

0 comments on commit 2213f9c

Please sign in to comment.