Skip to content
Permalink
Browse files

switch from go/flags to spf13/cobra

Signed-off-by: RaviTezu <ravi-teja@live.com>
  • Loading branch information...
RaviTezu authored and alexellis committed Mar 11, 2019
1 parent 875b085 commit 94488e8452b8506c8549672f8fce8dbc1a2950ac
Showing with 9,191 additions and 147 deletions.
  1. +1 −1 Dockerfile
  2. +25 −0 Gopkg.lock
  3. +9 −0 Gopkg.toml
  4. +15 −15 README.md
  5. +96 −0 cmd/client.go
  6. +71 −0 cmd/server.go
  7. +1 −1 hack/inlets.service
  8. +4 −99 main.go
  9. +0 −31 parse_upstream.go
  10. +13 −0 vendor/github.com/inconshreveable/mousetrap/LICENSE
  11. +23 −0 vendor/github.com/inconshreveable/mousetrap/README.md
  12. +15 −0 vendor/github.com/inconshreveable/mousetrap/trap_others.go
  13. +98 −0 vendor/github.com/inconshreveable/mousetrap/trap_windows.go
  14. +46 −0 vendor/github.com/inconshreveable/mousetrap/trap_windows_1.4.go
  15. +36 −0 vendor/github.com/spf13/cobra/.gitignore
  16. +3 −0 vendor/github.com/spf13/cobra/.mailmap
  17. +21 −0 vendor/github.com/spf13/cobra/.travis.yml
  18. +174 −0 vendor/github.com/spf13/cobra/LICENSE.txt
  19. +736 −0 vendor/github.com/spf13/cobra/README.md
  20. +89 −0 vendor/github.com/spf13/cobra/args.go
  21. +584 −0 vendor/github.com/spf13/cobra/bash_completions.go
  22. +221 −0 vendor/github.com/spf13/cobra/bash_completions.md
  23. +200 −0 vendor/github.com/spf13/cobra/cobra.go
  24. +202 −0 vendor/github.com/spf13/cobra/cobra/cmd/testdata/LICENSE.golden
  25. +1,517 −0 vendor/github.com/spf13/cobra/command.go
  26. +5 −0 vendor/github.com/spf13/cobra/command_notwin.go
  27. +20 −0 vendor/github.com/spf13/cobra/command_win.go
  28. +126 −0 vendor/github.com/spf13/cobra/zsh_completions.go
  29. +2 −0 vendor/github.com/spf13/pflag/.gitignore
  30. +21 −0 vendor/github.com/spf13/pflag/.travis.yml
  31. +28 −0 vendor/github.com/spf13/pflag/LICENSE
  32. +296 −0 vendor/github.com/spf13/pflag/README.md
  33. +94 −0 vendor/github.com/spf13/pflag/bool.go
  34. +147 −0 vendor/github.com/spf13/pflag/bool_slice.go
  35. +209 −0 vendor/github.com/spf13/pflag/bytes.go
  36. +96 −0 vendor/github.com/spf13/pflag/count.go
  37. +86 −0 vendor/github.com/spf13/pflag/duration.go
  38. +128 −0 vendor/github.com/spf13/pflag/duration_slice.go
  39. +1,227 −0 vendor/github.com/spf13/pflag/flag.go
  40. +88 −0 vendor/github.com/spf13/pflag/float32.go
  41. +84 −0 vendor/github.com/spf13/pflag/float64.go
  42. +105 −0 vendor/github.com/spf13/pflag/golangflag.go
  43. +84 −0 vendor/github.com/spf13/pflag/int.go
  44. +88 −0 vendor/github.com/spf13/pflag/int16.go
  45. +88 −0 vendor/github.com/spf13/pflag/int32.go
  46. +84 −0 vendor/github.com/spf13/pflag/int64.go
  47. +88 −0 vendor/github.com/spf13/pflag/int8.go
  48. +128 −0 vendor/github.com/spf13/pflag/int_slice.go
  49. +94 −0 vendor/github.com/spf13/pflag/ip.go
  50. +148 −0 vendor/github.com/spf13/pflag/ip_slice.go
  51. +122 −0 vendor/github.com/spf13/pflag/ipmask.go
  52. +98 −0 vendor/github.com/spf13/pflag/ipnet.go
  53. +80 −0 vendor/github.com/spf13/pflag/string.go
  54. +103 −0 vendor/github.com/spf13/pflag/string_array.go
  55. +149 −0 vendor/github.com/spf13/pflag/string_slice.go
  56. +149 −0 vendor/github.com/spf13/pflag/string_to_int.go
  57. +160 −0 vendor/github.com/spf13/pflag/string_to_string.go
  58. +88 −0 vendor/github.com/spf13/pflag/uint.go
  59. +88 −0 vendor/github.com/spf13/pflag/uint16.go
  60. +88 −0 vendor/github.com/spf13/pflag/uint32.go
  61. +88 −0 vendor/github.com/spf13/pflag/uint64.go
  62. +88 −0 vendor/github.com/spf13/pflag/uint8.go
  63. +126 −0 vendor/github.com/spf13/pflag/uint_slice.go
