-
Notifications
You must be signed in to change notification settings - Fork 9
/
makeconf.go
182 lines (170 loc) · 4.62 KB
/
makeconf.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
// This file is part of BOSSWAVE.
//
// BOSSWAVE is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// BOSSWAVE is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BOSSWAVE. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright © 2015 Michael Andersen <[email protected]>
package main
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"github.com/immesys/bw2/objects"
"github.com/immesys/bw2/util"
"github.com/urfave/cli"
)
type configparams struct {
BW2Version string
Entfile string
DBPath string
Lpath string
ListenOn string
AmLight string
MinerThreads int
Benificiary string
ExternalIP string
ListenPort int
MaxPeers int
MaxLightPeers int
}
const configTemplate = `# Generated for {{.BW2Version}}
[config]
# this is the version of config file
Version=2
[router]
# this entity is used only if you are a DR
Entity={{.Entfile}}
DB={{.DBPath}}
LogPath={{.Lpath}}
[native]
# this is for DR peering. You can set this to an
# internal IP if you are not planning on acting
# as a router
ListenOn=:4514
[oob]
# OOB clients must be trusted. It is best to leave this
# on 127.0.0.1 but if you are in a container you must
# set it to 0.0.0.0
ListenOn={{.ListenOn}}
[altruism]
# this decides how many light clients you will allow
# to connect to you.
MaxLightPeers={{.MaxLightPeers}}
# this decides what fraction of total resources can
# be spent on helping light clients
MaxLightResourcePercentage=50
[p2p]
# having more peers may increase bandwidth usage
# but also improve your sync speed
MaxPeers={{.MaxPeers}}
# Are we a light client?
IAmLight={{.AmLight}}
# What networks will we allow p2p traffic to/from
PermittedNetworks=0.0.0.0/0,::0/0
# If you are on a NAT or inside a container,
# set this to improve peering. Without this
# you can originate connections to other peers,
# but they cannot originate connections to you.
ExternalIP={{.ExternalIP}}
# This is the port to listen on for peering
# it will need the port above it, so if you are forwarding
# make sure to forward both of them. Also make sure you
# forward the same port, don't remap
Port={{.ListenPort}}
[mining]
# A nonzero value implies we will CPU mine
Threads={{.MinerThreads}}
# Where the mining ether goes.
# The 0x475b312fa8c3cdc6a770694d2929b9dc66fe0f33
# address is the 410 Reserve Bank used for funding
# paper experiments. You can check its balance
# with bw2 i reservebank
Benificiary={{.Benificiary}}
`
func makeConf(c *cli.Context) error {
fname := "bw2.ini"
if c.String("conf") != "" {
fname = c.String("conf")
}
conf, err := os.Create(fname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = conf.Chmod(0600)
if err != nil {
fmt.Println("WARN: chmod failed:", err)
}
abs, err := filepath.Abs(fname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configdir := filepath.Dir(abs)
ent := objects.CreateNewEntity("", "", nil)
wrapped := make([]byte, len(ent.GetSigningBlob())+1)
copy(wrapped[1:], ent.GetSigningBlob())
wrapped[0] = objects.ROEntityWKey
entfile := filepath.Join(configdir, "router.ent")
err = ioutil.WriteFile(entfile, wrapped, 0600)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
lpath := filepath.Join(configdir, "bw2.log")
if c.String("logfile") != "" {
lpath = c.String("logfile")
}
dbpath := filepath.Join(configdir, ".bw.db")
if c.String("dbpath") != "" {
dbpath = c.String("dbpath")
}
amlight := "false"
if c.Bool("light") {
amlight = "true"
}
listenon := "127.0.0.1:28589"
if c.Bool("listenglobal") {
listenon = "0.0.0.0:28589"
}
tmp, err := template.New("root").Parse(configTemplate)
if err != nil {
panic(err)
}
params := configparams{
BW2Version: util.BW2Version,
Entfile: entfile,
DBPath: dbpath,
Lpath: lpath,
ListenOn: listenon,
AmLight: amlight,
MinerThreads: c.Int("minerthreads"),
Benificiary: c.String("benificiary"),
ExternalIP: c.String("externalip"),
ListenPort: c.Int("listenport"),
MaxPeers: c.Int("maxpeers"),
MaxLightPeers: c.Int("maxlightpeers"),
}
err = tmp.ExecuteTemplate(conf, "root", params)
if err != nil {
panic(err)
}
err = conf.Close()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
}