-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_edge.go
293 lines (269 loc) · 8.72 KB
/
main_edge.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2021 Kusakabe Si. All Rights Reserved.
*/
package main
import (
"encoding/binary"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/google/shlex"
"github.com/KusakabeSi/EtherGuard-VPN/conn"
"github.com/KusakabeSi/EtherGuard-VPN/device"
"github.com/KusakabeSi/EtherGuard-VPN/gencfg"
"github.com/KusakabeSi/EtherGuard-VPN/mtypes"
"github.com/KusakabeSi/EtherGuard-VPN/path"
"github.com/KusakabeSi/EtherGuard-VPN/tap"
yaml "gopkg.in/yaml.v2"
)
func printExampleEdgeConf() {
tconfig, _ := gencfg.GetExampleEdgeConf("", true)
toprint, _ := yaml.Marshal(tconfig)
fmt.Print(string(toprint))
}
func Edge(configPath string, useUAPI bool, printExample bool, bindmode string) (err error) {
if printExample {
printExampleEdgeConf()
return nil
}
var econfig mtypes.EdgeConfig
//printExampleConf()
//return
err = mtypes.ReadYaml(configPath, &econfig)
if err != nil {
fmt.Printf("Error read config: %v\t%v\n", configPath, err)
return err
}
NodeName := econfig.NodeName
if len(NodeName) > 32 {
return errors.New("Node name can't longer than 32 :" + NodeName)
}
var logLevel int
switch econfig.LogLevel.LogLevel {
case "verbose", "debug":
logLevel = device.LogLevelVerbose
case "error":
logLevel = device.LogLevelError
case "silent":
logLevel = device.LogLevelSilent
default:
logLevel = device.LogLevelError
}
logger := device.NewLogger(
logLevel,
fmt.Sprintf("(%s) ", NodeName),
)
if err != nil {
logger.Errorf("UAPI listen error: %v", err)
os.Exit(ExitSetupFailed)
return
}
var thetap tap.Device
// open TUN device (or use supplied fd)
switch econfig.Interface.IType {
case "dummy":
thetap, err = tap.CreateDummyTAP()
case "stdio":
thetap, err = tap.CreateStdIOTAP(econfig.Interface, econfig.NodeID)
case "udpsock":
thetap, err = tap.CreateUDPSockTAP(econfig.Interface, econfig.NodeID)
case "tcpsock":
thetap, err = tap.CreateSockTAP(econfig.Interface, "tcp", econfig.NodeID, econfig.LogLevel)
case "unixsock":
thetap, err = tap.CreateSockTAP(econfig.Interface, "unix", econfig.NodeID, econfig.LogLevel)
case "unixgramsock":
thetap, err = tap.CreateSockTAP(econfig.Interface, "unixgram", econfig.NodeID, econfig.LogLevel)
case "unixpacketsock":
thetap, err = tap.CreateSockTAP(econfig.Interface, "unixpacket", econfig.NodeID, econfig.LogLevel)
case "fd":
thetap, err = tap.CreateFdTAP(econfig.Interface, econfig.NodeID)
case "vpp":
thetap, err = tap.CreateVppTAP(econfig.Interface, econfig.NodeID, econfig.LogLevel.LogLevel)
case "tap":
thetap, err = tap.CreateTAP(econfig.Interface, econfig.NodeID)
default:
return errors.New("Unknown interface type:" + econfig.Interface.IType)
}
if err != nil {
logger.Errorf("Failed to create TAP device: %v", err)
os.Exit(ExitSetupFailed)
}
if econfig.DefaultTTL <= 0 {
return errors.New("DefaultTTL must > 0")
}
////////////////////////////////////////////////////
// Config
if !econfig.DynamicRoute.P2P.UseP2P && !econfig.DynamicRoute.SuperNode.UseSuperNode {
econfig.LogLevel.LogNTP = false // NTP in static mode is useless
}
graph, err := path.NewGraph(3, false, econfig.DynamicRoute.P2P.GraphRecalculateSetting, econfig.DynamicRoute.NTPConfig, econfig.LogLevel)
if err != nil {
return err
}
graph.SetNHTable(econfig.NextHopTable)
EnabledAf := econfig.DisableAf.Disalbed2Enabled()
the_device := device.NewDevice(thetap, econfig.NodeID, conn.NewDefaultBind(EnabledAf, bindmode, econfig.FwMark), logger, graph, false, configPath, &econfig, nil, nil, Version)
defer the_device.Close()
pk, err := device.Str2PriKey(econfig.PrivKey)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
}
the_device.SetPrivateKey(pk)
the_device.IpcSet("fwmark=" + fmt.Sprint(econfig.FwMark) + "\n")
the_device.IpcSet("listen_port=" + strconv.Itoa(econfig.ListenPort) + "\n")
the_device.IpcSet("replace_peers=true\n")
for _, peerconf := range econfig.Peers {
pk, err := device.Str2PubKey(peerconf.PubKey)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
}
the_device.NewPeer(pk, peerconf.NodeID, false, peerconf.PersistentKeepalive)
if peerconf.EndPoint != "" {
peer := the_device.LookupPeer(pk)
err = peer.SetEndpointFromConnURL(peerconf.EndPoint, EnabledAf, econfig.AfPrefer, peerconf.Static)
if err != nil {
logger.Errorf("Failed to set endpoint %v: %w", peerconf.EndPoint, err)
return err
}
}
}
if econfig.DynamicRoute.SuperNode.UseSuperNode {
S4 := true
S6 := true
if econfig.DynamicRoute.SuperNode.EndpointV4 != "" && EnabledAf.IPv4 {
pk, err := device.Str2PubKey(econfig.DynamicRoute.SuperNode.PubKeyV4)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
}
psk, err := device.Str2PSKey(econfig.DynamicRoute.SuperNode.PSKey)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
}
peer, err := the_device.NewPeer(pk, mtypes.NodeID_SuperNode, true, 0)
if err != nil {
return err
}
peer.SetPSK(psk)
StaticSuper := true
sc := econfig.DynamicRoute.SuperNode.EndpointV4
if strings.Contains(sc, ":") {
i := strings.LastIndex(sc, ":")
sch := sc[:i]
if sch == "127.0.0.1" {
StaticSuper = false
}
}
err = peer.SetEndpointFromConnURL(econfig.DynamicRoute.SuperNode.EndpointV4, EnabledAf.GetOnly4(), 0, StaticSuper)
if err != nil {
logger.Errorf("Failed to set endpoint for supernode v4 %v: %v", econfig.DynamicRoute.SuperNode.EndpointV4, err)
S4 = false
}
}
if econfig.DynamicRoute.SuperNode.EndpointV6 != "" && EnabledAf.IPv6 {
pk, err := device.Str2PubKey(econfig.DynamicRoute.SuperNode.PubKeyV6)
if err != nil {
fmt.Println("Error decode base64 ", err)
}
psk, err := device.Str2PSKey(econfig.DynamicRoute.SuperNode.PSKey)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
}
peer, err := the_device.NewPeer(pk, mtypes.NodeID_SuperNode, true, 0)
if err != nil {
return err
}
peer.SetPSK(psk)
StaticSuper := true
sc := econfig.DynamicRoute.SuperNode.EndpointV6
if strings.Contains(sc, ":") {
i := strings.LastIndex(sc, ":")
sch := sc[:i]
if sch == "[::1]" {
StaticSuper = false
}
}
err = peer.SetEndpointFromConnURL(econfig.DynamicRoute.SuperNode.EndpointV6, EnabledAf.GetOnly6(), 0, StaticSuper)
if err != nil {
logger.Errorf("Failed to set endpoint for supernode v6 %v: %v", econfig.DynamicRoute.SuperNode.EndpointV6, err)
S6 = false
}
if !(S4 || S6) {
return errors.New("failed to connect to supernode")
}
}
}
logger.Verbosef("Device started")
errs := make(chan error)
term := make(chan os.Signal, 1)
if useUAPI {
startUAPI(NodeName, logger, the_device, errs)
}
if econfig.PostScript != "" {
envs := make(map[string]string)
nid := econfig.NodeID
nid_bytearr := []byte{0, 0}
MacAddr, _ := tap.GetMacAddr(econfig.Interface.MacAddrPrefix, uint32(nid))
binary.LittleEndian.PutUint16(nid_bytearr, uint16(nid))
envs["EG_MODE"] = "edge"
envs["EG_NODE_NAME"] = econfig.NodeName
envs["EG_NODE_ID_INT_DEC"] = fmt.Sprintf("%d", nid)
envs["EG_NODE_ID_BYTE0_DEC"] = fmt.Sprintf("%d", nid_bytearr[0])
envs["EG_NODE_ID_BYTE1_DEC"] = fmt.Sprintf("%d", nid_bytearr[1])
envs["EG_NODE_ID_INT_HEX"] = fmt.Sprintf("%x", nid)
envs["EG_NODE_ID_BYTE0_HEX"] = fmt.Sprintf("%X", nid_bytearr[0])
envs["EG_NODE_ID_BYTE1_HEX"] = fmt.Sprintf("%X", nid_bytearr[1])
envs["EG_INTERFACE_NAME"] = econfig.Interface.Name
envs["EG_INTERFACE_TYPE"] = econfig.Interface.IType
envs["EG_INTERFACE_MAC_PREFIX"] = econfig.Interface.MacAddrPrefix
envs["EG_INTERFACE_MAC_ADDR"] = MacAddr.String()
cmdarg, err := shlex.Split(econfig.PostScript)
if err != nil {
return fmt.Errorf("error parse PostScript %v", err)
}
if econfig.LogLevel.LogInternal {
fmt.Printf("PostScript: exec.Command(%v)\n", cmdarg)
}
cmd := exec.Command(cmdarg[0], cmdarg[1:]...)
cmd.Env = os.Environ()
for k, v := range envs {
cmd.Env = append(cmd.Env, k+"="+v)
}
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("exec.Command(%v) failed with %v", cmdarg, err)
}
if econfig.LogLevel.LogInternal {
fmt.Printf("PostScript output: %s\n", string(out))
}
}
// wait for program to terminate
signal.Notify(term, syscall.SIGTERM)
signal.Notify(term, os.Interrupt)
the_device.Chan_Device_Initialized <- struct{}{}
mtypes.SdNotify(false, mtypes.SdNotifyReady)
SdNotify, err := mtypes.SdNotify(false, mtypes.SdNotifyReady)
if econfig.LogLevel.LogInternal {
fmt.Printf("Internal: SdNotify:%v err:%v\n", SdNotify, err)
}
select {
case <-term:
case <-errs:
case errcode := <-the_device.Wait():
if errcode != 0 {
return syscall.Errno(errcode)
}
}
logger.Verbosef("Shutting down")
return
}