Repository navigation
Expand file tree
/
Copy pathquick_tunnel.go
More file actions
91 lines (84 loc) · 2.62 KB
/
Copy pathquick_tunnel.go
File metadata and controls
91 lines (84 loc) · 2.62 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
package cloudflared
import (
"context"
"io"
"net"
"net/http"
"strings"
"time"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/google/uuid"
)
const (
quickTunnelService = "https://api.trycloudflare.com/tunnel"
quickTunnelTimeout = 15 * time.Second
)
type QuickTunnel struct {
ID uuid.UUID `json:"id"`
Hostname string `json:"hostname"`
AccountTag string `json:"account_tag"`
Secret []byte `json:"secret"`
}
func (t QuickTunnel) Credentials() Credentials {
return Credentials{
AccountTag: t.AccountTag,
TunnelSecret: t.Secret,
TunnelID: t.ID,
}
}
func RequestQuickTunnel(ctx context.Context, dialer N.Dialer, userAgent string) (QuickTunnel, error) {
if dialer == nil {
dialer = N.SystemDialer
}
client := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.DialContext(ctx, network, M.ParseSocksaddr(address))
},
ForceAttemptHTTP2: true,
},
Timeout: quickTunnelTimeout,
}
defer client.CloseIdleConnections()
request, err := http.NewRequestWithContext(ctx, http.MethodPost, quickTunnelService, nil)
if err != nil {
return QuickTunnel{}, E.Cause(err, "build quick tunnel request")
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("User-Agent", userAgent)
response, err := client.Do(request)
if err != nil {
return QuickTunnel{}, E.Cause(err, "request quick tunnel")
}
defer response.Body.Close()
var result struct {
Success bool `json:"success"`
Result QuickTunnel `json:"result"`
Errors []struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"errors"`
}
err = json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&result)
if err != nil {
if response.StatusCode != http.StatusOK {
return QuickTunnel{}, E.New("request quick tunnel: ", response.Status)
}
return QuickTunnel{}, E.Cause(err, "decode quick tunnel")
}
if !result.Success || len(result.Errors) > 0 {
messages := make([]string, 0, len(result.Errors))
for _, item := range result.Errors {
messages = append(messages, F.ToString("[", item.Code, "] ", item.Message))
}
return QuickTunnel{}, E.New("request quick tunnel: ", response.Status, ": ", strings.Join(messages, "; "))
}
if result.Result.ID == uuid.Nil || result.Result.Hostname == "" {
return QuickTunnel{}, E.New("request quick tunnel: incomplete response")
}
return result.Result, nil
}