Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize upnp Port Mapping in P2P Network by Bytom

Shulou Source: shulou.com Published: 2022-06-01 10:45:01 10月03日 Update

This article mainly introduces "how Bytom realizes P2P network upnp port mapping". In daily operation, I believe many people have doubts about how Bytom realizes P2P network upnp port mapping. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt of "how to realize P2P network upnp port mapping by Bytom"! Next, please follow the editor to study!

UPNP introduction

UPNP (Universal Plug and Play) Universal plug and play. UPNP port mapping maps an external port to an intranet ip:port. Thus, the P2P network can penetrate the gateway and access the bytomd node of the internal network from the external network.

UPNP protocol

SSDP (Simple Service Discovery Protocol simple Service Discovery Protocol) GENA (Generic Event Notification Architecture Universal event Notification structure) SOAP (Simple Object Access Protocol simple object access Protocol) XML (Extensible Markup Language Extensible markup language)

UPNP code

* * p2p/upnp/upnp.go * *

Discover devices in the network that support UPNP functionality

Find the device that supports UPNP function from the network, and get the location, url and other related information of the device.

Type upnpNAT struct {description file URL of the serviceURL string / / device Used to get the description information of the device ourIP string / / Node local ip address urnDomain string / / device type} func Discover () (nat NAT, err error) {ssdp, err: = net.ResolveUDPAddr ("udp4", "239.255.255.250 if err 1900") if err! = nil {return} conn, err: = net.ListenPacket ("udp4") ": 0") if err! = nil {return} socket: = conn. (* net.UDPConn) defer socket.Close () err = socket.SetDeadline (time.Now () .Add (3 * time.Second) if err! = nil {return} st: = "InternetGatewayDevice:1" / / Multicast request: discovery request defined by M-SEARCH SSDP protocol. Buf: = bytes.NewBufferString ("M-SEARCH * HTTP/1.1\ r\ n" + "HOST: 239.255.255.250 HTTP/1.1 1900\ r\ n" + "ST: ssdp:all\ r\ n" + "MAN:\" ssdp:discover\ "\ r\ n" + "MX: 2\ r\ n\ r\ n") message: = buf.Bytes () answerBytes: = make ([] byte 1024) for I: = 0 I < 3 I send a multicast request _, err = socket.WriteToUDP (message) to 239.255.255.250 message 1900 Ssdp) if err! = nil {return} / / if a UPNP device is found in the network, a response message var n int n, _ will be received from 239.255.255.250Rod 1900. Err = socket.ReadFromUDP (answerBytes) for {n, _ Err = socket.ReadFromUDP (answerBytes) if err! = nil {break} answer: = string (bearerBytes [0: n]) if strings.Index (answer St) < 0 {continue} / / HTTP header field names are case-insensitive. / / http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 / / get the device location locString: = "\ r\ nlocation:" answer = strings.ToLower (answer) locIndex: = strings.Index (answer) LocString) if locIndex < 0 {continue} loc: = answer [locIndex+len (locString):] endIndex: = strings.Index (loc) "\ r\ n") if endIndex < 0 {continue} / / get the device description url and device type locURL: = strings.TrimSpace (loc0: endIndex]) var serviceURL UrnDomain string serviceURL, urnDomain, err = getServiceURL (locURL) if err! = nil {return} var ourIP net.IP ourIP Err = localIPv4 () if err! = nil {return} nat = & upnpNAT {serviceURL: serviceURL, ourIP: ourIP.String (), urnDomain: urnDomain} return}} err = errors.New ("UPnP port discovery failed.") Return} add Port Mapping

Send a http post request to the upnp device to map the internal network ip:port to the external network ip:port

Func (n * upnpNAT) AddPortMapping (protocol string, externalPort, internalPort int, description string, timeout int) (mappedExternalPort int, err error) {/ / A single concatenation would break ARM compilation. Message: = "\ r\ n" + "+ strconv.Itoa (externalPort) message + =" + protocol + "message + ="+ strconv.Itoa (internalPort) +"+"+ n.ourIP +" + 1 "message + = description +"+ strconv.Itoa (timeout) + "" var response * http.Response response " Err = soapRequest (n.serviceURL, AddPortMapping, message, n.urnDomain) if response! = nil {defer response.Body.Close ()} if err! = nil {return} / / TODO: check response to see if the port was forwarded / / log.Println (message, response) / / JAE: / / body Err: = ioutil.ReadAll (response.Body) / / fmt.Println (string (body), err) mappedExternalPort = externalPort _ = response return} Delete port mapping

Send a http post request to the upnp device to remove the mapping between the internal network ip:port and the external network ip:port

Func (n * upnpNAT) DeletePortMapping (protocol string, externalPort, internalPort int) (err error) {message: = "\ r\ n" + "" + strconv.Itoa (externalPort) + "" + protocol + "" + "var response * http.Response response, err = soapRequest (n.serviceURL," DeletePortMapping ", message) N.urnDomain) if response! = nil {defer response.Body.Close ()} if err! = nil {return} / / TODO: check response to see if the port was deleted / / log.Println (message, response) _ = response return} obtain the mapped public network address func (n * upnpNAT) GetExternalAddress () (addr net.IP Err error) {info, err: = n.getExternalIPAddress () if err! = nil {return} addr = net.ParseIP (info.externalIpAddress) return} func (n * upnpNAT) getExternalIPAddress () (info statusInfo, err error) {message: = "\ r\ n" + "var response * http.Response response, err = soapRequest (n.serviceURL) "GetExternalIPAddress", message, n.urnDomain) if response! = nil {defer response.Body.Close ()} if err! = nil {return} var envelope Envelope data Err: = ioutil.ReadAll (response.Body) reader: = bytes.NewReader (data) xml.NewDecoder (reader). Decode (& envelope) info = statusInfo {envelope.Soap.ExternalIP.IPAddress} if err! = nil {return} return} so far The study on "how to realize upnp port mapping in P2P network by Bytom" is over. I hope it can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Network device port learning information function address more type node help support utility next event code object file article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi OPPO Reno vpn Shulou Tech Info