2015-01-14 03:43:45 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-05 06:23:16 +08:00
|
|
|
"bytes"
|
2015-01-14 07:14:00 +08:00
|
|
|
"flag"
|
2015-04-16 04:11:32 +08:00
|
|
|
"fmt"
|
2015-05-05 06:23:16 +08:00
|
|
|
"io/ioutil"
|
2015-01-14 03:43:45 +08:00
|
|
|
"log"
|
2015-10-26 08:45:55 +08:00
|
|
|
"net"
|
2015-05-04 20:53:54 +08:00
|
|
|
"os"
|
2015-05-21 10:46:27 +08:00
|
|
|
"os/exec"
|
2015-05-04 20:53:54 +08:00
|
|
|
"path"
|
2015-04-25 10:08:14 +08:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-10-26 08:45:55 +08:00
|
|
|
"time"
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-05-21 14:06:53 +08:00
|
|
|
"github.com/mholt/caddy/app"
|
2015-01-14 03:43:45 +08:00
|
|
|
"github.com/mholt/caddy/config"
|
2015-10-18 10:17:24 +08:00
|
|
|
"github.com/mholt/caddy/config/letsencrypt"
|
2015-01-14 03:43:45 +08:00
|
|
|
"github.com/mholt/caddy/server"
|
|
|
|
)
|
|
|
|
|
2015-04-10 00:08:22 +08:00
|
|
|
var (
|
2015-05-08 03:09:40 +08:00
|
|
|
conf string
|
|
|
|
cpu string
|
|
|
|
version bool
|
2015-10-21 14:09:45 +08:00
|
|
|
revoke string
|
2015-04-10 00:08:22 +08:00
|
|
|
)
|
2015-01-19 14:11:21 +08:00
|
|
|
|
|
|
|
func init() {
|
2015-05-06 13:19:14 +08:00
|
|
|
flag.StringVar(&conf, "conf", "", "Configuration file to use (default="+config.DefaultConfigFile+")")
|
2015-10-10 06:35:34 +08:00
|
|
|
flag.BoolVar(&app.HTTP2, "http2", true, "Enable HTTP/2 support") // TODO: temporary flag until http2 merged into std lib
|
2015-05-21 14:06:53 +08:00
|
|
|
flag.BoolVar(&app.Quiet, "quiet", false, "Quiet mode (no initialization output)")
|
2015-04-25 10:08:14 +08:00
|
|
|
flag.StringVar(&cpu, "cpu", "100%", "CPU cap")
|
2015-05-04 20:53:54 +08:00
|
|
|
flag.StringVar(&config.Root, "root", config.DefaultRoot, "Root path to default site")
|
2015-04-30 12:30:12 +08:00
|
|
|
flag.StringVar(&config.Host, "host", config.DefaultHost, "Default host")
|
2015-04-29 12:13:00 +08:00
|
|
|
flag.StringVar(&config.Port, "port", config.DefaultPort, "Default port")
|
2015-05-08 03:09:40 +08:00
|
|
|
flag.BoolVar(&version, "version", false, "Show version")
|
2015-10-18 10:17:24 +08:00
|
|
|
flag.BoolVar(&letsencrypt.Agreed, "agree", false, "Agree to Let's Encrypt Subscriber Agreement")
|
|
|
|
flag.StringVar(&letsencrypt.DefaultEmail, "email", "", "Default email address to use for Let's Encrypt transactions")
|
2015-10-21 14:09:45 +08:00
|
|
|
flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate")
|
2015-01-19 14:11:21 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 03:43:45 +08:00
|
|
|
func main() {
|
2015-05-07 04:57:32 +08:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-05-08 03:09:40 +08:00
|
|
|
if version {
|
2015-05-21 14:06:53 +08:00
|
|
|
fmt.Printf("%s %s\n", app.Name, app.Version)
|
2015-05-08 03:09:40 +08:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-10-21 14:09:45 +08:00
|
|
|
if revoke != "" {
|
|
|
|
err := letsencrypt.Revoke(revoke)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Revoked certificate for %s\n", revoke)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-05-08 03:09:40 +08:00
|
|
|
|
2015-04-25 10:08:14 +08:00
|
|
|
// Set CPU cap
|
2015-05-21 14:06:53 +08:00
|
|
|
err := app.SetCPU(cpu)
|
2015-04-25 10:08:14 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-15 13:45:28 +08:00
|
|
|
// Load config from file
|
2015-10-26 08:45:55 +08:00
|
|
|
groupings, err := loadConfigs()
|
2015-04-16 04:11:32 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start each server with its one or more configurations
|
2015-10-26 08:45:55 +08:00
|
|
|
for i, group := range groupings {
|
|
|
|
s, err := server.New(group.BindAddr.String(), group.Configs)
|
2015-01-14 03:43:45 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-10-10 06:35:34 +08:00
|
|
|
s.HTTP2 = app.HTTP2 // TODO: This setting is temporary
|
2015-10-26 08:45:55 +08:00
|
|
|
|
2015-05-21 14:06:53 +08:00
|
|
|
app.Wg.Add(1)
|
2015-10-26 08:45:55 +08:00
|
|
|
go func(s *server.Server, i int) {
|
2015-05-21 14:06:53 +08:00
|
|
|
defer app.Wg.Done()
|
2015-10-26 08:45:55 +08:00
|
|
|
|
|
|
|
if os.Getenv("CADDY_RESTART") == "true" {
|
|
|
|
file := os.NewFile(uintptr(3+i), "")
|
|
|
|
ln, err := net.FileListener(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("FILE LISTENER:", err)
|
|
|
|
}
|
|
|
|
|
2015-10-26 09:30:29 +08:00
|
|
|
lnf, ok := ln.(server.ListenerFile)
|
2015-10-26 08:45:55 +08:00
|
|
|
if !ok {
|
|
|
|
log.Fatal("Listener was not a ListenerFile")
|
|
|
|
}
|
|
|
|
|
2015-10-26 09:30:29 +08:00
|
|
|
err = s.Serve(lnf)
|
2015-10-26 08:45:55 +08:00
|
|
|
// TODO: Better error logging... also, is it even necessary?
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := s.ListenAndServe()
|
|
|
|
// TODO: Better error logging... also, is it even necessary?
|
|
|
|
// For example, "use of closed network connection" is normal if doing graceful shutdown...
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
2015-10-26 08:45:55 +08:00
|
|
|
}(s, i)
|
2015-04-24 03:28:05 +08:00
|
|
|
|
2015-10-26 08:45:55 +08:00
|
|
|
app.ServersMutex.Lock()
|
2015-05-21 14:06:53 +08:00
|
|
|
app.Servers = append(app.Servers, s)
|
2015-10-26 08:45:55 +08:00
|
|
|
app.ServersMutex.Unlock()
|
2015-05-21 14:40:05 +08:00
|
|
|
}
|
2015-05-21 14:06:53 +08:00
|
|
|
|
2015-05-21 14:40:05 +08:00
|
|
|
// Show initialization output
|
|
|
|
if !app.Quiet {
|
|
|
|
var checkedFdLimit bool
|
2015-10-26 08:45:55 +08:00
|
|
|
for _, group := range groupings {
|
|
|
|
for _, conf := range group.Configs {
|
2015-05-21 14:40:05 +08:00
|
|
|
// Print address of site
|
|
|
|
fmt.Println(conf.Address())
|
|
|
|
|
|
|
|
// Note if non-localhost site resolves to loopback interface
|
2015-10-26 08:45:55 +08:00
|
|
|
if group.BindAddr.IP.IsLoopback() && !isLocalhost(conf.Host) {
|
2015-05-21 14:40:05 +08:00
|
|
|
fmt.Printf("Notice: %s is only accessible on this machine (%s)\n",
|
2015-10-26 08:45:55 +08:00
|
|
|
conf.Host, group.BindAddr.IP.String())
|
2015-05-21 10:06:30 +08:00
|
|
|
}
|
2015-10-26 08:45:55 +08:00
|
|
|
if !checkedFdLimit && !group.BindAddr.IP.IsLoopback() && !isLocalhost(conf.Host) {
|
2015-08-05 08:29:54 +08:00
|
|
|
checkFdlimit()
|
|
|
|
checkedFdLimit = true
|
|
|
|
}
|
2015-05-21 10:06:30 +08:00
|
|
|
}
|
2015-04-24 03:28:05 +08:00
|
|
|
}
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-26 08:45:55 +08:00
|
|
|
// TODO: Temporary; testing restart
|
|
|
|
if os.Getenv("CADDY_RESTART") != "true" {
|
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
fmt.Println("restarting")
|
|
|
|
log.Println("RESTART ERR:", app.Restart([]byte{}))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all servers to be stopped
|
2015-05-21 14:06:53 +08:00
|
|
|
app.Wg.Wait()
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
2015-04-16 04:11:32 +08:00
|
|
|
|
2015-05-23 08:34:00 +08:00
|
|
|
// checkFdlimit issues a warning if the OS max file descriptors is below a recommended minimum.
|
|
|
|
func checkFdlimit() {
|
|
|
|
const min = 4096
|
|
|
|
|
|
|
|
// Warn if ulimit is too low for production sites
|
|
|
|
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
|
|
|
|
out, err := exec.Command("sh", "-c", "ulimit -n").Output() // use sh because ulimit isn't in Linux $PATH
|
|
|
|
if err == nil {
|
|
|
|
// Note that an error here need not be reported
|
|
|
|
lim, err := strconv.Atoi(string(bytes.TrimSpace(out)))
|
|
|
|
if err == nil && lim < min {
|
2015-08-05 08:29:54 +08:00
|
|
|
fmt.Printf("Warning: File descriptor limit %d is too low for production sites. At least %d is recommended. Set with \"ulimit -n %d\".\n", lim, min, min)
|
2015-05-23 08:34:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 14:40:58 +08:00
|
|
|
// isLocalhost returns true if the string looks explicitly like a localhost address.
|
2015-05-21 10:06:30 +08:00
|
|
|
func isLocalhost(s string) bool {
|
|
|
|
return s == "localhost" || s == "::1" || strings.HasPrefix(s, "127.")
|
|
|
|
}
|
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
// loadConfigs loads configuration from a file or stdin (piped).
|
2015-08-02 03:08:31 +08:00
|
|
|
// The configurations are grouped by bind address.
|
2015-10-15 13:45:28 +08:00
|
|
|
// Configuration is obtained from one of four sources, tried
|
2015-09-09 04:38:30 +08:00
|
|
|
// in this order: 1. -conf flag, 2. stdin, 3. command line argument 4. Caddyfile.
|
2015-10-15 13:45:28 +08:00
|
|
|
// If none of those are available, a default configuration is loaded.
|
2015-08-02 03:08:31 +08:00
|
|
|
func loadConfigs() (config.Group, error) {
|
2015-05-05 06:23:16 +08:00
|
|
|
// -conf flag
|
|
|
|
if conf != "" {
|
|
|
|
file, err := os.Open(conf)
|
2015-05-04 20:53:54 +08:00
|
|
|
if err != nil {
|
2015-08-02 03:08:31 +08:00
|
|
|
return nil, err
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
2015-05-05 06:23:16 +08:00
|
|
|
defer file.Close()
|
|
|
|
return config.Load(path.Base(conf), file)
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
// stdin
|
2015-05-06 10:31:25 +08:00
|
|
|
fi, err := os.Stdin.Stat()
|
|
|
|
if err == nil && fi.Mode()&os.ModeCharDevice == 0 {
|
2015-05-06 23:16:10 +08:00
|
|
|
// Note that a non-nil error is not a problem. Windows
|
|
|
|
// will not create a stdin if there is no pipe, which
|
|
|
|
// produces an error when calling Stat(). But Unix will
|
|
|
|
// make one either way, which is why we also check that
|
|
|
|
// bitmask.
|
2015-05-06 10:31:25 +08:00
|
|
|
confBody, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(confBody) > 0 {
|
|
|
|
return config.Load("stdin", bytes.NewReader(confBody))
|
|
|
|
}
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-26 09:30:29 +08:00
|
|
|
// Command line args
|
2015-09-09 02:14:23 +08:00
|
|
|
if flag.NArg() > 0 {
|
|
|
|
confBody := ":" + config.DefaultPort + "\n" + strings.Join(flag.Args(), "\n")
|
|
|
|
return config.Load("args", bytes.NewBufferString(confBody))
|
|
|
|
}
|
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
// Caddyfile
|
|
|
|
file, err := os.Open(config.DefaultConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2015-08-02 03:08:31 +08:00
|
|
|
return config.Default()
|
2015-05-05 06:23:16 +08:00
|
|
|
}
|
2015-08-02 03:08:31 +08:00
|
|
|
return nil, err
|
2015-05-05 06:23:16 +08:00
|
|
|
}
|
|
|
|
defer file.Close()
|
2015-05-06 23:16:10 +08:00
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
return config.Load(config.DefaultConfigFile, file)
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|