mirror of
https://github.com/inverse-inc/upnp.git
synced 2025-12-24 12:12:37 +08:00
translate some of the library to english
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
## upnp protocol
|
||||
====
|
||||
|
||||
A simple implements UPnP protocol for Golang library. Add port mapping for NAT devices.
|
||||
A simple implementation of the UPnP protocol as a Golang library. Add port mappings for NAT devices.
|
||||
|
||||
在内网中搜索网关设备,检查网关是否支持upnp协议,若支持,添加端口映射。
|
||||
Look for a gateway device, check if it supports UPnP, and if so, add port mappings.
|
||||
|
||||
====
|
||||
|
||||
|
||||
@@ -62,26 +62,26 @@ func (this *SearchGateway) send(c chan string) {
|
||||
}(conn)
|
||||
remotAddr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
|
||||
if err != nil {
|
||||
log.Println("组播地址格式不正确")
|
||||
log.Println("The multicast address format is incorrect")
|
||||
}
|
||||
locaAddr, err := net.ResolveUDPAddr("udp", this.upnp.LocalHost+":")
|
||||
|
||||
if err != nil {
|
||||
log.Println("本地ip地址格式不正确")
|
||||
log.Println("The local ip address is not in the correct format")
|
||||
}
|
||||
conn, err = net.ListenUDP("udp", locaAddr)
|
||||
defer conn.Close()
|
||||
if err != nil {
|
||||
log.Println("监听udp出错")
|
||||
log.Println("Failed to bind udp multicast socket")
|
||||
}
|
||||
_, err = conn.WriteToUDP([]byte(this.searchMessage), remotAddr)
|
||||
if err != nil {
|
||||
log.Println("发送msg到组播地址出错")
|
||||
log.Println("An error occurred sending msg to the multicast address")
|
||||
}
|
||||
buf := make([]byte, 1024)
|
||||
n, _, err := conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
log.Println("从组播地址接搜消息出错")
|
||||
log.Println("An error occurred reading the multicast address search message")
|
||||
}
|
||||
|
||||
result := string(buf[:n])
|
||||
|
||||
@@ -16,7 +16,7 @@ func GetLocalIntenetIp() string {
|
||||
|
||||
conn, err := net.Dial("udp", "google.com:80")
|
||||
if err != nil {
|
||||
panic(errors.New("不能连接网络"))
|
||||
panic(errors.New("Can not connect to network"))
|
||||
}
|
||||
defer conn.Close()
|
||||
return strings.Split(conn.LocalAddr().String(), ":")[0]
|
||||
|
||||
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/prestonTao/upnp"
|
||||
"github.com/scottjg/upnp"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -25,10 +25,10 @@ func main() {
|
||||
|
||||
func Start() {
|
||||
if !CheckNet() {
|
||||
fmt.Println("你的路由器不支持upnp协议")
|
||||
fmt.Println("Your router does not support the UPnP protocol.")
|
||||
return
|
||||
}
|
||||
fmt.Println("本机ip地址:", mapping.LocalHost)
|
||||
fmt.Println("Local IP Address: ", mapping.LocalHost)
|
||||
|
||||
ExternalIPAddr()
|
||||
|
||||
@@ -41,11 +41,11 @@ tag:
|
||||
}
|
||||
|
||||
fmt.Println("--------------------------------------")
|
||||
fmt.Println("1. stop 停止程序并回收映射的端口")
|
||||
fmt.Println("2. add 添加一个端口映射")
|
||||
fmt.Println("3. del 手动删除一个端口映射")
|
||||
fmt.Println("\n 注意:此程序映射的端口默认是TCP端口")
|
||||
fmt.Println(" 需要映射udp端口请访问:")
|
||||
fmt.Println("1. stop stop the program and reclaim mapped port")
|
||||
fmt.Println("2. add add a port mapping")
|
||||
fmt.Println("3. del manually delete a port mapping")
|
||||
fmt.Println("\n NOTE: This progrma maps tcp ports. If you need to")
|
||||
fmt.Println(" map a UDP port, please visit:")
|
||||
fmt.Println(" http://github.com/prestonTao/upnp")
|
||||
fmt.Println("--------------------------------------")
|
||||
|
||||
@@ -92,7 +92,7 @@ func ExternalIPAddr() {
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Println("外网ip地址为:", mapping.GatewayOutsideIP)
|
||||
fmt.Println("External network IP address: ", mapping.GatewayOutsideIP)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,27 +101,27 @@ func ExternalIPAddr() {
|
||||
*/
|
||||
func GetInput() bool {
|
||||
var err error
|
||||
fmt.Println("请输入要映射的本地端口:")
|
||||
fmt.Println("Enter the local port to map: ")
|
||||
data, _, _ := reader.ReadLine()
|
||||
localPort, err = strconv.Atoi(string(data))
|
||||
if err != nil {
|
||||
fmt.Println("输入的端口号错误,请输入 0-65535 的数字")
|
||||
fmt.Println("Invalid port, please specify a valid port between 0-65535")
|
||||
return false
|
||||
}
|
||||
if localPort < 0 || localPort > 65535 {
|
||||
fmt.Println("输入的端口号错误,请输入 0-65535 的数字")
|
||||
fmt.Println("Invalid port, please specify a valid port between 0-65535")
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Println("请输入要映射到外网的端口:")
|
||||
fmt.Println("Please enter the external port to be mapped:")
|
||||
data, _, _ = reader.ReadLine()
|
||||
remotePort, err = strconv.Atoi(string(data))
|
||||
if err != nil {
|
||||
fmt.Println("输入的端口号错误,请输入 0-65535 的数字")
|
||||
fmt.Println("Invalid port, please specify a valid port between 0-65535")
|
||||
return false
|
||||
}
|
||||
if remotePort < 0 || remotePort > 65535 {
|
||||
fmt.Println("输入的端口号错误,请输入 0-65535 的数字")
|
||||
fmt.Println("Invalid port, please specify a valid port between 0-65535")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -133,10 +133,10 @@ func GetInput() bool {
|
||||
func AddPortMapping(localPort, remotePort int) bool {
|
||||
//添加一个端口映射
|
||||
if err := mapping.AddPortMapping(localPort, remotePort, "TCP"); err == nil {
|
||||
fmt.Println("端口映射成功")
|
||||
fmt.Println("Port mapped successfully")
|
||||
return true
|
||||
} else {
|
||||
fmt.Println("端口映射失败")
|
||||
fmt.Println("Port failed to map")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
// "bufio"
|
||||
"fmt"
|
||||
"github.com/prestonTao/upnp"
|
||||
"github.com/scottjg/upnp"
|
||||
// "os"
|
||||
)
|
||||
|
||||
@@ -20,8 +20,8 @@ func SearchGateway() {
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Println("本机ip地址:", upnpMan.LocalHost)
|
||||
fmt.Println("upnp设备地址:", upnpMan.Gateway.Host)
|
||||
fmt.Println("Local IP Address:", upnpMan.LocalHost)
|
||||
fmt.Println("UPNP Device IP Address:", upnpMan.Gateway.Host)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func ExternalIPAddr() {
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Println("外网ip地址为:", upnpMan.GatewayOutsideIP)
|
||||
fmt.Println("External Network IP Address:", upnpMan.GatewayOutsideIP)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,10 +40,10 @@ func ExternalIPAddr() {
|
||||
func AddPortMapping() {
|
||||
mapping := new(upnp.Upnp)
|
||||
if err := mapping.AddPortMapping(55789, 55789, "TCP"); err == nil {
|
||||
fmt.Println("端口映射成功")
|
||||
fmt.Println("Port mapping succeeded.")
|
||||
mapping.Reclaim()
|
||||
} else {
|
||||
fmt.Println("端口映射失败")
|
||||
fmt.Println("Port mapping failed.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func simple1() {
|
||||
}
|
||||
|
||||
func searchDevice(localAddr, remoteAddr string) string {
|
||||
fmt.Println("搜素网关设备")
|
||||
fmt.Println("Searching for gateway device...")
|
||||
msg := "M-SEARCH * HTTP/1.1\r\n" +
|
||||
"HOST: 239.255.255.250:1900\r\n" +
|
||||
"ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n" +
|
||||
@@ -99,7 +99,7 @@ func searchDevice(localAddr, remoteAddr string) string {
|
||||
}
|
||||
|
||||
func readDeviceDesc(rAddr string) string {
|
||||
fmt.Println("查看设备描述")
|
||||
fmt.Println("Fetching device description...")
|
||||
msg := "GET /igd.xml HTTP/1.1\r\n" +
|
||||
"User-Agent: Java/1.7.0_45\r\n" +
|
||||
"Host: 192.168.1.1:1900\r\n" +
|
||||
@@ -148,7 +148,7 @@ func readDeviceDesc(rAddr string) string {
|
||||
|
||||
func getDeviceStatusInfo(rAddr string) {
|
||||
|
||||
fmt.Println("查看设备状态")
|
||||
fmt.Println("Fetching device status...")
|
||||
|
||||
readMappingBody := `<?xml version="1.0"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
||||
@@ -182,7 +182,7 @@ func getDeviceStatusInfo(rAddr string) {
|
||||
|
||||
func addPortMapping(rAddr string) {
|
||||
|
||||
fmt.Println("添加一个端口映射")
|
||||
fmt.Println("Adding a port mapping...")
|
||||
|
||||
readMappingBody := `<?xml version="1.0"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
||||
@@ -225,7 +225,7 @@ func addPortMapping(rAddr string) {
|
||||
}
|
||||
|
||||
func remotePort(rAddr string) {
|
||||
fmt.Println("删除一个端口映射")
|
||||
fmt.Println("Deleting a port mapping...")
|
||||
|
||||
readMappingBody := `<?xml version="1.0"?>
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
|
||||
设备描述回复
|
||||
Device description reply
|
||||
|
||||
<root xmlns="urn:schemas-upnp-org:device-1-0">
|
||||
<specVersion>
|
||||
@@ -81,7 +81,7 @@ e>
|
||||
</root>
|
||||
|
||||
|
||||
设备状态 请求
|
||||
Device status request
|
||||
|
||||
POST /ipc HTTP/1.1
|
||||
Content-Type: text/xml
|
||||
@@ -101,7 +101,7 @@ Content-Length: 311
|
||||
</SOAP-ENV:Envelope>
|
||||
|
||||
|
||||
设备状态 回复
|
||||
Device status reply
|
||||
|
||||
HTTP/1.1 200 OK
|
||||
CONNECTION: close
|
||||
@@ -122,7 +122,7 @@ CONTENT-TYPE: text/xml; charset="utf-8"
|
||||
|
||||
|
||||
|
||||
请求已经映射的端口号列表
|
||||
Requests a list of port numbers that have been mapped
|
||||
|
||||
POST /ipc HTTP/1.1
|
||||
Content-Type: text/xml
|
||||
@@ -144,7 +144,7 @@ Content-Length: 343
|
||||
|
||||
|
||||
|
||||
返回已经映射到端口号列表
|
||||
Returns a list that has been mapped to the port number
|
||||
|
||||
HTTP/1.1 500 Internal Server Error
|
||||
CONNECTION: close
|
||||
@@ -171,7 +171,7 @@ SERVER: Wireless N Router WR845N, UPnP/1.0
|
||||
|
||||
|
||||
|
||||
查看普通端口列表 请求
|
||||
View the normal port list request
|
||||
|
||||
POST /ipc HTTP/1.1
|
||||
Content-Type: text/xml
|
||||
@@ -193,7 +193,7 @@ Content-Length: 381
|
||||
|
||||
|
||||
|
||||
回复
|
||||
Reply
|
||||
|
||||
HTTP/1.1 500 Internal Server Error
|
||||
CONNECTION: close
|
||||
@@ -219,7 +219,7 @@ SERVER: Wireless N Router WR845N, UPnP/1.0
|
||||
</SOAP-ENV:Envelope>
|
||||
|
||||
|
||||
获得外网ip地址 请求
|
||||
Access to external network ip address request
|
||||
|
||||
POST /ipc HTTP/1.1
|
||||
Content-Type: text/xml
|
||||
@@ -239,7 +239,7 @@ Content-Length: 325
|
||||
</SOAP-ENV:Envelope>
|
||||
|
||||
|
||||
获得外网ip地址 回复
|
||||
Access to external network ip address reply
|
||||
|
||||
HTTP/1.1 200 OK
|
||||
CONNECTION: close
|
||||
@@ -261,7 +261,7 @@ CONTENT-TYPE: text/xml; charset="utf-8"
|
||||
|
||||
|
||||
|
||||
得到明确的端口映射列表 请求
|
||||
Obtain an explicit port map list request
|
||||
|
||||
POST /ipc HTTP/1.1
|
||||
Content-Type: text/xml
|
||||
@@ -285,7 +285,7 @@ Content-Length: 439
|
||||
</SOAP-ENV:Envelope>
|
||||
|
||||
|
||||
回复
|
||||
Reply
|
||||
|
||||
HTTP/1.1 500 Internal Server Error
|
||||
CONNECTION: close
|
||||
@@ -312,7 +312,7 @@ SERVER: Wireless N Router WR845N, UPnP/1.0
|
||||
|
||||
|
||||
|
||||
添加端口映射 请求
|
||||
Add a port mapping request
|
||||
|
||||
POST /ipc HTTP/1.1
|
||||
Content-Type: text/xml
|
||||
@@ -339,7 +339,7 @@ Content-Length: 627
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>
|
||||
|
||||
回复
|
||||
Reply
|
||||
|
||||
HTTP/1.1 200 OK
|
||||
CONNECTION: close
|
||||
@@ -354,7 +354,7 @@ CONTENT-TYPE: text/xml; charset="utf-8"
|
||||
</SOAP-ENV:Envelope>
|
||||
|
||||
|
||||
删除端口映射 请求
|
||||
Remove the port mapping request
|
||||
|
||||
POST /ipc HTTP/1.1
|
||||
Content-Type: text/xml
|
||||
@@ -378,7 +378,7 @@ Content-Length: 419
|
||||
|
||||
|
||||
|
||||
回复
|
||||
Reply
|
||||
|
||||
HTTP/1.1 200 OK
|
||||
CONNECTION: close
|
||||
8
upnp.go
8
upnp.go
@@ -104,7 +104,7 @@ func (this *Upnp) SearchGateway() (err error) {
|
||||
if searchGateway.Send() {
|
||||
return nil
|
||||
}
|
||||
return errors.New("未发现网关设备")
|
||||
return errors.New("No gateway device found")
|
||||
}
|
||||
|
||||
func (this *Upnp) deviceStatus() {
|
||||
@@ -142,7 +142,7 @@ func (this *Upnp) ExternalIPAddr() (err error) {
|
||||
func (this *Upnp) AddPortMapping(localPort, remotePort int, protocol string) (err error) {
|
||||
defer func(err error) {
|
||||
if errTemp := recover(); errTemp != nil {
|
||||
log.Println("upnp模块报错了", errTemp)
|
||||
log.Println("upnp module being given", errTemp)
|
||||
err = errTemp.(error)
|
||||
}
|
||||
}(err)
|
||||
@@ -159,7 +159,7 @@ func (this *Upnp) AddPortMapping(localPort, remotePort int, protocol string) (er
|
||||
} else {
|
||||
this.Active = false
|
||||
// log.Println("添加一个端口映射失败")
|
||||
return errors.New("添加一个端口映射失败")
|
||||
return errors.New("Adding a port mapping failed")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ func (this *Upnp) DelPortMapping(remotePort int, protocol string) bool {
|
||||
issuccess := delMapping.Send(remotePort, protocol)
|
||||
if issuccess {
|
||||
this.MappingPort.delMapping(remotePort, protocol)
|
||||
log.Println("删除了一个端口映射: remote:", remotePort)
|
||||
log.Println("Removed a port mapping: remote:", remotePort)
|
||||
}
|
||||
return issuccess
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user