caddy.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. // Copyright 2015 Light Code Labs, LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package caddy implements the Caddy server manager.
  15. //
  16. // To use this package:
  17. //
  18. // 1. Set the AppName and AppVersion variables.
  19. // 2. Call LoadCaddyfile() to get the Caddyfile.
  20. // Pass in the name of the server type (like "http").
  21. // Make sure the server type's package is imported
  22. // (import _ "github.com/mholt/caddy/caddyhttp").
  23. // 3. Call caddy.Start() to start Caddy. You get back
  24. // an Instance, on which you can call Restart() to
  25. // restart it or Stop() to stop it.
  26. //
  27. // You should call Wait() on your instance to wait for
  28. // all servers to quit before your process exits.
  29. package caddy
  30. import (
  31. "bytes"
  32. "encoding/gob"
  33. "fmt"
  34. "io"
  35. "io/ioutil"
  36. "log"
  37. "net"
  38. "os"
  39. "strconv"
  40. "strings"
  41. "sync"
  42. "time"
  43. "github.com/mholt/caddy/caddyfile"
  44. )
  45. // Configurable application parameters
  46. var (
  47. // AppName is the name of the application.
  48. AppName string
  49. // AppVersion is the version of the application.
  50. AppVersion string
  51. // Quiet mode will not show any informative output on initialization.
  52. Quiet bool
  53. // PidFile is the path to the pidfile to create.
  54. PidFile string
  55. // GracefulTimeout is the maximum duration of a graceful shutdown.
  56. GracefulTimeout time.Duration
  57. // isUpgrade will be set to true if this process
  58. // was started as part of an upgrade, where a parent
  59. // Caddy process started this one.
  60. isUpgrade = os.Getenv("CADDY__UPGRADE") == "1"
  61. // started will be set to true when the first
  62. // instance is started; it never gets set to
  63. // false after that.
  64. started bool
  65. // mu protects the variables 'isUpgrade' and 'started'.
  66. mu sync.Mutex
  67. )
  68. func init() {
  69. OnProcessExit = append(OnProcessExit, func() {
  70. if PidFile != "" {
  71. os.Remove(PidFile)
  72. }
  73. })
  74. }
  75. // Instance contains the state of servers created as a result of
  76. // calling Start and can be used to access or control those servers.
  77. // It is literally an instance of a server type. Instance values
  78. // should NOT be copied. Use *Instance for safety.
  79. type Instance struct {
  80. // serverType is the name of the instance's server type
  81. serverType string
  82. // caddyfileInput is the input configuration text used for this process
  83. caddyfileInput Input
  84. // wg is used to wait for all servers to shut down
  85. wg *sync.WaitGroup
  86. // context is the context created for this instance,
  87. // used to coordinate the setting up of the server type
  88. context Context
  89. // servers is the list of servers with their listeners
  90. servers []ServerListener
  91. // these callbacks execute when certain events occur
  92. onFirstStartup []func() error // starting, not as part of a restart
  93. onStartup []func() error // starting, even as part of a restart
  94. onRestart []func() error // before restart commences
  95. onShutdown []func() error // stopping, even as part of a restart
  96. onFinalShutdown []func() error // stopping, not as part of a restart
  97. // storing values on an instance is preferable to
  98. // global state because these will get garbage-
  99. // collected after in-process reloads when the
  100. // old instances are destroyed; use StorageMu
  101. // to access this value safely
  102. Storage map[interface{}]interface{}
  103. StorageMu sync.RWMutex
  104. }
  105. func Instances() []*Instance {
  106. return instances
  107. }
  108. // Servers returns the ServerListeners in i.
  109. func (i *Instance) Servers() []ServerListener { return i.servers }
  110. // Stop stops all servers contained in i. It does NOT
  111. // execute shutdown callbacks.
  112. func (i *Instance) Stop() error {
  113. // stop the servers
  114. for _, s := range i.servers {
  115. if gs, ok := s.server.(GracefulServer); ok {
  116. if err := gs.Stop(); err != nil {
  117. log.Printf("[ERROR] Stopping %s: %v", gs.Address(), err)
  118. }
  119. }
  120. }
  121. // splice i out of instance list, causing it to be garbage-collected
  122. instancesMu.Lock()
  123. for j, other := range instances {
  124. if other == i {
  125. instances = append(instances[:j], instances[j+1:]...)
  126. break
  127. }
  128. }
  129. instancesMu.Unlock()
  130. return nil
  131. }
  132. // ShutdownCallbacks executes all the shutdown callbacks of i,
  133. // including ones that are scheduled only for the final shutdown
  134. // of i. An error returned from one does not stop execution of
  135. // the rest. All the non-nil errors will be returned.
  136. func (i *Instance) ShutdownCallbacks() []error {
  137. var errs []error
  138. for _, shutdownFunc := range i.onShutdown {
  139. err := shutdownFunc()
  140. if err != nil {
  141. errs = append(errs, err)
  142. }
  143. }
  144. for _, finalShutdownFunc := range i.onFinalShutdown {
  145. err := finalShutdownFunc()
  146. if err != nil {
  147. errs = append(errs, err)
  148. }
  149. }
  150. return errs
  151. }
  152. // Restart replaces the servers in i with new servers created from
  153. // executing the newCaddyfile. Upon success, it returns the new
  154. // instance to replace i. Upon failure, i will not be replaced.
  155. func (i *Instance) Restart(newCaddyfile Input) (*Instance, error) {
  156. log.Println("[INFO] Reloading")
  157. i.wg.Add(1)
  158. defer i.wg.Done()
  159. // run restart callbacks
  160. for _, fn := range i.onRestart {
  161. err := fn()
  162. if err != nil {
  163. return i, err
  164. }
  165. }
  166. if newCaddyfile == nil {
  167. newCaddyfile = i.caddyfileInput
  168. }
  169. // Add file descriptors of all the sockets that are capable of it
  170. restartFds := make(map[string]restartTriple)
  171. for _, s := range i.servers {
  172. gs, srvOk := s.server.(GracefulServer)
  173. ln, lnOk := s.listener.(Listener)
  174. pc, pcOk := s.packet.(PacketConn)
  175. if srvOk {
  176. if lnOk && pcOk {
  177. restartFds[gs.Address()] = restartTriple{server: gs, listener: ln, packet: pc}
  178. continue
  179. }
  180. if lnOk {
  181. restartFds[gs.Address()] = restartTriple{server: gs, listener: ln}
  182. continue
  183. }
  184. if pcOk {
  185. restartFds[gs.Address()] = restartTriple{server: gs, packet: pc}
  186. continue
  187. }
  188. }
  189. }
  190. // create new instance; if the restart fails, it is simply discarded
  191. newInst := &Instance{serverType: newCaddyfile.ServerType(), wg: i.wg, Storage: make(map[interface{}]interface{})}
  192. // attempt to start new instance
  193. err := startWithListenerFds(newCaddyfile, newInst, restartFds)
  194. if err != nil {
  195. return i, err
  196. }
  197. // success! stop the old instance
  198. for _, shutdownFunc := range i.onShutdown {
  199. err = shutdownFunc()
  200. if err != nil {
  201. return i, err
  202. }
  203. }
  204. err = i.Stop()
  205. if err != nil {
  206. return i, err
  207. }
  208. // Execute instantiation events
  209. EmitEvent(InstanceStartupEvent, newInst)
  210. log.Println("[INFO] Reloading complete")
  211. return newInst, nil
  212. }
  213. // SaveServer adds s and its associated listener ln to the
  214. // internally-kept list of servers that is running. For
  215. // saved servers, graceful restarts will be provided.
  216. func (i *Instance) SaveServer(s Server, ln net.Listener) {
  217. i.servers = append(i.servers, ServerListener{server: s, listener: ln})
  218. }
  219. // HasListenerWithAddress returns whether this package is
  220. // tracking a server using a listener with the address
  221. // addr.
  222. func HasListenerWithAddress(addr string) bool {
  223. instancesMu.Lock()
  224. defer instancesMu.Unlock()
  225. for _, inst := range instances {
  226. for _, sln := range inst.servers {
  227. if listenerAddrEqual(sln.listener, addr) {
  228. return true
  229. }
  230. }
  231. }
  232. return false
  233. }
  234. // listenerAddrEqual compares a listener's address with
  235. // addr. Extra care is taken to match addresses with an
  236. // empty hostname portion, as listeners tend to report
  237. // [::]:80, for example, when the matching address that
  238. // created the listener might be simply :80.
  239. func listenerAddrEqual(ln net.Listener, addr string) bool {
  240. lnAddr := ln.Addr().String()
  241. hostname, port, err := net.SplitHostPort(addr)
  242. if err != nil {
  243. return lnAddr == addr
  244. }
  245. if lnAddr == net.JoinHostPort("::", port) {
  246. return true
  247. }
  248. if lnAddr == net.JoinHostPort("0.0.0.0", port) {
  249. return true
  250. }
  251. return hostname != "" && lnAddr == addr
  252. }
  253. // TCPServer is a type that can listen and serve connections.
  254. // A TCPServer must associate with exactly zero or one net.Listeners.
  255. type TCPServer interface {
  256. // Listen starts listening by creating a new listener
  257. // and returning it. It does not start accepting
  258. // connections. For UDP-only servers, this method
  259. // can be a no-op that returns (nil, nil).
  260. Listen() (net.Listener, error)
  261. // Serve starts serving using the provided listener.
  262. // Serve must start the server loop nearly immediately,
  263. // or at least not return any errors before the server
  264. // loop begins. Serve blocks indefinitely, or in other
  265. // words, until the server is stopped. For UDP-only
  266. // servers, this method can be a no-op that returns nil.
  267. Serve(net.Listener) error
  268. }
  269. // UDPServer is a type that can listen and serve packets.
  270. // A UDPServer must associate with exactly zero or one net.PacketConns.
  271. type UDPServer interface {
  272. // ListenPacket starts listening by creating a new packetconn
  273. // and returning it. It does not start accepting connections.
  274. // TCP-only servers may leave this method blank and return
  275. // (nil, nil).
  276. ListenPacket() (net.PacketConn, error)
  277. // ServePacket starts serving using the provided packetconn.
  278. // ServePacket must start the server loop nearly immediately,
  279. // or at least not return any errors before the server
  280. // loop begins. ServePacket blocks indefinitely, or in other
  281. // words, until the server is stopped. For TCP-only servers,
  282. // this method can be a no-op that returns nil.
  283. ServePacket(net.PacketConn) error
  284. }
  285. // Server is a type that can listen and serve. It supports both
  286. // TCP and UDP, although the UDPServer interface can be used
  287. // for more than just UDP.
  288. //
  289. // If the server uses TCP, it should implement TCPServer completely.
  290. // If it uses UDP or some other protocol, it should implement
  291. // UDPServer completely. If it uses both, both interfaces should be
  292. // fully implemented. Any unimplemented methods should be made as
  293. // no-ops that simply return nil values.
  294. type Server interface {
  295. TCPServer
  296. UDPServer
  297. }
  298. // Stopper is a type that can stop serving. The stop
  299. // does not necessarily have to be graceful.
  300. type Stopper interface {
  301. // Stop stops the server. It blocks until the
  302. // server is completely stopped.
  303. Stop() error
  304. }
  305. // GracefulServer is a Server and Stopper, the stopping
  306. // of which is graceful (whatever that means for the kind
  307. // of server being implemented). It must be able to return
  308. // the address it is configured to listen on so that its
  309. // listener can be paired with it upon graceful restarts.
  310. // The net.Listener that a GracefulServer creates must
  311. // implement the Listener interface for restarts to be
  312. // graceful (assuming the listener is for TCP).
  313. type GracefulServer interface {
  314. Server
  315. Stopper
  316. // Address returns the address the server should
  317. // listen on; it is used to pair the server to
  318. // its listener during a graceful/zero-downtime
  319. // restart. Thus when implementing this method,
  320. // you must not access a listener to get the
  321. // address; you must store the address the
  322. // server is to serve on some other way.
  323. Address() string
  324. }
  325. // Listener is a net.Listener with an underlying file descriptor.
  326. // A server's listener should implement this interface if it is
  327. // to support zero-downtime reloads.
  328. type Listener interface {
  329. net.Listener
  330. File() (*os.File, error)
  331. }
  332. // PacketConn is a net.PacketConn with an underlying file descriptor.
  333. // A server's packetconn should implement this interface if it is
  334. // to support zero-downtime reloads (in sofar this holds true for datagram
  335. // connections).
  336. type PacketConn interface {
  337. net.PacketConn
  338. File() (*os.File, error)
  339. }
  340. // AfterStartup is an interface that can be implemented
  341. // by a server type that wants to run some code after all
  342. // servers for the same Instance have started.
  343. type AfterStartup interface {
  344. OnStartupComplete()
  345. }
  346. // LoadCaddyfile loads a Caddyfile by calling the plugged in
  347. // Caddyfile loader methods. An error is returned if more than
  348. // one loader returns a non-nil Caddyfile input. If no loaders
  349. // load a Caddyfile, the default loader is used. If no default
  350. // loader is registered or it returns nil, the server type's
  351. // default Caddyfile is loaded. If the server type does not
  352. // specify any default Caddyfile value, then an empty Caddyfile
  353. // is returned. Consequently, this function never returns a nil
  354. // value as long as there are no errors.
  355. func LoadCaddyfile(serverType string) (Input, error) {
  356. // If we are finishing an upgrade, we must obtain the Caddyfile
  357. // from our parent process, regardless of configured loaders.
  358. if IsUpgrade() {
  359. err := gob.NewDecoder(os.Stdin).Decode(&loadedGob)
  360. if err != nil {
  361. return nil, err
  362. }
  363. return loadedGob.Caddyfile, nil
  364. }
  365. // Ask plugged-in loaders for a Caddyfile
  366. cdyfile, err := loadCaddyfileInput(serverType)
  367. if err != nil {
  368. return nil, err
  369. }
  370. // Otherwise revert to default
  371. if cdyfile == nil {
  372. cdyfile = DefaultInput(serverType)
  373. }
  374. // Still nil? Geez.
  375. if cdyfile == nil {
  376. cdyfile = CaddyfileInput{ServerTypeName: serverType}
  377. }
  378. return cdyfile, nil
  379. }
  380. // Wait blocks until all of i's servers have stopped.
  381. func (i *Instance) Wait() {
  382. i.wg.Wait()
  383. }
  384. // CaddyfileFromPipe loads the Caddyfile input from f if f is
  385. // not interactive input. f is assumed to be a pipe or stream,
  386. // such as os.Stdin. If f is not a pipe, no error is returned
  387. // but the Input value will be nil. An error is only returned
  388. // if there was an error reading the pipe, even if the length
  389. // of what was read is 0.
  390. func CaddyfileFromPipe(f *os.File, serverType string) (Input, error) {
  391. fi, err := f.Stat()
  392. if err == nil && fi.Mode()&os.ModeCharDevice == 0 {
  393. // Note that a non-nil error is not a problem. Windows
  394. // will not create a stdin if there is no pipe, which
  395. // produces an error when calling Stat(). But Unix will
  396. // make one either way, which is why we also check that
  397. // bitmask.
  398. // NOTE: Reading from stdin after this fails (e.g. for the let's encrypt email address) (OS X)
  399. confBody, err := ioutil.ReadAll(f)
  400. if err != nil {
  401. return nil, err
  402. }
  403. return CaddyfileInput{
  404. Contents: confBody,
  405. Filepath: f.Name(),
  406. ServerTypeName: serverType,
  407. }, nil
  408. }
  409. // not having input from the pipe is not itself an error,
  410. // just means no input to return.
  411. return nil, nil
  412. }
  413. // Caddyfile returns the Caddyfile used to create i.
  414. func (i *Instance) Caddyfile() Input {
  415. return i.caddyfileInput
  416. }
  417. // Start starts Caddy with the given Caddyfile.
  418. //
  419. // This function blocks until all the servers are listening.
  420. func Start(cdyfile Input) (*Instance, error) {
  421. inst := &Instance{serverType: cdyfile.ServerType(), wg: new(sync.WaitGroup), Storage: make(map[interface{}]interface{})}
  422. err := startWithListenerFds(cdyfile, inst, nil)
  423. if err != nil {
  424. return inst, err
  425. }
  426. signalSuccessToParent()
  427. if pidErr := writePidFile(); pidErr != nil {
  428. log.Printf("[ERROR] Could not write pidfile: %v", pidErr)
  429. }
  430. return inst, nil
  431. }
  432. func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]restartTriple) error {
  433. // save this instance in the list now so that
  434. // plugins can access it if need be, for example
  435. // the caddytls package, so it can perform cert
  436. // renewals while starting up; we just have to
  437. // remove the instance from the list later if
  438. // it fails
  439. instancesMu.Lock()
  440. instances = append(instances, inst)
  441. instancesMu.Unlock()
  442. var err error
  443. defer func() {
  444. if err != nil {
  445. instancesMu.Lock()
  446. for i, otherInst := range instances {
  447. if otherInst == inst {
  448. instances = append(instances[:i], instances[i+1:]...)
  449. break
  450. }
  451. }
  452. instancesMu.Unlock()
  453. }
  454. }()
  455. if cdyfile == nil {
  456. cdyfile = CaddyfileInput{}
  457. }
  458. err = ValidateAndExecuteDirectives(cdyfile, inst, false)
  459. if err != nil {
  460. return err
  461. }
  462. slist, err := inst.context.MakeServers()
  463. if err != nil {
  464. return err
  465. }
  466. // run startup callbacks
  467. if !IsUpgrade() && restartFds == nil {
  468. // first startup means not a restart or upgrade
  469. for _, firstStartupFunc := range inst.onFirstStartup {
  470. err = firstStartupFunc()
  471. if err != nil {
  472. return err
  473. }
  474. }
  475. }
  476. for _, startupFunc := range inst.onStartup {
  477. err = startupFunc()
  478. if err != nil {
  479. return err
  480. }
  481. }
  482. err = startServers(slist, inst, restartFds)
  483. if err != nil {
  484. return err
  485. }
  486. // run any AfterStartup callbacks if this is not
  487. // part of a restart; then show file descriptor notice
  488. if restartFds == nil {
  489. for _, srvln := range inst.servers {
  490. if srv, ok := srvln.server.(AfterStartup); ok {
  491. srv.OnStartupComplete()
  492. }
  493. }
  494. if !Quiet {
  495. for _, srvln := range inst.servers {
  496. // only show FD notice if the listener is not nil.
  497. // This can happen when only serving UDP or TCP
  498. if srvln.listener == nil {
  499. continue
  500. }
  501. if !IsLoopback(srvln.listener.Addr().String()) {
  502. checkFdlimit()
  503. break
  504. }
  505. }
  506. }
  507. }
  508. mu.Lock()
  509. started = true
  510. mu.Unlock()
  511. return nil
  512. }
  513. // ValidateAndExecuteDirectives will load the server blocks from cdyfile
  514. // by parsing it, then execute the directives configured by it and store
  515. // the resulting server blocks into inst. If justValidate is true, parse
  516. // callbacks will not be executed between directives, since the purpose
  517. // is only to check the input for valid syntax.
  518. func ValidateAndExecuteDirectives(cdyfile Input, inst *Instance, justValidate bool) error {
  519. // If parsing only inst will be nil, create an instance for this function call only.
  520. if justValidate {
  521. inst = &Instance{serverType: cdyfile.ServerType(), wg: new(sync.WaitGroup), Storage: make(map[interface{}]interface{})}
  522. }
  523. stypeName := cdyfile.ServerType()
  524. stype, err := getServerType(stypeName)
  525. if err != nil {
  526. return err
  527. }
  528. inst.caddyfileInput = cdyfile
  529. sblocks, err := loadServerBlocks(stypeName, cdyfile.Path(), bytes.NewReader(cdyfile.Body()))
  530. if err != nil {
  531. return err
  532. }
  533. inst.context = stype.NewContext(inst)
  534. if inst.context == nil {
  535. return fmt.Errorf("server type %s produced a nil Context", stypeName)
  536. }
  537. sblocks, err = inst.context.InspectServerBlocks(cdyfile.Path(), sblocks)
  538. if err != nil {
  539. return fmt.Errorf("error inspecting server blocks: %v", err)
  540. }
  541. return executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks, justValidate)
  542. }
  543. func executeDirectives(inst *Instance, filename string,
  544. directives []string, sblocks []caddyfile.ServerBlock, justValidate bool) error {
  545. // map of server block ID to map of directive name to whatever.
  546. storages := make(map[int]map[string]interface{})
  547. // It is crucial that directives are executed in the proper order.
  548. // We loop with the directives on the outer loop so we execute
  549. // a directive for all server blocks before going to the next directive.
  550. // This is important mainly due to the parsing callbacks (below).
  551. for _, dir := range directives {
  552. for i, sb := range sblocks {
  553. var once sync.Once
  554. if _, ok := storages[i]; !ok {
  555. storages[i] = make(map[string]interface{})
  556. }
  557. for j, key := range sb.Keys {
  558. // Execute directive if it is in the server block
  559. if tokens, ok := sb.Tokens[dir]; ok {
  560. controller := &Controller{
  561. instance: inst,
  562. Key: key,
  563. Dispenser: caddyfile.NewDispenserTokens(filename, tokens),
  564. OncePerServerBlock: func(f func() error) error {
  565. var err error
  566. once.Do(func() {
  567. err = f()
  568. })
  569. return err
  570. },
  571. ServerBlockIndex: i,
  572. ServerBlockKeyIndex: j,
  573. ServerBlockKeys: sb.Keys,
  574. ServerBlockStorage: storages[i][dir],
  575. }
  576. setup, err := DirectiveAction(inst.serverType, dir)
  577. if err != nil {
  578. return err
  579. }
  580. err = setup(controller)
  581. if err != nil {
  582. return err
  583. }
  584. storages[i][dir] = controller.ServerBlockStorage // persist for this server block
  585. }
  586. }
  587. }
  588. if !justValidate {
  589. // See if there are any callbacks to execute after this directive
  590. if allCallbacks, ok := parsingCallbacks[inst.serverType]; ok {
  591. callbacks := allCallbacks[dir]
  592. for _, callback := range callbacks {
  593. if err := callback(inst.context); err != nil {
  594. return err
  595. }
  596. }
  597. }
  598. }
  599. }
  600. return nil
  601. }
  602. func startServers(serverList []Server, inst *Instance, restartFds map[string]restartTriple) error {
  603. errChan := make(chan error, len(serverList))
  604. for _, s := range serverList {
  605. var (
  606. ln net.Listener
  607. pc net.PacketConn
  608. err error
  609. )
  610. // if performing an upgrade, obtain listener file descriptors
  611. // from parent process
  612. if IsUpgrade() {
  613. if gs, ok := s.(GracefulServer); ok {
  614. addr := gs.Address()
  615. if fdIndex, ok := loadedGob.ListenerFds["tcp"+addr]; ok {
  616. file := os.NewFile(fdIndex, "")
  617. ln, err = net.FileListener(file)
  618. if err != nil {
  619. return err
  620. }
  621. err = file.Close()
  622. if err != nil {
  623. return err
  624. }
  625. }
  626. if fdIndex, ok := loadedGob.ListenerFds["udp"+addr]; ok {
  627. file := os.NewFile(fdIndex, "")
  628. pc, err = net.FilePacketConn(file)
  629. if err != nil {
  630. return err
  631. }
  632. err = file.Close()
  633. if err != nil {
  634. return err
  635. }
  636. }
  637. }
  638. }
  639. // If this is a reload and s is a GracefulServer,
  640. // reuse the listener for a graceful restart.
  641. if gs, ok := s.(GracefulServer); ok && restartFds != nil {
  642. addr := gs.Address()
  643. if old, ok := restartFds[addr]; ok {
  644. // listener
  645. if old.listener != nil {
  646. file, err := old.listener.File()
  647. if err != nil {
  648. return err
  649. }
  650. ln, err = net.FileListener(file)
  651. if err != nil {
  652. return err
  653. }
  654. err = file.Close()
  655. if err != nil {
  656. return err
  657. }
  658. }
  659. // packetconn
  660. if old.packet != nil {
  661. file, err := old.packet.File()
  662. if err != nil {
  663. return err
  664. }
  665. pc, err = net.FilePacketConn(file)
  666. if err != nil {
  667. return err
  668. }
  669. err = file.Close()
  670. if err != nil {
  671. return err
  672. }
  673. }
  674. }
  675. }
  676. if ln == nil {
  677. ln, err = s.Listen()
  678. if err != nil {
  679. return err
  680. }
  681. }
  682. if pc == nil {
  683. pc, err = s.ListenPacket()
  684. if err != nil {
  685. return err
  686. }
  687. }
  688. inst.wg.Add(2)
  689. go func(s Server, ln net.Listener, pc net.PacketConn, inst *Instance) {
  690. defer inst.wg.Done()
  691. go func() {
  692. errChan <- s.Serve(ln)
  693. defer inst.wg.Done()
  694. }()
  695. errChan <- s.ServePacket(pc)
  696. }(s, ln, pc, inst)
  697. inst.servers = append(inst.servers, ServerListener{server: s, listener: ln, packet: pc})
  698. }
  699. // Log errors that may be returned from Serve() calls,
  700. // these errors should only be occurring in the server loop.
  701. go func() {
  702. for err := range errChan {
  703. if err == nil {
  704. continue
  705. }
  706. if strings.Contains(err.Error(), "use of closed network connection") {
  707. // this error is normal when closing the listener
  708. continue
  709. }
  710. log.Println(err)
  711. }
  712. }()
  713. return nil
  714. }
  715. func getServerType(serverType string) (ServerType, error) {
  716. stype, ok := serverTypes[serverType]
  717. if ok {
  718. return stype, nil
  719. }
  720. if len(serverTypes) == 0 {
  721. return ServerType{}, fmt.Errorf("no server types plugged in")
  722. }
  723. if serverType == "" {
  724. if len(serverTypes) == 1 {
  725. for _, stype := range serverTypes {
  726. return stype, nil
  727. }
  728. }
  729. return ServerType{}, fmt.Errorf("multiple server types available; must choose one")
  730. }
  731. return ServerType{}, fmt.Errorf("unknown server type '%s'", serverType)
  732. }
  733. func loadServerBlocks(serverType, filename string, input io.Reader) ([]caddyfile.ServerBlock, error) {
  734. validDirectives := ValidDirectives(serverType)
  735. serverBlocks, err := caddyfile.Parse(filename, input, validDirectives)
  736. if err != nil {
  737. return nil, err
  738. }
  739. if len(serverBlocks) == 0 && serverTypes[serverType].DefaultInput != nil {
  740. newInput := serverTypes[serverType].DefaultInput()
  741. serverBlocks, err = caddyfile.Parse(newInput.Path(),
  742. bytes.NewReader(newInput.Body()), validDirectives)
  743. if err != nil {
  744. return nil, err
  745. }
  746. }
  747. return serverBlocks, nil
  748. }
  749. // Stop stops ALL servers. It blocks until they are all stopped.
  750. // It does NOT execute shutdown callbacks, and it deletes all
  751. // instances after stopping is completed. Do not re-use any
  752. // references to old instances after calling Stop.
  753. func Stop() error {
  754. // This awkward for loop is to avoid a deadlock since
  755. // inst.Stop() also acquires the instancesMu lock.
  756. for {
  757. instancesMu.Lock()
  758. if len(instances) == 0 {
  759. break
  760. }
  761. inst := instances[0]
  762. instancesMu.Unlock()
  763. if err := inst.Stop(); err != nil {
  764. log.Printf("[ERROR] Stopping %s: %v", inst.serverType, err)
  765. }
  766. }
  767. return nil
  768. }
  769. // IsLoopback returns true if the hostname of addr looks
  770. // explicitly like a common local hostname. addr must only
  771. // be a host or a host:port combination.
  772. func IsLoopback(addr string) bool {
  773. host, _, err := net.SplitHostPort(addr)
  774. if err != nil {
  775. host = addr // happens if the addr is just a hostname
  776. }
  777. return host == "localhost" ||
  778. strings.Trim(host, "[]") == "::1" ||
  779. strings.HasPrefix(host, "127.")
  780. }
  781. // IsInternal returns true if the IP of addr
  782. // belongs to a private network IP range. addr must only
  783. // be an IP or an IP:port combination.
  784. // Loopback addresses are considered false.
  785. func IsInternal(addr string) bool {
  786. privateNetworks := []string{
  787. "10.0.0.0/8",
  788. "172.16.0.0/12",
  789. "192.168.0.0/16",
  790. "fc00::/7",
  791. }
  792. host, _, err := net.SplitHostPort(addr)
  793. if err != nil {
  794. host = addr // happens if the addr is just a hostname, missing port
  795. // if we encounter an error, the brackets need to be stripped
  796. // because SplitHostPort didn't do it for us
  797. host = strings.Trim(host, "[]")
  798. }
  799. ip := net.ParseIP(host)
  800. if ip == nil {
  801. return false
  802. }
  803. for _, privateNetwork := range privateNetworks {
  804. _, ipnet, _ := net.ParseCIDR(privateNetwork)
  805. if ipnet.Contains(ip) {
  806. return true
  807. }
  808. }
  809. return false
  810. }
  811. // Started returns true if at least one instance has been
  812. // started by this package. It never gets reset to false
  813. // once it is set to true.
  814. func Started() bool {
  815. mu.Lock()
  816. defer mu.Unlock()
  817. return started
  818. }
  819. // CaddyfileInput represents a Caddyfile as input
  820. // and is simply a convenient way to implement
  821. // the Input interface.
  822. type CaddyfileInput struct {
  823. Filepath string
  824. Contents []byte
  825. ServerTypeName string
  826. }
  827. // Body returns c.Contents.
  828. func (c CaddyfileInput) Body() []byte { return c.Contents }
  829. // Path returns c.Filepath.
  830. func (c CaddyfileInput) Path() string { return c.Filepath }
  831. // ServerType returns c.ServerType.
  832. func (c CaddyfileInput) ServerType() string { return c.ServerTypeName }
  833. // Input represents a Caddyfile; its contents and file path
  834. // (which should include the file name at the end of the path).
  835. // If path does not apply (e.g. piped input) you may use
  836. // any understandable value. The path is mainly used for logging,
  837. // error messages, and debugging.
  838. type Input interface {
  839. // Gets the Caddyfile contents
  840. Body() []byte
  841. // Gets the path to the origin file
  842. Path() string
  843. // The type of server this input is intended for
  844. ServerType() string
  845. }
  846. // DefaultInput returns the default Caddyfile input
  847. // to use when it is otherwise empty or missing.
  848. // It uses the default host and port (depends on
  849. // host, e.g. localhost is 2015, otherwise 443) and
  850. // root.
  851. func DefaultInput(serverType string) Input {
  852. if _, ok := serverTypes[serverType]; !ok {
  853. return nil
  854. }
  855. if serverTypes[serverType].DefaultInput == nil {
  856. return nil
  857. }
  858. return serverTypes[serverType].DefaultInput()
  859. }
  860. // writePidFile writes the process ID to the file at PidFile.
  861. // It does nothing if PidFile is not set.
  862. func writePidFile() error {
  863. if PidFile == "" {
  864. return nil
  865. }
  866. pid := []byte(strconv.Itoa(os.Getpid()) + "\n")
  867. return ioutil.WriteFile(PidFile, pid, 0644)
  868. }
  869. type restartTriple struct {
  870. server GracefulServer
  871. listener Listener
  872. packet PacketConn
  873. }
  874. var (
  875. // instances is the list of running Instances.
  876. instances []*Instance
  877. // instancesMu protects instances.
  878. instancesMu sync.Mutex
  879. )
  880. var (
  881. // DefaultConfigFile is the name of the configuration file that is loaded
  882. // by default if no other file is specified.
  883. DefaultConfigFile = "Caddyfile"
  884. )
  885. // CtxKey is a value type for use with context.WithValue.
  886. type CtxKey string