2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-18 04:20:42 -07:00
|
|
|
|
|
|
|
// 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)
|
2014-06-01 13:50:14 -07:00
|
|
|
|
|
|
|
// Package upnp implements UPnP Internet Gateway upnpDevice port mappings
|
|
|
|
package upnp
|
2014-04-18 04:20:42 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-07-22 10:23:43 -07:00
|
|
|
"io"
|
2014-04-18 04:20:42 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IGD struct {
|
|
|
|
serviceURL string
|
2014-07-22 10:23:43 -07:00
|
|
|
device string
|
2014-04-18 04:20:42 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-28 07:04:20 -07:00
|
|
|
searchStr := `M-SEARCH * HTTP/1.1
|
2014-04-18 04:20:42 -07:00
|
|
|
Host: 239.255.255.250:1900
|
|
|
|
St: urn:schemas-upnp-org:device:InternetGatewayDevice:1
|
|
|
|
Man: "ssdp:discover"
|
|
|
|
Mx: 3
|
|
|
|
|
2014-05-28 07:04:20 -07:00
|
|
|
`
|
|
|
|
search := []byte(strings.Replace(searchStr, "\n", "\r\n", -1))
|
2014-04-18 04:20:42 -07:00
|
|
|
|
|
|
|
_, 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 {
|
2014-05-14 17:08:56 -07:00
|
|
|
l.Debugln(string(resp[:n]))
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(bytes.NewBuffer(resp[:n]))
|
|
|
|
request := &http.Request{}
|
|
|
|
response, err := http.ReadResponse(reader, request)
|
2014-05-16 10:04:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-18 04:20:42 -07:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2014-07-22 10:23:43 -07:00
|
|
|
serviceURL, device, err := getServiceURL(locURL)
|
2014-04-18 04:20:42 -07:00
|
|
|
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,
|
2014-07-22 10:23:43 -07:00
|
|
|
device: device,
|
2014-04-18 04:20:42 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-22 10:23:43 -07:00
|
|
|
func getServiceURL(rootURL string) (string, string, error) {
|
2014-04-18 04:20:42 -07:00
|
|
|
r, err := http.Get(rootURL)
|
|
|
|
if err != nil {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", err
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
if r.StatusCode >= 400 {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", errors.New(r.Status)
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
2014-07-22 10:23:43 -07:00
|
|
|
return getServiceURLReader(rootURL, r.Body)
|
|
|
|
}
|
2014-04-18 04:20:42 -07:00
|
|
|
|
2014-07-22 10:23:43 -07:00
|
|
|
func getServiceURLReader(rootURL string, r io.Reader) (string, string, error) {
|
2014-04-18 04:20:42 -07:00
|
|
|
var upnpRoot upnpRoot
|
2014-07-22 10:23:43 -07:00
|
|
|
err := xml.NewDecoder(r).Decode(&upnpRoot)
|
2014-04-18 04:20:42 -07:00
|
|
|
if err != nil {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", err
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dev := upnpRoot.Device
|
|
|
|
if dev.DeviceType != "urn:schemas-upnp-org:device:InternetGatewayDevice:1" {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", errors.New("No InternetGatewayDevice")
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dev, ok := getChildDevice(dev, "urn:schemas-upnp-org:device:WANDevice:1")
|
|
|
|
if !ok {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", errors.New("No WANDevice")
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dev, ok = getChildDevice(dev, "urn:schemas-upnp-org:device:WANConnectionDevice:1")
|
|
|
|
if !ok {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", errors.New("No WANConnectionDevice")
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
2014-07-22 10:23:43 -07:00
|
|
|
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)
|
2014-04-18 04:20:42 -07:00
|
|
|
if !ok {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", errors.New("No WANIPConnection nor WANPPPConnection")
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(svc.ControlURL) == 0 {
|
2014-07-22 10:23:43 -07:00
|
|
|
return "", "", errors.New("no controlURL")
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
u, _ := url.Parse(rootURL)
|
2014-07-02 11:28:03 -07:00
|
|
|
replaceRawPath(u, svc.ControlURL)
|
2014-07-22 10:23:43 -07:00
|
|
|
return u.String(), device, nil
|
2014-07-02 11:28:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-04-18 04:20:42 -07:00
|
|
|
} else {
|
2014-07-02 11:28:03 -07:00
|
|
|
u.Path += p
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
2014-07-02 11:28:03 -07:00
|
|
|
u.RawQuery = q
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
2014-07-22 10:23:43 -07:00
|
|
|
func soapRequest(url, device, function, message string) error {
|
2014-04-18 04:20:42 -07:00
|
|
|
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")
|
2014-07-22 10:23:43 -07:00
|
|
|
req.Header.Set("SOAPAction", fmt.Sprintf(`"%s#%s"`, device, function))
|
2014-04-18 04:20:42 -07:00
|
|
|
req.Header.Set("Connection", "Close")
|
|
|
|
req.Header.Set("Cache-Control", "no-cache")
|
|
|
|
req.Header.Set("Pragma", "no-cache")
|
|
|
|
|
|
|
|
if debug {
|
2014-05-14 17:08:56 -07:00
|
|
|
l.Debugln(req.Header.Get("SOAPAction"))
|
|
|
|
l.Debugln(body)
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
resp, _ := ioutil.ReadAll(r.Body)
|
2014-05-14 17:08:56 -07:00
|
|
|
l.Debugln(string(resp))
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-07-22 10:23:43 -07:00
|
|
|
return soapRequest(n.serviceURL, n.device, "AddPortMapping", body)
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-07-22 10:23:43 -07:00
|
|
|
return soapRequest(n.serviceURL, n.device, "DeletePortMapping", body)
|
2014-04-18 04:20:42 -07:00
|
|
|
}
|