Skip to content

Commit

Permalink
Merge pull request #181 from ngrok/kc/add-url-flag-to-agent-all-types…
Browse files Browse the repository at this point in the history
…-tcp-http-tls

Kc/add url flag to agent all types tcp http tls
  • Loading branch information
KristopherPaulsen committed Aug 27, 2024
2 parents ac81e74 + 4c15661 commit 3e82737
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 13 deletions.
7 changes: 7 additions & 0 deletions config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ type commonOpts struct {
Metadata string
// Tunnel backend metadata. Viewable via the dashboard and API, but has no
// bearing on tunnel behavior.

// The URL to request for this endpoint
URL string

// user supplied description of the endpoint
Description string

// If not set, defaults to a URI in the format `app://hostname/path/to/executable?pid=12345`
ForwardsTo string

Expand Down
28 changes: 28 additions & 0 deletions config/description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

type descriptionOption string

func WithDescription(name string) interface {
HTTPEndpointOption
TCPEndpointOption
TLSEndpointOption
LabeledTunnelOption
} {
return descriptionOption(name)
}

func (opt descriptionOption) ApplyHTTP(opts *httpOptions) {
opts.Description = string(opt)
}

func (opt descriptionOption) ApplyTLS(opts *tlsOptions) {
opts.Description = string(opt)
}

func (opt descriptionOption) ApplyTCP(opts *tcpOptions) {
opts.Description = string(opt)
}

func (opt descriptionOption) ApplyLabeled(opts *labeledOptions) {
opts.Description = string(opt)
}
6 changes: 4 additions & 2 deletions config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type httpOptions struct {

func (cfg *httpOptions) toProtoConfig() *proto.HTTPEndpoint {
opts := &proto.HTTPEndpoint{
URL: cfg.URL,
Domain: cfg.Domain,
Hostname: cfg.Hostname,
Subdomain: cfg.Subdomain,
Expand Down Expand Up @@ -146,8 +147,9 @@ func (cfg *httpOptions) WithForwardsTo(url *url.URL) {

func (cfg httpOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
Bindings: cfg.Bindings,
Metadata: cfg.Metadata,
Description: cfg.Description,
Bindings: cfg.Bindings,
}
}

Expand Down
3 changes: 2 additions & 1 deletion config/labeled.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func (cfg *labeledOptions) WithForwardsTo(url *url.URL) {

func (cfg labeledOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
Metadata: cfg.Metadata,
Description: cfg.Description,
}
}

Expand Down
2 changes: 0 additions & 2 deletions config/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ func (p *policy) ApplyTCP(opts *tcpOptions) {

// policyToString converts the policy into a JSON string representation. This is to help remap Policy to TrafficPolicy.
func policyToString(p *policy) string {
fmt.Println("WithPolicy has been deprecated. Please use WithPolicyString instead, as WithPolicy will stop working soon.")

val, err := json.Marshal(p)
if err != nil {
panic(errors.New(fmt.Sprintf("failed to parse action configuration due to error: %s", err.Error())))
Expand Down
7 changes: 5 additions & 2 deletions config/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TCPEndpoint(opts ...TCPEndpointOption) Tunnel {
type tcpOptions struct {
// Common tunnel configuration options.
commonOpts

// The TCP address to request for this edge.
RemoteAddr string
// An HTTP Server to run traffic on
Expand All @@ -48,6 +49,7 @@ func WithRemoteAddr(addr string) TCPEndpointOption {

func (cfg *tcpOptions) toProtoConfig() *proto.TCPEndpoint {
return &proto.TCPEndpoint{
URL: cfg.URL,
Addr: cfg.RemoteAddr,
IPRestriction: cfg.commonOpts.CIDRRestrictions.toProtoConfig(),
ProxyProto: proto.ProxyProto(cfg.commonOpts.ProxyProto),
Expand All @@ -69,8 +71,9 @@ func (cfg *tcpOptions) WithForwardsTo(url *url.URL) {

func (cfg tcpOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
Bindings: cfg.Bindings,
Metadata: cfg.Metadata,
Description: cfg.Description,
Bindings: cfg.Bindings,
}
}

Expand Down
11 changes: 6 additions & 5 deletions config/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ type tlsOptions struct {

func (cfg *tlsOptions) toProtoConfig() *proto.TLSEndpoint {
opts := &proto.TLSEndpoint{
URL: cfg.URL,
Domain: cfg.Domain,
ProxyProto: proto.ProxyProto(cfg.ProxyProto),

Subdomain: cfg.Subdomain,
Hostname: cfg.Hostname,
Subdomain: cfg.Subdomain,
Hostname: cfg.Hostname,
}

opts.IPRestriction = cfg.commonOpts.CIDRRestrictions.toProtoConfig()
Expand Down Expand Up @@ -99,8 +99,9 @@ func (cfg *tlsOptions) WithForwardsTo(url *url.URL) {

func (cfg tlsOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
Bindings: cfg.Bindings,
Metadata: cfg.Metadata,
Description: cfg.Description,
Bindings: cfg.Bindings,
}
}

Expand Down
23 changes: 23 additions & 0 deletions config/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

type urlOption string

func WithURL(name string) interface {
HTTPEndpointOption
TLSEndpointOption
TCPEndpointOption
} {
return urlOption(name)
}

func (opt urlOption) ApplyHTTP(opts *httpOptions) {
opts.URL = string(opt)
}

func (opt urlOption) ApplyTLS(opts *tlsOptions) {
opts.URL = string(opt)
}

func (opt urlOption) ApplyTCP(opts *tcpOptions) {
opts.URL = string(opt)
}
4 changes: 4 additions & 0 deletions internal/tunnel/proto/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ type BindExtra struct {
Token string
IPPolicyRef string
Metadata string
Description string
Bindings []string
}

Expand Down Expand Up @@ -282,6 +283,7 @@ func ParseProxyProto(proxyProto string) (ProxyProto, bool) {
}

type HTTPEndpoint struct {
URL string
Domain string
Hostname string // public hostname of the bind
Subdomain string
Expand All @@ -308,6 +310,7 @@ type HTTPEndpoint struct {
}

type TCPEndpoint struct {
URL string
Addr string
ProxyProto

Expand All @@ -318,6 +321,7 @@ type TCPEndpoint struct {
}

type TLSEndpoint struct {
URL string
Domain string
Hostname string // public hostname of the bind
Subdomain string
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ func (s *sessionImpl) ListenAndServeHTTP(ctx context.Context, cfg config.Tunnel,
impl.server = server
} else {
// Inform end user that they're using a deprecated option.
fmt.Println("Tunnel is serving an HTTP server via HTTP options. This has been deprecated. Please use Session.ListenAndServeHTTP instead.")
s.inner().Logger.Warn("Tunnel is serving an HTTP server via HTTP options. This has been deprecated. Please use Session.ListenAndServeHTTP instead.")
}
}

Expand Down

0 comments on commit 3e82737

Please sign in to comment.