Skip to content

Commit

Permalink
core: proxy_url support (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtszalay authored Nov 9, 2023
1 parent ad5762d commit e945034
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ngrok.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net"
"net/url"
"time"

"github.com/caddyserver/caddy/v2"
Expand Down Expand Up @@ -64,6 +65,14 @@ type Ngrok struct {
// See the [heartbeat_interval parameter in the ngrok docs] for additional details.
HeartbeatInterval caddy.Duration `json:"heartbeat_interval,omitempty"`

// https://github.com/ngrok/ngrok-go/blob/main/session.go
// ProxyURL configures the session to connect to ngrok through an outbound
// HTTP or SOCKS5 proxy. This parameter is ignored if you override the dialer
// with [WithDialer].
//
// See the [proxy url parameter in the ngrok docs] for additional details.
ProxyURL string `json:"proxy_url,omitempty"`

tunnel Tunnel

ctx context.Context
Expand Down Expand Up @@ -131,6 +140,14 @@ func (n *Ngrok) provisionOpts() error {

n.opts = append(n.opts, ngrok.WithHeartbeatTolerance(time.Duration(n.HeartbeatTolerance)))

if n.ProxyURL != "" {
url, err := url.Parse(n.ProxyURL)
if err != nil {
return fmt.Errorf("provisioning proxy_url: %v", err)
}
n.opts = append(n.opts, ngrok.WithProxyURL(url))
}

return nil
}

Expand All @@ -141,6 +158,7 @@ func (n *Ngrok) doReplace() {
&n.Metadata,
&n.Region,
&n.Server,
&n.ProxyURL,
}

for _, field := range replaceableFields {
Expand Down Expand Up @@ -207,6 +225,10 @@ func (n *Ngrok) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if err := n.unmarshalHeartbeatInterval(d); err != nil {
return err
}
case "proxy_url":
if !d.AllArgs(&n.ProxyURL) {
return d.ArgErr()
}
case "tunnel":
if err := n.unmarshalTunnel(d); err != nil {
return err
Expand Down
47 changes: 47 additions & 0 deletions ngrok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,53 @@ func TestNgrokHeartbeat(t *testing.T) {
cases.runAll(t)
}

func TestNgrokProxyUrl(t *testing.T) {
cases := genericNgrokTestCases[*Ngrok]{
{
name: "empty",
caddyInput: `ngrok {
}`,
expectConfig: func(t *testing.T, actual *Ngrok) {
require.Empty(t, actual.ProxyURL)
},
},
{
name: "set proxy_url",
caddyInput: `ngrok {
proxy_url http://proxy.url
}`,
expectConfig: func(t *testing.T, actual *Ngrok) {
require.Equal(t, actual.ProxyURL, "http://proxy.url")
},
},
{
name: "proxy_url-no-arg",
caddyInput: `ngrok {
proxy_url
}`,
expectUnmarshalErr: true,
},
{
name: "proxy_url-too-many-arg",
caddyInput: `ngrok {
proxy_url http://proxy.url http://proxy2.url
}`,
expectUnmarshalErr: true,
},
{
name: "proxy_url-non-url",
caddyInput: `ngrok {
proxy_url 123.456.789.101:1234
}`,
expectConfig: func(t *testing.T, actual *Ngrok) {
require.Equal(t, actual.ProxyURL, "123.456.789.101:1234")
},
expectProvisionErr: true,
},
}
cases.runAll(t)
}

func TestNgrokTunnel(t *testing.T) {
cases := genericNgrokTestCases[*Ngrok]{
{
Expand Down

0 comments on commit e945034

Please sign in to comment.