Compare commits
2 Commits
2c34850415
...
aac894ae6f
| Author | SHA1 | Date | |
|---|---|---|---|
|
aac894ae6f
|
|||
|
f585fb393b
|
@@ -24,6 +24,8 @@ var command = &cobra.Command{
|
||||
viper.SetEnvPrefix("XIP")
|
||||
viper.AutomaticEnv()
|
||||
|
||||
utils.InitLogger(viper.GetString("log-file"))
|
||||
|
||||
email := viper.GetString("Email")
|
||||
_, err := mail.ParseAddress(email)
|
||||
if err != nil {
|
||||
@@ -84,6 +86,9 @@ var command = &cobra.Command{
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
command.Flags().String("log-file", utils.DefaultLogFile, "Path to log file")
|
||||
viper.BindPFlag("log-file", command.Flags().Lookup("log-file"))
|
||||
|
||||
command.Flags().Uint("dns-port", 53, "Port for the DNS server")
|
||||
viper.BindPFlag("dns-port", command.Flags().Lookup("dns-port"))
|
||||
|
||||
|
||||
@@ -8,14 +8,25 @@ import (
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var consoleWriter = zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
|
||||
var fileWriter = &lumberjack.Logger{
|
||||
Filename: "/var/log/local-ip.sh.log",
|
||||
MaxBackups: 3,
|
||||
MaxSize: 1, // megabytes
|
||||
MaxAge: 1, // days
|
||||
Compress: true, // disabled by default
|
||||
}
|
||||
var multi = zerolog.MultiLevelWriter(consoleWriter, fileWriter)
|
||||
const DefaultLogFile = "/var/log/local-ip.sh.log"
|
||||
|
||||
var Logger = zerolog.New(multi).With().Timestamp().Logger()
|
||||
var consoleWriter = zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
|
||||
|
||||
var Logger = zerolog.New(consoleWriter).With().Timestamp().Logger()
|
||||
|
||||
func InitLogger(logFile string) {
|
||||
if logFile == "" {
|
||||
logFile = DefaultLogFile
|
||||
}
|
||||
|
||||
fileWriter := &lumberjack.Logger{
|
||||
Filename: logFile,
|
||||
MaxBackups: 3,
|
||||
MaxSize: 1,
|
||||
MaxAge: 1,
|
||||
Compress: true,
|
||||
}
|
||||
|
||||
multi := zerolog.MultiLevelWriter(consoleWriter, fileWriter)
|
||||
Logger = zerolog.New(multi).With().Timestamp().Logger()
|
||||
}
|
||||
|
||||
69
xip/xip.go
69
xip/xip.go
@@ -15,6 +15,44 @@ import (
|
||||
type Xip struct {
|
||||
server dns.Server
|
||||
nameServers []string
|
||||
domain string
|
||||
email string
|
||||
dnsPort uint
|
||||
}
|
||||
|
||||
type Option func(*Xip)
|
||||
|
||||
func WithDomain(domain string) Option {
|
||||
return func(x *Xip) {
|
||||
x.domain = domain
|
||||
}
|
||||
}
|
||||
|
||||
func WithEmail(email string) Option {
|
||||
return func(x *Xip) {
|
||||
x.email = email
|
||||
}
|
||||
}
|
||||
|
||||
func WithDnsPort(port uint) Option {
|
||||
return func(x *Xip) {
|
||||
x.dnsPort = port
|
||||
}
|
||||
}
|
||||
|
||||
func WithNameServers(nameServers []string) Option {
|
||||
return func(x *Xip) {
|
||||
for i, ns := range nameServers {
|
||||
name := fmt.Sprintf("ns%d.%s.", i+1, x.domain)
|
||||
ip := net.ParseIP(ns)
|
||||
|
||||
entry := hardcodedRecords[name]
|
||||
entry.A = append(entry.A, ip)
|
||||
hardcodedRecords[name] = entry
|
||||
|
||||
x.nameServers = append(x.nameServers, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -26,29 +64,27 @@ var (
|
||||
|
||||
func (xip *Xip) SetTXTRecord(fqdn string, value string) {
|
||||
utils.Logger.Trace().Str("fqdn", fqdn).Str("value", value).Msg("Trying to set TXT record")
|
||||
config := utils.GetConfig()
|
||||
if fqdn != fmt.Sprintf("_acme-challenge.%s.", config.Domain) {
|
||||
if fqdn != fmt.Sprintf("_acme-challenge.%s.", xip.domain) {
|
||||
utils.Logger.Trace().Str("fqdn", fqdn).Msg("Not allowed, abort setting TXT record")
|
||||
return
|
||||
}
|
||||
|
||||
if rootRecords, ok := hardcodedRecords[fqdn]; ok {
|
||||
rootRecords.TXT = []string{value}
|
||||
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", config.Domain)] = rootRecords
|
||||
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", xip.domain)] = rootRecords
|
||||
}
|
||||
}
|
||||
|
||||
func (xip *Xip) UnsetTXTRecord(fqdn string) {
|
||||
utils.Logger.Trace().Str("fqdn", fqdn).Msg("Trying to unset TXT record")
|
||||
config := utils.GetConfig()
|
||||
if fqdn != fmt.Sprintf("_acme-challenge.%s.", config.Domain) {
|
||||
if fqdn != fmt.Sprintf("_acme-challenge.%s.", xip.domain) {
|
||||
utils.Logger.Trace().Str("fqdn", fqdn).Msg("Not allowed, abort unsetting TXT record")
|
||||
return
|
||||
}
|
||||
|
||||
if rootRecords, ok := hardcodedRecords[fqdn]; ok {
|
||||
rootRecords.TXT = []string{}
|
||||
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", config.Domain)] = rootRecords
|
||||
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", xip.domain)] = rootRecords
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,7 +304,6 @@ func emailToRname(email string) string {
|
||||
}
|
||||
|
||||
func (xip *Xip) soaRecord(question dns.Question) *dns.SOA {
|
||||
config := utils.GetConfig()
|
||||
soa := new(dns.SOA)
|
||||
soa.Hdr = dns.RR_Header{
|
||||
Name: question.Name,
|
||||
@@ -278,7 +313,7 @@ func (xip *Xip) soaRecord(question dns.Question) *dns.SOA {
|
||||
Rdlength: 0,
|
||||
}
|
||||
soa.Ns = xip.nameServers[0]
|
||||
soa.Mbox = emailToRname(config.Email)
|
||||
soa.Mbox = emailToRname(xip.email)
|
||||
soa.Serial = 2024072600
|
||||
soa.Refresh = uint32((time.Minute * 15).Seconds())
|
||||
soa.Retry = uint32((time.Minute * 15).Seconds())
|
||||
@@ -403,18 +438,28 @@ func (xip *Xip) initHardcodedRecords() {
|
||||
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", config.Domain)] = hardcodedRecord{TXT: []string{}}
|
||||
}
|
||||
|
||||
func NewXip() (xip *Xip) {
|
||||
func NewXip(opts ...Option) (xip *Xip) {
|
||||
config := utils.GetConfig()
|
||||
xip = &Xip{}
|
||||
xip = &Xip{
|
||||
domain: config.Domain,
|
||||
email: config.Email,
|
||||
dnsPort: config.DnsPort,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(xip)
|
||||
}
|
||||
|
||||
if len(xip.nameServers) == 0 {
|
||||
xip.initHardcodedRecords()
|
||||
}
|
||||
|
||||
xip.server = dns.Server{
|
||||
Addr: fmt.Sprintf(":%d", config.DnsPort),
|
||||
Addr: fmt.Sprintf(":%d", xip.dnsPort),
|
||||
Net: "udp",
|
||||
}
|
||||
|
||||
zone := fmt.Sprintf("%s.", config.Domain)
|
||||
zone := fmt.Sprintf("%s.", xip.domain)
|
||||
dns.HandleFunc(zone, xip.handleDnsRequest)
|
||||
|
||||
return xip
|
||||
|
||||
@@ -5,13 +5,14 @@ import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func TestResolveDashUnit(t *testing.T) {
|
||||
// viper.Set("dns-port", 9053)
|
||||
xip := NewXip()
|
||||
xip := NewXip(
|
||||
WithDomain("local-ip.sh"),
|
||||
WithDnsPort(9053),
|
||||
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
|
||||
)
|
||||
|
||||
A := xip.fqdnToA("192-168-1-29.local-ip.sh")
|
||||
expected := "192.168.1.29"
|
||||
@@ -41,20 +42,26 @@ func TestResolveDashUnit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConstructor(t *testing.T) {
|
||||
viper.Set("dns-port", 9053)
|
||||
xip := NewXip()
|
||||
xip := NewXip(
|
||||
WithDomain("local-ip.sh"),
|
||||
WithDnsPort(9053),
|
||||
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
|
||||
)
|
||||
|
||||
if xip.nameServers[0] != "ns1.local-ip.sh" {
|
||||
t.Error("")
|
||||
if xip.nameServers[0] != "ns1.local-ip.sh." {
|
||||
t.Errorf("expected ns1.local-ip.sh., got %s", xip.nameServers[0])
|
||||
}
|
||||
if xip.nameServers[1] != "ns2.local-ip.sh" {
|
||||
t.Error("")
|
||||
if xip.nameServers[1] != "ns2.local-ip.sh." {
|
||||
t.Errorf("expected ns2.local-ip.sh., got %s", xip.nameServers[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDashE2E(t *testing.T) {
|
||||
viper.Set("dns-port", 9053)
|
||||
xip := NewXip()
|
||||
xip := NewXip(
|
||||
WithDomain("local-ip.sh"),
|
||||
WithDnsPort(9053),
|
||||
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
|
||||
)
|
||||
go xip.StartServer()
|
||||
|
||||
cmd := exec.Command("dig", "@localhost", "-p", "9053", "192-168-1-29.local-ip.sh", "+short")
|
||||
@@ -70,25 +77,17 @@ func TestResolveDashE2E(t *testing.T) {
|
||||
|
||||
func BenchmarkResolveDashBasic(b *testing.B) {
|
||||
b.Skip()
|
||||
// var semaphore = make(chan int, 40)
|
||||
// var done = make(chan bool, 1)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
port := 9053 + i
|
||||
viper.Set("dns-port", port)
|
||||
xip := NewXip()
|
||||
xip := NewXip(
|
||||
WithDomain("local-ip.sh"),
|
||||
WithDnsPort(uint(port)),
|
||||
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
|
||||
)
|
||||
go xip.StartServer()
|
||||
|
||||
// semaphore <- 1
|
||||
// go func() {
|
||||
cmd := exec.Command("dig", "@localhost", "-p", fmt.Sprint(port), "192-168-1-29.local-ip.sh", "+short")
|
||||
cmd.Run()
|
||||
|
||||
// <-semaphore
|
||||
// if i == b.N {
|
||||
// done <- true
|
||||
// }
|
||||
// }()
|
||||
}
|
||||
// <-done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user