forked from kewka/give-me-bnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
91 lines (80 loc) · 1.69 KB
/
socket.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
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/gorilla/websocket"
"golang.org/x/sync/errgroup"
)
func NewSocketTransaction(
ctx context.Context,
socketUrl string,
captcha string,
account string,
proxy string,
) (string, error) {
dialer := websocket.Dialer{}
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err != nil {
return "", err
}
dialer.Proxy = http.ProxyURL(proxyUrl)
}
conn, _, err := dialer.Dial(socketUrl, nil)
if err != nil {
return "", err
}
defer conn.Close()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return requestSocketBnb(conn, captcha, account)
})
var ret string
g.Go(func() error {
var err error
ret, err = waitSocketTransaction(ctx, conn, account)
return err
})
return ret, g.Wait()
}
func requestSocketBnb(conn *websocket.Conn, captcha string, account string) error {
return conn.WriteJSON(map[string]interface{}{
"url": account,
"symbol": "BNB",
"tier": 0,
"captcha": captcha,
})
}
type SocketMessage struct {
Error *string `json:"error"`
Requests []struct {
Account string `json:"account"`
Tx struct {
Hash string `json:"hash"`
} `json:"tx"`
} `json:"requests"`
}
func waitSocketTransaction(ctx context.Context, conn *websocket.Conn, account string) (string, error) {
for {
select {
case <-ctx.Done():
return "", ctx.Err()
default:
msg := SocketMessage{}
if err := conn.ReadJSON(&msg); err != nil {
return "", err
}
if msg.Error != nil {
return "", fmt.Errorf("faucet error: %v", *msg.Error)
}
for _, r := range msg.Requests {
if strings.EqualFold(r.Account, account) {
return r.Tx.Hash, nil
}
}
}
}
}