@@ -5,8 +5,8 @@ WORKDIR /go/src/github.com/alexellis/inlets
COPY .git .git
COPY vendor vendor
COPY pkg pkg
COPY cmd cmd
COPY main.go .
COPY parse_upstream.go .

ARG GIT_COMMIT
ARG VERSION

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -32,3 +32,12 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"


[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.1"
@@ -77,15 +77,15 @@ Binaries for Linux, Darwin (MacOS) and armhf are made available via the [release
Start the tunnel server on a machine with a publicly-accessible IPv4 IP address such as a VPS.

```bash
./inlets -server=true -port=80
./inlets server --port=80
```

> Note: You can pass the `-token` argument followed by a token value to both the server and client to prevent unauthorized connections to the tunnel.
Example with token:

```bash
token=$(head -c 16 /dev/urandom | shasum | cut -d" " -f1); ./inlets -server=true -port=8090 -token="$token"
token=$(head -c 16 /dev/urandom | shasum | cut -d" " -f1); ./inlets server --port=8090 --token="$token"
```

Note down your public IPv4 IP address i.e. 192.168.0.101
@@ -108,19 +108,19 @@ port=3000 go run server.go
Start the tunnel client

```sh
./inlets -server=false \
-remote=192.168.0.101:80 \
-upstream=http://127.0.0.1:3000
./inlets client \
--remote=192.168.0.101:80 \
--upstream=http://127.0.0.1:3000
```

Replace the `-remote` with the address where your other machine is listening.
Replace the `--remote` with the address where your other machine is listening.

We now have an example service running (hash-browns), a tunnel server and a tunnel client.

So send a request to the public IP address or hostname:

```sh
./inlets -server=false -remote=192.168.0.101:80 -upstream "gateway.mydomain.tk=http://127.0.0.1:3000"
```sh
./inlets client --remote=192.168.0.101:80 --upstream "gateway.mydomain.tk=http://127.0.0.1:3000"
```

```sh
@@ -160,7 +160,7 @@ Contributions are welcome. All commits must be signed-off with `git commit -s` t

## Take things further

You can expose an OpenFaaS or OpenFaaS Cloud deployment with `inlets` - just change `-upstream=http://127.0.0.1:3000` to `-upstream=http://127.0.0.1:8080` or `-upstream=http://127.0.0.1:31112`. You can even point at an IP address inside or outside your network for instance: `-upstream=http://192.168.0.101:8080`.
You can expose an OpenFaaS or OpenFaaS Cloud deployment with `inlets` - just change `--upstream=http://127.0.0.1:3000` to `--upstream=http://127.0.0.1:8080` or `--upstream=http://127.0.0.1:31112`. You can even point at an IP address inside or outside your network for instance: `--upstream=http://192.168.0.101:8080`.

You can build a basic supervisor script for `inlets` in case of a crash, it will re-connect within 5 seconds:

@@ -169,13 +169,13 @@ In this example the Host/Client is acting as a relay for OpenFaaS running on por
Host/Client:

```sh
while [ true ] ; do sleep 5 && ./inlets -server=false -upstream=http://192.168.0.28:8080 -remote=exit.my.club ; done
while [ true ] ; do sleep 5 && ./inlets client --upstream=http://192.168.0.28:8080 --remote=exit.my.club ; done
```

Exit-node:

```sh
while [ true ] ; do sleep 5 && ./inlets -server=true -upstream=http://192.168.0.28:8080 ; done
while [ true ] ; do sleep 5 && ./inlets server --upstream=http://192.168.0.28:8080 ; done
```

### Run as a deployment on Kubernetes
@@ -200,12 +200,12 @@ spec:
imagePullPolicy: Always
command: ["./inlets"]
args:
- "-server=false"
- "-upstream=http://gateway.openfaas:8080"
- "-remote=your-public-ip"
- "client"
- "--upstream=http://gateway.openfaas:8080,http://endpoint.openfaas:9090"
- "--remote=your-public-ip"
```

Replace the line: `- "-remote=your-public-ip"` with the public IP belonging to your VPS.
Replace the line: `- "--remote=your-public-ip"` with the public IP belonging to your VPS.

### Run on a VPS

@@ -0,0 +1,96 @@
package cmd

import (
"log"
"strings"

"github.com/alexellis/inlets/pkg/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func init() {
inletsCmd.AddCommand(clientCmd)
clientCmd.Flags().StringP("remote", "r", "127.0.0.1:8000", "server address i.e. 127.0.0.1:8000")
clientCmd.Flags().StringP("upstream", "u", "", "upstream server i.e. http://127.0.0.1:3000")
clientCmd.Flags().StringP("token", "t", "", "token for authentication")
}

type UpstreamParser interface {
Parse(input string) map[string]string
}

type ArgsUpstreamParser struct {
}

func (a *ArgsUpstreamParser) Parse(input string) map[string]string {
upstreamMap := buildUpstreamMap(input)

return upstreamMap
}

func buildUpstreamMap(args string) map[string]string {
items := make(map[string]string)

entries := strings.Split(args, ",")
for _, entry := range entries {
kvp := strings.Split(entry, "=")
if len(kvp) == 1 {
items[""] = strings.TrimSpace(kvp[0])
} else {
items[strings.TrimSpace(kvp[0])] = strings.TrimSpace(kvp[1])
}
}
return items
}

// clientCmd represents the client sub command.
var clientCmd = &cobra.Command{
Use: "client",
Short: "Start the tunnel client.",
Long: `Start the tunnel client.
Example: inlets client --remote=192.168.0.101:80 --upstream=http://127.0.0.1:3000
Note: You can pass the --token argument followed by a token value to both the server and client to prevent unauthorized connections to the tunnel.`,
RunE: runClient,
}

// runClient does the actual work of reading the arguments passed to the client sub command.
func runClient(cmd *cobra.Command, _ []string) error {
upstream, err := cmd.Flags().GetString("upstream")
if err != nil {
return errors.Wrap(err, "failed to get 'upstream' value.")
}

if len(upstream) == 0 {
return errors.New("upstream is missing in the client argument.")
}

argsUpstreamParser := ArgsUpstreamParser{}
upstreamMap := argsUpstreamParser.Parse(upstream)
for k, v := range upstreamMap {
log.Printf("Upstream: %s => %s\n", k, v)
}

remote, err := cmd.Flags().GetString("remote")
if err != nil {
return errors.Wrap(err, "failed to get 'remote' value.")
}

token, err := cmd.Flags().GetString("token")
if err != nil {
return errors.Wrap(err, "failed to get 'token' value.")
}

inletsClient := client.Client{
Remote: remote,
UpstreamMap: upstreamMap,
Token: token,
}

if err := inletsClient.Connect(); err != nil {
return err
}

return nil
}
@@ -0,0 +1,71 @@
package cmd

import (
"log"

"github.com/alexellis/inlets/pkg/server"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"time"
)

func init() {
inletsCmd.AddCommand(serverCmd)
serverCmd.Flags().IntP("port", "p", 8000, "port for server")
serverCmd.Flags().StringP("token", "t", "", "token for authentication")
serverCmd.Flags().Bool("print-token", true, "prints the token in server mode")
serverCmd.Flags().String("gateway-timeout", "5s", "timeout for upstream gateway")
}

// serverCmd represents the server sub command.
var serverCmd = &cobra.Command{
Use: "server",
Short: "Start the tunnel server on a machine with a publicly-accessible IPv4 IP address such as a VPS.",
Long: `Start the tunnel server on a machine with a publicly-accessible IPv4 IP address such as a VPS.
Example: inlets server -p 80
Note: You can pass the --token argument followed by a token value to both the server and client to prevent unauthorized connections to the tunnel.`,
RunE: runServer,
}

// runServer does the actual work of reading the arguments passed to the server sub command.
func runServer(cmd *cobra.Command, _ []string) error {
token, err := cmd.Flags().GetString("token")
if err != nil {
return errors.Wrap(err, "failed to get 'token' value.")
}

printToken, err := cmd.Flags().GetBool("print-token")
if err != nil {
return errors.Wrap(err, "failed to get 'print-token' value.")
}

if len(token) > 0 && printToken {
log.Printf("Server token: %s", token)
}

gatewayTimeoutRaw, err := cmd.Flags().GetString("gateway-timeout")
if err != nil {
return errors.Wrap(err, "failed to get the 'gateway-timeout' value.")
}

gatewayTimeout, gatewayTimeoutErr := time.ParseDuration(gatewayTimeoutRaw)
if gatewayTimeoutErr != nil {
return gatewayTimeoutErr
}
log.Printf("Gateway timeout: %f secs\n", gatewayTimeout.Seconds())

port, err := cmd.Flags().GetInt("port")
if err != nil {
return errors.Wrap(err, "failed to get the 'port' value.")
}

inletsServer := server.Server{
Port: port,
GatewayTimeout: gatewayTimeout,
Token: token,
}

inletsServer.Serve()
return nil
}
@@ -8,7 +8,7 @@ Restart=always
RestartSec=1
StartLimitInterval=0
EnvironmentFile=/etc/default/inlets
ExecStart=/usr/local/bin/inlets -server=true -port=80 -token="${AUTHTOKEN}"
ExecStart=/usr/bin/inlets server -port=80 -token="${AUTHTOKEN}"

[Install]
WantedBy=multi-user.target
Oops, something went wrong.

0 comments on commit 94488e8

Please sign in to comment.
You can’t perform that action at this time.