代码拉取完成,页面将自动刷新
package upnp
import (
"encoding/xml"
"fmt"
"net/http"
"strings"
)
// GetExtIPAction get external ip address
type GetExtIPAction struct {
actionData
}
func newGetExtIPAction(srv *Service, in, out []string) *GetExtIPAction {
return &GetExtIPAction{
actionData{
name: "GetExternalIPAddress",
srv: srv,
args: in,
resps: out,
},
}
}
type getExtIPBody struct {
ActionBodyContent
}
func (a *GetExtIPAction) requestBody(i int) (*actionBodyType, error) {
body := new(getExtIPBody)
body.Init(a.actionData)
return newActionBody(body)
}
type getExtIPResponse struct {
Addr string `xml:"Body>GetExternalIPAddressResponse>NewExternalIPAddress"`
Code string `xml:"Body>Fault>faultcode"`
String string `xml:"Body>Fault>faultstring"`
Detail struct {
UPnPError struct {
Code int `xml:"errorCode"`
Description string `xml:"errorDescription"`
} `xml:"UPnPError"`
} `xml:"Body>Fault>detail"`
}
// Do get external ip address, ip address in ActionResponse.Data in string type
func (a *GetExtIPAction) Do(i int) (*ActionResponse, error) {
obj, err := a.requestBody(i)
if err != nil {
return nil, err
}
bodyData, err := xml.Marshal(obj)
if err != nil {
return nil, err
}
data := xml.Header + string(bodyData)
resp, err := a.sendRequest(strings.NewReader(data), len(data))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var ret getExtIPResponse
if err := xml.NewDecoder(resp.Body).Decode(&ret); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("AddPortMapping failed: %d/%s",
ret.Detail.UPnPError.Code, ret.Detail.UPnPError.Description)
}
return &ActionResponse{
Code: 0,
Data: ret.Addr,
}, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。