This commit is contained in:
Laurent Le Houerou 2020-10-27 16:55:53 +04:00
parent ba27df891e
commit ed50c32702
2 changed files with 21 additions and 10 deletions

View File

@ -15,21 +15,22 @@ const (
)
type IpRequester struct {
client *http.Client
statusUrl string
login string
password string
StatusUrl string
client *http.Client
login string
password string
}
func NewIpRequester(client *http.Client, statusUrl string, login string, password string) *IpRequester {
return &IpRequester{client: client, statusUrl: statusUrl, login: login, password: password}
return &IpRequester{client: client, StatusUrl: statusUrl, login: login, password: password}
}
func (r IpRequester) GetOutboundIp() <-chan outboundip.IpRequestResult {
result := make(chan outboundip.IpRequestResult)
go func() {
defer close(result)
url := r.statusUrl
url := r.StatusUrl
if url == "" {
url = DefaultUrl
}

View File

@ -3,17 +3,27 @@ package ddwrt
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func init() {
}
func TestRequester_GetOutboundIp(t *testing.T) {
requester := NewIpRequester(&http.Client{
Timeout: 30 * time.Second,
}, "", "laurent", "&951753seiko38613861")
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, `{wan_shortproto::dhcp_auth}{wan_status::Error&nbsp;&nbsp;<input class="button" type="button" value="Error" onclick="connect(this.form, '"&nbsp;"_dhcp_auth')" />}{wan_uptime::1 day, 4:17:41}{pppoe_ac_name::}{wan_3g_signal::n.A.}{wan_ipaddr::92.130.63.129}{wan_netmask::255.255.248.0}{wan_gateway::92.130.56.1}{wan_dns0::192.168.1.250}{wan_dns1::80.10.246.4}{wan_dns2::81.253.149.15}{dhcp_remaining::0 days 15:24:14}{ttraff_in::142865}{ttraff_out::70849}{uptime:: 08:18:05 up 1 day, 4:18, load average: 1.00, 1.02, 1.00}{ipinfo::&nbsp;IPv4: 92.130.63.129&nbsp;IPv6: 2a01:cb22:112:c800:200:ff:fe00:0}`)
}))
defer s.Close()
requester := NewIpRequester(s.Client(), s.URL, "laurent", "&951753seiko38613861")
result := <-requester.GetOutboundIp()
fmt.Printf("%+v\n", result)
if result.Error != nil {
t.Error(result.Error)
}
if result.Ip != "92.130.63.129" {
t.Errorf("expected : 92.130.63.129, actual : %s", result.Ip)
}
}