Skip to content

Commit

Permalink
Add workers flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jhead committed Jul 15, 2020
1 parent 5f84819 commit 962a598
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/phantom.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
debugArg := flag.Bool("debug", false, "Optional: Enables debug logging")
ipv6Arg := flag.Bool("6", false, "Optional: Enables IPv6 support on port 19133 (experimental)")
removePortsArg := flag.Bool("remove_ports", false, "Optional: Forces ports to be excluded from pong packets (experimental)")
workersArg := flag.Uint("workers", 1, "Optional: Number of workers, useful for tweaking performance (experimental)")

flag.Usage = usage
flag.Parse()
Expand Down Expand Up @@ -66,6 +67,7 @@ func main() {
idleTimeout,
*ipv6Arg,
*removePortsArg,
*workersArg,
})

if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ProxyPrefs struct {
IdleTimeout time.Duration
EnableIPv6 bool
RemovePorts bool
NumWorkers uint
}

var randSource = rand.NewSource(time.Now().UnixNano())
Expand Down Expand Up @@ -125,7 +126,7 @@ func (proxy *ProxyServer) Start() error {
log.Info().Msgf("Once your console pings phantom, you should see replies below.")

// Start processing everything else using the proxy listener
proxy.readLoop(proxy.server)
proxy.startWorkers(proxy.server)

return nil
}
Expand All @@ -148,9 +149,23 @@ func (proxy *ProxyServer) Close() {
proxy.dead.Set()
}

func (proxy *ProxyServer) startWorkers(listener net.PacketConn) {
log.Info().Msgf("Starting %d workers", proxy.prefs.NumWorkers)

for i := uint(0); i < proxy.prefs.NumWorkers; i++ {
if i < proxy.prefs.NumWorkers-1 {
go proxy.readLoop(listener)
} else {
proxy.readLoop(listener)
}
}
}

// Continually reads data from the provided listener and passes it to
// processDataFromClients until the ProxyServer has been closed.
func (proxy *ProxyServer) readLoop(listener net.PacketConn) {
log.Info().Msgf("Listener starting up: %s", listener.LocalAddr())

packetBuffer := make([]byte, maxMTU)

for !proxy.dead.IsSet() {
Expand Down

0 comments on commit 962a598

Please sign in to comment.