Skip to content

Commit 82d460e

Browse files
authored
mikrotik func remove meaningless loop (#254)
* add mikrotik api support * ignore test TestGetMikrotikIP * remove omitempty from Mikrotik struct * fixed TestGetMikrotikIP struct literal uses unkeyed fields * Remove meaningless loop
1 parent 33ffdaf commit 82d460e

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

configs/config_sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"ip_interface": "eth0",
4040
"mikrotik": {
4141
"enabled": false,
42-
"server": "http://192.168.88.1:81",
42+
"addr": "http://192.168.88.1:81",
4343
"username": "admin",
4444
"password": "",
4545
"interface": "pppoe-out"

pkg/lib/ip_helper.go

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -134,49 +134,44 @@ func (helper *IPHelper) getIPFromMikrotik() string {
134134
u.RawQuery = q.Encode()
135135

136136
req, _ := http.NewRequest("GET", u.String(), nil)
137-
138137
auth := fmt.Sprintf("%s:%s", helper.configuration.Mikrotik.Username, helper.configuration.Mikrotik.Password)
139138
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
140139
req.Header.Add("Content-Type", "application/json")
141140

142-
for {
143-
client := &http.Client{
144-
Timeout: time.Second * utils.DefaultTimeout,
145-
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
146-
}
147-
148-
response, err := client.Do(req)
149-
if err != nil {
150-
log.Error("Cannot get IP:", err)
151-
time.Sleep(time.Millisecond * 300)
152-
continue
153-
}
154-
defer response.Body.Close()
155-
156-
if response.StatusCode != http.StatusOK {
157-
log.Error("requst code failed: ", response.StatusCode)
158-
return ""
159-
}
141+
client := &http.Client{
142+
Timeout: time.Second * utils.DefaultTimeout,
143+
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
144+
}
160145

161-
body, err := io.ReadAll(response.Body)
162-
if err != nil {
163-
log.Error("read body failed: ", err)
164-
return ""
165-
}
146+
response, err := client.Do(req)
147+
if err != nil {
148+
log.Error("request mikrotik address failed:", err)
149+
return ""
150+
}
151+
defer response.Body.Close()
166152

167-
m := []map[string]string{}
168-
if err := json.Unmarshal(body, &m); err != nil {
169-
log.Error("unmarshal body failed: ", err)
170-
return ""
171-
}
172-
if len(m) < 1 {
173-
log.Error("could not get ip from: ", m)
174-
return ""
175-
}
153+
if response.StatusCode != http.StatusOK {
154+
log.Error("requst code failed: ", response.StatusCode)
155+
return ""
156+
}
157+
body, err := io.ReadAll(response.Body)
158+
if err != nil {
159+
log.Error("read body failed: ", err)
160+
return ""
161+
}
176162

177-
res := strings.Split(m[0]["address"], "/")
178-
return res[0]
163+
m := []map[string]string{}
164+
if err := json.Unmarshal(body, &m); err != nil {
165+
log.Error("unmarshal body failed: ", err)
166+
return ""
167+
}
168+
if len(m) < 1 {
169+
log.Error("could not get ip from response: ", m)
170+
return ""
179171
}
172+
173+
res := strings.Split(m[0]["address"], "/")
174+
return res[0]
180175
}
181176

182177
// getIPFromInterface gets IP address from the specific interface.

0 commit comments

Comments
 (0)