chore: replace cloudflare dev tunnel with frp (#867)

This commit is contained in:
Colin Adler
2022-04-14 11:29:40 -04:00
committed by GitHub
parent 42e9956779
commit fed02cdcdc
8 changed files with 222 additions and 374 deletions
+92
View File
@@ -0,0 +1,92 @@
package devtunnel
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
frpclient "github.com/fatedier/frp/client"
frpconfig "github.com/fatedier/frp/pkg/config"
frpconsts "github.com/fatedier/frp/pkg/consts"
frplog "github.com/fatedier/frp/pkg/util/log"
frpcrypto "github.com/fatedier/golib/crypto"
"github.com/google/uuid"
"github.com/moby/moby/pkg/namesgenerator"
"golang.org/x/xerrors"
)
// New creates a new tunnel pointing at the URL provided. Once created, it
// returns the external hostname that will resolve to it.
//
// The tunnel will exit when the context provided is canceled.
//
// Upstream connection occurs synchronously through a selfhosted
// https://github.com/fatedier/frp instance. The error channel sends an error
// when the frp client stops.
func New(ctx context.Context, coderurl *url.URL) (string, <-chan error, error) {
frpcrypto.DefaultSalt = "frp"
cfg := frpconfig.GetDefaultClientConf()
cfg.ServerAddr = "frp-tunnel.coder.app"
cfg.ServerPort = 7000
// Ignore all logs from frp.
frplog.InitLog("file", "/dev/null", "error", 0, false)
var (
id = uuid.NewString()
subdomain = strings.ReplaceAll(namesgenerator.GetRandomName(1), "_", "-")
portStr = coderurl.Port()
)
if portStr == "" {
portStr = "80"
}
port, err := strconv.ParseInt(portStr, 10, 64)
if err != nil {
return "", nil, xerrors.Errorf("parse port %q: %w", port, err)
}
httpcfg := map[string]frpconfig.ProxyConf{
id: &frpconfig.HTTPProxyConf{
BaseProxyConf: frpconfig.BaseProxyConf{
ProxyName: id,
ProxyType: frpconsts.HTTPProxy,
UseEncryption: false,
UseCompression: false,
LocalSvrConf: frpconfig.LocalSvrConf{
LocalIP: coderurl.Hostname(),
LocalPort: int(port),
},
},
DomainConf: frpconfig.DomainConf{
SubDomain: subdomain,
},
Locations: []string{""},
},
}
if err := httpcfg[id].CheckForCli(); err != nil {
return "", nil, xerrors.Errorf("check for cli: %w", err)
}
svc, err := frpclient.NewService(cfg, httpcfg, nil, "")
if err != nil {
return "", nil, xerrors.Errorf("create new proxy service: %w", err)
}
ch := make(chan error, 1)
go func() {
err := svc.Run()
ch <- err
close(ch)
}()
go func() {
<-ctx.Done()
svc.Close()
}()
return fmt.Sprintf("https://%s.try.coder.app", subdomain), ch, nil
}
+58
View File
@@ -0,0 +1,58 @@
package devtunnel_test
import (
"context"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"github.com/coder/coder/coderd/devtunnel"
)
// The tunnel leaks a few goroutines that aren't impactful to production scenarios.
// func TestMain(m *testing.M) {
// goleak.VerifyTestMain(m)
// }
func TestTunnel(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip()
return
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(srv.Close)
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
srvURL, err := url.Parse(srv.URL)
require.NoError(t, err)
tunURL, _, err := devtunnel.New(ctx, srvURL)
require.NoError(t, err)
t.Log(tunURL)
require.Eventually(t, func() bool {
req, err := http.NewRequestWithContext(ctx, "GET", tunURL, nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
var dnsErr *net.DNSError
// The name might take a bit to resolve!
if xerrors.As(err, &dnsErr) {
return false
}
require.NoError(t, err)
defer res.Body.Close()
return res.StatusCode == http.StatusOK
}, 5*time.Minute, 3*time.Second)
}