Skip to content

Commit

Permalink
split upnp into seperate package
Browse files Browse the repository at this point in the history
  • Loading branch information
ndecker committed Feb 29, 2016
1 parent cb4aa46 commit 05c803b
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 129 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fritzbox_exporter
62 changes: 44 additions & 18 deletions upnp.go → fritzbox_upnp/upnp.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package main
package fritzbox_upnp

import (
"bytes"
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
)

const TEXT_XML = `text/xml; charset="utf-8"`
const text_xml = `text/xml; charset="utf-8"`

var (
ErrResultNotFound = errors.New("result not found")
ErrResultWithoutChardata = errors.New("result without chardata")
ErrResultNotFound = errors.New("result not found")
ErrResultWithoutChardata = errors.New("result without chardata")
)

// curl "http://fritz.box:49000/igdupnp/control/WANIPConn1"
Expand All @@ -27,14 +28,17 @@ var (
// </s:Body> </s:Envelope>"

type UpnpValue struct {
path string
service string
method string
ret_tag string
Path string
Service string
Method string
RetTag string

ShortName string
Help string
}

func (v *UpnpValue) Query(device string) (string, error) {
url := fmt.Sprintf("http://%s:49000%s", device, v.path)
func (v *UpnpValue) query(device string, port uint16) (string, error) {
url := fmt.Sprintf("http://%s:%d%s", device, port, v.Path)

bodystr := fmt.Sprintf(`
<?xml version='1.0' encoding='utf-8'?>
Expand All @@ -43,7 +47,7 @@ func (v *UpnpValue) Query(device string) (string, error) {
<u:%s xmlns:u='urn:schemas-upnp-org:service:%s' />
</s:Body>
</s:Envelope>
`, v.method, v.service)
`, v.Method, v.Service)

body := strings.NewReader(bodystr)

Expand All @@ -52,20 +56,20 @@ func (v *UpnpValue) Query(device string) (string, error) {
return "", err
}

action := fmt.Sprintf("urn:schemas-upnp-org:service:%s#%s", v.service, v.method)
action := fmt.Sprintf("urn:schemas-upnp-org:service:%s#%s", v.Service, v.Method)

req.Header["Content-Type"] = []string{TEXT_XML}
req.Header["Content-Type"] = []string{text_xml}
req.Header["SoapAction"] = []string{action}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}

data := new(bytes.Buffer)
data.ReadFrom(resp.Body)
data := new(bytes.Buffer)
data.ReadFrom(resp.Body)

// fmt.Printf(data.String())
// fmt.Printf(data.String())

dec := xml.NewDecoder(data)

Expand All @@ -80,7 +84,7 @@ func (v *UpnpValue) Query(device string) (string, error) {
}

if se, ok := t.(xml.StartElement); ok {
if se.Name.Local == v.ret_tag {
if se.Name.Local == v.RetTag {
t2, err := dec.Token()
if err != nil {
return "", err
Expand All @@ -96,3 +100,25 @@ func (v *UpnpValue) Query(device string) (string, error) {

}
}

type UpnpValueString struct{ UpnpValue }

func (v *UpnpValueString) Query(device string, port uint16) (string, error) {
return v.query(device, port)
}

type UpnpValueUint struct{ UpnpValue }

func (v *UpnpValueUint) Query(device string, port uint16) (uint64, error) {
strval, err := v.query(device, port)
if err != nil {
return 0, err
}

val, err := strconv.ParseUint(strval, 10, 64)
if err != nil {
return 0, err
}

return val, nil
}
67 changes: 67 additions & 0 deletions fritzbox_upnp/values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fritzbox_upnp

// curl http://fritz.box:49000/igddesc.xml
// curl http://fritz.box:49000/any.xml
// curl http://fritz.box:49000/igdconnSCPD.xml
// curl http://fritz.box:49000/igdicfgSCPD.xml
// curl http://fritz.box:49000/igddslSCPD.xml
// curl http://fritz.box:49000/igd2ipv6fwcSCPD.xml

var (
WAN_IP = UpnpValueString{UpnpValue{
Path: "/igdupnp/control/WANIPConn1",
Service: "WANIPConnection:1",
Method: "GetExternalIPAddress",
RetTag: "NewExternalIPAddress",

ShortName: "wan_ip",
Help: "WAN IP Adress",
}}

WAN_Packets_Received = UpnpValueUint{UpnpValue{
Path: "/igdupnp/control/WANCommonIFC1",
Service: "WANCommonInterfaceConfig:1",
Method: "GetTotalPacketsReceived",
RetTag: "NewTotalPacketsReceived",

ShortName: "packets_received",
Help: "packets received on gateway WAN interface",
}}

WAN_Packets_Sent = UpnpValueUint{UpnpValue{
Path: "/igdupnp/control/WANCommonIFC1",
Service: "WANCommonInterfaceConfig:1",
Method: "GetTotalPacketsSent",
RetTag: "NewTotalPacketsSent",

ShortName: "packets_sent",
Help: "packets sent on gateway WAN interface",
}}

WAN_Bytes_Received = UpnpValueUint{UpnpValue{
Path: "/igdupnp/control/WANCommonIFC1",
Service: "WANCommonInterfaceConfig:1",
Method: "GetAddonInfos",
RetTag: "NewTotalBytesReceived",

ShortName: "bytes_received",
Help: "bytes received on gateway WAN interface",
}}

WAN_Bytes_Sent = UpnpValueUint{UpnpValue{
Path: "/igdupnp/control/WANCommonIFC1",
Service: "WANCommonInterfaceConfig:1",
Method: "GetAddonInfos",
RetTag: "NewTotalBytesSent",

ShortName: "bytes_sent",
Help: "bytes sent on gateway WAN interface",
}}
)

var Values = []UpnpValueUint{
WAN_Packets_Received,
WAN_Packets_Sent,
WAN_Bytes_Received,
WAN_Bytes_Sent,
}
Loading

0 comments on commit 05c803b

Please sign in to comment.