This repository was archived by the owner on Sep 15, 2018. It is now read-only.
forked from CuberL/goDrClient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
181 lines (164 loc) · 4.56 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
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
package main
import (
"encoding/base64"
"fmt"
"log"
"net"
"os"
"runtime"
"strings"
"github.com/robfig/config"
)
const (
AppName string = "gofsnet"
Version string = "0.8.4"
)
var (
GConfig Config // gofsnet configuration
InterfaceMAC net.HardwareAddr // mac address of interface
)
type Config struct {
// Account information
Username string
Password string
// Network information
ClientIP net.IP
Gateway net.IP
Netmask net.IPMask
DNS1 net.IP
DNS2 net.IP
// Device information
InterfaceName string // dev name to capture packet
// Server information
ServerIP net.IP
// preference
EnableCapture bool
EnableFileLog bool
}
// load configuration from file
//default setting is for dormitory network of scut
func loadConfig(configFile string) {
var cfg *config.Config
_, err := os.Stat(configFile)
if err == nil {
cfg, err = config.ReadDefault(configFile)
if err != nil {
log.Println(err)
}
} else {
cfg = config.NewDefault()
fmt.Println("Configuration file does not exist, create a new one:")
}
// load account info(username, password)
GConfig.Username, _ = cfg.String("account", "username")
GConfig.Password, _ = cfg.String("account", "password")
if GConfig.Username == "" || GConfig.Password == "" {
GConfig.Username, GConfig.Password = credentials()
cfg.AddOption("account", "username", GConfig.Username)
}
var appPrefix = AppName + "-"
if strings.HasPrefix(GConfig.Password, appPrefix) { // decode from masking
GConfig.Password = strings.TrimPrefix(GConfig.Password, appPrefix)
decodedPass, err := base64.StdEncoding.DecodeString(GConfig.Password)
if err != nil {
log.Println(err)
}
GConfig.Password = string(decodedPass)
} else {
encodedPass := base64.StdEncoding.EncodeToString([]byte(GConfig.Password))
cfg.AddOption("account", "password", appPrefix+encodedPass) // pass masking
}
// load device info
// TODO optimize the following codes and find the lan device automactially
ifaceName, _ := cfg.String("device", "interface")
if ifaceName == "" {
ifaces, err := listEthDevices()
if err != nil {
log.Println(err)
}
fmt.Println("Choose the network interface by entering your option")
for i, iface := range ifaces {
fmt.Printf("[%d] %s\n", i+1, iface.Name)
}
option := 0
fmt.Scan(&option)
if option >= 1 && option <= len(ifaces) {
ifaceName = ifaces[option-1].Name
cfg.AddOption("device", "interface", ifaceName)
} else {
log.Println("Bad selection input for interface!")
// os.Exit(0)
}
}
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
log.Println(err)
}
if iface == nil {
log.Println("null interface")
os.Exit(1)
}
InterfaceMAC = iface.HardwareAddr // mac address
if InterfaceMAC == nil {
log.Println("null mac")
os.Exit(1)
}
switch runtime.GOOS { // set device name
case "windows": // dev = adapter device name
adapterName, _ := getDeviceAdapterName(iface.Index)
GConfig.InterfaceName = "\\Device\\NPF_" + adapterName
default:
GConfig.InterfaceName = ifaceName
}
// Network information (optional)
addrs, err := iface.Addrs() // get client ip
if err != nil {
log.Println(err)
}
if len(addrs) == 0 {
log.Fatal("You haven't plugged the network cable yet!")
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
GConfig.ClientIP = ipnet.IP
break // only get the first one if there exists multiple addrs
}
}
}
// Authenticator Server information
serverIpStr, _ := cfg.String("server", "ip") // server ip
dns1, _ := cfg.String("server", "dns1") // dns1
dns2, _ := cfg.String("server", "dns2") // dns2
if serverIpStr == "" {
serverIpStr = "202.38.210.131"
cfg.AddOption("server", "ip", serverIpStr)
}
if dns1 == "" {
dns1 = "222.201.130.30"
cfg.AddOption("server", "dns1", dns1)
}
if dns2 == "" {
dns2 = "222.201.130.33"
cfg.AddOption("server", "dns2", dns2)
}
GConfig.ServerIP = net.ParseIP(serverIpStr)
GConfig.DNS1 = net.ParseIP(dns1)
GConfig.DNS2 = net.ParseIP(dns2)
capturePackets, _ := cfg.String("preference", "capture_packets")
fileLog, _ := cfg.String("preference", "log_to_file")
if capturePackets == "true" {
GConfig.EnableCapture = true
} else {
GConfig.EnableCapture = false
cfg.AddOption("preference", "capture_packets", "false")
}
if fileLog == "true" {
GConfig.EnableFileLog = true
} else {
GConfig.EnableFileLog = false
cfg.AddOption("preference", "log_to_file", "false")
}
// write back to configuration file
cfg.WriteFile(configFile, os.FileMode(0666), AppName+" "+Version+" Configuration")
}