forked from meshbird/meshbird_gg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (119 loc) · 2.62 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
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/gophergala2016/meshbird/common"
"github.com/gophergala2016/meshbird/secure"
"log"
"net"
"os"
"os/signal"
"time"
)
const (
MeshbirdKeyEnv = "MESHBIRD_KEY"
)
var (
// VERSION var using for auto versioning through Go linker
VERSION = "dev"
logger = log.New(os.Stderr, "[main] ", log.LstdFlags)
)
func main() {
app := cli.NewApp()
app.Name = "MeshBird"
app.Usage = "distributed private networking"
app.Version = VERSION
app.Commands = []cli.Command{
{
Name: "new",
Aliases: []string{"n"},
Usage: "create new network",
Action: actionNew,
ArgsUsage: "<key>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "CIDR",
Value: "10.7.0.0/16",
Usage: "Define custom CIDR",
},
},
},
{
Name: "join",
Aliases: []string{"j"},
Usage: "join network",
Action: actionJoin,
},
{
Name: "ip",
Aliases: []string{"i"},
Usage: "init state",
Action: actionGetIP,
ArgsUsage: "<key>",
},
}
err := app.Run(os.Args)
if err != nil {
logger.Printf("error: %s", err)
}
}
func actionGetIP(ctx *cli.Context) {
keyStr := os.Getenv(MeshbirdKeyEnv)
if keyStr == "" {
logger.Fatalf("environment variable %s is not specified", MeshbirdKeyEnv)
}
secret, err := secure.NetworkSecretUnmarshal(keyStr)
if err != nil {
logger.Fatal(err)
}
state := common.NewState(secret)
state.Save()
fmt.Println(state.PrivateIP)
}
func actionNew(ctx *cli.Context) {
var secret *secure.NetworkSecret
var err error
if len(ctx.Args()) > 0 {
keyStr := ctx.Args().First()
secret, err = secure.NetworkSecretUnmarshal(keyStr)
if err != nil {
logger.Fatal(err)
}
} else {
_, ipnet, err := net.ParseCIDR(ctx.String("CIDR"))
if err != nil {
logger.Fatalf("cidr parse error: %s", err)
}
secret = secure.NewNetworkSecret(ipnet)
}
keyStr := secret.Marshal()
logger.Printf("key: %s", keyStr)
}
func actionJoin(ctx *cli.Context) {
key := os.Getenv(MeshbirdKeyEnv)
if key == "" {
logger.Fatalf("environment variable %s is not specified", MeshbirdKeyEnv)
}
nodeConfig := &common.Config{
SecretKey: key,
}
node, err := common.NewLocalNode(nodeConfig)
if err != nil {
logger.Fatalf("local node init error: %s", err)
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, os.Kill)
defer signal.Stop(signalChan)
go func() {
s := <-signalChan
logger.Printf("received signal %s, stopping...", s)
node.Stop()
time.Sleep(2 * time.Second)
os.Exit(0)
}()
err = node.Start()
if err != nil {
logger.Fatalf("node start error: %s", err)
}
node.WaitStop()
}