-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.go
executable file
·206 lines (171 loc) · 5.87 KB
/
routes.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
package main
import (
//"strconv"
"fmt"
"net/http"
"github.com/gorilla/mux"
"encoding/json"
"net/http/pprof"
// "../middleware"
//"../models"
// "../sessions"
// "../utils"
)
func NewRouter() *mux.Router {
r := mux.NewRouter()
//r.HandleFunc("/", AuthRequired(indexGetHandler)).Methods("GET")
r.HandleFunc("/", indexGetHandler).Methods("GET")
//r.HandleFunc("/info", infoGetHandler).Methods("GET")
r.HandleFunc("/info", AuthRequired(infoGetHandler)).Methods("GET")
r.HandleFunc("/sniffers", AuthRequired(SniffersGetHandler)).Methods("GET")
//r.HandleFunc("/sniffers", SniffersGetHandler).Methods("GET")
r.HandleFunc("/login", loginGetHandler).Methods("GET")
// pprof debug
//r.HandleFunc("/debug/pprof", pprof.Profile)
r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
//r.HandleFunc("/login", loginPostHandler).Methods("POST")
//r.HandleFunc("/logout", logoutGetHandler).Methods("GET")
//r.HandleFunc("/register", registerGetHandler).Methods("GET")
//r.HandleFunc("/register", registerPostHandler).Methods("POST")
//fs := http.FileServer(http.Dir("./static/"))
//r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
//r.HandleFunc("/{username}",
// middleware.AuthRequired(userGetHandler)).Methods("GET")
return r
}
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there !\n This is a sniffer's collector (named Talky).")
}
func infoGetHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "some statistics will be here\n")
}
func loginGetHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Auth is Required!")
}
// func SniffersGetHandler(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json")
// main_cache.Lock()
// snifs := main_cache.snifs
// type keyvalue map[string]string
// keyvalueslice := make([]keyvalue,0)
// for _,vi := range snifs {
// data := map[string]string{
// "id": vi.id, //strconv.FormatInt(vi.id, 10),
// "ip": vi.ip, // strconv.FormatInt(vi.ip, 10),
// "macs": strconv.Itoa(len(vi.macs)),
// "post_macs": strconv.FormatInt(vi.post_macs, 10),
// "new_macs": strconv.FormatInt(vi.new_macs, 10),
// "pps": strconv.FormatInt(vi.pps, 10),
// "pct_cnt": strconv.FormatInt(vi.packets_period, 10),
// "total_cnt": strconv.FormatInt(vi.packets_total, 10),
// "ts_first": strconv.FormatInt(vi.first_time, 10),
// "ts_last": strconv.FormatInt(vi.update_time/1000000000, 10),
// //"ts_fist": vi.first_time,
// //"ts_last": vi.update_time,
// }
// keyvalueslice = append(keyvalueslice,data)
// }
// main_cache.Unlock()
// j, err := json.Marshal(keyvalueslice)
// if err != nil{
// log.Error("json err: ",err)
// } else { w.Write(j) }
// }
func SniffersGetHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
snifs := GetSnifs()
result,err := json.Marshal(snifs)
if err == nil{
w.Write(result)
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
}
// func userGetHandler(w http.ResponseWriter, r *http.Request) {
// session, _ := sessions.Store.Get(r, "session")
// untypedUserId := session.Values["user_id"]
// currentUserId, ok := untypedUserId.(int64)
// if !ok {
// utils.InternalServerError(w)
// return
// }
// vars := mux.Vars(r)
// username := vars["username"]
// user, err := models.GetUserByUsername(username)
// if err != nil {
// utils.InternalServerError(w)
// return
// }
// userId, err := user.GetId()
// if err != nil {
// utils.InternalServerError(w)
// return
// }
// updates, err := models.GetUpdates(userId)
// if err != nil {
// utils.InternalServerError(w)
// return
// }
// utils.ExecuteTemplate(w, "index.html", struct {
// Title string
// Updates []*models.Update
// DisplayForm bool
// } {
// Title: username,
// Updates: updates,
// DisplayForm: currentUserId == userId,
// })
// }
// func loginGetHandler(w http.ResponseWriter, r *http.Request) {
// utils.ExecuteTemplate(w, "login.html", nil)
// }
// func loginPostHandler(w http.ResponseWriter, r *http.Request) {
// r.ParseForm()
// username := r.PostForm.Get("nickname")
// password := r.PostForm.Get("password")
// user, err := models.AuthenticateUser(username, password)
// if err != nil {
// switch err {
// case models.ErrUserNotFound:
// utils.ExecuteTemplate(w, "auth/login.html", "unknown user")
// case models.ErrInvalidLogin:
// utils.ExecuteTemplate(w, "auth/login.html", "invalid login")
// default:
// utils.InternalServerError(w)
// }
// return
// }
// userId, err := user.GetId()
// if err != nil {
// utils.InternalServerError(w)
// return
// }
// session, _ := sessions.Store.Get(r, "session")
// session.Values["user_id"] = userId
// session.Save(r, w)
// http.Redirect(w, r, "/", 302)
// }
// func logoutGetHandler(w http.ResponseWriter, r *http.Request) {
// session, _ := sessions.Store.Get(r, "session")
// delete(session.Values, "user_id")
// session.Save(r, w)
// http.Redirect(w, r, "/login", 302)
// }
// func registerGetHandler(w http.ResponseWriter, r *http.Request) {
// utils.ExecuteTemplate(w, "register.html", nil)
// }
// func registerPostHandler(w http.ResponseWriter, r *http.Request) {
// r.ParseForm()
// username := r.PostForm.Get("username")
// password := r.PostForm.Get("password")
// err := models.RegisterUser(username, password)
// if err == models.ErrUsernameTaken {
// utils.ExecuteTemplate(w, "register.html", "username taken")
// return
// } else if err != nil {
// utils.InternalServerError(w)
// return
// }
// http.Redirect(w, r, "/login", 302)
// }