-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
775eb54
commit 67324c9
Showing
8 changed files
with
180 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2021 Liuxiangchao [email protected]. All rights reserved. | ||
|
||
package nodes | ||
|
||
import ( | ||
"github.com/TeaOSLab/EdgeNode/internal/ttlcache" | ||
"github.com/TeaOSLab/EdgeNode/internal/utils" | ||
"github.com/iwind/TeaGo/types" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var sharedCNAMEManager = NewServerCNAMEManager() | ||
|
||
// ServerCNAMEManager 服务CNAME管理 | ||
// TODO 需要自动更新缓存里的记录 | ||
type ServerCNAMEManager struct { | ||
ttlCache *ttlcache.Cache | ||
|
||
locker sync.Mutex | ||
} | ||
|
||
func NewServerCNAMEManager() *ServerCNAMEManager { | ||
return &ServerCNAMEManager{ | ||
ttlCache: ttlcache.NewCache(), | ||
} | ||
} | ||
|
||
func (this *ServerCNAMEManager) Lookup(domain string) string { | ||
if len(domain) == 0 { | ||
return "" | ||
} | ||
|
||
var item = this.ttlCache.Read(domain) | ||
if item != nil { | ||
return types.String(item.Value) | ||
} | ||
|
||
cname, _ := utils.LookupCNAME(domain) | ||
if len(cname) > 0 { | ||
cname = strings.TrimSuffix(cname, ".") | ||
} | ||
|
||
this.ttlCache.Write(domain, cname, time.Now().Unix()+600) | ||
|
||
return cname | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2021 Liuxiangchao [email protected]. All rights reserved. | ||
|
||
package nodes | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestServerCNameManager_Lookup(t *testing.T) { | ||
var cnameManager = NewServerCNAMEManager() | ||
t.Log(cnameManager.Lookup("www.yun4s.cn")) | ||
|
||
var before = time.Now() | ||
defer func() { | ||
t.Log(time.Since(before).Seconds()*1000, "ms") | ||
}() | ||
t.Log(cnameManager.Lookup("www.yun4s.cn")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils" | ||
"github.com/miekg/dns" | ||
) | ||
|
||
// LookupCNAME 获取CNAME | ||
func LookupCNAME(host string) (string, error) { | ||
config, err := dns.ClientConfigFromFile("/etc/resolv.conf") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
c := new(dns.Client) | ||
m := new(dns.Msg) | ||
|
||
m.SetQuestion(host+".", dns.TypeCNAME) | ||
m.RecursionDesired = true | ||
|
||
var lastErr error | ||
for _, serverAddr := range config.Servers { | ||
r, _, err := c.Exchange(m, configutils.QuoteIP(serverAddr)+":"+config.Port) | ||
if err != nil { | ||
lastErr = err | ||
continue | ||
} | ||
if len(r.Answer) == 0 { | ||
continue | ||
} | ||
|
||
return r.Answer[0].(*dns.CNAME).Target, nil | ||
} | ||
return "", lastErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2021 Liuxiangchao [email protected]. All rights reserved. | ||
|
||
package utils | ||
|
||
import "testing" | ||
|
||
func TestLookupCNAME(t *testing.T) { | ||
t.Log(LookupCNAME("www.yun4s.cn")) | ||
} |