Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jun 24, 2022
1 parent 3e4bad2 commit d9babfb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
16 changes: 6 additions & 10 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *PairedConnection) handleServerMessage() {
func (c *PairedConnection) process() {
defer c.stop()

conn, err := net.Dial("tcp", settings.RemoteHost)
conn, err := net.Dial("tcp", settings.Remote)
if err != nil {
display.PrintlnWithTime(color.HiRedString("[x][%d] Couldn't connect to server: %v", c.id, err))
return
Expand Down Expand Up @@ -100,19 +100,14 @@ func (c *PairedConnection) stop() {
}

func startListener() error {
listenerPort := settings.LocalPort
conn, err := net.Listen("tcp", fmt.Sprint(settings.LocalHost, ":", settings.LocalPort))
conn, err := net.Listen("tcp", fmt.Sprintf("%s:%d", settings.LocalHost, settings.LocalPort))
if err != nil {
return fmt.Errorf("failed to start listener: %w", err)
}

if listenerPort == 0 {
listenerPort = conn.Addr().(*net.TCPAddr).Port
}
display.PrintfWithTime("Listening on %s...\n", fmt.Sprint(settings.LocalHost, ":", listenerPort))

defer conn.Close()

display.PrintfWithTime("Listening on %s...\n", conn.Addr().String())

var connIndex int
for {
cliConn, err := conn.Accept()
Expand All @@ -121,7 +116,8 @@ func startListener() error {
}

connIndex++
display.PrintlnWithTime(color.HiGreenString("[%d] Accepted from: %s", connIndex, cliConn.RemoteAddr()))
display.PrintlnWithTime(color.HiGreenString("[%d] Accepted from: %s",
connIndex, cliConn.RemoteAddr()))

pconn := NewPairedConnection(connIndex, cliConn)
go pconn.process()
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func main() {
flag.Parse()
saveSettings(*localHost, *localPort, *remote, *delay, *protocol, *quiet)

if settings.RemoteHost == "" {
fmt.Fprintln(os.Stderr, color.HiRedString("[x] Remote host required"))
if len(settings.Remote) == 0 {
fmt.Fprintln(os.Stderr, color.HiRedString("[x] Remote target required"))
flag.PrintDefaults()
os.Exit(1)
}
Expand Down
18 changes: 9 additions & 9 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ package main
import "time"

type Settings struct {
RemoteHost string
LocalHost string
LocalPort int
Delay time.Duration
Protocol string
Quiet bool
Remote string
LocalHost string
LocalPort int
Delay time.Duration
Protocol string
Quiet bool
}

func saveSettings(localHost string, localPort int, remoteHost string, delay time.Duration,
func saveSettings(localHost string, localPort int, remote string, delay time.Duration,
protocol string, quiet bool) {
if localHost != "" {
settings.LocalHost = localHost
}
if localPort != 0 {
settings.LocalPort = localPort
}
if remoteHost != "" {
settings.RemoteHost = remoteHost
if remote != "" {
settings.Remote = remote
}
settings.Delay = delay
settings.Protocol = protocol
Expand Down

0 comments on commit d9babfb

Please sign in to comment.