-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathstart.go
231 lines (211 loc) · 6.09 KB
/
start.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
package main
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
ptp "github.com/subutai-io/p2p/lib"
)
// CommandStart will create new P2P instance
func CommandStart(restPort int, ip, hash, mac, dev, keyfile, key, ttl string, fwd bool, port int) {
args := &DaemonArgs{}
args.IP = ip
if hash == "" {
fmt.Fprintln(os.Stderr, "Hash cannot be empty. Please start new instances with -hash VALUE argument")
os.Exit(12)
}
if strings.Index(hash, "~") != -1 {
fmt.Fprintln(os.Stderr, "Hash cannot contain the ~. Please start new instances with hash value that doesn't contain it")
os.Exit(17)
}
args.Hash = hash
if mac != "" {
_, err := net.ParseMAC(mac)
if err != nil {
fmt.Fprintln(os.Stderr, "Invalid MAC address provided")
os.Exit(13)
}
}
args.Mac = mac
args.Dev = dev
args.Keyfile = keyfile
args.Key = key
args.TTL = ttl
args.Fwd = fwd
args.Port = port
out, err := sendRequest(restPort, "start", args)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if out.Code > 0 {
fmt.Fprintln(os.Stderr, out.Message)
} else {
fmt.Println(out.Message)
}
os.Exit(out.Code)
}
func (d *Daemon) execRESTStart(w http.ResponseWriter, r *http.Request) {
if !ReadyToServe {
resp, _ := getResponse(105, "P2P Daemon is in initialization state")
w.Write(resp)
return
}
if !bootstrap.isActive {
resp, _ := getResponse(106, "Not connected to DHT nodes")
w.Write(resp)
return
}
if bootstrap.ip == "" {
resp, _ := getResponse(107, "Didn't received outbound IP yet")
w.Write(resp)
return
}
args := new(DaemonArgs)
err := getJSON(r.Body, args)
if handleMarshalError(err, w) != nil {
return
}
ptp.Log(ptp.Debug, "Executing start command: %+v", args)
response := new(Response)
err = d.run(&RunArgs{
IP: args.IP,
Mac: args.Mac,
Dev: args.Dev,
Hash: args.Hash,
Dht: args.Dht,
Keyfile: args.Keyfile,
Key: args.Key,
TTL: args.TTL,
Fwd: args.Fwd,
Port: args.Port,
}, response)
ls, _ := time.Unix(0, 0).MarshalText()
// We add new save entry. If save entry already exists with
// hash specified, we will just update it's last success timestamp
if d.Restore.addEntry(saveEntry{
IP: args.IP,
Mac: args.Mac,
Dev: args.Dev,
Hash: args.Hash,
Keyfile: args.Keyfile,
Key: args.Key,
TTL: args.TTL,
LastSuccess: string(ls),
Enabled: true,
}) != nil {
d.Restore.bumpInstance(args.Hash)
}
err = d.Restore.save()
if err != nil {
ptp.Log(ptp.Error, "Failed to save instance information: %s", err.Error())
}
resp, err := getResponse(response.ExitCode, response.Output)
if err != nil {
ptp.Log(ptp.Error, "Internal error: %s", err)
return
}
w.Write(resp)
}
// Run starts a P2P instance
func (d *Daemon) run(args *RunArgs, resp *Response) error {
resp.ExitCode = 0
resp.Output = "Running new P2P instance for " + args.Hash + "\n"
ptp.Log(ptp.Trace, "Requested new P2P instance: %+v", args)
// Validate if interface name is unique
if args.Dev != "" {
instances := d.Instances.get()
for _, inst := range instances {
if inst.PTP.Interface.GetName() == args.Dev {
resp.ExitCode = 1
resp.Output = "Device with specified name is already in use"
return errors.New(resp.Output)
}
}
}
inst := d.Instances.getInstance(args.Hash)
if inst == nil {
resp.Output = resp.Output + "Lookup finished\n"
if args.Key != "" {
if len(args.Key) < 16 {
args.Key += "0000000000000000"[:16-len(args.Key)]
} else if len(args.Key) > 16 && len(args.Key) < 24 {
args.Key += "000000000000000000000000"[:24-len(args.Key)]
} else if len(args.Key) > 24 && len(args.Key) < 32 {
args.Key += "00000000000000000000000000000000"[:32-len(args.Key)]
} else if len(args.Key) > 32 {
args.Key = args.Key[:32]
}
}
newInst := new(P2PInstance)
newInst.ID = args.Hash
newInst.Args = *args
newInst.PTP = ptp.New(args.Mac, args.Hash, args.Keyfile, args.Key, args.TTL, TargetURL, args.Fwd, args.Port, OutboundIP)
if newInst.PTP == nil {
resp.Output = resp.Output + "Failed to create P2P Instance"
resp.ExitCode = 1
return errors.New("Failed to create P2P Instance")
}
err := bootstrap.registerInstance(newInst.ID, newInst)
if err != nil {
ptp.Log(ptp.Error, "Failed to register instance with bootstrap nodes: %s", err.Error())
if newInst.PTP != nil {
newInst.PTP.Close()
newInst.PTP = nil
}
resp.Output = resp.Output + "Failed to register instance: %s" + err.Error()
resp.ExitCode = 601
return errors.New("Failed to register instance")
}
go newInst.PTP.ReadDHT()
newInst.PTP.Dht.LocalPort = newInst.PTP.UDPSocket.GetPort()
newInst.PTP.FindNetworkAddresses()
err = newInst.PTP.Dht.Connect(newInst.PTP.LocalIPs, newInst.PTP.ProxyManager.GetList())
if err != nil {
if newInst.PTP != nil {
newInst.PTP.Close()
newInst.PTP = nil
}
bootstrap.unregisterInstance(newInst.ID)
resp.Output = resp.Output + err.Error()
resp.ExitCode = 602
return err
}
err = newInst.PTP.PrepareInterfaces(args.IP, args.Dev)
if err != nil {
ptp.Log(ptp.Error, "Failed to configure network interface: %s", err)
if newInst.PTP != nil {
newInst.PTP.Close()
newInst.PTP = nil
}
bootstrap.unregisterInstance(newInst.ID)
resp.Output = resp.Output + "Failed to configure network: " + err.Error()
resp.ExitCode = 603
return errors.New("Failed to configure network interface")
}
go newInst.PTP.ListenInterface()
// Saving interface name
infFound := false
for _, inf := range InterfaceNames {
if inf == newInst.PTP.Interface.GetName() {
infFound = true
}
}
if !infFound && newInst.PTP.Interface.GetName() != "" {
InterfaceNames = append(InterfaceNames, newInst.PTP.Interface.GetName())
}
usedIPs = append(usedIPs, newInst.PTP.Interface.GetIP().String())
ptp.Log(ptp.Info, "Instance created")
newInst.Args.LastSuccess = time.Now()
d.Instances.update(args.Hash, newInst)
go newInst.PTP.Run()
resp.Output = resp.Output + "Instance created: " + args.Hash + "\n"
} else {
resp.ExitCode = 119
resp.Output = resp.Output + "Hash already in use\n"
}
return nil
}