mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #98639 from knabben/udp-connect-agnhost
UDP protocol on connect agnhost command
This commit is contained in:
commit
ab5a8c4af8
@ -1,7 +1,7 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
# agnhost: bump this one first
|
# agnhost: bump this one first
|
||||||
- name: "agnhost"
|
- name: "agnhost"
|
||||||
version: "2.27"
|
version: "2.28"
|
||||||
refPaths:
|
refPaths:
|
||||||
- path: test/images/agnhost/VERSION
|
- path: test/images/agnhost/VERSION
|
||||||
match: \d.\d
|
match: \d.\d
|
||||||
|
@ -1 +1 @@
|
|||||||
2.27
|
2.28
|
||||||
|
@ -51,7 +51,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
rootCmd := &cobra.Command{
|
rootCmd := &cobra.Command{
|
||||||
Use: "app",
|
Use: "app",
|
||||||
Version: "2.27",
|
Version: "2.28",
|
||||||
}
|
}
|
||||||
|
|
||||||
rootCmd.AddCommand(auditproxy.CmdAuditProxy)
|
rootCmd.AddCommand(auditproxy.CmdAuditProxy)
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -30,8 +31,8 @@ import (
|
|||||||
// CmdConnect is used by agnhost Cobra.
|
// CmdConnect is used by agnhost Cobra.
|
||||||
var CmdConnect = &cobra.Command{
|
var CmdConnect = &cobra.Command{
|
||||||
Use: "connect [host:port]",
|
Use: "connect [host:port]",
|
||||||
Short: "Attempts a TCP or SCTP connection and returns useful errors",
|
Short: "Attempts a TCP, UDP or SCTP connection and returns useful errors",
|
||||||
Long: `Tries to open a TCP or SCTP connection to the given host and port. On error it prints an error message prefixed with a specific fixed string that test cases can check for:
|
Long: `Tries to open a TCP, UDP or SCTP connection to the given host and port. On error it prints an error message prefixed with a specific fixed string that test cases can check for:
|
||||||
|
|
||||||
* UNKNOWN - Generic/unknown (non-network) error (eg, bad arguments)
|
* UNKNOWN - Generic/unknown (non-network) error (eg, bad arguments)
|
||||||
* TIMEOUT - The connection attempt timed out
|
* TIMEOUT - The connection attempt timed out
|
||||||
@ -42,12 +43,16 @@ var CmdConnect = &cobra.Command{
|
|||||||
Run: main,
|
Run: main,
|
||||||
}
|
}
|
||||||
|
|
||||||
var timeout time.Duration
|
var (
|
||||||
var protocol string
|
timeout time.Duration
|
||||||
|
protocol string
|
||||||
|
udpData string
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
CmdConnect.Flags().DurationVar(&timeout, "timeout", time.Duration(0), "Maximum time before returning an error")
|
CmdConnect.Flags().DurationVar(&timeout, "timeout", time.Duration(0), "Maximum time before returning an error")
|
||||||
CmdConnect.Flags().StringVar(&protocol, "protocol", "tcp", "The protocol to use to perform the connection, can be tcp or sctp")
|
CmdConnect.Flags().StringVar(&protocol, "protocol", "tcp", "The protocol to use to perform the connection, can be tcp, udp or sctp")
|
||||||
|
CmdConnect.Flags().StringVar(&udpData, "udp-data", "hostname", "The UDP payload send to the server")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main(cmd *cobra.Command, args []string) {
|
func main(cmd *cobra.Command, args []string) {
|
||||||
@ -55,6 +60,8 @@ func main(cmd *cobra.Command, args []string) {
|
|||||||
switch protocol {
|
switch protocol {
|
||||||
case "", "tcp":
|
case "", "tcp":
|
||||||
connectTCP(dest, timeout)
|
connectTCP(dest, timeout)
|
||||||
|
case "udp":
|
||||||
|
connectUDP(dest, timeout, udpData)
|
||||||
case "sctp":
|
case "sctp":
|
||||||
connectSCTP(dest, timeout)
|
connectSCTP(dest, timeout)
|
||||||
default:
|
default:
|
||||||
@ -125,3 +132,54 @@ func connectSCTP(dest string, timeout time.Duration) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func connectUDP(dest string, timeout time.Duration, data string) {
|
||||||
|
var (
|
||||||
|
readBytes int
|
||||||
|
buf = make([]byte, 1024)
|
||||||
|
)
|
||||||
|
|
||||||
|
if _, err := net.ResolveUDPAddr("udp", dest); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "DNS: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.Dial("udp", dest)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "OTHER: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
|
if err = conn.SetDeadline(time.Now().Add(timeout)); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "OTHER: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = conn.Write([]byte(fmt.Sprintf("%s\n", data))); err != nil {
|
||||||
|
parseUDPErrorAndExit(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if readBytes, err = conn.Read(buf); err != nil {
|
||||||
|
parseUDPErrorAndExit(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure the response from UDP server
|
||||||
|
if readBytes == 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "OTHER: No data received from the server. Cannot guarantee the server received the request.\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseUDPErrorAndExit(err error) {
|
||||||
|
neterr, ok := err.(net.Error)
|
||||||
|
if ok && neterr.Timeout() {
|
||||||
|
fmt.Fprintf(os.Stderr, "TIMEOUT: %v\n", err)
|
||||||
|
} else if strings.Contains(err.Error(), "connection refused") {
|
||||||
|
fmt.Fprintf(os.Stderr, "REFUSED: %v\n", err)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "UNKNOWN: %v\n", err)
|
||||||
|
}
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user