Skip to content

Commit

Permalink
Merge pull request #6 from rosahaj/master
Browse files Browse the repository at this point in the history
feat: Generate cert and key at paths specified through CLI
  • Loading branch information
LyleMi authored Jan 6, 2024
2 parents 963dd6b + b4c1f31 commit 6c5576d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

func generateCertificate() error {
func generateCertificate(certPath, keyPath string) error {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
Expand All @@ -35,14 +35,14 @@ func generateCertificate() error {
return err
}

certOut, err := os.Create("cert.pem")
certOut, err := os.Create(certPath)
if err != nil {
return err
}
defer certOut.Close()
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})

keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,16 @@ func main() {
flag.Parse()

if !fileExists(Config.Cert) || !fileExists(Config.Key) {
log.Println("cert not exists, generate")
generateCertificate()
if fileExists(Config.Cert) {
log.Println("found cert, but no corresponding key")
os.Exit(-1)
} else if fileExists(Config.Key) {
log.Println("found key, but no corresponding cert")
os.Exit(-1)
}

log.Println("cert and key do not exist, generating")
generateCertificate(Config.Cert, Config.Key)
}

server := &http.Server{
Expand Down

0 comments on commit 6c5576d

Please sign in to comment.