diff --git a/cmd/wireproxy/main.go b/cmd/wireproxy/main.go index 713943a..d9ec396 100644 --- a/cmd/wireproxy/main.go +++ b/cmd/wireproxy/main.go @@ -236,7 +236,7 @@ func main() { // Wireguard doesn't allow configuring which FD to use for logging // https://github.com/WireGuard/wireguard-go/blob/master/device/logger.go#L39 // so redirect STDOUT to STDERR, we don't want to print anything to STDOUT anyways - os.Stdout = os.NewFile(uintptr(syscall.Stderr), "/dev/stderr") + os.Stdout = os.Stderr logLevel := device.LogLevelVerbose if *silent { logLevel = device.LogLevelSilent diff --git a/config.go b/config.go index 1f6e4e4..1cd9254 100644 --- a/config.go +++ b/config.go @@ -40,6 +40,8 @@ type TCPClientTunnelConfig struct { type STDIOTunnelConfig struct { Target string + Input *os.File + Output *os.File } type TCPServerTunnelConfig struct { @@ -377,6 +379,8 @@ func parseSTDIOTunnelConfig(section *ini.Section) (RoutineSpawner, error) { return nil, err } config.Target = targetSection + config.Input = os.Stdin + config.Output = os.Stdout return config, nil } diff --git a/routine.go b/routine.go index edfc793..f0a3356 100644 --- a/routine.go +++ b/routine.go @@ -219,20 +219,13 @@ func tcpClientForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) { } // STDIOTcpForward starts a new connection via wireguard and forward traffic from `conn` -func STDIOTcpForward(vt *VirtualTun, raddr *addressPort) { +func STDIOTcpForward(vt *VirtualTun, raddr *addressPort, input *os.File, output *os.File) { target, err := vt.resolveToAddrPort(raddr) if err != nil { errorLogger.Printf("Name resolution error for %s: %s\n", raddr.address, err.Error()) return } - // os.Stdout has previously been remapped to stderr, se we can't use it - stdout, err := os.OpenFile("/dev/stdout", os.O_WRONLY, 0) - if err != nil { - errorLogger.Printf("Failed to open /dev/stdout: %s\n", err.Error()) - return - } - tcpAddr := TCPAddrFromAddrPort(*target) sconn, err := vt.Tnet.DialTCP(tcpAddr) if err != nil { @@ -240,8 +233,8 @@ func STDIOTcpForward(vt *VirtualTun, raddr *addressPort) { return } - go connForward(os.Stdin, sconn) - go connForward(sconn, stdout) + go connForward(input, sconn) + go connForward(sconn, output) } // SpawnRoutine spawns a local TCP server which acts as a proxy to the specified target @@ -272,7 +265,7 @@ func (conf *STDIOTunnelConfig) SpawnRoutine(vt *VirtualTun) { log.Fatal(err) } - go STDIOTcpForward(vt, raddr) + go STDIOTcpForward(vt, raddr, conf.Input, conf.Output) } // tcpServerForward starts a new connection locally and forward traffic from `conn`