upnp

package module
v0.0.0-...-5661950 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2021 License: Apache-2.0 Imports: 12 Imported by: 1

README

UPnP protocol

A simple implementation of the UPnP protocol as a Golang library. Add port mappings for NAT devices. Look for a gateway device, check if it supports UPnP, and if so, add port mappings.

Examples:

1. add a port mapping

mapping := new(upnp.Upnp)
if err := mapping.AddPortMapping(55789, 55789, "TCP"); err == nil {
	fmt.Println("success !")
	// remove port mapping in gateway
	mapping.Reclaim()
} else {
	fmt.Println("failed:", err.Error())
}

2. search gateway device.

upnpMan := new(upnp.Upnp)
err := upnpMan.SearchGateway()
if err != nil {
	fmt.Println(err.Error())
} else {
	fmt.Println("local ip address: ", upnpMan.LocalHost)
	fmt.Println("gateway ip address: ", upnpMan.Gateway.Host)
}

3. get an internet ip address in gateway.

upnpMan := new(upnp.Upnp)
err := upnpMan.ExternalIPAddr()
if err != nil {
	fmt.Println(err.Error())
} else {
	fmt.Println("internet ip address: ", upnpMan.GatewayOutsideIP)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocalIPs

func GetLocalIPs() ([]*net.IP, error)

This returns the list of local ip addresses which other hosts can connect to (NOTE: Loopback ip is ignored).

func GetLocalIntenetIp

func GetLocalIntenetIp() string

GetLocalIntenetIp returns all local addresses, checks internet connectivity

Types

type AddPortMapping

type AddPortMapping struct {
	// contains filtered or unexported fields
}

func (*AddPortMapping) Send

func (this *AddPortMapping) Send(localPort, remotePort, duration int, internalClient string, protocol string, desc string) bool

type DelPortMapping

type DelPortMapping struct {
	// contains filtered or unexported fields
}

func (*DelPortMapping) Send

func (this *DelPortMapping) Send(remotePort int, protocol string) bool

Send sends a request to delete a port mapping

type DeviceDesc

type DeviceDesc struct {
	// contains filtered or unexported fields
}

func (*DeviceDesc) BuildRequest

func (this *DeviceDesc) BuildRequest() *http.Request

func (*DeviceDesc) Send

func (this *DeviceDesc) Send() bool

type ExternalIPAddress

type ExternalIPAddress struct {
	// contains filtered or unexported fields
}

func (*ExternalIPAddress) BuildRequest

func (this *ExternalIPAddress) BuildRequest() *http.Request

func (*ExternalIPAddress) Send

func (this *ExternalIPAddress) Send() bool

type Gateway

type Gateway struct {
	GatewayName   string
	Host          string
	DeviceDescUrl string
	Cache         string
	ST            string
	USN           string

	ControlURL  string
	ServiceType string
	// contains filtered or unexported fields
}

type MappingPortStruct

type MappingPortStruct struct {
	// contains filtered or unexported fields
}

Manage all Ports

func (*MappingPortStruct) GetAllMapping

func (this *MappingPortStruct) GetAllMapping() map[string][][]int

type Node

type Node struct {
	Name    string
	Content string
	Attr    map[string]string
	Child   []Node
}

func (*Node) AddChild

func (n *Node) AddChild(node Node)

func (*Node) BuildXML

func (n *Node) BuildXML() string

type SearchGateway

type SearchGateway struct {
	// contains filtered or unexported fields
}

func (*SearchGateway) Send

func (this *SearchGateway) Send() bool

type SearchGatewayReq

type SearchGatewayReq struct {
	// contains filtered or unexported fields
}

func (SearchGatewayReq) BuildRequest

func (this SearchGatewayReq) BuildRequest() *http.Request

func (SearchGatewayReq) Send

func (this SearchGatewayReq) Send()

type Upnp

type Upnp struct {
	Active              bool // is this upnp protocol available?
	DurationUnsupported bool
	LocalHost           string            // local (our) IP Address
	GatewayInsideIP     string            // LAN Gateway IP
	GatewayOutsideIP    string            // Gateway Public IP
	OutsideMappingPort  map[string]int    // Map the external port
	InsideMappingPort   map[string]int    // Map local port
	Gateway             *Gateway          // Gateway information
	CtrlUrl             string            // Control request URL
	MappingPort         MappingPortStruct // Existing mappings, e.g {"TCP":[1990],"UDP":[1991]}
}

func (*Upnp) AddPortMapping

func (this *Upnp) AddPortMapping(localPort, remotePort, duration int, internalClient string, protocol string, desc string) (err error)

AddPortMapping adds a port mapping TODO: accept an IP address to port forward to another LAN host(internalClient)

func (*Upnp) DelPortMapping

func (this *Upnp) DelPortMapping(remotePort int, protocol string) bool

DelPortMapping probably deletes a port mapping

func (*Upnp) ExternalIPAddr

func (this *Upnp) ExternalIPAddr() (err error)

ExternalIPAddr gets our external IP address

func (*Upnp) GetAllMapping

func (this *Upnp) GetAllMapping() map[string][][]int

GetAllMapping returns all active mappings TODO: for this host only?

func (*Upnp) Reclaim

func (this *Upnp) Reclaim()

Reclaim recycles (deletes?) a port

func (*Upnp) SearchGateway

func (this *Upnp) SearchGateway() (err error)

SearchGateway gets the Gateway's LAN IP address

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL