Move top level packages to internal.
This commit is contained in:
17
internal/upnp/debug.go
Normal file
17
internal/upnp/debug.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
||||
// All rights reserved. Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package upnp
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/syncthing/syncthing/internal/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
debug = strings.Contains(os.Getenv("STTRACE"), "upnp") || os.Getenv("STTRACE") == "all"
|
||||
l = logger.DefaultLogger
|
||||
)
|
||||
93
internal/upnp/testdata/technicolor.xml
vendored
Normal file
93
internal/upnp/testdata/technicolor.xml
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
<?xml version="1.0"?>
|
||||
<root xmlns="urn:schemas-upnp-org:device-1-0">
|
||||
<specVersion>
|
||||
<major>1</major>
|
||||
<minor>0</minor>
|
||||
</specVersion>
|
||||
<URLBase>http://192.168.1.254:8000</URLBase>
|
||||
<device>
|
||||
<deviceType>urn:schemas-upnp-org:device:InternetGatewayDevice:1</deviceType>
|
||||
<friendlyName>Technicolor TG784n v3 (1321RAWMS)</friendlyName>
|
||||
<manufacturer>Technicolor</manufacturer>
|
||||
<manufacturerURL>http://www.technicolor.com
|
||||
</manufacturerURL>
|
||||
<modelDescription>Technicolor Internet Gateway Device</modelDescription>
|
||||
<modelName>Technicolor TG</modelName>
|
||||
<modelNumber>784n v3</modelNumber>
|
||||
<modelURL>http://www.technicolor.com</modelURL>
|
||||
<serialNumber>1321RAWMS</serialNumber>
|
||||
<UDN>uuid:UPnP_Technicolor TG784n v3-1_A4-B1-E9-D8-F4-78</UDN>
|
||||
<presentationURL>/</presentationURL>
|
||||
<serviceList>
|
||||
<service>
|
||||
<serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType>
|
||||
<serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId>
|
||||
<controlURL>/hou74cq4tw9/IGD/upnp/control/igd/layer3f</controlURL>
|
||||
<eventSubURL>/hou74cq4tw9/IGD/upnp/event/igd/layer3f</eventSubURL>
|
||||
<SCPDURL>/hou74cq4tw9/IGD/upnp/Layer3Forwarding.xml</SCPDURL>
|
||||
</service>
|
||||
</serviceList>
|
||||
<deviceList>
|
||||
<device>
|
||||
<deviceType>urn:schemas-upnp-org:device:LANDevice:1</deviceType>
|
||||
<friendlyName>LANDevice</friendlyName>
|
||||
<manufacturer>Technicolor</manufacturer>
|
||||
<modelName>Technicolor TG784n v3</modelName>
|
||||
<serialNumber>A4-B1-E9-D8-F4-78</serialNumber>
|
||||
<UDN>uuid:UPnP_Technicolor TG784n v3-1_A4-B1-E9-D8-F4-78_LD_1</UDN>
|
||||
<serviceList>
|
||||
<service>
|
||||
<serviceType>urn:schemas-upnp-org:service:LANHostConfigManagement:1</serviceType>
|
||||
<serviceId>urn:upnp-org:serviceId:LANHostCfg1</serviceId>
|
||||
<controlURL>/hou74cq4tw9/IGD/upnp/control/igd/lanhcm_1</controlURL>
|
||||
<eventSubURL></eventSubURL>
|
||||
<SCPDURL>/hou74cq4tw9/IGD/upnp/LANHostConfigManagement.xml</SCPDURL>
|
||||
</service>
|
||||
</serviceList>
|
||||
</device>
|
||||
<device>
|
||||
<deviceType>urn:schemas-upnp-org:device:WANDevice:1</deviceType>
|
||||
<friendlyName>WANDevice</friendlyName>
|
||||
<manufacturer>Technicolor</manufacturer>
|
||||
<modelName>Technicolor TG784n v3</modelName>
|
||||
<serialNumber>A4-B1-E9-D8-F4-78</serialNumber>
|
||||
<UDN>uuid:UPnP_Technicolor TG784n v3-1_A4-B1-E9-D8-F4-78_WD_1</UDN>
|
||||
<serviceList>
|
||||
<service>
|
||||
<serviceType>urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1</serviceType>
|
||||
<serviceId>urn:upnp-org:serviceId:WANCommonIFC1</serviceId>
|
||||
<controlURL>/hou74cq4tw9/IGD/upnp/control/igd/wancic_1</controlURL>
|
||||
<eventSubURL>/hou74cq4tw9/IGD/upnp/event/igd/wancic_1</eventSubURL>
|
||||
<SCPDURL>/hou74cq4tw9/IGD/upnp/WANCommonInterfaceConfig.xml</SCPDURL>
|
||||
</service>
|
||||
</serviceList>
|
||||
<deviceList>
|
||||
<device>
|
||||
<deviceType>urn:schemas-upnp-org:device:WANConnectionDevice:1</deviceType>
|
||||
<friendlyName>WANConnectionDevice</friendlyName>
|
||||
<manufacturer>Technicolor</manufacturer>
|
||||
<modelName>Technicolor TG784n v3</modelName>
|
||||
<serialNumber>A4-B1-E9-D8-F4-78</serialNumber>
|
||||
<UDN>uuid:UPnP_Technicolor TG784n v3-1_A4-B1-E9-D8-F4-78_WCD_1_1</UDN>
|
||||
<serviceList>
|
||||
<service>
|
||||
<serviceType>urn:schemas-upnp-org:service:WANDSLLinkConfig:1</serviceType>
|
||||
<serviceId>urn:upnp-org:serviceId:WANDSLLinkC1</serviceId>
|
||||
<controlURL>/hou74cq4tw9/IGD/upnp/control/igd/wandsllc_1_1</controlURL>
|
||||
<eventSubURL>/hou74cq4tw9/IGD/upnp/event/igd/wandsllc_1_1</eventSubURL>
|
||||
<SCPDURL>/hou74cq4tw9/IGD/upnp/WANDSLLinkConfig.xml</SCPDURL>
|
||||
</service>
|
||||
<service>
|
||||
<serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>
|
||||
<serviceId>urn:upnp-org:serviceId:WANPPPConn1</serviceId>
|
||||
<controlURL>/hou74cq4tw9/IGD/upnp/control/igd/wanpppc_1_1_1</controlURL>
|
||||
<eventSubURL>/hou74cq4tw9/IGD/upnp/event/igd/wanpppc_1_1_1</eventSubURL>
|
||||
<SCPDURL>/hou74cq4tw9/IGD/upnp/WANPPPConnection.xml</SCPDURL>
|
||||
</service>
|
||||
</serviceList>
|
||||
</device>
|
||||
</deviceList>
|
||||
</device>
|
||||
</deviceList>
|
||||
</device>
|
||||
</root>
|
||||
307
internal/upnp/upnp.go
Normal file
307
internal/upnp/upnp.go
Normal file
@@ -0,0 +1,307 @@
|
||||
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
||||
// All rights reserved. Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Adapted from https://github.com/jackpal/Taipei-Torrent/blob/dd88a8bfac6431c01d959ce3c745e74b8a911793/IGD.go
|
||||
// Copyright (c) 2010 Jack Palevich (https://github.com/jackpal/Taipei-Torrent/blob/dd88a8bfac6431c01d959ce3c745e74b8a911793/LICENSE)
|
||||
|
||||
// Package upnp implements UPnP Internet Gateway upnpDevice port mappings
|
||||
package upnp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IGD struct {
|
||||
serviceURL string
|
||||
device string
|
||||
ourIP string
|
||||
}
|
||||
|
||||
type Protocol string
|
||||
|
||||
const (
|
||||
TCP Protocol = "TCP"
|
||||
UDP = "UDP"
|
||||
)
|
||||
|
||||
type upnpService struct {
|
||||
ServiceType string `xml:"serviceType"`
|
||||
ControlURL string `xml:"controlURL"`
|
||||
}
|
||||
|
||||
type upnpDevice struct {
|
||||
DeviceType string `xml:"deviceType"`
|
||||
Devices []upnpDevice `xml:"deviceList>device"`
|
||||
Services []upnpService `xml:"serviceList>service"`
|
||||
}
|
||||
|
||||
type upnpRoot struct {
|
||||
Device upnpDevice `xml:"device"`
|
||||
}
|
||||
|
||||
func Discover() (*IGD, error) {
|
||||
ssdp := &net.UDPAddr{IP: []byte{239, 255, 255, 250}, Port: 1900}
|
||||
|
||||
socket, err := net.ListenUDP("udp4", &net.UDPAddr{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer socket.Close()
|
||||
|
||||
err = socket.SetDeadline(time.Now().Add(3 * time.Second))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
searchStr := `M-SEARCH * HTTP/1.1
|
||||
Host: 239.255.255.250:1900
|
||||
St: urn:schemas-upnp-org:device:InternetGatewayDevice:1
|
||||
Man: "ssdp:discover"
|
||||
Mx: 3
|
||||
|
||||
`
|
||||
search := []byte(strings.Replace(searchStr, "\n", "\r\n", -1))
|
||||
|
||||
_, err = socket.WriteTo(search, ssdp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := make([]byte, 1500)
|
||||
n, _, err := socket.ReadFrom(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if debug {
|
||||
l.Debugln(string(resp[:n]))
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(bytes.NewBuffer(resp[:n]))
|
||||
request := &http.Request{}
|
||||
response, err := http.ReadResponse(reader, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if response.Header.Get("St") != "urn:schemas-upnp-org:device:InternetGatewayDevice:1" {
|
||||
return nil, errors.New("no igd")
|
||||
}
|
||||
|
||||
locURL := response.Header.Get("Location")
|
||||
if locURL == "" {
|
||||
return nil, errors.New("no location")
|
||||
}
|
||||
|
||||
serviceURL, device, err := getServiceURL(locURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Figure out our IP number, on the network used to reach the IGD. We
|
||||
// do this in a fairly roundabout way by connecting to the IGD and
|
||||
// checking the address of the local end of the socket. I'm open to
|
||||
// suggestions on a better way to do this...
|
||||
ourIP, err := localIP(locURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
igd := &IGD{
|
||||
serviceURL: serviceURL,
|
||||
device: device,
|
||||
ourIP: ourIP,
|
||||
}
|
||||
return igd, nil
|
||||
}
|
||||
|
||||
func localIP(tgt string) (string, error) {
|
||||
url, err := url.Parse(tgt)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
conn, err := net.Dial("tcp", url.Host)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
ourIP, _, err := net.SplitHostPort(conn.LocalAddr().String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ourIP, nil
|
||||
}
|
||||
|
||||
func getChildDevice(d upnpDevice, deviceType string) (upnpDevice, bool) {
|
||||
for _, dev := range d.Devices {
|
||||
if dev.DeviceType == deviceType {
|
||||
return dev, true
|
||||
}
|
||||
}
|
||||
return upnpDevice{}, false
|
||||
}
|
||||
|
||||
func getChildService(d upnpDevice, serviceType string) (upnpService, bool) {
|
||||
for _, svc := range d.Services {
|
||||
if svc.ServiceType == serviceType {
|
||||
return svc, true
|
||||
}
|
||||
}
|
||||
return upnpService{}, false
|
||||
}
|
||||
|
||||
func getServiceURL(rootURL string) (string, string, error) {
|
||||
r, err := http.Get(rootURL)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
if r.StatusCode >= 400 {
|
||||
return "", "", errors.New(r.Status)
|
||||
}
|
||||
return getServiceURLReader(rootURL, r.Body)
|
||||
}
|
||||
|
||||
func getServiceURLReader(rootURL string, r io.Reader) (string, string, error) {
|
||||
var upnpRoot upnpRoot
|
||||
err := xml.NewDecoder(r).Decode(&upnpRoot)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
dev := upnpRoot.Device
|
||||
if dev.DeviceType != "urn:schemas-upnp-org:device:InternetGatewayDevice:1" {
|
||||
return "", "", errors.New("No InternetGatewayDevice")
|
||||
}
|
||||
|
||||
dev, ok := getChildDevice(dev, "urn:schemas-upnp-org:device:WANDevice:1")
|
||||
if !ok {
|
||||
return "", "", errors.New("No WANDevice")
|
||||
}
|
||||
|
||||
dev, ok = getChildDevice(dev, "urn:schemas-upnp-org:device:WANConnectionDevice:1")
|
||||
if !ok {
|
||||
return "", "", errors.New("No WANConnectionDevice")
|
||||
}
|
||||
|
||||
device := "urn:schemas-upnp-org:service:WANIPConnection:1"
|
||||
svc, ok := getChildService(dev, device)
|
||||
if !ok {
|
||||
device = "urn:schemas-upnp-org:service:WANPPPConnection:1"
|
||||
}
|
||||
svc, ok = getChildService(dev, device)
|
||||
if !ok {
|
||||
return "", "", errors.New("No WANIPConnection nor WANPPPConnection")
|
||||
}
|
||||
|
||||
if len(svc.ControlURL) == 0 {
|
||||
return "", "", errors.New("no controlURL")
|
||||
}
|
||||
|
||||
u, _ := url.Parse(rootURL)
|
||||
replaceRawPath(u, svc.ControlURL)
|
||||
return u.String(), device, nil
|
||||
}
|
||||
|
||||
func replaceRawPath(u *url.URL, rp string) {
|
||||
var p, q string
|
||||
fs := strings.Split(rp, "?")
|
||||
p = fs[0]
|
||||
if len(fs) > 1 {
|
||||
q = fs[1]
|
||||
}
|
||||
|
||||
if p[0] == '/' {
|
||||
u.Path = p
|
||||
} else {
|
||||
u.Path += p
|
||||
}
|
||||
u.RawQuery = q
|
||||
}
|
||||
|
||||
func soapRequest(url, device, function, message string) error {
|
||||
tpl := `<?xml version="1.0" ?>
|
||||
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
||||
<s:Body>%s</s:Body>
|
||||
</s:Envelope>
|
||||
`
|
||||
body := fmt.Sprintf(tpl, message)
|
||||
|
||||
req, err := http.NewRequest("POST", url, strings.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", `text/xml; charset="utf-8"`)
|
||||
req.Header.Set("User-Agent", "syncthing/1.0")
|
||||
req.Header.Set("SOAPAction", fmt.Sprintf(`"%s#%s"`, device, function))
|
||||
req.Header.Set("Connection", "Close")
|
||||
req.Header.Set("Cache-Control", "no-cache")
|
||||
req.Header.Set("Pragma", "no-cache")
|
||||
|
||||
if debug {
|
||||
l.Debugln(req.Header.Get("SOAPAction"))
|
||||
l.Debugln(body)
|
||||
}
|
||||
|
||||
r, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if debug {
|
||||
resp, _ := ioutil.ReadAll(r.Body)
|
||||
l.Debugln(string(resp))
|
||||
}
|
||||
|
||||
r.Body.Close()
|
||||
|
||||
if r.StatusCode >= 400 {
|
||||
return errors.New(function + ": " + r.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *IGD) AddPortMapping(protocol Protocol, externalPort, internalPort int, description string, timeout int) error {
|
||||
tpl := `<u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
|
||||
<NewRemoteHost></NewRemoteHost>
|
||||
<NewExternalPort>%d</NewExternalPort>
|
||||
<NewProtocol>%s</NewProtocol>
|
||||
<NewInternalPort>%d</NewInternalPort>
|
||||
<NewInternalClient>%s</NewInternalClient>
|
||||
<NewEnabled>1</NewEnabled>
|
||||
<NewPortMappingDescription>%s</NewPortMappingDescription>
|
||||
<NewLeaseDuration>%d</NewLeaseDuration>
|
||||
</u:AddPortMapping>
|
||||
`
|
||||
|
||||
body := fmt.Sprintf(tpl, externalPort, protocol, internalPort, n.ourIP, description, timeout)
|
||||
return soapRequest(n.serviceURL, n.device, "AddPortMapping", body)
|
||||
}
|
||||
|
||||
func (n *IGD) DeletePortMapping(protocol Protocol, externalPort int) (err error) {
|
||||
tpl := `<u:DeletePortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
|
||||
<NewRemoteHost></NewRemoteHost>
|
||||
<NewExternalPort>%d</NewExternalPort>
|
||||
<NewProtocol>%s</NewProtocol>
|
||||
</u:DeletePortMapping>
|
||||
`
|
||||
|
||||
body := fmt.Sprintf(tpl, externalPort, protocol)
|
||||
return soapRequest(n.serviceURL, n.device, "DeletePortMapping", body)
|
||||
}
|
||||
21
internal/upnp/upnp_test.go
Normal file
21
internal/upnp/upnp_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
||||
// All rights reserved. Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package upnp
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetTechnicolorRootURL(t *testing.T) {
|
||||
r, _ := os.Open("testdata/technicolor.xml")
|
||||
_, action, err := getServiceURLReader("http://localhost:1234/", r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if action != "urn:schemas-upnp-org:service:WANPPPConnection:1" {
|
||||
t.Error("Unexpected action", action)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user