-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
localproxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |