Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cedws committed Aug 9, 2023
1 parent 5f2d18f commit 886cf8d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
9 changes: 8 additions & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

var (
debug bool
compress bool
listen string
project string
Expand All @@ -19,9 +20,14 @@ var (
var rootCmd = &cobra.Command{
Use: "iapc",
Long: "Utility for Google Cloud's Identity-Aware Proxy",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if debug {
log.SetLevel(log.DebugLevel)
}
},
}

func getTokenSource() *oauth2.TokenSource {
func tokenSource() *oauth2.TokenSource {
tokenSource, err := google.DefaultTokenSource(context.Background())
if err != nil {
log.Fatal(err)
Expand All @@ -30,6 +36,7 @@ func getTokenSource() *oauth2.TokenSource {
}

func init() {
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug logging")
rootCmd.PersistentFlags().BoolVarP(&compress, "compress", "c", false, "Enable WebSocket compression")
rootCmd.PersistentFlags().StringVarP(&listen, "listen", "l", "127.0.0.1:0", "Listen address and port")
rootCmd.PersistentFlags().StringVar(&project, "project", "", "Project ID")
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/to_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ var hostCmd = &cobra.Command{
Long: "Create a tunnel to a remote private IP or FQDN (requires BeyondCorp Enterprise)",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
log.Info("Starting proxy", "dest", fmt.Sprintf("%v:%v", args[0], port), "project", project)
log.Info("Starting proxy", "dest", fmt.Sprintf("%v:%v", args[0], port), "port", port, "project", project)
},
Run: func(cmd *cobra.Command, args []string) {
opts := []iap.DialOption{
iap.WithProject(project),
iap.WithHost(args[0], region, network, destGroup),
iap.WithPort(fmt.Sprint(port)),
iap.WithTokenSource(getTokenSource()),
iap.WithTokenSource(tokenSource()),
}
if compress {
opts = append(opts, iap.WithCompression())
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/to_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ var instanceCmd = &cobra.Command{
Long: "Create a tunnel to a remote Compute Engine instance",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
log.Info("Starting proxy", "dest", fmt.Sprintf("%v:%v", args[0], port), "project", project)
log.Info("Starting proxy", "dest", fmt.Sprintf("%v:%v", args[0], port), "port", port, "project", project)
},
Run: func(cmd *cobra.Command, args []string) {
opts := []iap.DialOption{
iap.WithProject(project),
iap.WithInstance(args[0], zone, ninterface),
iap.WithPort(fmt.Sprint(port)),
iap.WithTokenSource(getTokenSource()),
iap.WithTokenSource(tokenSource()),
}
if compress {
opts = append(opts, iap.WithCompression())
Expand Down
5 changes: 4 additions & 1 deletion internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ func testConn(opts []iap.DialOption) error {
func handleClient(opts []iap.DialOption, conn net.Conn) {
log.Info("Client connected", "client", conn.RemoteAddr())

log.Debug("Dialing IAP", "client", conn.RemoteAddr())

tun, err := iap.Dial(context.Background(), opts...)
if err != nil {
log.Errorf("Error dialing IAP: %v", err)
return
}
defer tun.Close()
log.Info("Established connection with proxy", "client", conn.RemoteAddr(), "sid", tun.SessionID())

log.Debug("Dialed IAP", "client", conn.RemoteAddr(), "sid", tun.SessionID())

go io.Copy(conn, tun)
io.Copy(tun, conn)
Expand Down

0 comments on commit 886cf8d

Please sign in to comment.