Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f74e30

Browse files
committed
chore: replace cloudflare dev tunnel with frp
1 parent 8a1ae18 commit 6f74e30

File tree

3 files changed

+132
-314
lines changed

3 files changed

+132
-314
lines changed

coderd/tunnel/tunnel.go

Lines changed: 47 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@ package tunnel
22

33
import (
44
"context"
5-
"encoding/json"
6-
"flag"
7-
"net/http"
8-
"os"
5+
"fmt"
96
"strings"
10-
"time"
117

12-
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
13-
"github.com/cloudflare/cloudflared/cmd/cloudflared/tunnel"
14-
"github.com/cloudflare/cloudflared/connection"
8+
frpclient "github.com/fatedier/frp/client"
9+
frpconfig "github.com/fatedier/frp/pkg/config"
10+
frpconsts "github.com/fatedier/frp/pkg/consts"
11+
frpcrypto "github.com/fatedier/golib/crypto"
1512
"github.com/google/uuid"
16-
"github.com/rs/zerolog"
17-
"github.com/urfave/cli/v2"
13+
"github.com/moby/moby/pkg/namesgenerator"
1814
"golang.org/x/xerrors"
19-
20-
"github.com/coder/retry"
2115
)
2216

2317
// New creates a new tunnel pointing at the URL provided.
@@ -28,90 +22,57 @@ import (
2822
// Upstream connection occurs async through Cloudflare, so the error channel
2923
// will only be executed if the tunnel has failed after numerous attempts.
3024
func New(ctx context.Context, url string) (string, <-chan error, error) {
31-
_ = os.Setenv("QUIC_GO_DISABLE_RECEIVE_BUFFER_WARNING", "true")
25+
frpcrypto.DefaultSalt = "frp"
3226

33-
httpTimeout := time.Second * 30
34-
client := http.Client{
35-
Transport: &http.Transport{
36-
TLSHandshakeTimeout: httpTimeout,
37-
ResponseHeaderTimeout: httpTimeout,
38-
},
39-
Timeout: httpTimeout,
40-
}
27+
cfg := frpconfig.GetDefaultClientConf()
28+
cfg.ServerAddr = "tunnel-test.cdr.co"
29+
cfg.ServerPort = 7000
30+
cfg.LogLevel = "warn"
4131

42-
// Taken from:
43-
// https://github.com/cloudflare/cloudflared/blob/22cd8ceb8cf279afc1c412ae7f98308ffcfdd298/cmd/cloudflared/tunnel/quick_tunnel.go#L38
44-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api.trycloudflare.com/tunnel", nil)
45-
if err != nil {
46-
return "", nil, xerrors.Errorf("create request: %w", err)
47-
}
48-
req.Header.Set("Content-Type", "application/json")
49-
resp, err := client.Do(req)
50-
if err != nil {
51-
return "", nil, xerrors.Errorf("request quick tunnel: %w", err)
52-
}
53-
defer resp.Body.Close()
54-
var data quickTunnelResponse
55-
err = json.NewDecoder(resp.Body).Decode(&data)
56-
if err != nil {
57-
return "", nil, xerrors.Errorf("decode: %w", err)
58-
}
59-
tunnelID, err := uuid.Parse(data.Result.ID)
60-
if err != nil {
61-
return "", nil, xerrors.Errorf("parse tunnel id: %w", err)
32+
subdomain := strings.ReplaceAll(namesgenerator.GetRandomName(1), "_", "-")
33+
34+
id := uuid.NewString()
35+
httpcfg := map[string]frpconfig.ProxyConf{
36+
id: &frpconfig.HTTPProxyConf{
37+
BaseProxyConf: frpconfig.BaseProxyConf{
38+
ProxyName: id,
39+
ProxyType: frpconsts.HTTPProxy,
40+
UseEncryption: false,
41+
UseCompression: false,
42+
LocalSvrConf: frpconfig.LocalSvrConf{
43+
LocalIP: "127.0.0.1",
44+
LocalPort: 3000,
45+
},
46+
},
47+
DomainConf: frpconfig.DomainConf{
48+
SubDomain: subdomain,
49+
},
50+
Locations: []string{""},
51+
},
6252
}
6353

64-
credentials := connection.Credentials{
65-
AccountTag: data.Result.AccountTag,
66-
TunnelSecret: data.Result.Secret,
67-
TunnelID: tunnelID,
54+
if err := httpcfg[id].CheckForCli(); err != nil {
55+
return "", nil, xerrors.Errorf("check for cli: %w", err)
6856
}
6957

70-
namedTunnel := &connection.NamedTunnelProperties{
71-
Credentials: credentials,
72-
QuickTunnelUrl: data.Result.Hostname,
58+
svc, err := frpclient.NewService(cfg, httpcfg, nil, "")
59+
if err != nil {
60+
return "", nil, xerrors.Errorf("create new proxy service: %w", err)
7361
}
7462

75-
set := flag.NewFlagSet("", 0)
76-
set.String("protocol", "", "")
77-
set.String("url", "", "")
78-
// set.Int("retries", 5, "")
79-
appCtx := cli.NewContext(&cli.App{}, set, nil)
80-
appCtx.Context = ctx
81-
_ = appCtx.Set("url", url)
82-
_ = appCtx.Set("protocol", "quic")
83-
logger := zerolog.New(os.Stdout).Level(zerolog.Disabled)
84-
errCh := make(chan error, 1)
63+
ch := make(chan error, 1)
8564
go func() {
86-
for retry.New(250*time.Millisecond, 5*time.Second).Wait(ctx) {
87-
err := tunnel.StartServer(appCtx, &cliutil.BuildInfo{}, namedTunnel, &logger, false)
88-
if err != nil && strings.Contains(err.Error(), "Failed to get tunnel") {
89-
continue
90-
}
91-
errCh <- err
65+
err := svc.Run()
66+
if err != nil {
67+
ch <- err
68+
}
69+
}()
70+
go func() {
71+
select {
72+
case <-ctx.Done():
73+
svc.Close()
9274
}
9375
}()
94-
if !strings.HasPrefix(data.Result.Hostname, "https://") {
95-
data.Result.Hostname = "https://" + data.Result.Hostname
96-
}
97-
return data.Result.Hostname, errCh, nil
98-
}
99-
100-
type quickTunnelResponse struct {
101-
Success bool
102-
Result quickTunnel
103-
Errors []quickTunnelError
104-
}
105-
106-
type quickTunnelError struct {
107-
Code int
108-
Message string
109-
}
11076

111-
type quickTunnel struct {
112-
ID string `json:"id"`
113-
Name string `json:"name"`
114-
Hostname string `json:"hostname"`
115-
AccountTag string `json:"account_tag"`
116-
Secret []byte `json:"secret"`
77+
return fmt.Sprintf("http://%s.tunnel-test.cdr.co", subdomain), ch, nil
11778
}

go.mod

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ require (
4141
github.com/charmbracelet/charm v0.11.0
4242
github.com/charmbracelet/lipgloss v0.5.0
4343
github.com/cli/safeexec v1.0.0
44-
github.com/cloudflare/cloudflared v0.0.0-20220308214351-5352b3cf0489
4544
github.com/coder/retry v1.3.0
4645
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
4746
github.com/creack/pty v1.1.18
47+
github.com/fatedier/frp v0.41.0
48+
github.com/fatedier/golib v0.1.1-0.20220321042308-c306138b83ac
4849
github.com/fatih/color v1.13.0
4950
github.com/gliderlabs/ssh v0.3.3
5051
github.com/go-chi/chi/v5 v5.0.7
@@ -74,13 +75,11 @@ require (
7475
github.com/pion/webrtc/v3 v3.1.27
7576
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
7677
github.com/quasilyte/go-ruleguard/dsl v0.3.19
77-
github.com/rs/zerolog v1.26.1
7878
github.com/spf13/cobra v1.4.0
7979
github.com/spf13/pflag v1.0.5
8080
github.com/stretchr/testify v1.7.1
8181
github.com/tabbed/pqtype v0.1.1
8282
github.com/unrolled/secure v1.10.0
83-
github.com/urfave/cli/v2 v2.3.0
8483
go.uber.org/atomic v1.9.0
8584
go.uber.org/goleak v1.1.12
8685
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd
@@ -94,9 +93,24 @@ require (
9493
storj.io/drpc v0.0.30
9594
)
9695

96+
require (
97+
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c // indirect
98+
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 // indirect
99+
github.com/coreos/go-oidc v2.2.1+incompatible // indirect
100+
github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb // indirect
101+
github.com/fatedier/kcp-go v2.0.4-0.20190803094908-fe8645b0a904+incompatible // indirect
102+
github.com/golang/snappy v0.0.4 // indirect
103+
github.com/klauspost/cpuid/v2 v2.0.6 // indirect
104+
github.com/klauspost/reedsolomon v1.9.15 // indirect
105+
github.com/pires/go-proxyproto v0.5.0 // indirect
106+
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
107+
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b // indirect
108+
github.com/tjfoc/gmsm v1.4.1 // indirect
109+
gopkg.in/ini.v1 v1.62.0 // indirect
110+
)
111+
97112
require (
98113
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
99-
github.com/BurntSushi/toml v1.0.0 // indirect
100114
github.com/DataDog/datadog-agent/pkg/obfuscate v0.0.0-20211129110424-6491aa3bf583 // indirect
101115
github.com/DataDog/datadog-go v4.8.2+incompatible // indirect
102116
github.com/DataDog/datadog-go/v5 v5.0.2 // indirect
@@ -106,24 +120,15 @@ require (
106120
github.com/agext/levenshtein v1.2.3 // indirect
107121
github.com/alecthomas/chroma v0.10.0 // indirect
108122
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
109-
github.com/apparentlymart/go-cidr v1.1.0 // indirect
110123
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
111-
github.com/beorn7/perks v1.0.1 // indirect
112124
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
113-
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
114125
github.com/cespare/xxhash/v2 v2.1.2 // indirect
115126
github.com/charmbracelet/bubbles v0.10.3 // indirect
116127
github.com/charmbracelet/bubbletea v0.20.0 // indirect
117-
github.com/cheekybits/genny v1.0.0 // indirect
118128
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
119129
github.com/clbanning/mxj/v2 v2.5.5 // indirect
120-
github.com/cloudflare/brotli-go v0.0.0-20191101163834-d34379f7ff93 // indirect
121-
github.com/cloudflare/golibs v0.0.0-20210909181612-21743d7dd02a // indirect
122130
github.com/containerd/console v1.0.3 // indirect
123131
github.com/containerd/continuity v0.2.2 // indirect
124-
github.com/coredns/caddy v1.1.1 // indirect
125-
github.com/coredns/coredns v1.9.0 // indirect
126-
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
127132
github.com/davecgh/go-spew v1.1.1 // indirect
128133
github.com/dgraph-io/ristretto v0.1.0 // indirect
129134
github.com/dhui/dktest v0.3.9 // indirect
@@ -134,30 +139,17 @@ require (
134139
github.com/docker/go-connections v0.4.0 // indirect
135140
github.com/docker/go-units v0.4.0 // indirect
136141
github.com/dustin/go-humanize v1.0.0 // indirect
137-
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 // indirect
138-
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
139-
github.com/francoispqt/gojay v1.2.13 // indirect
140-
github.com/fsnotify/fsnotify v1.5.1 // indirect
141-
github.com/gdamore/encoding v1.0.0 // indirect
142-
github.com/gdamore/tcell v1.4.0 // indirect
143-
github.com/getsentry/raven-go v0.2.0 // indirect
144142
github.com/go-playground/locales v0.14.0 // indirect
145143
github.com/go-playground/universal-translator v0.18.0 // indirect
146-
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
147144
github.com/gobwas/glob v0.2.3 // indirect
148-
github.com/gobwas/httphead v0.1.0 // indirect
149-
github.com/gobwas/pool v0.2.1 // indirect
150145
github.com/gobwas/ws v1.1.0 // indirect
151146
github.com/gogo/protobuf v1.3.2 // indirect
152-
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
153147
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
154148
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
155149
github.com/golang/protobuf v1.5.2 // indirect
156150
github.com/google/go-cmp v0.5.7 // indirect
157151
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
158152
github.com/gorilla/mux v1.8.0 // indirect
159-
github.com/gorilla/websocket v1.5.0 // indirect
160-
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
161153
github.com/hashicorp/errwrap v1.1.0 // indirect
162154
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
163155
github.com/hashicorp/go-multierror v1.1.1 // indirect
@@ -171,33 +163,21 @@ require (
171163
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
172164
github.com/klauspost/compress v1.15.0 // indirect
173165
github.com/leodido/go-urn v1.2.1 // indirect
174-
github.com/lucas-clemente/quic-go v0.25.1-0.20220307142123-ad1cb27c1b64 // indirect
175166
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
176167
github.com/lunixbochs/vtclean v1.0.0 // indirect
177168
github.com/mailru/easyjson v0.7.7 // indirect
178-
github.com/marten-seemann/qtls-go1-16 v0.1.4 // indirect
179-
github.com/marten-seemann/qtls-go1-17 v0.1.0 // indirect
180-
github.com/marten-seemann/qtls-go1-18 v0.1.0-beta.1 // indirect
181169
github.com/mattn/go-colorable v0.1.12 // indirect
182170
github.com/mattn/go-runewidth v0.0.13 // indirect
183-
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
184171
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
185-
github.com/miekg/dns v1.1.46 // indirect
186-
github.com/mitchellh/go-homedir v1.1.0 // indirect
187172
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
188173
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
189-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
190-
github.com/modern-go/reflect2 v1.0.2 // indirect
191174
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
192175
github.com/muesli/reflow v0.3.0 // indirect
193176
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
194177
github.com/niklasfasching/go-org v1.6.2 // indirect
195-
github.com/nxadm/tail v1.4.8 // indirect
196-
github.com/onsi/ginkgo v1.16.5 // indirect
197178
github.com/opencontainers/go-digest v1.0.0 // indirect
198179
github.com/opencontainers/image-spec v1.0.2 // indirect
199180
github.com/opencontainers/runc v1.1.0 // indirect
200-
github.com/opentracing/opentracing-go v1.2.0 // indirect
201181
github.com/pelletier/go-toml/v2 v2.0.0-beta.6 // indirect
202182
github.com/philhofer/fwd v1.1.1 // indirect
203183
github.com/pion/dtls/v2 v2.1.3 // indirect
@@ -216,14 +196,8 @@ require (
216196
github.com/pkg/errors v0.9.1 // indirect
217197
github.com/pmezard/go-difflib v1.0.0 // indirect
218198
github.com/pquerna/cachecontrol v0.1.0 // indirect
219-
github.com/prometheus/client_golang v1.12.1 // indirect
220-
github.com/prometheus/client_model v0.2.0 // indirect
221-
github.com/prometheus/common v0.32.1 // indirect
222-
github.com/prometheus/procfs v0.7.3 // indirect
223-
github.com/rivo/tview v0.0.0-20200712113419-c65badfc3d92 // indirect
224199
github.com/rivo/uniseg v0.2.0 // indirect
225200
github.com/robfig/cron/v3 v3.0.1
226-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
227201
github.com/sirupsen/logrus v1.8.1 // indirect
228202
github.com/spf13/afero v1.8.1 // indirect
229203
github.com/spf13/cast v1.4.1 // indirect
@@ -235,21 +209,15 @@ require (
235209
github.com/zclconf/go-cty v1.10.0 // indirect
236210
github.com/zeebo/errs v1.2.2 // indirect
237211
go.opencensus.io v0.23.0 // indirect
238-
golang.org/x/mod v0.5.1 // indirect
239212
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
240213
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
241214
golang.org/x/text v0.3.7 // indirect
242215
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
243-
golang.org/x/tools v0.1.9 // indirect
244216
google.golang.org/appengine v1.6.7 // indirect
245217
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb // indirect
246218
google.golang.org/grpc v1.45.0 // indirect
247219
gopkg.in/DataDog/dd-trace-go.v1 v1.37.1
248-
gopkg.in/coreos/go-oidc.v2 v2.2.1 // indirect
249-
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
250220
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
251-
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
252221
gopkg.in/yaml.v2 v2.4.0 // indirect
253222
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
254-
zombiezen.com/go/capnproto2 v2.18.2+incompatible // indirect
255223
)

0 commit comments

Comments
 (0)