-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
telegram-bot-api with custom DNS, v0.1.0
- Loading branch information
Showing
5 changed files
with
241 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package tgbotapi | ||
|
||
// Package tgbotapi caches DNS lookups | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Resolver struct { | ||
lock sync.RWMutex | ||
cache map[string][]net.IP | ||
} | ||
|
||
func New(refreshRate time.Duration) *Resolver { | ||
resolver := &Resolver{ | ||
cache: make(map[string][]net.IP, 64), | ||
} | ||
if refreshRate > 0 { | ||
go resolver.autoRefresh(refreshRate) | ||
} | ||
return resolver | ||
} | ||
|
||
func (r *Resolver) Fetch(address string) ([]net.IP, error) { | ||
r.lock.RLock() | ||
ips, exists := r.cache[address] | ||
r.lock.RUnlock() | ||
if exists { | ||
return ips, nil | ||
} | ||
|
||
return r.Lookup(address) | ||
} | ||
|
||
func (r *Resolver) FetchOne(address string) (net.IP, error) { | ||
ips, err := r.Fetch(address) | ||
if err != nil || len(ips) == 0 { | ||
return nil, err | ||
} | ||
return ips[0], nil | ||
} | ||
|
||
func (r *Resolver) FetchOneString(address string) (string, error) { | ||
ip, err := r.FetchOne(address) | ||
if err != nil || ip == nil { | ||
return "", err | ||
} | ||
return ip.String(), nil | ||
} | ||
|
||
func (r *Resolver) Refresh() { | ||
i := 0 | ||
r.lock.RLock() | ||
addresses := make([]string, len(r.cache)) | ||
for key, _ := range r.cache { | ||
addresses[i] = key | ||
i++ | ||
} | ||
r.lock.RUnlock() | ||
|
||
for _, address := range addresses { | ||
r.Lookup(address) | ||
time.Sleep(time.Second * 2) | ||
} | ||
} | ||
|
||
func (r *Resolver) Lookup(address string) ([]net.IP, error) { | ||
ips, err := net.LookupIP(address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r.lock.Lock() | ||
r.cache[address] = ips | ||
r.lock.Unlock() | ||
return ips, nil | ||
} | ||
|
||
func (r *Resolver) autoRefresh(rate time.Duration) { | ||
for { | ||
time.Sleep(rate) | ||
r.Refresh() | ||
} | ||
} |
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,79 @@ | ||
package tgbotapi | ||
|
||
import ( | ||
"net" | ||
"sort" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFetchReturnsAndErrorOnInvalidLookup(t *testing.T) { | ||
ips, err := New(0).Lookup("invalid.viki.io") | ||
if ips != nil { | ||
t.Errorf("Expecting nil ips, got %v", ips) | ||
} | ||
expected := "lookup invalid.viki.io: no such host" | ||
if err.Error() != expected { | ||
t.Errorf("Expecting %q error, got %q", expected, err.Error()) | ||
} | ||
} | ||
|
||
func TestFetchReturnsAListOfIps(t *testing.T) { | ||
ips, _ := New(0).Lookup("dnscache.go.test.viki.io") | ||
assertIps(t, ips, []string{"1.123.58.13", "31.85.32.110"}) | ||
} | ||
|
||
func TestCallingLookupAddsTheItemToTheCache(t *testing.T) { | ||
r := New(0) | ||
r.Lookup("dnscache.go.test.viki.io") | ||
assertIps(t, r.cache["dnscache.go.test.viki.io"], []string{"1.123.58.13", "31.85.32.110"}) | ||
} | ||
|
||
func TestFetchLoadsValueFromTheCache(t *testing.T) { | ||
r := New(0) | ||
r.cache["invalid.viki.io"] = []net.IP{net.ParseIP("1.1.2.3")} | ||
ips, _ := r.Fetch("invalid.viki.io") | ||
assertIps(t, ips, []string{"1.1.2.3"}) | ||
} | ||
|
||
func TestFetchOneLoadsTheFirstValue(t *testing.T) { | ||
r := New(0) | ||
r.cache["something.viki.io"] = []net.IP{net.ParseIP("1.1.2.3"), net.ParseIP("100.100.102.103")} | ||
ip, _ := r.FetchOne("something.viki.io") | ||
assertIps(t, []net.IP{ip}, []string{"1.1.2.3"}) | ||
} | ||
|
||
func TestFetchOneStringLoadsTheFirstValue(t *testing.T) { | ||
r := New(0) | ||
r.cache["something.viki.io"] = []net.IP{net.ParseIP("100.100.102.103"), net.ParseIP("100.100.102.104")} | ||
ip, _ := r.FetchOneString("something.viki.io") | ||
if ip != "100.100.102.103" { | ||
t.Errorf("expected 100.100.102.103 but got %v", ip) | ||
} | ||
} | ||
|
||
func TestFetchLoadsTheIpAndCachesIt(t *testing.T) { | ||
r := New(0) | ||
ips, _ := r.Fetch("dnscache.go.test.viki.io") | ||
assertIps(t, ips, []string{"1.123.58.13", "31.85.32.110"}) | ||
assertIps(t, r.cache["dnscache.go.test.viki.io"], []string{"1.123.58.13", "31.85.32.110"}) | ||
} | ||
|
||
func TestItReloadsTheIpsAtAGivenInterval(t *testing.T) { | ||
r := New(1) | ||
r.cache["dnscache.go.test.viki.io"] = nil | ||
time.Sleep(time.Second * 2) | ||
assertIps(t, r.cache["dnscache.go.test.viki.io"], []string{"1.123.58.13", "31.85.32.110"}) | ||
} | ||
|
||
func assertIps(t *testing.T, actuals []net.IP, expected []string) { | ||
if len(actuals) != len(expected) { | ||
t.Errorf("Expecting %d ips, got %d", len(expected), len(actuals)) | ||
} | ||
sort.Strings(expected) | ||
for _, ip := range actuals { | ||
if sort.SearchStrings(expected, ip.String()) == -1 { | ||
t.Errorf("Got an unexpected ip: %v:", actuals[0]) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/go-telegram-bot-api/telegram-bot-api/v5 | ||
module github.com/artlibs/telegram-bot-api | ||
|
||
go 1.16 | ||
go 1.22 |