-
Notifications
You must be signed in to change notification settings - Fork 1
/
net-info.go
140 lines (129 loc) · 2.76 KB
/
net-info.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
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"log"
"net"
"context"
"time"
shell "github.com/ipfs/go-ipfs-api"
)
type NetInfo struct {
ID string
IPS []net.IPNet
Port []int
PublishAddrs []string
}
/*
* Setup
* This will populate the struct with
* the local IP's and exclude the
* localhost
*/
func (ni *NetInfo) Setup() error {
if len(ni.PublishAddrs) != 0 {
return nil
}
// first get local addresses
err := ni.my_addresses()
// now populate with IPFS addresses
sh := shell.NewShell("localhost:5001")
foo, err := sh.ID()
if err != nil {
return(err)
}
ni.ID = foo.ID
return ni.ipfs_addresses(foo.Addresses)
}
/*
* local_host
* This will check if the IP is on one of our
* local networks
*/
func (ni *NetInfo) local_host(ip net.IP) bool {
for _, f := range(ni.IPS) {
if f.Contains(ip) == true {
return true
}
}
return false
}
/*
* my_addresses
* does what it sounds like it does. It
* will grab all local addresses and save
* them.
*/
func (ni *NetInfo) my_addresses() error {
ifaces, err := net.Interfaces()
if err != nil {
return(err)
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
return(err)
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if v.IP.IsLoopback() != true {
ni.IPS = append(ni.IPS, *v)
}
}
}
}
return nil
}
/*
* IPFSAddresses
* We need to loop through our addresses and only add
* announceable ones that are on our subnet and exclude
* localhost
*/
func (ni *NetInfo) ipfs_addresses(a []string) error {
var foo IPFSAddr
for _, f := range a {
err := foo.Parse(f)
if err != nil {
return err
}
if ni.HaveIP(foo.IP) {
ni.PublishAddrs = append(ni.PublishAddrs, f)
ni.Port = append(ni.Port, foo.Port)
}
}
return nil
}
/*
* HaveIP
* This is a utility function to see if we have an
* address in our network (in other words exclude
* public NAT'd addresses
*/
func (ni *NetInfo) HaveIP(ip net.IP) bool {
for _,i := range ni.IPS {
if(i.IP.IsLoopback() != true) && (i.IP.Equal(ip) == true) {
return true
}
}
return false
}
/*
* AddHost
* This will check to see if the address is on one of
* our local subnets. If so, it will then call IPFS
* swarm connect so we can connect to the host
*/
func (ni *NetInfo) AddHost(h IPFSAddr) (error) {
if (ni.local_host(h.IP) == true) && (ni.HaveIP(h.IP) == false) {
log.Print("Adding: ", h.URL)
sh := shell.NewShell("localhost:5001")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
err := sh.SwarmConnect(ctx, h.URL)
if err != nil {
log.Print(err)
return err
}
}
return nil
}