You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.1 KiB
133 lines
4.1 KiB
4 years ago
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package cmd
|
||
|
|
||
|
import (
|
||
3 years ago
|
"crypto/x509"
|
||
|
"encoding/pem"
|
||
|
"fmt"
|
||
4 years ago
|
"net/http"
|
||
3 years ago
|
"os"
|
||
4 years ago
|
"strconv"
|
||
4 years ago
|
"strings"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/modules/graceful"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
|
"code.gitea.io/gitea/modules/setting"
|
||
|
|
||
|
"github.com/caddyserver/certmagic"
|
||
|
)
|
||
|
|
||
3 years ago
|
func getCARoot(path string) (*x509.CertPool, error) {
|
||
|
r, err := os.ReadFile(path)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
block, _ := pem.Decode(r)
|
||
|
if block == nil {
|
||
|
return nil, fmt.Errorf("no PEM found in the file %s", path)
|
||
|
}
|
||
|
caRoot, err := x509.ParseCertificate(block.Bytes)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
certPool := x509.NewCertPool()
|
||
|
certPool.AddCert(caRoot)
|
||
|
return certPool, nil
|
||
|
}
|
||
|
|
||
|
func runACME(listenAddr string, m http.Handler) error {
|
||
4 years ago
|
// If HTTP Challenge enabled, needs to be serving on port 80. For TLSALPN needs 443.
|
||
3 years ago
|
// Due to docker port mapping this can't be checked programmatically
|
||
4 years ago
|
// TODO: these are placeholders until we add options for each in settings with appropriate warning
|
||
|
enableHTTPChallenge := true
|
||
|
enableTLSALPNChallenge := true
|
||
4 years ago
|
altHTTPPort := 0
|
||
4 years ago
|
altTLSALPNPort := 0
|
||
4 years ago
|
|
||
|
if p, err := strconv.Atoi(setting.PortToRedirect); err == nil {
|
||
|
altHTTPPort = p
|
||
|
}
|
||
4 years ago
|
if p, err := strconv.Atoi(setting.HTTPPort); err == nil {
|
||
|
altTLSALPNPort = p
|
||
|
}
|
||
4 years ago
|
|
||
|
magic := certmagic.NewDefault()
|
||
3 years ago
|
magic.Storage = &certmagic.FileStorage{Path: setting.AcmeLiveDirectory}
|
||
|
// Try to use private CA root if provided, otherwise defaults to system's trust
|
||
|
var certPool *x509.CertPool
|
||
|
if setting.AcmeCARoot != "" {
|
||
|
var err error
|
||
|
certPool, err = getCARoot(setting.AcmeCARoot)
|
||
|
if err != nil {
|
||
|
log.Warn("Failed to parse CA Root certificate, using default CA trust: %v", err)
|
||
|
}
|
||
|
}
|
||
4 years ago
|
myACME := certmagic.NewACMEManager(magic, certmagic.ACMEManager{
|
||
3 years ago
|
CA: setting.AcmeURL,
|
||
|
TrustedRoots: certPool,
|
||
|
Email: setting.AcmeEmail,
|
||
|
Agreed: setting.AcmeTOS,
|
||
4 years ago
|
DisableHTTPChallenge: !enableHTTPChallenge,
|
||
|
DisableTLSALPNChallenge: !enableTLSALPNChallenge,
|
||
4 years ago
|
ListenHost: setting.HTTPAddr,
|
||
|
AltTLSALPNPort: altTLSALPNPort,
|
||
4 years ago
|
AltHTTPPort: altHTTPPort,
|
||
4 years ago
|
})
|
||
|
|
||
4 years ago
|
magic.Issuers = []certmagic.Issuer{myACME}
|
||
4 years ago
|
|
||
|
// this obtains certificates or renews them if necessary
|
||
3 years ago
|
err := magic.ManageSync(graceful.GetManager().HammerContext(), []string{setting.Domain})
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
tlsConfig := magic.TLSConfig()
|
||
3 years ago
|
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2")
|
||
4 years ago
|
|
||
3 years ago
|
if version := toTLSVersion(setting.SSLMinimumVersion); version != 0 {
|
||
|
tlsConfig.MinVersion = version
|
||
|
}
|
||
|
if version := toTLSVersion(setting.SSLMaximumVersion); version != 0 {
|
||
|
tlsConfig.MaxVersion = version
|
||
|
}
|
||
|
|
||
|
// Set curve preferences
|
||
|
if curves := toCurvePreferences(setting.SSLCurvePreferences); len(curves) > 0 {
|
||
|
tlsConfig.CurvePreferences = curves
|
||
|
}
|
||
|
|
||
|
// Set cipher suites
|
||
|
if ciphers := toTLSCiphers(setting.SSLCipherSuites); len(ciphers) > 0 {
|
||
|
tlsConfig.CipherSuites = ciphers
|
||
|
}
|
||
|
|
||
4 years ago
|
if enableHTTPChallenge {
|
||
|
go func() {
|
||
|
log.Info("Running Let's Encrypt handler on %s", setting.HTTPAddr+":"+setting.PortToRedirect)
|
||
|
// all traffic coming into HTTP will be redirect to HTTPS automatically (LE HTTP-01 validation happens here)
|
||
3 years ago
|
err := runHTTP("tcp", setting.HTTPAddr+":"+setting.PortToRedirect, "Let's Encrypt HTTP Challenge", myACME.HTTPChallengeHandler(http.HandlerFunc(runLetsEncryptFallbackHandler)))
|
||
4 years ago
|
if err != nil {
|
||
|
log.Fatal("Failed to start the Let's Encrypt handler on port %s: %v", setting.PortToRedirect, err)
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
3 years ago
|
return runHTTPSWithTLSConfig("tcp", listenAddr, "Web", tlsConfig, m)
|
||
4 years ago
|
}
|
||
|
|
||
|
func runLetsEncryptFallbackHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||
|
http.Error(w, "Use HTTPS", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
// Remove the trailing slash at the end of setting.AppURL, the request
|
||
|
// URI always contains a leading slash, which would result in a double
|
||
|
// slash
|
||
|
target := strings.TrimSuffix(setting.AppURL, "/") + r.URL.RequestURI()
|
||
|
http.Redirect(w, r, target, http.StatusFound)
|
||
|
}
|