Skip to content

Commit

Permalink
fix: create Certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
janiltonmaciel committed Aug 27, 2020
1 parent 5f10dcc commit f3d5020
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ statiks --cors /home
- start server at http://192.168.1.100:9080 serving "/tmp" with gzip compression
```bash
statiks --address 192.168.1.100 --compression /tmp
statiks --host 192.168.1.100 --compression /tmp
```
- start server at https://0.0.0.0:9080 serving "." with HTTPS
```bash
statiks --ssl
statiks --ssl --cert cert.pem --key key.pem
```
6 changes: 3 additions & 3 deletions lib/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ OPTIONS:
{{- end }}
EXAMPLES:
- start server at http://0.0.0.0:9000 serving "."
- start server at http://0.0.0.0:9000 serving "." current directory
statiks -port 9000
- start server at http://0.0.0.0:9080 serving "/home" with CORS
statiks --cors /home
- start server at http://192.168.1.100:9080 serving "/tmp" with gzip compression
statiks --host 192.168.1.100 --gzip /tmp
statiks --host 192.168.1.100 --compression /tmp
- start server at https://0.0.0.0:9080 serving "." with HTTPS
statiks --ssl
statiks --ssl --cert cert.pem --key key.pem
{{- if .Version }}
Expand Down
41 changes: 2 additions & 39 deletions lib/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,8 @@ func GetMkCert(host string) (certArray []byte, keyArray []byte) {
OrganizationalUnit: []string{userAndHostname},
},

NotAfter: time.Now().AddDate(10, 0, 0),

// Fix the notBefore to temporarily bypass macOS Catalina's limit on
// certificate lifespan. Once mkcert provides an ACME server, automation
// will be the recommended way to guarantee uninterrupted functionality,
// and the lifespan will be shortened to 825 days. See issue 174 and
// https://support.apple.com/en-us/HT210176.
NotBefore: time.Date(2019, time.June, 1, 0, 0, 0, 0, time.UTC),
NotAfter: time.Now().AddDate(0, 1, 0),
NotBefore: time.Now(),

KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
Expand Down Expand Up @@ -160,34 +154,3 @@ func fatalIfErr(err error, msg string) {
logger.Fatalf("ERROR: %s: %s", msg, err)
}
}

// func runHTTPS(config statiksConfig, handler http.Handler) error {
// addr := fmt.Sprintf("%s:%s", config.address, config.port)
// cert, key := GetMkCert(addr)

// keyPair, err := tls.X509KeyPair(cert, key)
// if err != nil {
// logger.Fatal("Error: Couldn't create key pair")
// }

// var certificates []tls.Certificate
// certificates = append(certificates, keyPair)

// cfg := &tls.Config{
// MinVersion: tls.VersionTLS12,
// PreferServerCipherSuites: true,
// Certificates: certificates,
// }

// s := &http.Server{
// Addr: addr,
// Handler: handler,
// ReadTimeout: readTimeout,
// WriteTimeout: writeTimeout,
// TLSConfig: cfg,
// }

// fmt.Printf("Running on \n ⚡️ https://%s, serving '%s'\n\n", addr, config.path)
// fmt.Print("CTRL-C to stop the️ server\n")
// return s.ListenAndServeTLS("", "")
// }
32 changes: 31 additions & 1 deletion lib/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"crypto/tls"
"fmt"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -83,11 +84,40 @@ func (s *Server) runHTTP() error {
fmt.Printf("Running on HTTP\n ⚡️ http://%s, serving '%s'\n\n", s.config.address, s.config.path)
fmt.Print("CTRL-C to stop the️ server\n")
return http.ListenAndServe(s.config.address, s.handler)

}

func (s *Server) runHTTPS() error {
fmt.Printf("Running on HTTPS\n ⚡️ https://%s, serving '%s'\n\n", s.config.address, s.config.path)
fmt.Print("CTRL-C to stop the️ server\n")
return http.ListenAndServeTLS(s.config.address, s.config.cert, s.config.key, s.handler)
}

func (s *Server) runHTTPSMemory() error {
cert, key := GetMkCert(s.config.host)

keyPair, err := tls.X509KeyPair(cert, key)
if err != nil {
logger.Fatal("Error: Couldn't create key pair")
}

var certificates []tls.Certificate
certificates = append(certificates, keyPair)

cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
Certificates: certificates,
}

srv := &http.Server{
Addr: s.config.address,
Handler: s.handler,
// ReadTimeout: readTimeout,
// WriteTimeout: writeTimeout,
TLSConfig: cfg,
}

fmt.Printf("Running on HTTPS\n ⚡️ https://%s, serving '%s'\n\n", s.config.address, s.config.path)
fmt.Print("CTRL-C to stop the️ server\n")
return srv.ListenAndServeTLS("", "")
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func init() {
func main() {
app := cli.NewApp()
app.Name = "statiks"
app.Usage = "Fast, zero-configuration, static HTTP filer server."
app.Usage = "fast, zero-configuration, static HTTP filer server."
app.UsageText = "statiks [options] <path>"
app.Author = author
app.Version = version
app.Action = lib.MainAction

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "a, address",
Name: "h, host",
Value: "0.0.0.0",
Usage: "host address to bind to",
},
Expand Down Expand Up @@ -100,7 +100,7 @@ func main() {
}

cli.HelpFlag = cli.BoolFlag{
Name: "h, help",
Name: "help",
Usage: "show help",
}

Expand Down

0 comments on commit f3d5020

Please sign in to comment.