forked from cruzbit/cruzbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upnp.go
51 lines (43 loc) · 1007 Bytes
/
upnp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package cruzbit
import (
"gitlab.com/NebulousLabs/go-upnp"
)
// HandlePortForward manages port (un)forwarding using UPnP.
func HandlePortForward(port uint16, fwd bool) (string, bool, error) {
// discover router
router, err := upnp.Discover()
if err != nil {
return "", false, err
}
if fwd {
// discover external IP
ip, err := router.ExternalIP()
if err != nil {
return "", false, err
}
// forward the port
err = router.Forward(port, "cruzbit")
if err != nil {
return "", false, err
}
// check the port
ok, err := router.IsForwardedTCP(port)
if err != nil {
return "", false, err
}
return ip, ok, nil
}
// clear the port
err = router.Clear(port)
if err != nil {
return "", false, err
}
// check the port
ok, err := router.IsForwardedTCP(port)
if err != nil {
return "", false, err
}
return "", !ok, nil
}