-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitiate.go
154 lines (151 loc) · 5.47 KB
/
initiate.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
func initiateConnection(ConfigFile ServerIpAddress) {
if ConfigFile.Dhcp == "true" {
fmt.Println("Setting terminal to DHCP")
output, err := exec.Command("nmcli", "con", "mod", ConfigFile.Connection, "ipv4.method", "auto").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Terminal set to DHCP: " + string(output))
} else {
fmt.Println("Setting terminal to static")
pattern := regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
if pattern.MatchString(ConfigFile.IpAddress) && pattern.MatchString(ConfigFile.Gateway) {
maskNumber := GetMaskNumberFrom(ConfigFile.Mask)
output, err := exec.Command("nmcli", "con", "mod", ConfigFile.Connection, "ipv4.addresses", ConfigFile.IpAddress+"/"+maskNumber, "ipv4.gateway", ConfigFile.Gateway).Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(output))
output, err = exec.Command("nmcli", "con", "mod", ConfigFile.Connection, "ipv4.method", "manual").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(output))
output, err = exec.Command("nmcli", "con", "down", ConfigFile.Connection).Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Terminal connection turned down: " + string(output))
output, err = exec.Command("systemctl", "restart", "NetworkManager").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Terminal connection turned up: " + string(output))
}
}
initiated = true
}
func updateConfigFile(ConfigFile ServerIpAddress) bool {
output, err := exec.Command("nmcli", "con", "show").Output()
if err != nil {
fmt.Println(err.Error())
return false
}
result := string(output)
if len(strings.Split(strings.TrimSuffix(result, "\n"), "\n")) > 2 {
fmt.Println("We have multiple available connections")
for index, line := range strings.Split(strings.TrimSuffix(result, "\n"), "\n") {
if index > 0 {
splitted := strings.Split(line, " ")
if splitted[8] != "--" && splitted[6] == "ethernet" {
fmt.Println("Active connection found, setting up connection")
ConfigFile.Connection = splitted[0] + " " + splitted[1] + " " + splitted[2]
output, err = exec.Command("mount", "-o", "remount,rw", "/ro").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(output))
configDirectory := filepath.Join("/ro", "home", "pi", "config")
configFileName := "config.json"
configFullPath := strings.Join([]string{configDirectory, configFileName}, "/")
data := ServerIpAddress{
ServerIpAddress: ConfigFile.ServerIpAddress,
IpAddress: ConfigFile.IpAddress,
Mask: ConfigFile.Mask,
Gateway: ConfigFile.Gateway,
Dhcp: ConfigFile.Dhcp,
Connection: ConfigFile.Connection,
}
file, _ := json.MarshalIndent(data, "", " ")
_ = ioutil.WriteFile(configFullPath, file, 0666)
output, err = exec.Command("mount", "-o", "remount,ro", "/ro").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Config file updated: + " + string(output))
return true
}
}
}
output, err = exec.Command("mount", "-o", "remount,rw", "/ro").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(output))
configDirectory := filepath.Join("/ro", "home", "pi", "config")
configFileName := "config.json"
configFullPath := strings.Join([]string{configDirectory, configFileName}, "/")
data := ServerIpAddress{
ServerIpAddress: ConfigFile.ServerIpAddress,
IpAddress: ConfigFile.IpAddress,
Mask: ConfigFile.Mask,
Gateway: ConfigFile.Gateway,
Dhcp: ConfigFile.Dhcp,
Connection: "",
}
file, _ := json.MarshalIndent(data, "", " ")
_ = ioutil.WriteFile(configFullPath, file, 0666)
output, err = exec.Command("mount", "-o", "remount,ro", "/ro").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Config file updated: + " + string(output))
return false
} else {
fmt.Println("We have one available connection")
for index, line := range strings.Split(strings.TrimSuffix(result, "\n"), "\n") {
if index == 1 {
splitted := strings.Split(line, " ")
if splitted[6] == "ethernet" {
fmt.Println("Setting up connection")
ConfigFile.Connection = splitted[0] + " " + splitted[1] + " " + splitted[2]
output, err = exec.Command("mount", "-o", "remount,rw", "/ro").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(output)
configDirectory := filepath.Join("/ro", "home", "pi", "config")
configFileName := "config.json"
configFullPath := strings.Join([]string{configDirectory, configFileName}, "/")
data := ServerIpAddress{
ServerIpAddress: ConfigFile.ServerIpAddress,
IpAddress: ConfigFile.IpAddress,
Mask: ConfigFile.Mask,
Gateway: ConfigFile.Gateway,
Dhcp: ConfigFile.Dhcp,
Connection: ConfigFile.Connection,
}
file, _ := json.MarshalIndent(data, "", " ")
_ = ioutil.WriteFile(configFullPath, file, 0666)
output, err = exec.Command("mount", "-o", "remount,ro", "/ro").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Config file updated: + " + string(output))
return true
}
}
}
return false
}
}