2015-01-13 14:43:45 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-26 14:34:31 -05:00
|
|
|
"errors"
|
2015-01-13 18:14:00 -05:00
|
|
|
"flag"
|
2015-04-15 15:11:32 -05:00
|
|
|
"fmt"
|
2015-05-04 17:23:16 -05:00
|
|
|
"io/ioutil"
|
2015-01-13 14:43:45 -05:00
|
|
|
"log"
|
2015-05-04 07:53:54 -05:00
|
|
|
"os"
|
2015-04-24 21:08:14 -05:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-11-14 20:00:18 -05:00
|
|
|
"time"
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
"github.com/mholt/caddy/caddy"
|
2016-02-11 02:06:05 -05:00
|
|
|
"github.com/mholt/caddy/caddy/https"
|
2016-01-03 19:05:10 -05:00
|
|
|
"github.com/xenolf/lego/acme"
|
2016-02-12 09:30:47 -05:00
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2015-01-13 14:43:45 -05:00
|
|
|
)
|
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
func init() {
|
2015-11-14 23:59:43 -05:00
|
|
|
caddy.TrapSignals()
|
2016-02-26 02:21:20 -05:00
|
|
|
setVersion()
|
2016-02-11 02:06:05 -05:00
|
|
|
flag.BoolVar(&https.Agreed, "agree", false, "Agree to Let's Encrypt Subscriber Agreement")
|
|
|
|
flag.StringVar(&https.CAUrl, "ca", "https://acme-v01.api.letsencrypt.org/directory", "Certificate authority ACME server")
|
2015-10-26 14:34:31 -05:00
|
|
|
flag.StringVar(&conf, "conf", "", "Configuration file to use (default="+caddy.DefaultConfigFile+")")
|
2015-04-24 21:08:14 -05:00
|
|
|
flag.StringVar(&cpu, "cpu", "100%", "CPU cap")
|
2016-02-11 02:06:05 -05:00
|
|
|
flag.StringVar(&https.DefaultEmail, "email", "", "Default Let's Encrypt account email address")
|
2015-11-14 20:00:18 -05:00
|
|
|
flag.DurationVar(&caddy.GracefulTimeout, "grace", 5*time.Second, "Maximum duration of graceful shutdown")
|
2015-10-26 14:34:31 -05:00
|
|
|
flag.StringVar(&caddy.Host, "host", caddy.DefaultHost, "Default host")
|
2016-02-25 12:26:42 -05:00
|
|
|
flag.BoolVar(&caddy.HTTP2, "http2", true, "Use HTTP/2")
|
2015-11-10 21:50:40 -05:00
|
|
|
flag.StringVar(&logfile, "log", "", "Process log file")
|
2015-11-12 14:58:01 -05:00
|
|
|
flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file")
|
2015-11-10 20:52:29 -05:00
|
|
|
flag.StringVar(&caddy.Port, "port", caddy.DefaultPort, "Default port")
|
|
|
|
flag.BoolVar(&caddy.Quiet, "quiet", false, "Quiet mode (no initialization output)")
|
2015-11-10 21:50:40 -05:00
|
|
|
flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate")
|
2015-11-10 20:52:29 -05:00
|
|
|
flag.StringVar(&caddy.Root, "root", caddy.DefaultRoot, "Root path to default site")
|
2015-05-07 14:09:40 -05:00
|
|
|
flag.BoolVar(&version, "version", false, "Show version")
|
2016-03-13 06:59:35 -05:00
|
|
|
flag.BoolVar(&directives, "directives", false, "List supported directives")
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
|
2015-01-13 14:43:45 -05:00
|
|
|
func main() {
|
2015-11-05 16:07:34 -05:00
|
|
|
flag.Parse() // called here in main() to allow other packages to set flags in their inits
|
2015-05-06 15:57:32 -05:00
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
caddy.AppName = appName
|
|
|
|
caddy.AppVersion = appVersion
|
2016-01-03 19:05:10 -05:00
|
|
|
acme.UserAgent = appName + "/" + appVersion
|
2015-10-26 14:34:31 -05:00
|
|
|
|
2015-11-05 16:07:34 -05:00
|
|
|
// set up process log before anything bad happens
|
|
|
|
switch logfile {
|
|
|
|
case "stdout":
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
case "stderr":
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
case "":
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
default:
|
2016-02-12 09:30:47 -05:00
|
|
|
log.SetOutput(&lumberjack.Logger{
|
|
|
|
Filename: logfile,
|
|
|
|
MaxSize: 100,
|
|
|
|
MaxAge: 14,
|
|
|
|
MaxBackups: 10,
|
|
|
|
})
|
2015-11-05 16:07:34 -05:00
|
|
|
}
|
|
|
|
|
2015-10-21 01:09:45 -05:00
|
|
|
if revoke != "" {
|
2016-02-11 02:06:05 -05:00
|
|
|
err := https.Revoke(revoke)
|
2015-10-21 01:09:45 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Revoked certificate for %s\n", revoke)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-11-10 20:52:29 -05:00
|
|
|
if version {
|
2016-02-26 02:21:20 -05:00
|
|
|
fmt.Printf("%s %s\n", appName, appVersion)
|
|
|
|
if devBuild && gitShortStat != "" {
|
|
|
|
fmt.Printf("%s\n%s\n", gitShortStat, gitFilesModified)
|
|
|
|
}
|
2015-11-10 20:52:29 -05:00
|
|
|
os.Exit(0)
|
2015-11-10 20:44:00 -05:00
|
|
|
}
|
2016-03-13 06:59:35 -05:00
|
|
|
if directives {
|
2016-03-13 12:29:26 -05:00
|
|
|
for _, d := range caddy.Directives() {
|
2016-03-13 06:59:35 -05:00
|
|
|
fmt.Println(d)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-05-07 14:09:40 -05:00
|
|
|
|
2015-04-24 21:08:14 -05:00
|
|
|
// Set CPU cap
|
2015-10-26 14:34:31 -05:00
|
|
|
err := setCPU(cpu)
|
2015-04-24 21:08:14 -05:00
|
|
|
if err != nil {
|
2015-11-05 16:07:34 -05:00
|
|
|
mustLogFatal(err)
|
2015-04-24 21:08:14 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
// Get Caddyfile input
|
|
|
|
caddyfile, err := caddy.LoadCaddyfile(loadCaddyfile)
|
2015-04-15 15:11:32 -05:00
|
|
|
if err != nil {
|
2015-11-05 16:07:34 -05:00
|
|
|
mustLogFatal(err)
|
2015-04-15 15:11:32 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
// Start your engines
|
|
|
|
err = caddy.Start(caddyfile)
|
|
|
|
if err != nil {
|
2015-11-13 01:54:42 -05:00
|
|
|
mustLogFatal(err)
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
// Twiddle your thumbs
|
|
|
|
caddy.Wait()
|
2015-05-20 21:06:30 -05:00
|
|
|
}
|
|
|
|
|
2015-11-05 16:07:34 -05:00
|
|
|
// mustLogFatal just wraps log.Fatal() in a way that ensures the
|
2015-11-13 01:54:42 -05:00
|
|
|
// output is always printed to stderr so the user can see it
|
|
|
|
// if the user is still there, even if the process log was not
|
|
|
|
// enabled. If this process is a restart, however, and the user
|
|
|
|
// might not be there anymore, this just logs to the process log
|
|
|
|
// and exits.
|
2015-11-05 16:07:34 -05:00
|
|
|
func mustLogFatal(args ...interface{}) {
|
2015-11-13 01:54:42 -05:00
|
|
|
if !caddy.IsRestart() {
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
}
|
2015-11-05 16:07:34 -05:00
|
|
|
log.Fatal(args...)
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
func loadCaddyfile() (caddy.Input, error) {
|
2015-11-10 17:06:47 -05:00
|
|
|
// Try -conf flag
|
|
|
|
if conf != "" {
|
|
|
|
if conf == "stdin" {
|
|
|
|
return caddy.CaddyfileFromPipe(os.Stdin)
|
2015-11-06 15:22:22 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
contents, err := ioutil.ReadFile(conf)
|
2015-05-04 07:53:54 -05:00
|
|
|
if err != nil {
|
2015-08-01 14:08:31 -05:00
|
|
|
return nil, err
|
2015-05-04 07:53:54 -05:00
|
|
|
}
|
2015-11-10 17:06:47 -05:00
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
return caddy.CaddyfileInput{
|
|
|
|
Contents: contents,
|
|
|
|
Filepath: conf,
|
2015-10-26 18:57:32 -05:00
|
|
|
RealFile: true,
|
2015-10-26 14:34:31 -05:00
|
|
|
}, nil
|
2015-05-04 07:53:54 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
// command line args
|
2015-09-08 13:14:23 -05:00
|
|
|
if flag.NArg() > 0 {
|
2015-11-07 22:03:02 -05:00
|
|
|
confBody := caddy.Host + ":" + caddy.Port + "\n" + strings.Join(flag.Args(), "\n")
|
2015-10-26 14:34:31 -05:00
|
|
|
return caddy.CaddyfileInput{
|
|
|
|
Contents: []byte(confBody),
|
|
|
|
Filepath: "args",
|
|
|
|
}, nil
|
2015-09-08 13:14:23 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
// Caddyfile in cwd
|
|
|
|
contents, err := ioutil.ReadFile(caddy.DefaultConfigFile)
|
2015-05-04 17:23:16 -05:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2015-11-03 10:10:16 -05:00
|
|
|
return caddy.DefaultInput(), nil
|
2015-05-04 17:23:16 -05:00
|
|
|
}
|
2015-08-01 14:08:31 -05:00
|
|
|
return nil, err
|
2015-05-04 17:23:16 -05:00
|
|
|
}
|
2015-10-26 14:34:31 -05:00
|
|
|
return caddy.CaddyfileInput{
|
|
|
|
Contents: contents,
|
|
|
|
Filepath: caddy.DefaultConfigFile,
|
2015-10-26 18:57:32 -05:00
|
|
|
RealFile: true,
|
2015-10-26 14:34:31 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCPU parses string cpu and sets GOMAXPROCS
|
|
|
|
// according to its value. It accepts either
|
|
|
|
// a number (e.g. 3) or a percent (e.g. 50%).
|
|
|
|
func setCPU(cpu string) error {
|
|
|
|
var numCPU int
|
|
|
|
|
|
|
|
availCPU := runtime.NumCPU()
|
|
|
|
|
|
|
|
if strings.HasSuffix(cpu, "%") {
|
|
|
|
// Percent
|
|
|
|
var percent float32
|
|
|
|
pctStr := cpu[:len(cpu)-1]
|
|
|
|
pctInt, err := strconv.Atoi(pctStr)
|
|
|
|
if err != nil || pctInt < 1 || pctInt > 100 {
|
|
|
|
return errors.New("invalid CPU value: percentage must be between 1-100")
|
|
|
|
}
|
|
|
|
percent = float32(pctInt) / 100
|
|
|
|
numCPU = int(float32(availCPU) * percent)
|
|
|
|
} else {
|
|
|
|
// Number
|
|
|
|
num, err := strconv.Atoi(cpu)
|
|
|
|
if err != nil || num < 1 {
|
|
|
|
return errors.New("invalid CPU value: provide a number or percent greater than 0")
|
|
|
|
}
|
|
|
|
numCPU = num
|
|
|
|
}
|
|
|
|
|
|
|
|
if numCPU > availCPU {
|
|
|
|
numCPU = availCPU
|
|
|
|
}
|
2015-05-06 10:16:10 -05:00
|
|
|
|
2015-10-26 14:34:31 -05:00
|
|
|
runtime.GOMAXPROCS(numCPU)
|
|
|
|
return nil
|
2015-05-04 07:53:54 -05:00
|
|
|
}
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
// setVersion figures out the version information based on
|
|
|
|
// variables set by -ldflags.
|
|
|
|
func setVersion() {
|
|
|
|
// A development build is one that's not at a tag or has uncommitted changes
|
|
|
|
devBuild = gitTag == "" || gitShortStat != ""
|
|
|
|
|
|
|
|
// Only set the appVersion if -ldflags was used
|
|
|
|
if gitNearestTag != "" || gitTag != "" {
|
|
|
|
if devBuild && gitNearestTag != "" {
|
|
|
|
appVersion = fmt.Sprintf("%s (+%s %s)",
|
|
|
|
strings.TrimPrefix(gitNearestTag, "v"), gitCommit, buildDate)
|
|
|
|
} else if gitTag != "" {
|
|
|
|
appVersion = strings.TrimPrefix(gitTag, "v")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const appName = "Caddy"
|
|
|
|
|
|
|
|
// Flags that control program flow or startup
|
|
|
|
var (
|
2016-03-13 06:59:35 -05:00
|
|
|
conf string
|
|
|
|
cpu string
|
|
|
|
logfile string
|
|
|
|
revoke string
|
|
|
|
version bool
|
|
|
|
directives bool
|
2016-02-26 02:21:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Build information obtained with the help of -ldflags
|
|
|
|
var (
|
|
|
|
appVersion = "(untracked dev build)" // inferred at startup
|
|
|
|
devBuild = true // inferred at startup
|
|
|
|
|
|
|
|
buildDate string // date -u
|
|
|
|
gitTag string // git describe --exact-match HEAD 2> /dev/null
|
|
|
|
gitNearestTag string // git describe --abbrev=0 --tags HEAD
|
|
|
|
gitCommit string // git rev-parse HEAD
|
|
|
|
gitShortStat string // git diff-index --shortstat
|
|
|
|
gitFilesModified string // git diff-index --name-only HEAD
|
|
|
|
)
|