-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
133 lines (122 loc) · 3.42 KB
/
websocket.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
package main
import (
"context"
"encoding/binary"
"encoding/hex"
"log"
"net"
"net/http"
"strconv"
"time"
"zeroleaks/bittorrent"
"zeroleaks/utils"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
)
const WS_LOG_TAG = "Websocket server:"
const DNS_LEAK_TESTS_NUMBER = 6
type dnsLeakTestParams struct {
Base string `json:"base"`
Subdomains []string `json:"subdomains"`
}
type IPSender struct {
ws *websocket.Conn
ctx context.Context
timeout time.Duration
ch chan net.IP
Callback func(net.IP)
}
func NewIPSender(ws *websocket.Conn, ctx context.Context, timeout time.Duration) *IPSender {
ch := make(chan net.IP, 32)
return &IPSender{
ws: ws,
ctx: ctx,
timeout: timeout,
ch: ch,
Callback: func(ip net.IP) {
select {
case ch <- ip:
default:
// websocket connection closed
}
},
}
}
func (s *IPSender) Start() {
go func() {
time.Sleep(s.timeout)
s.ch <- nil
}()
ipSet := make(map[string]struct{})
for {
ip := <-s.ch
if ip == nil { // timeout
s.ws.Close(websocket.StatusNormalClosure, "")
break
} else {
ipStr := ip.String()
if _, ok := ipSet[ipStr]; !ok {
ipSet[ipStr] = struct{}{}
if err := s.ws.Write(s.ctx, websocket.MessageText, []byte(ipStr)); err != nil {
log.Println(WS_LOG_TAG, "failed to send IP:", err.Error())
}
}
}
}
}
func dnsLeakTest(ws *websocket.Conn) {
random := utils.RandomBytes(4 * DNS_LEAK_TESTS_NUMBER)
ctx := context.Background()
ws.CloseRead(ctx)
params := dnsLeakTestParams{
Base: conf.DNS.Domain,
Subdomains: make([]string, 0, DNS_LEAK_TESTS_NUMBER),
}
ipSender := NewIPSender(ws, ctx, conf.DNS.Timeout)
for i := 0; i < DNS_LEAK_TESTS_NUMBER; i++ {
s := binary.LittleEndian.Uint32(random[i : i+4])
params.Subdomains = append(params.Subdomains, strconv.FormatUint(uint64(s), 10))
dnsServer.RegisterCallback(s, ipSender.Callback)
}
if err := wsjson.Write(ctx, ws, params); err != nil {
log.Println(WS_LOG_TAG, "failed to send DNS params:", err.Error())
ws.CloseNow()
return
}
go ipSender.Start()
}
func bittorrentLeakTest(ws *websocket.Conn) {
infoHash := bittorrent.InfoHash(utils.RandomBytes(20))
ctx := context.Background()
ws.CloseRead(ctx)
ipSender := NewIPSender(ws, ctx, conf.BitTorrent.Timeout)
bittorrentTracker.RegisterCallback(infoHash, ipSender.Callback)
magnetLink := "magnet:?xt=urn:btih:" + hex.EncodeToString(infoHash[:]) + "&tr=udp://" + conf.Host + ":" + strconv.FormatInt(int64(bittorrentTrackerPort), 10)
if err := ws.Write(ctx, websocket.MessageText, []byte(magnetLink)); err != nil {
log.Println(WS_LOG_TAG, "failed to send magnet link:", err)
ws.CloseNow()
return
}
go ipSender.Start()
}
func startWebsocketServer(addr string, tls TLSConfig, options websocket.AcceptOptions) {
acceptWebsocket := func(callback func(*websocket.Conn)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Accept(w, r, &options)
if err != nil {
log.Println(WS_LOG_TAG, "failed to accept:", err.Error())
return
}
callback(ws)
}
}
http.HandleFunc("/v1/dns", acceptWebsocket(dnsLeakTest))
http.HandleFunc("/v1/bittorrent", acceptWebsocket(bittorrentLeakTest))
var err error
if tls.Cert == "" && tls.Key == "" {
err = http.ListenAndServe(addr, nil)
} else {
err = http.ListenAndServeTLS(addr, tls.Cert, tls.Key, nil)
}
log.Fatalln(WS_LOG_TAG, "failed to start:", err)
}