forked from schollz/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·252 lines (225 loc) · 8.28 KB
/
server.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2015-2016 Zack Scholl. All rights reserved.
// Use of this source code is governed by a AGPL
// license that can be found in the LICENSE file.
// server.go handles Flag parsing and starts the Gin-Tonic webserver.
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"strings"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
)
// RuntimeArgs contains all runtime
// arguments available
var RuntimeArgs struct {
RFPort string
FilterMacFile string
ExternalIP string
Port string
ServerCRT string
ServerKey string
SourcePath string
Socket string
Cwd string
MqttServer string
MqttAdmin string
MosquittoPID string
MqttAdminPassword string
Dump string
Message string
Mqtt bool
MqttExisting bool
Svm bool
RandomForests bool
Filtering bool
FilterMacs map[string]bool
}
// VersionNum keeps track of the version
var VersionNum string
var BuildTime string
var Build string
// init initiates the paths in RuntimeArgs
func init() {
cwd, _ := os.Getwd()
RuntimeArgs.Cwd = cwd
RuntimeArgs.SourcePath = path.Join(RuntimeArgs.Cwd, "data")
RuntimeArgs.Message = ""
}
func main() {
// _, executableFile, _, _ := runtime.Caller(0) // get full path of this file
if len(Build) == 0 {
Build = "devdevdevdevdevdevdev"
}
// Bing flags for changing parameters of FIND
flag.StringVar(&RuntimeArgs.Port, "p", ":8003", "port to bind")
flag.StringVar(&RuntimeArgs.Socket, "s", "", "unix socket")
flag.StringVar(&RuntimeArgs.ServerCRT, "crt", "", "location of ssl crt")
flag.StringVar(&RuntimeArgs.ServerKey, "key", "", "location of ssl key")
flag.StringVar(&RuntimeArgs.MqttServer, "mqtt", "", "ADDRESS:PORT of mosquitto server")
flag.StringVar(&RuntimeArgs.MqttAdmin, "mqttadmin", "", "admin to read all messages")
flag.StringVar(&RuntimeArgs.MqttAdminPassword, "mqttadminpass", "", "admin to read all messages")
flag.StringVar(&RuntimeArgs.MosquittoPID, "mosquitto", "", "mosquitto PID (`pgrep mosquitto`)")
flag.StringVar(&RuntimeArgs.Dump, "dump", "", "group to dump to folder")
flag.StringVar(&RuntimeArgs.Message, "message", "", "message to display to all users")
flag.StringVar(&RuntimeArgs.SourcePath, "data", "", "path to data folder")
flag.StringVar(&RuntimeArgs.RFPort, "rf", "", "port for random forests calculations")
flag.StringVar(&RuntimeArgs.FilterMacFile, "filter", "", "JSON file for macs to filter")
flag.CommandLine.Usage = func() {
fmt.Println(`find (version ` + VersionNum + ` (` + Build[0:8] + `), built ` + BuildTime + `)
Example: 'findserver yourserver.com'
Example: 'findserver -p :8080 localhost:8080'
Example (mosquitto): 'findserver -mqtt 127.0.0.1:1883 -mqttadmin admin -mqttadminpass somepass -mosquitto ` + "`pgrep mosquitto`" + `
Options:`)
flag.CommandLine.PrintDefaults()
}
flag.Parse()
RuntimeArgs.ExternalIP = flag.Arg(0)
if RuntimeArgs.ExternalIP == "" {
RuntimeArgs.ExternalIP = GetLocalIP() + RuntimeArgs.Port
}
if RuntimeArgs.SourcePath == "" {
RuntimeArgs.SourcePath = path.Join(RuntimeArgs.Cwd, "data")
}
fmt.Println(RuntimeArgs.SourcePath)
// Check whether all the MQTT variables are passed to initiate the MQTT routines
if len(RuntimeArgs.MqttServer) > 0 && len(RuntimeArgs.MqttAdmin) > 0 && len(RuntimeArgs.MosquittoPID) > 0 {
RuntimeArgs.Mqtt = true
setupMqtt()
} else {
if len(RuntimeArgs.MqttServer) > 0 {
RuntimeArgs.Mqtt = true
RuntimeArgs.MqttExisting = true
setupMqtt()
} else {
RuntimeArgs.Mqtt = false
}
}
// Check whether random forests are used
if len(RuntimeArgs.RFPort) > 0 {
RuntimeArgs.RandomForests = true
}
// Check whether macs should be filtered
if len(RuntimeArgs.FilterMacFile) > 0 {
b, err := ioutil.ReadFile(RuntimeArgs.FilterMacFile)
if err != nil {
panic(err)
}
RuntimeArgs.FilterMacs = make(map[string]bool)
json.Unmarshal(b, &RuntimeArgs.FilterMacs)
fmt.Printf("Filtering %+v", RuntimeArgs.FilterMacs)
RuntimeArgs.Filtering = true
}
// Check whether we are just dumping the database
if len(RuntimeArgs.Dump) > 0 {
err := dumpFingerprints(strings.ToLower(RuntimeArgs.Dump))
if err == nil {
fmt.Println("Successfully dumped.")
} else {
log.Fatal(err)
}
os.Exit(1)
}
// Check if there is a message from the admin
if _, err := os.Stat(path.Join(RuntimeArgs.Cwd, "message.txt")); err == nil {
messageByte, _ := ioutil.ReadFile(path.Join(RuntimeArgs.Cwd, "message.txt"))
RuntimeArgs.Message = string(messageByte)
}
// Check whether SVM libraries are available
cmdOut, _ := exec.Command("svm-scale", "").CombinedOutput()
if len(cmdOut) == 0 {
RuntimeArgs.Svm = false
fmt.Println("SVM is not detected.")
fmt.Println(`To install:
sudo apt-get install g++
wget http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+tar.gz
tar -xvf libsvm-*.tar.gz
cd libsvm-*
make
cp svm-scale /usr/local/bin/
cp svm-predict /usr/local/bin/
cp svm-train /usr/local/bin/`)
} else {
RuntimeArgs.Svm = true
}
// Setup Gin-Gonic
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// Load templates
r.LoadHTMLGlob(path.Join(RuntimeArgs.Cwd, "templates/*"))
// Load static files (if they are not hosted by external service)
r.Static("static/", path.Join(RuntimeArgs.Cwd, "static/"))
// Create cookie store to keep track of logged in user
store := sessions.NewCookieStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
// 404-page redirects to login
r.NoRoute(func(c *gin.Context) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"ErrorMessage": "Please login first.",
})
})
// r.PUT("/message", putMessage)
// Routes for logging in and viewing dashboards (routes.go)
r.GET("/", slash)
r.GET("/login", slashLogin)
r.POST("/login", slashLoginPOST)
r.GET("/logout", slashLogout)
r.GET("/dashboard/:group", slashDashboard)
r.GET("/explore/:group/:network/:location", slashExplore2)
r.GET("/pie/:group/:network/:location", slashPie)
// Routes for performing fingerprinting (fingerprint.go)
r.POST("/learn", learnFingerprintPOST)
r.POST("/track", trackFingerprintPOST)
// Routes for MQTT (mqtt.go)
r.PUT("/mqtt", putMQTT)
// Routes for API access (api.go)
r.GET("/location", getUserLocations)
r.GET("/locations", getLocationList)
r.GET("/editname", editName)
r.GET("/editusername", editUserName)
r.GET("/editnetworkname", editNetworkName)
r.DELETE("/location", deleteLocation)
r.DELETE("/locations", deleteLocations)
r.DELETE("/user", deleteUser)
r.DELETE("/database", deleteDatabase)
r.GET("/calculate", calculate)
r.GET("/status", getStatus)
r.GET("/userlocs", userLocations) // to be deprecated
r.GET("/whereami", whereAmI) // to be deprecated
r.PUT("/mixin", putMixinOverride)
r.PUT("/cutoff", putCutoffOverride)
r.PUT("/database", migrateDatabase)
r.GET("/lastfingerprint", apiGetLastFingerprint)
// Load and display the logo
dat, _ := ioutil.ReadFile("./static/logo.txt")
fmt.Println(string(dat))
// Check whether user is providing certificates
if RuntimeArgs.Socket != "" {
r.RunUnix(RuntimeArgs.Socket)
} else if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" {
fmt.Println(`(version ` + VersionNum + ` build ` + Build[0:8] + `) is up and running on https://` + RuntimeArgs.ExternalIP)
fmt.Println("-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----")
r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey)
} else {
fmt.Println(`(version ` + VersionNum + ` build ` + Build[0:8] + `) is up and running on http://` + RuntimeArgs.ExternalIP)
fmt.Println("-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----")
r.Run(RuntimeArgs.Port)
}
}
// // putMessage usage: curl -G -X PUT "http://localhost:8003/message" --data-urlencode "text=hello world"
// func putMessage(c *gin.Context) {
// newText := c.DefaultQuery("text", "none")
// if newText != "none" {
// RuntimeArgs.Message = newText
// c.JSON(http.StatusOK, gin.H{"success": true, "message": "Message set as '" + newText + "'"})
// } else {
// c.JSON(http.StatusOK, gin.H{"success": false, "message": "Error parsing request"})
// }
// }