Skip to content

Commit

Permalink
Add --rm flag to "inletsctl create"
Browse files Browse the repository at this point in the history
The --rm flag (default true) will enable to point to
an upstream or a remote-tcp and will delete the exit-node
on a SIGINT (control + c)

Fixes inlets#41

Signed-off-by: Utsav Anand <[email protected]>
  • Loading branch information
utsavanand2 committed Feb 1, 2020
1 parent 00d03e0 commit e8fb6f8
Showing 1 changed file with 149 additions and 36 deletions.
185 changes: 149 additions & 36 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

names "github.com/inlets/inletsctl/pkg/names"
Expand Down Expand Up @@ -38,6 +42,10 @@ func init() {
createCmd.Flags().StringP("remote-tcp", "c", "", `Remote host for inlets-pro to use for forwarding TCP connections`)

createCmd.Flags().DurationP("poll", "n", time.Second*2, "poll every N seconds, use a higher value if you encounter rate-limiting")

createCmd.Flags().Bool("rm", true, "Delete the exit node on")
createCmd.Flags().StringP("upstream", "u", "http://127.0.0.1:3000", "")
createCmd.Flags().StringP("license", "l", "", "The license key for inlets-pro")
}

// clientCmd represents the client sub command.
Expand All @@ -61,6 +69,11 @@ along with what OS version and spec will be used is explained in the README.

