Skip to content

Commit

Permalink
Publish initial version of libngrok-go
Browse files Browse the repository at this point in the history
Squashed merge of jrobsonchase/develop (4e064d3)
  • Loading branch information
jrobsonchase committed Sep 9, 2022
1 parent 816d9fc commit fe6c40f
Show file tree
Hide file tree
Showing 79 changed files with 9,833 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GOPATH=`pwd`/.direnv/go
PATH=${GOPATH}/bin:${PATH}
use flake
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.direnv
*.swp
cover.out
7 changes: 7 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 ngrok, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.PHONY: build
build:
go build

.PHONY: examples
examples: example/http

.PHONY: run-example-%
run-example-%:
cd example && go run $*.go

.PHONY: example/%
example/%: example/%.go
cd example && go build -o $* $*.go

.PHONY: test
test:
go test -coverprofile cover.out

.PHONY: coverage
coverage: test
go tool cover -html cover.out
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# libngrok-go

**Note: This is alpha-quality software. Interfaces are subject to change
without warning**

This is the ngrok agent in library form, suitable for integrating directly into
an application. See `example/http.go` for example usage, or the tests in
`libngrok_test.go`.

## License

libngrok-go is licensed under the terms of the MIT license.

See [LICENSE](./LICENSE) for details.
96 changes: 96 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package libngrok

import (
"net"

"github.com/ngrok/libngrok-go/internal/pb_agent"
)

type CommonConfig[T any] struct {
parent *T

CIDRRestrictions *CIDRRestriction
ProxyProto ProxyProtoVersion
Metadata string
ForwardsTo string
}

type ProxyProtoVersion int32

const (
ProxyProtoV1 = ProxyProtoVersion(1)
ProxyProtoV2 = ProxyProtoVersion(2)
)

func (cfg *CommonConfig[T]) WithProxyProto(version ProxyProtoVersion) *T {
cfg.ProxyProto = version
return cfg.parent
}

func (cfg *CommonConfig[T]) WithMetadata(meta string) *T {
cfg.Metadata = meta
return cfg.parent
}

func (cfg *CommonConfig[T]) WithForwardsTo(address string) *T {
cfg.ForwardsTo = address
return cfg.parent
}

type CIDRRestriction struct {
Allowed []string
Denied []string
}

func CIDRSet() *CIDRRestriction {
return &CIDRRestriction{}
}

func (cr *CIDRRestriction) AllowString(cidr ...string) *CIDRRestriction {
cr.Allowed = append(cr.Allowed, cidr...)
return cr
}

func (cr *CIDRRestriction) Allow(net ...*net.IPNet) *CIDRRestriction {
for _, n := range net {
cr.AllowString(n.String())
}
return cr
}

func (cr *CIDRRestriction) DenyString(cidr ...string) *CIDRRestriction {
cr.Denied = append(cr.Denied, cidr...)
return cr
}

func (cr *CIDRRestriction) Deny(net ...*net.IPNet) *CIDRRestriction {
for _, n := range net {
cr.DenyString(n.String())
}
return cr
}

func (ir *CIDRRestriction) toProtoConfig() *pb_agent.MiddlewareConfiguration_IPRestriction {
if ir == nil {
return nil
}

return &pb_agent.MiddlewareConfiguration_IPRestriction{
AllowCIDRs: ir.Allowed,
DenyCIDRs: ir.Denied,
}
}

func (cfg *CommonConfig[T]) WithCIDRRestriction(set ...*CIDRRestriction) *T {
if cfg.CIDRRestrictions == nil {
cfg.CIDRRestrictions = CIDRSet()
}

for _, s := range set {
if s != nil {
cfg.CIDRRestrictions.AllowString(s.Allowed...)
cfg.CIDRRestrictions.DenyString(s.Denied...)
}
}
return cfg.parent
}
79 changes: 79 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package libngrok

import (
"fmt"
"net/url"
"reflect"
)

type ErrContext interface {
message() string
}
type Error[C ErrContext] struct {
Inner error
Context C
}

func (e Error[C]) Unwrap() error {
return e.Inner
}

func (e Error[C]) Error() string {
msg := e.Context.message()
if e.Inner != nil {
return fmt.Sprintf("%s: %v", msg, e.Inner.Error())
} else {
return msg
}
}

func (e Error[C]) Is(other error) bool {
return reflect.TypeOf(e) == reflect.TypeOf(other)
}

type ErrAuthFailed = Error[AuthFailedContext]
type AuthFailedContext struct {
Remote bool
}

func (c AuthFailedContext) message() string {
if c.Remote {
return "authentication failed"
} else {
return "failed to send authentication request"
}
}

type ErrAcceptFailed = Error[AcceptContext]
type AcceptContext struct{}

func (c AcceptContext) message() string {
return "failed to accept connection"
}

type ErrStartTunnel = Error[StartContext]
type StartContext struct {
Config TunnelConfig
}

func (c StartContext) message() string {
return "failed to start tunnel"
}

type ErrProxyInit = Error[ProxyInitContext]
type ProxyInitContext struct {
URL *url.URL
}

func (c ProxyInitContext) message() string {
return fmt.Sprintf("failed to construct proxy dialer from \"%s\"", c.URL.String())
}

type ErrSessionDial = Error[DialContext]
type DialContext struct {
Addr string
}

func (c DialContext) message() string {
return fmt.Sprintf("failed to dial ngrok server with address \"%s\"", c.Addr)
}
30 changes: 30 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package libngrok

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
)

var testError = errors.New("testing, 1 2 3!")

// Sanity check for my appraoch to error construction/wrapping
func TestErrorStrategy(t *testing.T) {
var accept error = ErrAcceptFailed{Inner: testError}
var auth error = ErrAuthFailed{accept, AuthFailedContext{Remote: true}}

require.True(t, errors.Is(accept, ErrAcceptFailed{}))
require.True(t, errors.Is(auth, ErrAuthFailed{}))
require.True(t, errors.Is(auth, ErrAcceptFailed{}))

var downcastAuth ErrAuthFailed
var downcastAccept ErrAcceptFailed

require.True(t, errors.As(auth, &downcastAuth))
require.True(t, errors.As(auth, &downcastAccept))

require.True(t, errors.As(accept, &downcastAccept))

require.True(t, downcastAuth.Context.Remote)
}
27 changes: 27 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module github.com/ngrok/libngrok-go/examples

go 1.18

require (
github.com/davecgh/go-spew v1.1.1
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac
github.com/ngrok/libngrok-go v0.0.0
github.com/ngrok/libngrok-go/log/log15adapter v0.0.0
)

require (
github.com/go-stack/stack v1.8.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/ngrok/libngrok-go/log v0.0.0 // indirect
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

replace (
github.com/ngrok/libngrok-go => ../
github.com/ngrok/libngrok-go/log => ../log
github.com/ngrok/libngrok-go/log/log15adapter => ../log/log15adapter
)
31 changes: 31 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac h1:n1DqxAo4oWPMvH1+v+DLYlMCecgumhhgnxAPdqDIFHI=
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E=
golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Loading

0 comments on commit fe6c40f

Please sign in to comment.