gonnel

package module
v0.0.0-...-a1b2284 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2022 License: BSD-2-Clause Imports: 14 Imported by: 2

README

Gonnel

Go Report Card GoDoc

>Golang wrapper for ngrok. Expose your localhost to the internet.

Tested on linux, hopefully supports Mac, Windows, and Linux

Installation

  • Download ngrok binary file
  • Install package
go get github.com/afdalwahyu/gonnel

Update

go get -u github.com/afdalwahyu/gonnel

Examples:

Create client & tunnel
package main

import (
	"fmt"
	"github.com/afdalwahyu/gonnel"
	"bufio"
	"os"
)

func main() {
	client, err := gonnel.NewClient(gonnel.Options{
		BinaryPath: "../ngrok-bin/ngrok_linux",
	})
	if err != nil {
		fmt.Println(err)
	}
	defer client.Close()

	done := make(chan bool)
	go client.StartServer(done)
	<-done

	client.AddTunnel(&gonnel.Tunnel{
		Proto:        gonnel.HTTP,
		Name:         "awesome",
		LocalAddress: "127.0.0.1:4040",
		Auth:         "username:password",
	})

	client.ConnectAll()

	fmt.Print("Press any to disconnect")
	reader := bufio.NewReader(os.Stdin)
	reader.ReadRune()

	client.DisconnectAll()
}

How it works

Inspired from node.js wrapper that use ngrok binary, run it and use client api to create or close tunnel

Documentation

Overview

Package gonnel provides direct API tunnel using code. The implementation in this project inspired from ngrok wrapper node.js that using EventEmitter. In this package channel used to check if binary running successfully or not

The Client package intended to handle all function that provided from binary like using auth token or create a tunnel

Here is a simple example, initialize client binary and auth token automatically by running StartServer

client, err := gonnel.NewClient(gonnel.Options{
	BinaryPath: "../ngrok-bin/ngrok_linux",
})
if err != nil {
	fmt.Println(err)
}
defer client.Close()

done := make(chan bool)
go client.StartServer(done)
<-done

This package also can directly create tunnel if you already started ngrok binary separately, WebUIAddress type need hostname and port

client := go_ngrok.Client{
	WebUIAddress: "127.0.0.1:4040",
	LogApi:       true,
}

// Create pointer tunnel
t := &go_ngrok.Tunnel{
	Name:         "awesome",
	Auth:         "username:password",
	Inspect:      false,
	LocalAddress: "4040",
	Proto:        go_ngrok.HTTP,
}

if err := client.CreateTunnel(t); err != nil {
	log.Fatalln(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Options      *Options  // Options that will be used for command
	Tunnel       []*Tunnel // List of all tunnel
	WebUIAddress string    // Client server for API communication
	LogApi       bool      // Log response from API or not
	// contains filtered or unexported fields
}

Client that provides all option and tunnel

You don't need NewClient method if server client already started

func NewClient

func NewClient(opt Options) (*Client, error)

NewClient that return Client pointer

Client pointer can be used to close binary or start binary

func (*Client) AddTunnel

func (c *Client) AddTunnel(t *Tunnel)

AddTunnel create a new tunnel without connecting it

func (*Client) Close

func (c *Client) Close() error

Close running command and send kill signal to ngrok binary

func (*Client) CloseTunnel

func (c *Client) CloseTunnel(t *Tunnel) (err error)

CloseTunnel that close tunnel from ngrok server

Close tunnel call API using DELETE method

func (*Client) ConnectAll

func (c *Client) ConnectAll() error

ConnectAll connect all tunnels that created

func (*Client) CreateTunnel

func (c *Client) CreateTunnel(t *Tunnel) (err error)

CreateTunnel that create connection to ngrok server

Error will be from api ngrok server client, retries is used because server client not always success when started. Need at least 1 or 2 second to start.

func (*Client) DisconnectAll

func (c *Client) DisconnectAll() error

DisconnectAll disconnect all tunnels that previously tunneled

func (*Client) Signal

func (c *Client) Signal(signal os.Signal) error

Signal handle signal input and proceed to command

func (*Client) StartServer

func (c *Client) StartServer(isReady chan bool)

StartServer will be run command from previous options

Channel needed to send information about WebUI started or not. stdout will be pipe and check using regex.

type Options

type Options struct {
	SubDomain     string // Sub domain config if you're using premium plan
	AuthToken     string // Auth token to authenticate client
	Region        string // Region that will tunneling from
	ConfigPath    string // Path config to store auth token or specific WebUI port
	BinaryPath    string // Binary file that will be running
	LogBinary     bool   // You can watch binary log or not
	IgnoreSignals bool   // Run child processes in a separate process group to ignore signals
}

Options that represents command that will be used to start binary

Not all of this option necessary, if AuthToken provided then binary will run auth token first.

func (*Options) AuthTokenCommand

func (o *Options) AuthTokenCommand() error

AuthTokenCommand that will be authenticate api token

type Protocol

type Protocol int

Protocol type

const (
	HTTP Protocol = iota
	TCP
	TLS
)

Protocol that ngrok support

func (Protocol) String

func (p Protocol) String() string

type Tunnel

type Tunnel struct {
	Proto         Protocol // Protocol that use in tunneling process
	Name          string   // A name that used for creating or closing
	LocalAddress  string   // Can be host with port or port only
	Auth          string   // Username & password that will authenticate to access tunnel
	Inspect       bool     // Inspect transaction data tunnel that will be logged in binary file
	RemoteAddress string   // Result ngrok connection address
	IsCreated     bool     // Information tunnel created or not
}

Tunnel will be used in creating or closing a tunnel. a tunnel also can connect to ngrok directly as long ngrok client server already running

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL