mirror of
https://github.com/Luzifer/expose.git
synced 2024-12-22 10:51:21 +00:00
Initial version
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
commit
1d35b0988e
14 changed files with 800 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
expose
|
9
CONTRIBUTING.md
Normal file
9
CONTRIBUTING.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# Contributing to this project
|
||||||
|
|
||||||
|
Contributions are encouraged and welcome. Thank you very much for taking your time to contribute!
|
||||||
|
|
||||||
|
## Code of Conduct
|
||||||
|
This project adheres to the [Contributor Covenant code of conduct](http://contributor-covenant.org/version/1/4/). By participating, you are expected to uphold this code. Please report unacceptable behavior to the [project maintainer](mailto:help@luzifer.io).
|
||||||
|
|
||||||
|
## Developer Certificate of Origin
|
||||||
|
Please also review the [Developer Certificate of Origin](https://developercertificate.org/). All commits must be signed off using `git commit -s` to indicate you've read the document and your contribution meets the criteria defined in it.
|
202
LICENSE
Normal file
202
LICENSE
Normal file
|
@ -0,0 +1,202 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright 2017 Knut Ahlers <knut@ahlers.me>
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
37
README.md
Normal file
37
README.md
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
# Luzifer / expose
|
||||||
|
|
||||||
|
`expose` is a small CLI utility to control a running [ngrok 2](https://ngrok.com/) daemon.
|
||||||
|
|
||||||
|
## Use cases
|
||||||
|
|
||||||
|
- Quickly open a local port to demonstrate something to someone
|
||||||
|
```bash
|
||||||
|
# expose create 4000
|
||||||
|
Created tunnel to address "localhost:4000" with URL https://8d35e4bf.eu.ngrok.io
|
||||||
|
```
|
||||||
|
- Close that port again
|
||||||
|
```bash
|
||||||
|
# go run *.go delete 4000
|
||||||
|
Successfully closed tunnel "expose_4000" to address "localhost:4000".
|
||||||
|
```
|
||||||
|
- Quickly share a folder with files through HTTP
|
||||||
|
```bash
|
||||||
|
# go run *.go serve
|
||||||
|
Created HTTP server for this directory with URL https://81e668af.eu.ngrok.io
|
||||||
|
Press Ctrl+C to stop server and tunnel
|
||||||
|
```
|
||||||
|
- List the active tunnels in a nice table
|
||||||
|
```bash
|
||||||
|
# go run *.go list
|
||||||
|
NAME | TYPE | ADDRESS | PUBLIC URL
|
||||||
|
+-------------+-------+----------------+------------------------------+
|
||||||
|
expose_3000 | https | localhost:3000 | https://9c8d36d6.eu.ngrok.io
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to set up
|
||||||
|
|
||||||
|
- Download the latest release of [ngrok 2](https://ngrok.com/)
|
||||||
|
- Configure ngrok (see the official documentation for that!)
|
||||||
|
- For this to work you only need an `authtoken` set in `~/.ngrok2/ngrok.yml`
|
||||||
|
- Set it up to start automatically (see `docs/ngrok.service` for a systemd service file)
|
||||||
|
- Download the [latest release](https://github.com/Luzifer/expose/releases/latest) of this tool
|
48
cmd/clear.go
Normal file
48
cmd/clear.go
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// clearCmd represents the clear command
|
||||||
|
var clearCmd = &cobra.Command{
|
||||||
|
Use: "clear",
|
||||||
|
Short: "Remove all active tunnels",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
tunnels, err := client.ListTunnels()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tunnels) == 0 {
|
||||||
|
fmt.Println("No tunnels active to be closed.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tun := range tunnels {
|
||||||
|
if err := client.StopTunnel(tun.Name); err != nil {
|
||||||
|
return fmt.Errorf("Unable to stop tunnel %q: %s", tun.Name, err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Successfully closed tunnel %q to address %q.\n", tun.Name, tun.Config.Addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(clearCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// clearCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// clearCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
|
||||||
|
}
|
66
cmd/create.go
Normal file
66
cmd/create.go
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/Luzifer/expose/ngrok2"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// createCmd represents the create command
|
||||||
|
var createCmd = &cobra.Command{
|
||||||
|
Use: "create <port>",
|
||||||
|
Short: "Create a new HTTPs tunnel to a local port",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return errors.New("Port not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(args[0])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Port %q is not a valid integer: %s", args[0], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
name := fmt.Sprintf("expose_%d", port)
|
||||||
|
if tun, err := client.GetTunnelByName(name); err == nil {
|
||||||
|
return fmt.Errorf("Address %q already has an active tunnel with URL %s", tun.Config.Addr, tun.PublicURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
inspect, err := cmd.Flags().GetBool("inspect")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to read inspect flag: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tun, err := client.CreateTunnel(ngrok2.CreateTunnelInput{
|
||||||
|
Name: name,
|
||||||
|
Addr: strconv.Itoa(port),
|
||||||
|
Proto: "http",
|
||||||
|
BindTLS: "true",
|
||||||
|
Inspect: inspect,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to create tunnel: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Created tunnel to address %q with URL %s\n", tun.Config.Addr, tun.PublicURL)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(createCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
createCmd.Flags().BoolP("inspect", "i", false, "Enable HTTP inspection on tunnel")
|
||||||
|
}
|
53
cmd/delete.go
Normal file
53
cmd/delete.go
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// deleteCmd represents the delete command
|
||||||
|
var deleteCmd = &cobra.Command{
|
||||||
|
Use: "delete <port>",
|
||||||
|
Short: "Close tunnel to specified local port",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return errors.New("Port not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(args[0])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Port %q is not a valid integer: %s", args[0], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
name := fmt.Sprintf("expose_%d", port)
|
||||||
|
tun, err := client.GetTunnelByName(name)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("There is no active tunnel for port %d", port)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.StopTunnel(tun.Name); err != nil {
|
||||||
|
return fmt.Errorf("Unable to stop tunnel %q: %s", tun.Name, err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Successfully closed tunnel %q to address %q.\n", tun.Name, tun.Config.Addr)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(deleteCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
|
||||||
|
}
|
58
cmd/list.go
Normal file
58
cmd/list.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// listCmd represents the list command
|
||||||
|
var listCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List all currently active tunnels",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
tunnels, err := client.ListTunnels()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tunnels) == 0 {
|
||||||
|
fmt.Println("No tunnels are active right now.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
|
table.SetHeader([]string{"Name", "Type", "Address", "Public URL"})
|
||||||
|
table.SetBorder(false)
|
||||||
|
|
||||||
|
for _, tun := range tunnels {
|
||||||
|
table.Append([]string{
|
||||||
|
tun.Name,
|
||||||
|
tun.Proto,
|
||||||
|
tun.Config.Addr,
|
||||||
|
tun.PublicURL,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
table.Render()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(listCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
|
||||||
|
}
|
49
cmd/root.go
Normal file
49
cmd/root.go
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/Luzifer/expose/ngrok2"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var client *ngrok2.Client
|
||||||
|
|
||||||
|
// RootCmd represents the base command when called without any subcommands
|
||||||
|
var RootCmd = &cobra.Command{
|
||||||
|
Use: "expose",
|
||||||
|
Short: "Control ngrok dameon with simple CLI commands",
|
||||||
|
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
client = ngrok2.New()
|
||||||
|
|
||||||
|
baseURL, err := cmd.Flags().GetString("api-base")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client.BaseURL = baseURL
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute adds all child commands to the root command sets flags appropriately.
|
||||||
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
|
func Execute() {
|
||||||
|
if err := RootCmd.Execute(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
// Cobra supports Persistent Flags, which, if defined here,
|
||||||
|
// will be global for your application.
|
||||||
|
|
||||||
|
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.expose.yaml)")
|
||||||
|
// Cobra also supports local flags, which will only run
|
||||||
|
// when this action is called directly.
|
||||||
|
RootCmd.PersistentFlags().String("api-base", "http://localhost:4040", "Base URL to contact the ngrok daemon")
|
||||||
|
}
|
76
cmd/serve.go
Normal file
76
cmd/serve.go
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"github.com/Luzifer/expose/ngrok2"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// serveCmd represents the serve command
|
||||||
|
var serveCmd = &cobra.Command{
|
||||||
|
Use: "serve",
|
||||||
|
Short: "A brief description of your command",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to bind local HTTP server: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, port, err := net.SplitHostPort(listener.Addr().String())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to determine bound port: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
name := fmt.Sprintf("expose_%s", port)
|
||||||
|
if tun, err := client.GetTunnelByName(name); err == nil {
|
||||||
|
return fmt.Errorf("Address %q already has an active tunnel with URL %s", tun.Config.Addr, tun.PublicURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
tun, err := client.CreateTunnel(ngrok2.CreateTunnelInput{
|
||||||
|
Name: name,
|
||||||
|
Addr: port,
|
||||||
|
Proto: "http",
|
||||||
|
BindTLS: "true",
|
||||||
|
Inspect: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to create tunnel: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go http.Serve(listener, http.FileServer(http.Dir(".")))
|
||||||
|
fmt.Printf("Created HTTP server for this directory with URL %s\nPress Ctrl+C to stop server and tunnel", tun.PublicURL)
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, os.Interrupt)
|
||||||
|
for range c {
|
||||||
|
if err := client.StopTunnel(tun.Name); err != nil {
|
||||||
|
return fmt.Errorf("Unable to stop tunnel %q: %s", tun.Name, err)
|
||||||
|
}
|
||||||
|
fmt.Printf("\nSuccessfully stopped server and tunnel.\n")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(serveCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
|
||||||
|
}
|
14
docs/ngrok.service
Normal file
14
docs/ngrok.service
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
[Unit]
|
||||||
|
Description=ngrok port forwarding
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
TimeoutStartSec=0
|
||||||
|
TimeoutStopSec=0
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
ExecStart=/usr/local/bin/ngrok start --none
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
|
7
main.go
Normal file
7
main.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/Luzifer/expose/cmd"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cmd.Execute()
|
||||||
|
}
|
71
ngrok2/client.go
Normal file
71
ngrok2/client.go
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
package ngrok2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultBaseURL = "http://localhost:4040"
|
||||||
|
defaultRequestTimeout = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client represents the API of the ngrok2 daemon
|
||||||
|
type Client struct {
|
||||||
|
HTTPClient *http.Client
|
||||||
|
BaseURL string
|
||||||
|
RequestTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new Client with default settings
|
||||||
|
func New() *Client {
|
||||||
|
return &Client{
|
||||||
|
HTTPClient: http.DefaultClient,
|
||||||
|
BaseURL: defaultBaseURL,
|
||||||
|
RequestTimeout: defaultRequestTimeout,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) do(method, subPath string, payload interface{}, response interface{}) error {
|
||||||
|
var (
|
||||||
|
req *http.Request
|
||||||
|
urlStr = c.BaseURL + subPath
|
||||||
|
)
|
||||||
|
|
||||||
|
if payload != nil {
|
||||||
|
buf := bytes.NewBufferString("")
|
||||||
|
if err := json.NewEncoder(buf).Encode(payload); err != nil {
|
||||||
|
return fmt.Errorf("Unable to encode payload: %s", err)
|
||||||
|
}
|
||||||
|
req, _ = http.NewRequest(method, urlStr, buf)
|
||||||
|
} else {
|
||||||
|
req, _ = http.NewRequest(method, urlStr, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), c.RequestTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
resp, err := c.HTTPClient.Do(req.WithContext(ctx))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to execute request: %s", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode >= 400 {
|
||||||
|
return fmt.Errorf("Received unexpected status code %d on request.", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
if response != nil {
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
|
||||||
|
return fmt.Errorf("Unable to decode response: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
109
ngrok2/tunnels.go
Normal file
109
ngrok2/tunnels.go
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
package ngrok2
|
||||||
|
|
||||||
|
// Tunnel represents one open tunnel in the daemon
|
||||||
|
type Tunnel struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
URI string `json:"uri"`
|
||||||
|
PublicURL string `json:"public_url"`
|
||||||
|
Proto string `json:"proto"`
|
||||||
|
Config struct {
|
||||||
|
Addr string `json:"addr"`
|
||||||
|
Inspect bool `json:"inspect"`
|
||||||
|
} `json:"config"`
|
||||||
|
Metrics TunnelMetrics `json:"metrics"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TunnelMetrics contains details about the tunnel
|
||||||
|
type TunnelMetrics struct {
|
||||||
|
HTTP struct {
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Rate1 float64 `json:"rate1"`
|
||||||
|
Rate5 float64 `json:"rate5"`
|
||||||
|
Rate15 float64 `json:"rate15"`
|
||||||
|
P50 float64 `json:"p50"`
|
||||||
|
P90 float64 `json:"p90"`
|
||||||
|
P95 float64 `json:"p95"`
|
||||||
|
P99 float64 `json:"p99"`
|
||||||
|
} `json:"http"`
|
||||||
|
Conns struct {
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Gauge int64 `json:"gauge"`
|
||||||
|
Rate1 float64 `json:"rate1"`
|
||||||
|
Rate5 float64 `json:"rate5"`
|
||||||
|
Rate15 float64 `json:"rate15"`
|
||||||
|
P50 float64 `json:"p50"`
|
||||||
|
P90 float64 `json:"p90"`
|
||||||
|
P95 float64 `json:"p95"`
|
||||||
|
P99 float64 `json:"p99"`
|
||||||
|
} `json:"conns"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateTunnelInput represents all options possible to be set on a tunnel
|
||||||
|
type CreateTunnelInput struct {
|
||||||
|
// [required, all] Name of the tunnel to create
|
||||||
|
Name string `json:"name"`
|
||||||
|
|
||||||
|
// [required, all] tunnel protocol name, one of `http`, `tcp`, `tls`
|
||||||
|
Proto string `json:"proto"`
|
||||||
|
|
||||||
|
// [required, all] forward traffic to this local port number or network address
|
||||||
|
Addr string `json:"addr"`
|
||||||
|
|
||||||
|
// [all] enable http request inspection
|
||||||
|
Inspect bool `json:"inspect,omitempty"`
|
||||||
|
|
||||||
|
// [http] HTTP basic authentication credentials to enforce on tunneled requests `username:password`
|
||||||
|
Auth string `json:"auth,omitempty"`
|
||||||
|
|
||||||
|
// [http] Rewrite the HTTP Host header to this value, or `preserve` to leave it unchanged
|
||||||
|
HostHeader string `json:"host_header,omitempty"`
|
||||||
|
|
||||||
|
// [http] bind an HTTPS or HTTP endpoint or both `true`, `false`, or `both`
|
||||||
|
BindTLS string `json:"bind_tls,omitempty"`
|
||||||
|
|
||||||
|
// [http, tls] subdomain name to request. If unspecified, uses the tunnel name
|
||||||
|
Subdomain string `json:"subdomain,omitempty"`
|
||||||
|
|
||||||
|
// [http, tls] hostname to request (requires reserved name and DNS CNAME)
|
||||||
|
Hostname string `json:"hostname,omitempty"`
|
||||||
|
|
||||||
|
// [tls] PEM TLS certificate at this path to terminate TLS traffic before forwarding locally
|
||||||
|
CRT string `json:"crt,omitempty"`
|
||||||
|
|
||||||
|
// [tls] PEM TLS private key at this path to terminate TLS traffic before forwarding locally
|
||||||
|
Key string `json:"key,omitempty"`
|
||||||
|
|
||||||
|
// [tls] PEM TLS certificate authority at this path will verify incoming TLS client connection certificates.
|
||||||
|
ClientCAS string `json:"client_cas,omitempty"`
|
||||||
|
|
||||||
|
// [tcp] bind the remote TCP port on the given address
|
||||||
|
RemoteAddr string `json:"remote_addr,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListTunnels returns a slice of all known open tunnels in the daemon
|
||||||
|
func (c *Client) ListTunnels() ([]Tunnel, error) {
|
||||||
|
var envelope = struct {
|
||||||
|
Tunnels []Tunnel `json:"tunnels"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
return envelope.Tunnels, c.do("GET", "/api/tunnels", nil, &envelope)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTunnelByName retrieves details for a specific tunnel by its name
|
||||||
|
func (c *Client) GetTunnelByName(name string) (Tunnel, error) {
|
||||||
|
resp := Tunnel{}
|
||||||
|
return resp, c.do("GET", "/api/tunnels/"+name, nil, &resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopTunnel closes a tunnel by its name
|
||||||
|
func (c *Client) StopTunnel(name string) error {
|
||||||
|
return c.do("DELETE", "/api/tunnels/"+name, nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateTunnel starts a new tunnel for given protocol and address
|
||||||
|
// See https://ngrok.com/docs#tunnel-definitions for details
|
||||||
|
func (c *Client) CreateTunnel(tunnelOpts CreateTunnelInput) (Tunnel, error) {
|
||||||
|
resp := Tunnel{}
|
||||||
|
|
||||||
|
return resp, c.do("POST", "/api/tunnels", tunnelOpts, &resp)
|
||||||
|
}
|
Loading…
Reference in a new issue