11package proxy
22
33import (
4- . "github.com/mageddo/dns-proxy-server/log"
54 "errors"
6- "github.com/miekg /dns"
7- "net "
5+ "github.com/mageddo /dns-proxy-server/cache "
6+ "github.com/mageddo/dns-proxy-server/cache/timed "
87 "github.com/mageddo/dns-proxy-server/events/local"
8+ . "github.com/mageddo/dns-proxy-server/log"
9+ "github.com/miekg/dns"
910 "golang.org/x/net/context"
10- "github.com/mageddo/dns-proxy-server/cache"
11+ "net"
12+ "strings"
1113 "time"
12- "github.com/mageddo/dns-proxy-server/cache/timed"
1314)
1415
1516type localDnsSolver struct {
@@ -19,40 +20,13 @@ type localDnsSolver struct {
1920func (s localDnsSolver ) Solve (ctx context.Context , question dns.Question ) (* dns.Msg , error ) {
2021
2122 key := question .Name [:len (question .Name )- 1 ]
22- var hostname * local.HostnameVo
23- if value , found := s .ContainsKey (key ); found {
24- LOGGER .Debugf ("solver=local, status=from-cache, hostname=%s, value=%v" , key , value )
25- if value != nil {
26- hostname = value .(* local.HostnameVo )
27- }
28- } else {
29- LOGGER .Debugf ("solver=local, status=hot-load, hostname=%s" , key )
30- conf , err := local .LoadConfiguration (ctx )
31- if err != nil {
32- LOGGER .Errorf ("status=could-not-load-conf, err=%v" , err )
33- return nil , err
34- }
35- activeEnv ,_ := conf .GetActiveEnv ()
36- if activeEnv == nil {
37- return nil , errors .New ("original env" )
38- }
39- var ttl int64 = 86400 // 24 hours
40- hostname ,_ = activeEnv .GetHostname (key )
41- if hostname != nil { ttl = int64 (hostname .Ttl ) }
42- val := s .Cache .PutIfAbsent (key , timed .NewTimedValue (hostname , time .Now (), time .Duration (ttl ) * time .Second ));
43- LOGGER .Debugf ("status=put, key=%s, value=%v, ttl=%d" , key , val , ttl )
23+ if msg , err := s .solveHostname (ctx , question , key ); err == nil {
24+ return msg , err
4425 }
4526
46- if hostname != nil {
47- rr := & dns.A {
48- Hdr : dns.RR_Header {Name : question .Name , Rrtype : dns .TypeA , Class : dns .ClassINET , Ttl : 0 },
49- A : net .IPv4 (hostname .Ip [0 ], hostname .Ip [1 ], hostname .Ip [2 ], hostname .Ip [3 ]),
50- }
51-
52- m := new (dns.Msg )
53- m .Answer = append (m .Answer , rr )
54- LOGGER .Debugf ("status=success, solver=local, key=%s" , key )
55- return m , nil
27+ i := strings .Index (key , "." )
28+ if i > 0 {
29+ return s .solveHostname (ctx , question , key [i :])
5630 }
5731 return nil , errors .New ("hostname not found " + key )
5832}
@@ -72,6 +46,47 @@ func (s localDnsSolver) ContainsKey(key interface{}) (interface{}, bool) {
7246 }
7347 LOGGER .Debugf ("status=expired, key=%v" , key )
7448 s .Cache .Remove (key )
75- return nil , false ;
49+ return nil , false
7650}
7751
52+ func (* localDnsSolver ) getMsg (question dns.Question , hostname * local.HostnameVo ) * dns.Msg {
53+ rr := & dns.A {
54+ Hdr : dns.RR_Header {Name : question .Name , Rrtype : dns .TypeA , Class : dns .ClassINET , Ttl : 0 },
55+ A : net .IPv4 (hostname .Ip [0 ], hostname .Ip [1 ], hostname .Ip [2 ], hostname .Ip [3 ]),
56+ }
57+ m := new (dns.Msg )
58+ m .Answer = append (m .Answer , rr )
59+ return m
60+ }
61+
62+ func (s localDnsSolver ) solveHostname (ctx context.Context , question dns.Question , key string ) (* dns.Msg , error ) {
63+ if value , found := s .ContainsKey (key ); found {
64+ LOGGER .Debugf ("solver=local, status=from-cache, hostname=%s, value=%v" , key , value )
65+ hostname := value .(* local.HostnameVo )
66+ if hostname != nil {
67+ return s .getMsg (question , hostname ), nil
68+ }
69+ }
70+
71+ LOGGER .Debugf ("solver=local, status=hot-load, hostname=%s" , key )
72+ conf , err := local .LoadConfiguration (ctx )
73+ if err != nil {
74+ LOGGER .Errorf ("status=could-not-load-conf, err=%v" , err )
75+ return nil , err
76+ }
77+ activeEnv , _ := conf .GetActiveEnv ()
78+ if activeEnv == nil {
79+ return nil , errors .New ("original env" )
80+ }
81+ var ttl int64 = 86400 // 24 hours
82+ hostname , _ := activeEnv .GetHostname (key )
83+ if hostname != nil {
84+ ttl = int64 (hostname .Ttl )
85+ }
86+ val := s .Cache .PutIfAbsent (key , timed .NewTimedValue (hostname , time .Now (), time .Duration (ttl )* time .Second ))
87+ LOGGER .Debugf ("status=put, key=%s, value=%v, ttl=%d" , key , val , ttl )
88+ if hostname != nil {
89+ return s .getMsg (question , hostname ), nil
90+ }
91+ return nil , errors .New ("hostname not found " + key )
92+ }
0 commit comments