caddy.go 29 KB

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