-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwss.go
More file actions
61 lines (53 loc) 路 1.09 KB
/
Copy pathwss.go
File metadata and controls
61 lines (53 loc) 路 1.09 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
package main
import (
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
gonanoid "github.com/matoous/go-nanoid"
)
var upgrader = websocket.Upgrader{
HandshakeTimeout: timeout,
CheckOrigin: func(r *http.Request) bool {
// Open to all origins
return true
},
}
func StartWSS() {
http.Handle("/", http.RedirectHandler(
"https://btidor.dev/content-security-policy", 307,
))
http.HandleFunc("/ws", WSSHandler)
go func() {
server := &http.Server{Addr: ":80"}
err := server.ListenAndServe()
panic(err)
}()
}
func WSSHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
id, err := gonanoid.Generate(idchars, 12)
if err != nil {
log.Println(err)
return
}
mutex.Lock()
defer mutex.Unlock()
clients[id] = conn
conn.WriteJSON(map[string]string{"id": id})
go func() {
time.Sleep(timeout)
mutex.Lock()
defer mutex.Unlock()
if conn, ok := clients[id]; ok {
conn.WriteControl(websocket.CloseMessage,
[]byte{}, time.Now().Add(timeout))
conn.Close()
delete(clients, id)
}
}()
}