2021-01-25 05:07:35 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-25 05:07:35 +05:30
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-02-08 11:15:35 +05:30
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2021-01-25 05:07:35 +05:30
|
|
|
"net/http"
|
2022-02-08 11:15:35 +05:30
|
|
|
"os"
|
2021-05-13 01:28:55 +05:30
|
|
|
"strconv"
|
2021-01-25 05:07:35 +05:30
|
|
|
"strings"
|
|
|
|
|
2022-01-01 15:13:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-01-25 05:07:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-03-31 22:31:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/process"
|
2021-01-25 05:07:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/caddyserver/certmagic"
|
|
|
|
)
|
|
|
|
|
2022-02-08 11:15:35 +05:30
|
|
|
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 {
|
2021-01-25 05:07:35 +05:30
|
|
|
// If HTTP Challenge enabled, needs to be serving on port 80. For TLSALPN needs 443.
|
2021-07-08 17:08:13 +05:30
|
|
|
// Due to docker port mapping this can't be checked programmatically
|
2021-01-25 05:07:35 +05:30
|
|
|
// TODO: these are placeholders until we add options for each in settings with appropriate warning
|
|
|
|
enableHTTPChallenge := true
|
|
|
|
enableTLSALPNChallenge := true
|
2021-05-13 01:28:55 +05:30
|
|
|
altHTTPPort := 0
|
2021-05-14 20:09:10 +05:30
|
|
|
altTLSALPNPort := 0
|
2021-05-13 01:28:55 +05:30
|
|
|
|
|
|
|
if p, err := strconv.Atoi(setting.PortToRedirect); err == nil {
|
|
|
|
altHTTPPort = p
|
|
|
|
}
|
2021-05-14 20:09:10 +05:30
|
|
|
if p, err := strconv.Atoi(setting.HTTPPort); err == nil {
|
|
|
|
altTLSALPNPort = p
|
|
|
|
}
|
2021-01-25 05:07:35 +05:30
|
|
|
|
|
|
|
magic := certmagic.NewDefault()
|
2022-02-08 11:15:35 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 16:02:42 +05:30
|
|
|
myACME := certmagic.NewACMEIssuer(magic, certmagic.ACMEIssuer{
|
2022-02-08 11:15:35 +05:30
|
|
|
CA: setting.AcmeURL,
|
|
|
|
TrustedRoots: certPool,
|
|
|
|
Email: setting.AcmeEmail,
|
|
|
|
Agreed: setting.AcmeTOS,
|
2021-01-25 05:07:35 +05:30
|
|
|
DisableHTTPChallenge: !enableHTTPChallenge,
|
|
|
|
DisableTLSALPNChallenge: !enableTLSALPNChallenge,
|
2021-05-14 20:09:10 +05:30
|
|
|
ListenHost: setting.HTTPAddr,
|
|
|
|
AltTLSALPNPort: altTLSALPNPort,
|
2021-05-13 01:28:55 +05:30
|
|
|
AltHTTPPort: altHTTPPort,
|
2021-01-25 05:07:35 +05:30
|
|
|
})
|
|
|
|
|
2021-04-23 02:12:33 +05:30
|
|
|
magic.Issuers = []certmagic.Issuer{myACME}
|
2021-01-25 05:07:35 +05:30
|
|
|
|
|
|
|
// this obtains certificates or renews them if necessary
|
2022-02-08 11:15:35 +05:30
|
|
|
err := magic.ManageSync(graceful.GetManager().HammerContext(), []string{setting.Domain})
|
2021-01-25 05:07:35 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := magic.TLSConfig()
|
2021-07-13 22:47:46 +05:30
|
|
|
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2")
|
2021-01-25 05:07:35 +05:30
|
|
|
|
2021-11-20 11:42:43 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-25 05:07:35 +05:30
|
|
|
if enableHTTPChallenge {
|
|
|
|
go func() {
|
2022-03-31 22:31:43 +05:30
|
|
|
_, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Web: ACME HTTP challenge server", process.SystemProcessType, true)
|
|
|
|
defer finished()
|
|
|
|
|
2021-01-25 05:07:35 +05:30
|
|
|
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)
|
2022-08-21 23:50:43 +05:30
|
|
|
err := runHTTP("tcp", setting.HTTPAddr+":"+setting.PortToRedirect, "Let's Encrypt HTTP Challenge", myACME.HTTPChallengeHandler(http.HandlerFunc(runLetsEncryptFallbackHandler)), setting.RedirectorUseProxyProtocol)
|
2021-01-25 05:07:35 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to start the Let's Encrypt handler on port %s: %v", setting.PortToRedirect, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-08-21 23:50:43 +05:30
|
|
|
return runHTTPSWithTLSConfig("tcp", listenAddr, "Web", tlsConfig, m, setting.UseProxyProtocol, setting.ProxyProtocolTLSBridging)
|
2021-01-25 05:07:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
|
2021-01-25 05:07:35 +05:30
|
|
|
}
|