func runCreate(cmd *cobra.Command, _ []string) error {

tempFlag, err := cmd.Flags().GetBool("rm")
if err != nil {
return fmt.Errorf("cannot get value for --rm flag: %v", err)
}

provider, err := cmd.Flags().GetString("provider")
if err != nil {
return errors.Wrap(err, "failed to get 'provider' value.")
Expand Down Expand Up @@ -139,15 +152,19 @@ func runCreate(cmd *cobra.Command, _ []string) error {
}

remoteTCP, _ := cmd.Flags().GetString("remote-tcp")
upstream, _ := cmd.Flags().GetString("upstream")

var pro bool
var inletsProLicenseKey string
if len(remoteTCP) > 0 {
pro = true
inletsProLicenseKey, _ = cmd.Flags().GetString("license")
}


name := strings.Replace(names.GetRandomName(10), "_", "-", -1)

inletsControlPort := 8080
proPort := 8123

userData := makeUserdata(inletsToken, inletsControlPort, remoteTCP)

Expand Down Expand Up @@ -184,42 +201,87 @@ func runCreate(cmd *cobra.Command, _ []string) error {
i+1, max, hostStatus.ID, hostStatus.Status)

if hostStatus.Status == "active" {
if !pro {
fmt.Printf(`Inlets OSS exit-node summary:
IP: %s
Auth-token: %s
Command:
export UPSTREAM=http://127.0.0.1:8000
inlets client --remote "ws://%s:%d" \
--token "%s" \
--upstream $UPSTREAM
To Delete:
inletsctl delete --provider %s --id "%s"
`,
hostStatus.IP, inletsToken, hostStatus.IP, inletsControlPort, inletsToken, provider, hostStatus.ID)
return nil
if tempFlag == true {
// TODO: Invoke inlets client to create a temp tunnel
sig := make(chan os.Signal, 1)
done := make(chan bool, 1)

signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

go func() {
sigval := <-sig
fmt.Printf("\n%v\n", sigval)
done <- true
}()

fmt.Printf("Your IP is: %s\n", hostStatus.IP)

port := inletsControlPort
if pro {
port = proPort
}

var err error = nil
if pro {
err = runInletsClient(pro, hostStatus.IP, remoteTCP, port, inletsToken, inletsProLicenseKey)
} else {
err = runInletsClient(pro, hostStatus.IP, upstream, port, inletsToken, "")
}
if err != nil {
return fmt.Errorf("Error running inlets: %v", err)
}

<-done
hostDelReq := provision.HostDeleteRequest{
ID: hostStatus.ID,
IP: hostStatus.IP,
ProjectID: projectID,
Zone: zone,
}
fmt.Println("Deleting tunnel")
err = provisioner.Delete(hostDelReq)
if err != nil {
return fmt.Errorf("error deleting the exitnode: %v", err)
}
fmt.Println("exiting")

} else {
if !pro {
fmt.Printf(`Inlets OSS exit-node summary:
IP: %s
Auth-token: %s
Command:
export UPSTREAM=http://127.0.0.1:8000
inlets client --remote "ws://%s:%d" \
--token "%s" \
--upstream $UPSTREAM
To Delete:
inletsctl delete --provider %s --id "%s"
`,
hostStatus.IP, inletsToken, hostStatus.IP, inletsControlPort, inletsToken, provider, hostStatus.ID)
return nil
}

fmt.Printf(`inlets-pro exit-node summary:
IP: %s
Auth-token: %s
Command:
export TCP_PORTS="8000"
export LICENSE=""
inlets-pro client --connect "wss://%s:%d/connect" \
--token "%s" \
--license "$LICENSE" \
--tcp-ports $TCP_PORTS
To Delete:
inletsctl delete --provider %s --id "%s"
`,
hostStatus.IP, inletsToken, hostStatus.IP, proPort, inletsToken, provider, hostStatus.ID)
}

proPort := 8123
fmt.Printf(`inlets-pro exit-node summary:
IP: %s
Auth-token: %s
Command:
export TCP_PORTS="8000"
export LICENSE=""
inlets-pro client --connect "wss://%s:%d/connect" \
--token "%s" \
--license "$LICENSE" \
--tcp-ports $TCP_PORTS
To Delete:
inletsctl delete --provider %s --id "%s"
`,
hostStatus.IP, inletsToken, hostStatus.IP, proPort, inletsToken, provider, hostStatus.ID)

return nil
}
}
Expand Down Expand Up @@ -313,7 +375,7 @@ func createHost(provider, name, region, zone, projectID, userData, inletsPort st
UserData: base64.StdEncoding.EncodeToString([]byte(userData)),
Additional: map[string]string{
"inlets-port": inletsPort,
"pro": fmt.Sprint(pro),
"pro": fmt.Sprint(pro),
},
}, nil
}
Expand Down Expand Up @@ -381,3 +443,54 @@ func getFileOrString(flags *pflag.FlagSet, file, value string, required bool) (s

return val, nil
}

func checkIfInletsIsInstalled(usingPro bool) (bool, error) {
basePath := "/usr/local/bin/%s"
if usingPro {
basePath = fmt.Sprintf(basePath, "inlets-pro")
} else {
basePath = fmt.Sprintf(basePath, "inlets")
}

fileInfo, err := os.Stat(basePath)
if err != nil {
return false, fmt.Errorf("Error finding file: %v", err)
}

if strings.SplitAfter(basePath, "/usr/local/bin/")[1] == fileInfo.Name() {
return true, nil
} else {
return false, nil
}

}

func runInletsClient(pro bool, exitNodeIP string, upstream string, inletsControlPort int, authToken string, license string) error {
installed, err := checkIfInletsIsInstalled(pro)
if err != nil {
return fmt.Errorf("could not check if inlets is installed: %v", err)
}

if !installed {
return fmt.Errorf("inlets/inlets-pro not installed")
}

if !pro {
fmt.Printf("Starting 'inlets client' now, hit control+c to delete the tunnel\n")
cmd := exec.Command("inlets", "client", "--remote", fmt.Sprintf("ws://%s:%d", exitNodeIP, inletsControlPort),
"--token", authToken, "--upstream", upstream)
err = cmd.Run()

} else {
fmt.Printf("Starting 'inlets-pro client' now, hit control+c to delete the tunnel\n")
cmd := exec.Command("inlets-pro", "client", "--connect", fmt.Sprintf("wss://%s:%d", exitNodeIP, inletsControlPort),
"--token", authToken, "--license", license, "--remote-tcp", upstream)
err = cmd.Run()
}

if err != nil && fmt.Sprintf("%s", err) != "signal: interrupt" {
return fmt.Errorf("%v", err)
}

return nil
}

0 comments on commit e8fb6f8

Please sign in to comment.