Repository navigation
Expand file tree
/
Copy pathvalidation.go
More file actions
175 lines (147 loc) · 4.21 KB
/
Copy pathvalidation.go
File metadata and controls
175 lines (147 loc) · 4.21 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2022 The CortexTheseus Authors
// This file is part of the CortexTheseus library.
//
// The CortexTheseus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The CortexTheseus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the CortexTheseus library. If not, see <http://www.gnu.org/licenses/>
package wormhole
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/CortexFoundation/CortexTheseus/log"
)
func (wh *Wormhole) healthCheck(s string) error {
log.Debug("Global best trackers", "url", s)
switch {
case strings.HasPrefix(s, "http"), strings.HasPrefix(s, "https"):
if err := checkHTTPTracker(s); err != nil {
log.Debug("tracker failed", "err", err)
return err
}
return nil
case strings.HasPrefix(s, "udp"):
u, err := url.Parse(s)
if err != nil {
return err
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
return err
}
if err := checkUDPTracker(net.JoinHostPort(host, port)); err != nil {
log.Debug("UDP ping err", "s", s, "err", err)
return err
}
return nil
default:
log.Warn("Other protocols trackers", "s", s)
return errors.New("invalid url protocol")
}
}
func random20Bytes() []byte {
b := make([]byte, 20)
rand.Read(b)
return b
}
func buildAnnounceURL(base string) (string, error) {
parsed, err := url.Parse(base)
if err != nil {
return "", err
}
infoHash := random20Bytes()
peerID := []byte("-GT0001-" + hex.EncodeToString(random20Bytes())[:12])
values := url.Values{
"info_hash": {string(infoHash)},
"peer_id": {string(peerID)},
"port": {"6881"},
"uploaded": {"0"},
"downloaded": {"0"},
"left": {"0"},
"compact": {"1"},
"event": {"started"},
}
parsed.RawQuery = values.Encode()
return parsed.String(), nil
}
func checkHTTPTracker(base string) error {
fullURL, err := buildAnnounceURL(base)
if err != nil {
return fmt.Errorf("invalid tracker URL: %w", err)
}
client := http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get(fullURL)
if err != nil {
return fmt.Errorf("HTTP request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected HTTP status: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read tracker response: %w", err)
}
if bytes.Contains(body, []byte("failure reason")) {
return fmt.Errorf("tracker responded with failure")
}
return nil
}
const (
udpTimeout = 5 * time.Second
actionConnect = 0
protocolID = 0x41727101980
)
func checkUDPTracker(trackerHostPort string) error {
host, port, err := net.SplitHostPort(trackerHostPort)
if err != nil {
return fmt.Errorf("invalid tracker URL: %w", err)
}
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(host, port))
if err != nil {
return fmt.Errorf("resolve failed: %w", err)
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return fmt.Errorf("dial failed: %w", err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(udpTimeout))
transactionID := make([]byte, 4)
rand.Read(transactionID)
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, uint64(protocolID))
binary.Write(&buf, binary.BigEndian, uint32(actionConnect))
buf.Write(transactionID)
if _, err = conn.Write(buf.Bytes()); err != nil {
return fmt.Errorf("send connect request failed: %w", err)
}
resp := make([]byte, 16)
if _, err = conn.Read(resp); err != nil {
return fmt.Errorf("connect response failed: %w", err)
}
if len(resp) < 16 || binary.BigEndian.Uint32(resp[:4]) != actionConnect || !bytes.Equal(resp[4:8], transactionID) {
return fmt.Errorf("invalid connect response")
}
return nil
}