forked from jphackworth/kestrel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
132 lines (115 loc) · 3.45 KB
/
config.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
// Copyright 2014 JPH <jph@hackworth.be>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
"github.com/nsjph/cryptoauth"
"io/ioutil"
"net"
"os"
)
// type tomlConfig struct {
// Server serverConfig
// }
// type serverConfig struct {
// Listen string `toml:"listen"`
// Device string `toml:"device"`
// PublicKey string `toml:"public_key"`
// PrivateKey string `toml:"private_key"`
// IPv6 string `toml:"ipv6"`
// }
// getApp is used to configure the command-line defaults, arguments and flags of kestrel
//
// It uses the cli package from https://github.com/codegangsta/cli
func getApp() *cli.App {
app := cli.NewApp()
app.Name = "kestrel"
app.Usage = "An experimental cjdns router"
app.Author = "JPH"
app.Email = "jph@hackworth.be"
app.Version = "0.1.0a"
app.Commands = []cli.Command{
{
Name: "genconf",
Usage: "generate a new config",
Action: func(c *cli.Context) {
generateConfig()
},
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "/etc/kestrel/kestrel.toml",
Usage: "Config file location",
},
}
app.Action = func(c *cli.Context) {
//config := readConfigFile(c.String("config"))
run(c.String("config"))
println("hi!")
}
return app
}
// Reads the kestrel config file and decodes into tomlConfig
func readConfigFile(configPath string) TomlConfig {
configData, err := ioutil.ReadFile(configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error %s\n", err)
os.Exit(1)
}
var conf TomlConfig
_, err = toml.Decode(string(configData), &conf)
check(err)
return conf
}
// Checks that the config has the minimum info to start kestrel:
// - public key
// - private key
// - valid ipv6
// - valid listen address
func validateConfig(config TomlConfig) {
}
// Creates a new kestrel config and prints to stdout
func generateConfig() error {
conf := TomlConfig{}
keys, err := cryptoauth.NewIdentityKeys()
checkFatal(err)
conf.Server.IPv6 = keys.IPv6.String()
conf.Server.PublicKey = fmt.Sprintf("%s.k", cryptoauth.Base32Encode(keys.PublicKey[:])[:52])
conf.Server.PrivateKey = hex.EncodeToString(keys.PrivateKey[:])
conf.Server.Listen = generateListenAddress()
conf.Server.Device = "tun1"
buf := new(bytes.Buffer)
err = toml.NewEncoder(buf).Encode(conf)
checkFatal(err)
fmt.Println(buf.String())
return nil
}
// Creates a local UDP IPv4 address host:port combination for kestrel to use
//
// By default, set to listen on all IPv4 interfaces (0.0.0.0)
// Will attempt to listen on a random high port temporarily and will assign this if successful.
//
// Returns a string in the format of "host:port", for example "0.0.0.0:30000"
func generateListenAddress() (listenAddress string) {
addr, err := net.ResolveUDPAddr("udp4", "0.0.0.0:0")
check(err)
conn, err := net.ListenUDP("udp4", addr)
check(err)
listenAddress = conn.LocalAddr().String()
conn.Close()
return listenAddress
}