/
p2p.go
127 lines (115 loc) · 3.4 KB
/
p2p.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package debugapi
import (
"bytes"
"encoding/hex"
"net/http"
"time"
"github.com/FavorLabs/favorX/pkg/boson"
"github.com/FavorLabs/favorX/pkg/crypto"
"github.com/FavorLabs/favorX/pkg/jsonhttp"
"github.com/FavorLabs/favorX/pkg/logging"
"github.com/multiformats/go-multiaddr"
)
type PublicIP struct {
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
}
type addressesResponse struct {
Overlay boson.Address `json:"overlay"`
Underlay []multiaddr.Multiaddr `json:"underlay"`
NATRoute []string `json:"nat_route"`
PublicIP PublicIP `json:"public_ip"`
NetworkID uint64 `json:"network_id"`
PublicKey string `json:"public_key"`
}
func GetPublicIp(logger logging.Logger) *PublicIP {
var (
ip4ServiceUrl = "https://api.ipify.org"
ip6ServiceUrl = "https://api6.ipify.org"
ip4Content = new(bytes.Buffer)
ip6Content = new(bytes.Buffer)
)
client := http.Client{
Timeout: time.Second * 2,
}
ip4Resp, err := client.Get(ip4ServiceUrl)
if err != nil {
logger.Debugf("debug api: p2p request public ipv4: %v", err)
} else {
if ip4Resp.StatusCode < 200 || ip4Resp.StatusCode >= 300 {
logger.Debugf("debug api: http service(%s) report http code %s(%d)", ip4ServiceUrl, ip4Resp.Status, ip4Resp.StatusCode)
} else {
_, err = ip4Content.ReadFrom(ip4Resp.Body)
if err != nil {
logger.Debugf("debug api: p2p parse ipv4 service response: %v", err)
}
}
}
ip6Resp, err := client.Get(ip6ServiceUrl)
if err != nil {
logger.Debugf("debug api: p2p request public ipv6: %v", err)
} else {
if ip6Resp.StatusCode < 200 || ip6Resp.StatusCode >= 300 {
logger.Debugf("debug api: http service(%s) report http code %s(%d)", ip6ServiceUrl, ip6Resp.Status, ip6Resp.StatusCode)
} else {
_, err = ip6Content.ReadFrom(ip6Resp.Body)
if err != nil {
logger.Debugf("debug api: p2p parse ipv6 service response: %v", err)
}
}
}
return &PublicIP{IPv4: ip4Content.String(), IPv6: ip6Content.String()}
}
var pubIP = &PublicIP{
IPv4: "",
IPv6: "",
}
func (s *Service) addressesHandler(w http.ResponseWriter, r *http.Request) {
// initialize variable to json encode as [] instead null if p2p is nil
underlay := make([]multiaddr.Multiaddr, 0)
natAddresses := make([]string, 0)
// addresses endpoint is exposed before p2p service is configured
// to provide information about other addresses.
if s.p2p != nil {
u, err := s.p2p.Addresses()
if err != nil {
s.logger.Debugf("debug api: p2p addresses: %v", err)
jsonhttp.InternalServerError(w, err)
return
}
underlay = u
n, err := s.p2p.NATAddresses()
if err != nil {
s.logger.Debugf("debug api: p2p nat addresses: %v", err)
jsonhttp.InternalServerError(w, err)
return
}
for _, a := range n {
natAddresses = append(natAddresses, a.String())
}
}
key := "debugapi_addressesHandler"
get, _ := s.cache.Get(s.cacheCtx, key)
if get == nil {
ch := make(chan struct{}, 1)
go func(ch chan struct{}) {
ip := GetPublicIp(s.logger)
pubIP.IPv6 = ip.IPv6
pubIP.IPv4 = ip.IPv4
_ = s.cache.Set(s.cacheCtx, key, 1, time.Second*5)
close(ch)
}(ch)
select {
case <-time.After(time.Millisecond * 200):
case <-ch:
}
}
jsonhttp.OK(w, addressesResponse{
Overlay: s.overlay,
Underlay: underlay,
NATRoute: natAddresses,
PublicIP: *pubIP,
NetworkID: s.nodeOptions.NetworkID,
PublicKey: hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(&s.publicKey)),
})
}