Skip to content

Commit e76e370

Browse files
authored
fix: Fix STDIOTunnel (windtf#190)
1 parent 1c6a10d commit e76e370

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

cmd/wireproxy/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func main() {
237237
// Wireguard doesn't allow configuring which FD to use for logging
238238
// https://github.com/WireGuard/wireguard-go/blob/master/device/logger.go#L39
239239
// so redirect STDOUT to STDERR, we don't want to print anything to STDOUT anyways
240-
os.Stdout = os.NewFile(uintptr(syscall.Stderr), "/dev/stderr")
240+
os.Stdout = os.Stderr
241241
logLevel := device.LogLevelVerbose
242242
if *silent {
243243
logLevel = device.LogLevelSilent

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type TCPClientTunnelConfig struct {
4646

4747
type STDIOTunnelConfig struct {
4848
Target string
49+
Input *os.File
50+
Output *os.File
4951
}
5052

5153
type TCPServerTunnelConfig struct {
@@ -383,6 +385,8 @@ func parseSTDIOTunnelConfig(section *ini.Section) (RoutineSpawner, error) {
383385
return nil, err
384386
}
385387
config.Target = targetSection
388+
config.Input = os.Stdin
389+
config.Output = os.Stdout
386390

387391
return config, nil
388392
}

routine.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,29 +219,22 @@ func tcpClientForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) {
219219
}
220220

221221
// STDIOTcpForward starts a new connection via wireguard and forward traffic from `conn`
222-
func STDIOTcpForward(vt *VirtualTun, raddr *addressPort) {
222+
func STDIOTcpForward(vt *VirtualTun, raddr *addressPort, input *os.File, output *os.File) {
223223
target, err := vt.resolveToAddrPort(raddr)
224224
if err != nil {
225225
errorLogger.Printf("Name resolution error for %s: %s\n", raddr.address, err.Error())
226226
return
227227
}
228228

229-
// os.Stdout has previously been remapped to stderr, se we can't use it
230-
stdout, err := os.OpenFile("/dev/stdout", os.O_WRONLY, 0)
231-
if err != nil {
232-
errorLogger.Printf("Failed to open /dev/stdout: %s\n", err.Error())
233-
return
234-
}
235-
236229
tcpAddr := TCPAddrFromAddrPort(*target)
237230
sconn, err := vt.Tnet.DialTCP(tcpAddr)
238231
if err != nil {
239232
errorLogger.Printf("TCP Client Tunnel to %s (%s): %s\n", target, tcpAddr, err.Error())
240233
return
241234
}
242235

243-
go connForward(os.Stdin, sconn)
244-
go connForward(sconn, stdout)
236+
go connForward(input, sconn)
237+
go connForward(sconn, output)
245238
}
246239

247240
// 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) {
272265
log.Fatal(err)
273266
}
274267

275-
go STDIOTcpForward(vt, raddr)
268+
go STDIOTcpForward(vt, raddr, conf.Input, conf.Output)
276269
}
277270

278271
// tcpServerForward starts a new connection locally and forward traffic from `conn`

0 commit comments

Comments
 (0)