Skip to content

Commit

Permalink
tunnel: add localproxy command
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat committed May 8, 2020
1 parent 1413c9b commit ca414b6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions tunnel/cmd/localproxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
localproxy
68 changes: 68 additions & 0 deletions tunnel/cmd/localproxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"flag"
"fmt"
"io"
"log"
"net"

"github.com/seqsense/aws-iot-device-sdk-go/v4/tunnel"
)

var (
accessToken = flag.String("access-token", "", "Client access token")
proxyEndpoint = flag.String("proxy-endpoint", "", "Endpoint of proxy server (e.g. data.tunneling.iot.ap-northeast-1.amazonaws.com:443)")
region = flag.String("region", "", "Endpoint region. Exclusive flag with -proxy-endpoint")
sourcePort = flag.Int("source-listen-port", 0, "Assigns source mode and sets the port to listen")
destinationApp = flag.String("destination-app", "", "Assigns destination mode and set the endpoint in address:port format")
noSSLHostVerify = flag.Bool("no-ssl-host-verify", false, "Turn off SSL host verification")
proxyScheme = flag.String("proxy-scheme", "wss", "Proxy server protocol scheme")
)

func main() {
flag.Parse()

if *accessToken == "" {
log.Fatal("error: -access-token must be specified")
}

var endpoint string
switch {
case *proxyEndpoint != "" && *region == "":
endpoint = *proxyEndpoint
case *region != "" && *proxyEndpoint == "":
endpoint = fmt.Sprintf("data.tunneling.iot.%s.amazonaws.com", *region)
default:
log.Fatal("error: one of -proxy-endpoint or -region must be specified")
}

proxyOpt := func(opt *tunnel.ProxyOptions) error {
opt.NoTLS = *noSSLHostVerify
opt.Scheme = *proxyScheme
return nil
}

switch {
case *sourcePort > 0 && *destinationApp == "":
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", *sourcePort))
if err != nil {
log.Fatalf("error: %v", err)
}
err = tunnel.ProxySource(listener, endpoint, *accessToken, proxyOpt)
if err != nil {
log.Fatalf("error: %v", err)
}

case *destinationApp != "" && *sourcePort == 0:
err := tunnel.ProxyDestination(func() (io.ReadWriteCloser, error) {
return net.Dial("tcp", *destinationApp)
}, endpoint, *accessToken, proxyOpt)
if err != nil {
log.Fatalf("error: %v", err)
}

default:
log.Fatal("error: one of -source-listen-port or -destination-app must be specified")
}
}

0 comments on commit ca414b6

Please sign in to comment.