Skip to content

Commit

Permalink
mikrotik func remove meaningless loop (#254)
Browse files Browse the repository at this point in the history
* add mikrotik api support

* ignore test TestGetMikrotikIP

* remove omitempty from Mikrotik struct

* fixed TestGetMikrotikIP struct literal uses unkeyed fields

* Remove meaningless loop
  • Loading branch information
0987363 authored Nov 24, 2024
1 parent 33ffdaf commit 82d460e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion configs/config_sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"ip_interface": "eth0",
"mikrotik": {
"enabled": false,
"server": "http://192.168.88.1:81",
"addr": "http://192.168.88.1:81",
"username": "admin",
"password": "",
"interface": "pppoe-out"
Expand Down
65 changes: 30 additions & 35 deletions pkg/lib/ip_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,49 +134,44 @@ func (helper *IPHelper) getIPFromMikrotik() string {
u.RawQuery = q.Encode()

req, _ := http.NewRequest("GET", u.String(), nil)

auth := fmt.Sprintf("%s:%s", helper.configuration.Mikrotik.Username, helper.configuration.Mikrotik.Password)
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
req.Header.Add("Content-Type", "application/json")

for {
client := &http.Client{
Timeout: time.Second * utils.DefaultTimeout,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}

response, err := client.Do(req)
if err != nil {
log.Error("Cannot get IP:", err)
time.Sleep(time.Millisecond * 300)
continue
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
log.Error("requst code failed: ", response.StatusCode)
return ""
}
client := &http.Client{
Timeout: time.Second * utils.DefaultTimeout,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}

body, err := io.ReadAll(response.Body)
if err != nil {
log.Error("read body failed: ", err)
return ""
}
response, err := client.Do(req)
if err != nil {
log.Error("request mikrotik address failed:", err)
return ""
}
defer response.Body.Close()

m := []map[string]string{}
if err := json.Unmarshal(body, &m); err != nil {
log.Error("unmarshal body failed: ", err)
return ""
}
if len(m) < 1 {
log.Error("could not get ip from: ", m)
return ""
}
if response.StatusCode != http.StatusOK {
log.Error("requst code failed: ", response.StatusCode)
return ""
}
body, err := io.ReadAll(response.Body)
if err != nil {
log.Error("read body failed: ", err)
return ""
}

res := strings.Split(m[0]["address"], "/")
return res[0]
m := []map[string]string{}
if err := json.Unmarshal(body, &m); err != nil {
log.Error("unmarshal body failed: ", err)
return ""
}
if len(m) < 1 {
log.Error("could not get ip from response: ", m)
return ""
}

res := strings.Split(m[0]["address"], "/")
return res[0]
}

// getIPFromInterface gets IP address from the specific interface.
Expand Down

0 comments on commit 82d460e

Please sign in to comment.