This repository has been archived by the owner on Oct 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (65 loc) · 1.72 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"text/template"
"golang.org/x/net/websocket"
)
var sockets = make(map[string]*websocket.Conn)
func fishHandler(ws *websocket.Conn) {
id := ws.RemoteAddr().String() + "-" + ws.Request().RemoteAddr + "-" + ws.Request().UserAgent()
sockets[id] = ws
// Wait here
log.Println(id, "is waiting")
msg := make([]byte, 512)
_, err := ws.Read(msg)
if err != nil {
// Client should exit
log.Println(id, "client disconnect", err)
delete(sockets, id)
}
}
func hook(w http.ResponseWriter, r *http.Request) {
log.Println("Request from", r.RemoteAddr)
r.ParseForm()
_, err := url.ParseRequestURI(r.FormValue("webhook"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Println(r.Form)
fmt.Fprintf(w, "Hooking %d client(s) with %s", len(r.Form["m"]), r.FormValue("webhook"))
for _, id := range r.Form["m"] {
if ws, ok := sockets[id]; ok {
_, err := ws.Write([]byte(r.FormValue("webhook")))
if err != nil {
log.Fatal("Error whilst writing", err)
} else {
log.Println("Successfully hooked", id)
}
} else {
log.Println(id, "not waiting")
}
}
}
func main() {
http.Handle("/fish", websocket.Handler(fishHandler))
http.HandleFunc("/hook/", hook)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "templates/client.html")
})
http.HandleFunc("/admin/", func(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/admin.html")
keys := make([]string, 0, len(sockets))
for k := range sockets {
keys = append(keys, k)
}
t.Execute(w, keys)
})
err := http.ListenAndServe(":80", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}