2025-04-21 18:37:11 +10:00

123 lines
3.0 KiB
Go

package main
import (
"fmt"
"gitea.suyono.dev/suyono/netbounce/cmd/slicewriter"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"net"
"time"
)
/*
Copyright 2025 Suyono <suyono3484@gmail.com>
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.
*/
func main() {
parseFlags()
log.Debug().Msgf("Sending messages to %s server: %s", viper.GetString("protocol"), viper.GetString("server"))
sendMessages()
}
func sendMessages() {
switch viper.GetString("protocol") {
case "udp":
sendUDP()
case "tcp":
sendTCP()
default:
log.Fatal().Str("protocol", viper.GetString("protocol")).Msg("Unknown protocol")
}
}
func sendTCP() {
var (
conn net.Conn
err error
buf, b []byte
)
if conn, err = net.Dial("tcp", viper.GetString("server")); err != nil {
log.Fatal().Err(err).Msg("Failed to connect to server")
}
defer func() {
_ = conn.Close()
}()
buf = make([]byte, 4096)
for {
sb := slicewriter.NewSliceWriter(buf)
if _, err = fmt.Fprintf(sb, "client %s | %v | %s", viper.GetString("name"), time.Now(), viper.GetString("message")); err != nil {
log.Error().Err(err).Msg("Failed to build client message")
return
}
b = sb.Bytes()
if _, err = conn.Write(b); err != nil {
log.Error().Err(err).Msg("Failed to send client message")
time.Sleep(viper.GetDuration("sleep"))
continue
}
//TODO: read the server reply
}
}
func sendUDP() {
//TODO: implement this part
}
func parseFlags() {
pflag.String("name", "", "client name")
pflag.IntP("number", "n", 5, "number of messages to send; default 5; set to 0 for infinite")
pflag.DurationP("sleep", "s", 10*time.Millisecond, "sleep time between requests; default 10ms")
pflag.StringP("message", "m", "message from client", "message to send")
pflag.Bool("debug", false, "run in debug mode")
pflag.String("tcp", "", "tcp server address")
pflag.String("udp", "", "udp server address")
pflag.Parse()
_ = viper.BindPFlags(pflag.CommandLine)
if !viper.IsSet("name") {
log.Fatal().Msg("server name is required")
}
if viper.IsSet("tcp") && viper.IsSet("udp") {
log.Fatal().Msg("cannot use tcp and udp at once")
}
if !viper.IsSet("tcp") && !viper.IsSet("udp") {
log.Fatal().Msg("server address is required")
}
if viper.IsSet("tcp") {
viper.Set("protocol", "tcp")
viper.Set("server", viper.GetString("tcp"))
}
if viper.IsSet("udp") {
viper.Set("protocol", "udp")
viper.Set("server", viper.GetString("udp"))
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if viper.GetBool("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
}