pull/18/head
Hugo Landau 9 years ago
parent 572641a8d9
commit 130225c263

@ -6,11 +6,13 @@ import "gopkg.in/hlandau/madns.v1/merr"
import "github.com/hlandau/ncdns/namecoin" import "github.com/hlandau/ncdns/namecoin"
import "github.com/hlandau/ncdns/util" import "github.com/hlandau/ncdns/util"
import "github.com/hlandau/ncdns/ncdomain" import "github.com/hlandau/ncdns/ncdomain"
import "github.com/hlandau/xlog"
import "sync" import "sync"
import "fmt" import "fmt"
import "net" import "net"
import "net/mail" import "net/mail"
import "strings" import "strings"
import "time"
// Provides an abstract zone file for the Namecoin .bit TLD. // Provides an abstract zone file for the Namecoin .bit TLD.
type Backend struct { type Backend struct {
@ -23,6 +25,8 @@ type Backend struct {
const defaultMaxEntries = 100 const defaultMaxEntries = 100
var log, Log = xlog.New("ncdns.backend")
// Backend configuration. // Backend configuration.
type Config struct { type Config struct {
NamecoinConn namecoin.Conn NamecoinConn namecoin.Conn
@ -337,12 +341,22 @@ func (b *Backend) resolveName(name string) (jsonValue string, err error) {
return fv, nil return fv, nil
} }
v, err := b.nc.Query(name) // The btcjson package has quite a long timeout, far in excess of standard
if err != nil { // DNS timeouts. We need to return an error response rapidly if we can't
return "", err // query the backend. Be generous with the timeout as responses from the
} // Namecoin JSON-RPC seem sluggish sometimes.
result := make(chan struct{}, 1)
go func() {
jsonValue, err = b.nc.Query(name)
result <- struct{}{}
}()
return v, nil select {
case <-result:
return
case <-time.After(1500 * time.Millisecond):
return "", fmt.Errorf("timeout")
}
} }
func (b *Backend) jsonToDomain(name, jsonValue string) (*domain, error) { func (b *Backend) jsonToDomain(name, jsonValue string) (*domain, error) {

@ -1,7 +1,6 @@
package server package server
import "gopkg.in/hlandau/madns.v1" import "gopkg.in/hlandau/madns.v1"
import "github.com/hlandau/degoutils/log"
import "github.com/hlandau/ncdns/backend" import "github.com/hlandau/ncdns/backend"
import "github.com/hlandau/ncdns/namecoin" import "github.com/hlandau/ncdns/namecoin"
import "github.com/miekg/dns" import "github.com/miekg/dns"
@ -12,9 +11,12 @@ import "sync"
import "strings" import "strings"
import "path/filepath" import "path/filepath"
import "crypto" import "crypto"
import "github.com/hlandau/xlog"
const version = "1.0" const version = "1.0"
var log, Log = xlog.New("ncdns.server")
type Server struct { type Server struct {
cfg ServerConfig cfg ServerConfig
@ -49,6 +51,8 @@ type ServerConfig struct {
Hostmaster string `default:"" usage:"Hostmaster e. mail address"` Hostmaster string `default:"" usage:"Hostmaster e. mail address"`
VanityIPs string `default:"" usage:"Comma separated list of IP addresses to place in A/AAAA records at the zone apex (default: don't add any records)"` VanityIPs string `default:"" usage:"Comma separated list of IP addresses to place in A/AAAA records at the zone apex (default: don't add any records)"`
vanityIPs []net.IP vanityIPs []net.IP
TplSet string `default:"std" usage:"The template set to use"`
TplPath string `default:"" usage:"The path to the tpl directory (empty: autodetect)"`
ConfigDir string // path to interpret filenames relative to ConfigDir string // path to interpret filenames relative to
} }
@ -170,7 +174,6 @@ func (s *Server) loadKey(fn, privateFn string) (k *dns.DNSKEY, privatek crypto.P
} }
func (s *Server) Start() error { func (s *Server) Start() error {
s.mux = dns.NewServeMux() s.mux = dns.NewServeMux()
s.mux.Handle(".", s.engine) s.mux.Handle(".", s.engine)
@ -179,6 +182,7 @@ func (s *Server) Start() error {
s.tcpListener = s.runListener("tcp") s.tcpListener = s.runListener("tcp")
s.wgStart.Wait() s.wgStart.Wait()
log.Info("Listeners started")
return nil return nil
} }

@ -2,7 +2,6 @@ package server
import "net/http" import "net/http"
import "html/template" import "html/template"
import "github.com/hlandau/degoutils/log"
import "github.com/hlandau/ncdns/util" import "github.com/hlandau/ncdns/util"
import "github.com/hlandau/ncdns/ncdomain" import "github.com/hlandau/ncdns/ncdomain"
import "github.com/miekg/dns" import "github.com/miekg/dns"
@ -11,35 +10,28 @@ import "path/filepath"
import "time" import "time"
import "strings" import "strings"
import "fmt" import "fmt"
import "flag"
var layoutTpl *template.Template var layoutTpl *template.Template
var mainPageTpl *template.Template var mainPageTpl *template.Template
var lookupPageTpl *template.Template var lookupPageTpl *template.Template
var tplSetFlag = flag.String("tplset", "std", "Subdirectory of tpl/ to look for templates in (default: std)") func (s *Server) initTemplates() error {
func initTemplates(configDir string) error {
if lookupPageTpl != nil { if lookupPageTpl != nil {
return nil return nil
} }
if *tplSetFlag == "" {
*tplSetFlag = "std"
}
var err error var err error
layoutTpl, err = template.ParseFiles(tplFilename(configDir, "layout")) layoutTpl, err = template.ParseFiles(s.tplFilename("layout"))
if err != nil { if err != nil {
return err return err
} }
mainPageTpl, err = deriveTemplate(tplFilename(configDir, "main")) mainPageTpl, err = deriveTemplate(s.tplFilename("main"))
if err != nil { if err != nil {
return err return err
} }
lookupPageTpl, err = deriveTemplate(tplFilename(configDir, "lookup")) lookupPageTpl, err = deriveTemplate(s.tplFilename("lookup"))
if err != nil { if err != nil {
return err return err
} }
@ -55,9 +47,13 @@ func deriveTemplate(filename string) (*template.Template, error) {
return cl.ParseFiles(filename) return cl.ParseFiles(filename)
} }
func tplFilename(configDir, filename string) string { func (s *Server) tplFilename(filename string) string {
s := "tpl/" + *tplSetFlag + "/" + filename + ".tpl" td := filepath.Join(s.cfg.ConfigDir, "..", "tpl")
return filepath.Join(configDir, "..", s) if s.cfg.TplPath != "" {
td = s.cfg.TplPath
}
return filepath.Join(td, s.cfg.TplSet, filename+".tpl")
} }
type webServer struct { type webServer struct {
@ -83,6 +79,11 @@ func (ws *webServer) layoutInfo() *layoutInfo {
cshtml = `<span id="logo1">` + csparts[0] + `</span><span id="logo2">.</span><span id="logo3">` + csparts[1] + `</span>` cshtml = `<span id="logo1">` + csparts[0] + `</span><span id="logo2">.</span><span id="logo3">` + csparts[1] + `</span>`
} }
var tld string
if len(csparts) > 1 {
tld = "." + csparts[1]
}
li := &layoutInfo{ li := &layoutInfo{
SelfName: ws.s.ServerName(), SelfName: ws.s.ServerName(),
Time: time.Now().Format("2006-01-02 15:04:05"), Time: time.Now().Format("2006-01-02 15:04:05"),
@ -90,7 +91,7 @@ func (ws *webServer) layoutInfo() *layoutInfo {
CanonicalNameservers: ws.s.cfg.canonicalNameservers, CanonicalNameservers: ws.s.cfg.canonicalNameservers,
Hostmaster: ws.s.cfg.Hostmaster, Hostmaster: ws.s.cfg.Hostmaster,
CanonicalSuffixHTML: template.HTML(cshtml), CanonicalSuffixHTML: template.HTML(cshtml),
TLD: "." + csparts[1], TLD: tld,
HasDNSSEC: ws.s.cfg.ZonePublicKey != "", HasDNSSEC: ws.s.cfg.ZonePublicKey != "",
} }
@ -200,7 +201,7 @@ func clearAllCookies(rw http.ResponseWriter, req *http.Request) {
} }
func webStart(listenAddr string, server *Server) error { func webStart(listenAddr string, server *Server) error {
err := initTemplates(server.cfg.ConfigDir) err := server.initTemplates()
if err != nil { if err != nil {
return err return err
} }

Loading…
Cancel
Save