2017-12-04 08:28:27 -08:00
|
|
|
package nsid
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2020-09-24 18:14:41 +02:00
|
|
|
"github.com/coredns/caddy"
|
2017-12-04 08:28:27 -08:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
|
)
|
|
|
|
|
|
2019-09-20 08:02:30 +01:00
|
|
|
func init() { plugin.Register("nsid", setup) }
|
2017-12-04 08:28:27 -08:00
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
|
|
|
nsid, err := nsidParse(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return plugin.Error("nsid", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
|
|
|
|
return Nsid{Next: next, Data: nsid}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nsidParse(c *caddy.Controller) (string, error) {
|
|
|
|
|
// Use hostname as the default
|
|
|
|
|
nsid, err := os.Hostname()
|
|
|
|
|
if err != nil {
|
|
|
|
|
nsid = "localhost"
|
|
|
|
|
}
|
2018-02-28 18:16:05 -08:00
|
|
|
i := 0
|
2017-12-04 08:28:27 -08:00
|
|
|
for c.Next() {
|
2018-02-28 18:16:05 -08:00
|
|
|
if i > 0 {
|
|
|
|
|
return nsid, plugin.ErrOnce
|
|
|
|
|
}
|
|
|
|
|
i++
|
2017-12-04 08:28:27 -08:00
|
|
|
args := c.RemainingArgs()
|
2018-02-28 18:16:05 -08:00
|
|
|
if len(args) > 0 {
|
|
|
|
|
nsid = strings.Join(args, " ")
|
2017-12-04 08:28:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nsid, nil
|
|
|
|
|
}
|