Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
piontec committed May 18, 2023
0 parents commit deb6190
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
134 changes: 134 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
project_name: kube-echo
builds:
- <<: &build_defaults
binary: kube-echo
main: main.go
ldflags:
- -s -w -X main.VERSION={{ .Version }}
env:
- CGO_ENABLED=0
id: linux
goos:
- linux
goarch:
- amd64
- arm64
- arm
goarm:
- 7
- <<: *build_defaults
id: darwin
goos:
- darwin
goarch:
- amd64
- arm64
- <<: *build_defaults
id: windows
goos:
- windows
archives:
- name_template: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
format_overrides:
- goos: windows
format: zip
id: all
rlcp: true
builds: [windows, linux, darwin]
format: tar.gz
files:
- none*
#source:
# enabled: true
# name_template: '{{ .ProjectName }}_{{ .Version }}_source_code'
sboms:
- id: source
artifacts: archive
documents:
- "{{ .ProjectName }}_{{ .Version }}_sbom.spdx.json"
#release: {}
#checksum: {}
#signs:
# - cmd: cosign
# env:
# - COSIGN_EXPERIMENTAL=1
# certificate: '${artifact}.pem'
# args:
# - sign-blob
# - "--yes"
# - '--output-certificate=${certificate}'
# - '--output-signature=${signature}'
# - '${artifact}'
# artifacts: checksum
# output: true
#dockers:
#- image_templates:
# - 'fluxcd/flux-cli:{{ .Tag }}-amd64'
# - 'ghcr.io/fluxcd/flux-cli:{{ .Tag }}-amd64'
# dockerfile: Dockerfile
# use: buildx
# goos: linux
# goarch: amd64
# build_flag_templates:
# - "--pull"
# - "--build-arg=ARCH=linux/amd64"
# - "--label=org.opencontainers.image.created={{ .Date }}"
# - "--label=org.opencontainers.image.name={{ .ProjectName }}"
# - "--label=org.opencontainers.image.revision={{ .FullCommit }}"
# - "--label=org.opencontainers.image.version={{ .Version }}"
# - "--label=org.opencontainers.image.source={{ .GitURL }}"
# - "--platform=linux/amd64"
#- image_templates:
# - 'fluxcd/flux-cli:{{ .Tag }}-arm64'
# - 'ghcr.io/fluxcd/flux-cli:{{ .Tag }}-arm64'
# dockerfile: Dockerfile
# use: buildx
# goos: linux
# goarch: arm64
# build_flag_templates:
# - "--pull"
# - "--build-arg=ARCH=linux/arm64"
# - "--label=org.opencontainers.image.created={{ .Date }}"
# - "--label=org.opencontainers.image.name={{ .ProjectName }}"
# - "--label=org.opencontainers.image.revision={{ .FullCommit }}"
# - "--label=org.opencontainers.image.version={{ .Version }}"
# - "--label=org.opencontainers.image.source={{ .GitURL }}"
# - "--platform=linux/arm64"
#- image_templates:
# - 'fluxcd/flux-cli:{{ .Tag }}-arm'
# - 'ghcr.io/fluxcd/flux-cli:{{ .Tag }}-arm'
# dockerfile: Dockerfile
# use: buildx
# goos: linux
# goarch: arm
# goarm: 7
# build_flag_templates:
# - "--pull"
# - "--build-arg=ARCH=linux/arm"
# - "--label=org.opencontainers.image.created={{ .Date }}"
# - "--label=org.opencontainers.image.name={{ .ProjectName }}"
# - "--label=org.opencontainers.image.revision={{ .FullCommit }}"
# - "--label=org.opencontainers.image.version={{ .Version }}"
# - "--label=org.opencontainers.image.source={{ .GitURL }}"
# - "--platform=linux/arm/v7"
#docker_manifests:
#- name_template: 'fluxcd/flux-cli:{{ .Tag }}'
# image_templates:
# - 'fluxcd/flux-cli:{{ .Tag }}-amd64'
# - 'fluxcd/flux-cli:{{ .Tag }}-arm64'
# - 'fluxcd/flux-cli:{{ .Tag }}-arm'
#- name_template: 'ghcr.io/fluxcd/flux-cli:{{ .Tag }}'
# image_templates:
# - 'ghcr.io/fluxcd/flux-cli:{{ .Tag }}-amd64'
# - 'ghcr.io/fluxcd/flux-cli:{{ .Tag }}-arm64'
# - 'ghcr.io/fluxcd/flux-cli:{{ .Tag }}-arm'
#docker_signs:
# - cmd: cosign
# env:
# - COSIGN_EXPERIMENTAL=1
# args:
# - sign
# - "--yes"
# - '${artifact}'
# artifacts: all
# output: true
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/piontec/kube-echo

go 1.20
111 changes: 111 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"log"
"net"
"os"
)

func handleUDPConnection(conn *net.UDPConn, logEnabled bool) {
buffer := make([]byte, 1024)

n, addr, err := conn.ReadFromUDP(buffer)
if err != nil {
log.Println("Error reading UDP message:", err)
return
}

message := string(buffer[:n])
if logEnabled {
log.Printf("UDP received from %s: %s\n", addr.String(), message)
}

_, err = conn.WriteToUDP(buffer[:n], addr)
if err != nil {
log.Println("Error sending UDP response:", err)
return
}
}

func handleTCPConnection(conn net.Conn, logEnabled bool) {
buffer := make([]byte, 1024)

n, err := conn.Read(buffer)
if err != nil {
log.Println("Error reading TCP message:", err)
return
}

message := string(buffer[:n])
if logEnabled {
log.Printf("TCP received from %s: %s\n", conn.RemoteAddr().String(), message)
}

_, err = conn.Write(buffer[:n])
if err != nil {
log.Println("Error sending TCP response:", err)
conn.Close()
return
}

conn.Close()
}

func main() {
listeningPort := os.Getenv("LISTEN_PORT")
if listeningPort == "" {
listeningPort = "7777"
}

logEnabled := os.Getenv("LOG_ENABLED") == "true"

// UDP
udpAddr, err := net.ResolveUDPAddr("udp", "localhost:"+listeningPort)
if err != nil {
log.Println("Error resolving UDP address:", err)
os.Exit(1)
}

udpConn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
log.Println("Error listening on UDP:", err)
os.Exit(1)
}

defer udpConn.Close()

log.Println("UDP Echo Server listening on", udpAddr.String())

go func() {
for {
handleUDPConnection(udpConn, logEnabled)
}
}()

// TCP
tcpAddr, err := net.ResolveTCPAddr("tcp", "localhost:"+listeningPort)
if err != nil {
log.Println("Error resolving TCP address:", err)
os.Exit(1)
}

tcpListener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Println("Error listening on TCP:", err)
os.Exit(1)
}

defer tcpListener.Close()

log.Println("TCP Echo Server listening on", tcpAddr.String())

for {
conn, err := tcpListener.Accept()
if err != nil {
log.Println("Error accepting TCP connection:", err)
continue
}

go handleTCPConnection(conn, logEnabled)
}
}

0 comments on commit deb6190

Please sign in to comment.