-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
230 lines (201 loc) · 5.46 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//go:build !windows
package main
import (
"encoding/hex"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"github.com/jessevdk/go-flags"
"github.com/lorenzosaino/go-sysctl"
"github.com/omani/nkn-link/config"
"github.com/omani/nkn-link/tun"
"github.com/songgao/packets/ethernet"
"github.com/vishvananda/netlink"
)
var opts struct {
// Client bool `short:"c" long:"client" description:"Client mode"`
// Server bool `short:"s" long:"server" description:"Server mode"`
ConfigFile string `short:"f" long:"config-file" default:"config.yaml" description:"Config file path"`
Debug bool `long:"debug" description:"Enable debug mode"`
Version bool `long:"version" description:"Print version"`
}
func main() {
defer func() {
if r := recover(); r != nil {
log.Fatalf("Panic: %+v", r)
}
}()
_, err := flags.Parse(&opts)
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
log.Fatal(err)
}
if opts.Version {
fmt.Println(opts.Version)
os.Exit(0)
}
conf, err := config.NewConfig(opts.ConfigFile)
if err != nil {
log.Fatal(err)
}
seed, err := hex.DecodeString(conf.NKNAccountSeed)
if err != nil {
log.Fatal(err)
}
// open NKN account with given seed
account, err := conf.NewAccount(seed)
if err != nil {
log.Fatal(err)
}
conf.Set("nkn_account_seed", hex.EncodeToString(account.Seed()))
// create new NKN multiclient
client, err := conf.NewMultiClient(account, config.IDENTIFIER, 1, true)
if err != nil {
log.Fatal(err)
}
defer client.Close()
tun_device, err := tun.CreateTUN(conf.TunDeviceName, config.DefaultMTU)
if err != nil {
log.Fatal(err)
}
defer tun_device.Close()
tun_device_name, err := tun_device.Name()
if err != nil {
log.Fatal(err)
}
tun_link, err := netlink.LinkByName(tun_device_name)
if err != nil {
log.Fatal(err)
}
// set IP address of new TUN device
addr, _ := netlink.ParseAddr(conf.TunDeviceIPAddress)
netlink.AddrAdd(tun_link, addr)
netlink.LinkSetUp(tun_link)
defer netlink.LinkDel(tun_link)
// don't care about the previous setting of ip_forward on this machine. just set if true.
if conf.EnableIPForwarding {
sysctl.Set("net.ipv4.ip_forward", "1")
}
var routes []netlink.Route
// if `gateway` cli argument is set, change routing table accordingly.
if conf.DefaultRouteEnable {
gateway := conf.DefaultRouteGatewayAddress
if len(gateway) > 0 {
routelist, err := netlink.RouteList(nil, 4)
if err != nil {
log.Fatal(err)
}
// fetch default route (in linux it is always the most top (first) route in the route table)
netlink.RouteDel(&routelist[0])
// lower its metric so our new default route is higher
routelist[0].Priority = 100
netlink.RouteAdd(&routelist[0])
// now add a new default route with our remote peer as the gateway
routes = append(routes, netlink.Route{
LinkIndex: tun_link.Attrs().Index,
Dst: &net.IPNet{
IP: net.IPv4(0, 0, 0, 0),
Mask: net.CIDRMask(0, 32),
},
Src: net.ParseIP(conf.TunDeviceIPAddress),
Priority: 0,
Table: routelist[0].Table,
Gw: net.ParseIP(gateway),
})
// iterate over node updates
// keep already established rpc node connection available via current gateway
go func() {
// add all populated routes
for _, route := range routes {
if err := netlink.RouteAdd(&route); err != nil {
log.Fatal(err)
}
}
for {
conn := <-client.OnConnect.C
rpc_node := strings.Split(conn.Addr, ":")[0]
route := netlink.Route{
LinkIndex: routelist[0].LinkIndex,
Dst: &net.IPNet{
IP: net.ParseIP(rpc_node),
Mask: net.CIDRMask(32, 32),
},
Src: routelist[0].Src,
Priority: routelist[0].Priority,
Table: routelist[0].Table,
Gw: routelist[0].Gw,
}
if err := netlink.RouteAdd(&route); err != nil {
log.Fatal(err)
}
routes = append(routes, route)
}
}()
} else {
log.Println("`default_route_gateway_address` not set in config.yaml. Skipping.")
}
}
// handle SIGINT
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("Cleanup.")
for _, route := range routes {
netlink.RouteDel(&route)
}
fmt.Println("Exiting.")
os.Exit(1)
}()
var tx_frame ethernet.Frame
var rx_frame ethernet.Frame
// sender (tx)
go func() {
for {
tx_frame.Resize(1500)
n, err := tun_device.Read([]byte(tx_frame), 0)
if err != nil {
log.Fatal(err)
}
tx_frame = tx_frame[:n]
if opts.Debug {
fmt.Println("----------------SENDING----------------")
log.Printf("Dst: %s\n", tx_frame.Destination())
log.Printf("Src: %s\n", tx_frame.Source())
log.Printf("Ethertype: % x\n", tx_frame.Ethertype())
log.Printf("Payload: % x\n", tx_frame.Payload())
fmt.Printf("----------------------------------------\n\n")
}
_, err = client.Send(
conf.GetNKNRemotePeer(),
[]byte(tx_frame),
nil,
)
if err != nil {
log.Fatal(err)
}
}
}()
// receiver (rx)
for {
msg := <-client.OnMessage.C
rx_frame = msg.Data
if opts.Debug {
fmt.Println("----------------RECEIVED----------------")
log.Printf("Dst: %s\n", rx_frame.Destination())
log.Printf("Src: %s\n", rx_frame.Source())
log.Printf("Ethertype: % x\n", rx_frame.Ethertype())
log.Printf("Payload: % x\n", rx_frame.Payload())
fmt.Printf("-----------------------------------------\n\n")
}
_, err := tun_device.Write([]byte(rx_frame), 0)
if err != nil {
log.Fatal(err)
}
}
}