Compare commits

..

1 commit

Author SHA1 Message Date
568aaa9f8c Add publig IP getter
All checks were successful
continuous-integration/drone/push Build is passing
Ref #3

This commit uses a GET query to ifconfig.me in order to get the current
public IP. It also modifies the initialization message to send the
current public IP to the Telegram channel instead of a simple message.
2023-01-25 21:06:00 +01:00
5 changed files with 25 additions and 155 deletions

View file

@ -15,7 +15,7 @@ type Notifier struct {
cancel context.CancelFunc
tgBot *bot.ConcreteBot
notifChannel int64
ipGetter *ip.IPGetter
ipGetter ip.Getter
}
func (n *Notifier) SendInitMessage() error {

View file

@ -7,34 +7,29 @@ import (
"io"
"net"
"net/http"
"time"
)
const ifconfigURL = "https://ifconfig.me"
const httpMaxRead = 100
const httpTimeout = 10 * time.Second
type IPGetter struct {
httpClt *http.Client
remoteAddress string
timeout time.Duration
// TODO: use a struct, but a baseHTTPClient inside it to perform unit tests
type Getter interface {
GetCurrentPublicIP(ctx context.Context) (net.IP, error)
}
func (c *IPGetter) GetCurrentPublicIP(ctx context.Context) (net.IP, error) {
reqCtx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
type concreteIPGetter struct {
}
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, c.remoteAddress, nil)
func (c *concreteIPGetter) GetCurrentPublicIP(ctx context.Context) (net.IP, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ifconfigURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to prepare public IP request: %w", err)
}
resp, err := c.httpClt.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to get current IP from ifconfig: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid returncode %d", resp.StatusCode)
}
if resp.ContentLength > httpMaxRead {
return nil, fmt.Errorf("response too big: %d/%d", resp.ContentLength, httpMaxRead)
}
@ -52,10 +47,6 @@ func (c *IPGetter) GetCurrentPublicIP(ctx context.Context) (net.IP, error) {
return res, nil
}
func New() *IPGetter {
return &IPGetter{
httpClt: http.DefaultClient,
remoteAddress: ifconfigURL,
timeout: httpTimeout,
}
func New() *concreteIPGetter {
return &concreteIPGetter{}
}

View file

@ -1,134 +0,0 @@
package ip
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestGetIPOK(t *testing.T) {
t.Parallel()
mockSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("198.51.100.42"))
}))
defer mockSrv.Close()
clt := IPGetter{
httpClt: mockSrv.Client(),
remoteAddress: mockSrv.URL,
timeout: 1 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
val, err := clt.GetCurrentPublicIP(ctx)
if err != nil {
t.Fatalf("Unexpected error %s", err.Error())
}
if val.String() != "198.51.100.42" {
t.Fatalf("Unexpected public IP %v", val)
}
}
func TestGetIPServerErr(t *testing.T) {
t.Parallel()
mockSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer mockSrv.Close()
clt := IPGetter{
httpClt: mockSrv.Client(),
remoteAddress: mockSrv.URL,
timeout: 1 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := clt.GetCurrentPublicIP(ctx)
if err == nil {
t.Fatal("Unexpected nil error")
} else if err.Error() != "invalid returncode 500" {
t.Fatalf("Unexpected error %s", err.Error())
}
}
func TestGetIPUnreachable(t *testing.T) {
t.Parallel()
mockSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
mockSrv.Close()
clt := IPGetter{
httpClt: mockSrv.Client(),
remoteAddress: mockSrv.URL,
timeout: 1 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := clt.GetCurrentPublicIP(ctx)
if err == nil {
t.Fatal("Unexpected nil error")
} else if !strings.Contains(err.Error(), "connect: connection refused") {
t.Fatalf("Unexpected error %s", err.Error())
}
}
func TestGetIPInvalidResponse(t *testing.T) {
t.Parallel()
mockSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("toto"))
}))
defer mockSrv.Close()
clt := IPGetter{
httpClt: mockSrv.Client(),
remoteAddress: mockSrv.URL,
timeout: 1 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := clt.GetCurrentPublicIP(ctx)
if err == nil {
t.Fatal("Unexpected nil error")
} else if err.Error() != `got an invalid public IP "toto"` {
t.Fatalf("Unexpected error %s", err.Error())
}
}
func TestGetIPTimeout(t *testing.T) {
t.Parallel()
mockSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
w.WriteHeader(http.StatusOK)
w.Write([]byte("toto"))
}))
defer mockSrv.Close()
clt := IPGetter{
httpClt: mockSrv.Client(),
remoteAddress: mockSrv.URL,
timeout: 1 * time.Millisecond,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := clt.GetCurrentPublicIP(ctx)
if err == nil {
t.Fatal("Unexpected nil error")
} else if !strings.Contains(err.Error(), "context deadline exceeded") {
t.Fatalf("Unexpected error %s", err.Error())
}
}

View file

@ -9,6 +9,10 @@ import (
"git.faercol.me/faercol/public-ip-tracker/tracker/config"
)
func testMethod(a int) int {
return a + 2
}
type cliArgs struct {
configPath string
}

9
tracker/main_test.go Normal file
View file

@ -0,0 +1,9 @@
package main
import "testing"
func TestTestMethod(t *testing.T) {
if testMethod(12) != 14 {
t.Fatalf("Unexpected result %d", testMethod((12)))
}
}