Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
150 lines (124 loc) · 3.17 KB
/
Copy pathmain.go
File metadata and controls
150 lines (124 loc) · 3.17 KB
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
package main
import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
type Connection struct {
Timestamp time.Time
IP string
Method string
Params map[string]string
}
type WebhookServer struct {
mux sync.Mutex
Connections []Connection
}
func GenerateRandomPath() string {
rand.Seed(time.Now().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
path := make([]byte, 10)
for i := range path {
path[i] = chars[rand.Intn(len(chars))]
}
return string(path)
}
func (wh *WebhookServer) LogConnection(r *http.Request) {
wh.mux.Lock()
defer wh.mux.Unlock()
ip := r.Header.Get("X-Real-IP")
if ip == "" {
ip = r.RemoteAddr
}
params := make(map[string]string)
for key, values := range r.URL.Query() {
params[key] = strings.Join(values, ", ")
}
connection := Connection{
Timestamp: time.Now(),
IP: ip,
Method: r.Method,
Params: params,
}
wh.Connections = append(wh.Connections, connection)
for client := range clients {
client.sendUpdate(connection)
}
}
func (wh *WebhookServer) HomeHandler(w http.ResponseWriter, r *http.Request) {
webhookURL := fmt.Sprintf("http://%s%s", r.Host, "/webhook")
data := struct {
WebhookURL string
Connections []Connection
}{
WebhookURL: webhookURL,
Connections: wh.Connections,
}
tmpl, err := template.ParseFiles("index.html")
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type Client struct {
conn *websocket.Conn
}
func (c *Client) sendUpdate(connection Connection) {
err := c.conn.WriteJSON(connection)
if err != nil {
log.Println("Error sending WebSocket update:", err)
}
}
var clients = make(map[*Client]bool)
var clientsMutex sync.Mutex
func WebSocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Error upgrading WebSocket connection:", err)
return
}
defer conn.Close()
client := &Client{conn: conn}
clientsMutex.Lock()
clients[client] = true
clientsMutex.Unlock()
for {
_, _, err := conn.ReadMessage()
if err != nil {
break
}
}
clientsMutex.Lock()
delete(clients, client)
clientsMutex.Unlock()
}
func (wh *WebhookServer) WebhookHandler(w http.ResponseWriter, r *http.Request) {
wh.LogConnection(r)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Request received successfully"))
}
func main() {
wh := &WebhookServer{}
http.HandleFunc("/", wh.HomeHandler)
http.HandleFunc("/webhook", wh.WebhookHandler)
http.HandleFunc("/ws", WebSocketHandler)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
log.Println("Webhook server started on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}