-
Notifications
You must be signed in to change notification settings - Fork 16
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
6 changed files
with
215 additions
and
9 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
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 |
---|---|---|
@@ -1 +1 @@ | ||
1.14.0 | ||
1.16.4 |
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
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,111 @@ | ||
// Copyright 2018 SumUp Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/palantir/stacktrace" | ||
"github.com/spf13/cobra" | ||
"github.com/sumup-oss/go-pkgs/logger" | ||
|
||
"github.com/sumup-oss/gocat/internal/relay" | ||
) | ||
|
||
func NewTCPToTCPcmd(logger logger.Logger) *cobra.Command { | ||
var tcpSourceAddress string | ||
var tcpDestinationAddress string | ||
var bufferSize int | ||
var tcpToUnixHealthCheckInterval time.Duration | ||
|
||
cmdInstance := &cobra.Command{ | ||
Use: "tcp-to-tcp", | ||
Short: "relay from a TCP source to TCP destination", | ||
Long: `relay from a TCP source to TCP destination`, | ||
RunE: func(command *cobra.Command, args []string) error { | ||
// nolint: gocritic | ||
if len(tcpSourceAddress) < 0 { | ||
return stacktrace.NewError("blank/empty `src` specified") | ||
} | ||
|
||
// nolint: gocritic | ||
if len(tcpDestinationAddress) < 0 { | ||
return stacktrace.NewError("blank/empty `dst` specified") | ||
} | ||
|
||
relayer, err := relay.NewTCPtoTCPsocket( | ||
logger, | ||
tcpToUnixHealthCheckInterval, | ||
tcpSourceAddress, | ||
tcpDestinationAddress, | ||
bufferSize, | ||
) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "couldn't create relay from TCP to TCP") | ||
} | ||
|
||
osSignalCh := make(chan os.Signal, 1) | ||
defer close(osSignalCh) | ||
|
||
signal.Notify(osSignalCh, os.Interrupt, syscall.SIGTERM) | ||
|
||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
defer cancelFunc() | ||
|
||
// Ctrl+C handler | ||
go func() { | ||
<-osSignalCh | ||
signal.Stop(osSignalCh) | ||
|
||
_ = os.RemoveAll(tcpSourceAddress) | ||
cancelFunc() | ||
}() | ||
|
||
err = relayer.Relay(ctx) | ||
if err != nil { | ||
_ = os.RemoveAll(tcpSourceAddress) | ||
return stacktrace.Propagate(err, "couldn't relay from TCP to TCP") | ||
} | ||
|
||
_ = os.RemoveAll(tcpSourceAddress) | ||
return nil | ||
}, | ||
} | ||
|
||
cmdInstance.Flags().DurationVar( | ||
&tcpToUnixHealthCheckInterval, | ||
"health-check-interval", | ||
30*time.Second, | ||
"health check interval for `src`, e.g values are 30m, 60s, 1h.", | ||
) | ||
|
||
cmdInstance.Flags().StringVar(&tcpSourceAddress, "src", "", "source of TCP address") | ||
_ = cmdInstance.MarkFlagRequired("src") | ||
|
||
cmdInstance.Flags().StringVar(&tcpDestinationAddress, "dst", "", "destination of TCP address") | ||
_ = cmdInstance.MarkFlagRequired("dst") | ||
|
||
cmdInstance.Flags().IntVar( | ||
&bufferSize, | ||
"buffer-size", | ||
DefaultBufferSize, | ||
"Buffer size in bytes of the data stream", | ||
) | ||
return cmdInstance | ||
} |
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
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,101 @@ | ||
// Copyright 2018 SumUp Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package relay | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/palantir/stacktrace" | ||
"github.com/sumup-oss/go-pkgs/logger" | ||
) | ||
|
||
type TCPtoTCPsocket struct { | ||
AbstractDuplexRelay | ||
} | ||
|
||
func NewTCPtoTCPsocket( | ||
logger logger.Logger, | ||
healthCheckInterval time.Duration, | ||
srcAddress, | ||
dstAddress string, | ||
bufferSize int, | ||
) (*TCPtoTCPsocket, error) { | ||
tcpAddressParts := strings.Split(srcAddress, ":") | ||
if len(tcpAddressParts) != 2 { | ||
return nil, stacktrace.NewError( | ||
"wrong format for tcp address %s. Expected <addr>:<port>", | ||
srcAddress, | ||
) | ||
} | ||
|
||
_, err := strconv.ParseInt(tcpAddressParts[1], 10, 32) | ||
if err != nil { | ||
return nil, stacktrace.Propagate( | ||
err, | ||
"could not parse specified port number %s", | ||
tcpAddressParts[1], | ||
) | ||
} | ||
|
||
return &TCPtoTCPsocket{ | ||
AbstractDuplexRelay{ | ||
healthCheckInterval: healthCheckInterval, | ||
logger: logger, | ||
sourceName: "source TCP connection", | ||
destinationName: "destination TCP connection", | ||
destinationAddr: dstAddress, | ||
bufferSize: bufferSize, | ||
dialSourceConn: func(ctx context.Context) (net.Conn, error) { | ||
dialer := &net.Dialer{ | ||
KeepAlive: tcpKeepAlivePeriod, | ||
} | ||
conn, err := dialer.DialContext( | ||
ctx, | ||
"tcp", | ||
srcAddress, | ||
) | ||
if err != nil { | ||
return nil, stacktrace.Propagate( | ||
err, | ||
"failed to dial TCP address: %s", | ||
srcAddress, | ||
) | ||
} | ||
|
||
tcpConn := conn.(*net.TCPConn) | ||
// TODO: Re-evaluate if this is redundant when `KeepAlive` and `net.Dialer` is used. | ||
_ = tcpConn.SetKeepAlive(true) | ||
_ = tcpConn.SetKeepAlivePeriod(tcpKeepAlivePeriod) | ||
return tcpConn, nil | ||
}, | ||
listenTargetConn: func(ctx context.Context) (net.Listener, error) { | ||
var lc net.ListenConfig | ||
listener, err := lc.Listen(ctx, "tcp", dstAddress) | ||
if err != nil { | ||
return nil, stacktrace.Propagate( | ||
err, | ||
"failed to listen at destination TCP address: %s", | ||
dstAddress, | ||
) | ||
} | ||
return listener, nil | ||
}, | ||
}, | ||
}, nil | ||
} |