This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
forked from jstarks/npiperelay
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnpiperelay.go
239 lines (194 loc) · 5.38 KB
/
npiperelay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"bufio"
"errors"
"flag"
"io"
"log"
"os"
"strconv"
"sync"
"syscall"
"time"
"golang.org/x/sys/windows"
)
const cERROR_PIPE_NOT_CONNECTED syscall.Errno = 233
const WSAECONNREFUSED syscall.Errno = 10061
const WSAENETUNREACH syscall.Errno = 10051
const WSAETIMEDOUT syscall.Errno = 10060
const ERROR_CONNECTION_REFUSED syscall.Errno = 1225
var (
poll = flag.Bool("p", false, "poll until the the named pipe exists")
closeWrite = flag.Bool("s", false, "send a 0-byte message to the pipe after EOF on stdin")
closeOnEOF = flag.Bool("ep", false, "terminate on EOF reading from the pipe, even if there is more data to write")
closeOnStdinEOF = flag.Bool("ei", false, "terminate on EOF reading from stdin, even if there is more data to write")
verbose = flag.Bool("v", false, "verbose output on stderr")
assuan = flag.Bool("a", false, "treat the target as a libassuan file socket (Used by GnuPG)")
)
func dialPipe(p string, poll bool) (*overlappedFile, error) {
p16, err := windows.UTF16FromString(p)
if err != nil {
return nil, err
}
h, err := windows.CreateFile(&p16[0], windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_OVERLAPPED, 0)
if err == nil {
return newOverlappedFile(h), nil
}
return nil, err
}
func dialPort(p int, poll bool) (*overlappedFile, error) {
if p < 0 || p > 65535 {
return nil, errors.New("Invalid port value")
}
h, err := windows.Socket(windows.AF_INET, windows.SOCK_STREAM, 0)
if err != nil {
return nil, err
}
// Create a SockaddrInet4 for connecting to
sa := &windows.SockaddrInet4{Addr: [4]byte{0x7F, 0x00, 0x00, 0x01}, Port: p}
// Bind to a randomly assigned local port
err = windows.Bind(h, &windows.SockaddrInet4{})
if err != nil {
return nil, err
}
// Wrap our socket up to be properly handled
conn := newOverlappedFile(h)
// Connect to the LibAssuan socket using overlapped ConnectEx operation
_, err = conn.asyncIo(func(h windows.Handle, n *uint32, o *windows.Overlapped) error {
return windows.ConnectEx(h, sa, nil, 0, nil, o)
})
if err == nil {
return conn, nil
}
return nil, err
}
func underlyingError(err error) error {
if serr, ok := err.(*os.SyscallError); ok {
return serr.Err
}
return err
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
flag.Usage()
os.Exit(1)
}
if *verbose {
log.Println("connecting to", args[0])
}
var conn *overlappedFile
var err error
// Loop only if we're polling the named pipe or socket
for {
conn, err = dialPipe(args[0], *poll)
if *poll && os.IsNotExist(err) {
time.Sleep(200 * time.Millisecond)
continue
}
if err != nil {
err = &os.PathError{Path: args[0], Op: "open", Err: err}
log.Fatalln(err)
}
// LibAssaaun file socket: Attempt to read contents of the target file and connect to a TCP port
if *assuan {
var port int
var nonce [16]byte
reader := bufio.NewReader(conn)
// Read the target port number from the first line
tmp, _, err := reader.ReadLine()
port, err = strconv.Atoi(string(tmp))
if err != nil {
log.Fatalln(err)
}
// Read the rest of the nonce from the file
n, err := reader.Read(nonce[:])
if err != nil {
log.Fatalln(err)
}
if n != 16 {
log.Fatalf("Read incorrect number of bytes for nonce. Expected 16, got %d (0x%X)", n, nonce)
}
if *verbose {
log.Printf("Port: %d, Nonce: %X", port, nonce)
}
_ = conn.Close()
// Try to connect to the libassaun TCP socket hosted on localhost
conn, err = dialPort(port, *poll)
if *poll && (err == WSAETIMEDOUT || err == WSAECONNREFUSED || err == WSAENETUNREACH || err == ERROR_CONNECTION_REFUSED) {
time.Sleep(200 * time.Millisecond)
continue
}
err = os.NewSyscallError("ConnectEx", err)
if err != nil {
log.Fatal(err)
}
_, err = conn.Write(nonce[:])
if err != nil {
log.Fatal(err)
}
}
break
}
if *verbose {
log.Println("connected")
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, err := io.Copy(conn, os.Stdin)
if err != nil {
log.Fatalln("copy from stdin to pipe failed:", err)
}
if *verbose {
log.Println("copy from stdin to pipe finished")
}
if *closeOnStdinEOF {
os.Exit(0)
}
if *closeWrite {
// A zero-byte write on a message pipe indicates that no more data
// is coming.
conn.Write(nil)
}
os.Stdin.Close()
wg.Done()
}()
_, err = io.Copy(os.Stdout, conn)
if underlyingError(err) == windows.ERROR_BROKEN_PIPE || underlyingError(err) == cERROR_PIPE_NOT_CONNECTED {
// The named pipe is closed and there is no more data to read. Since
// named pipes are not bidirectional, there is no way for the other side
// of the pipe to get more data, so do not wait for the stdin copy to
// finish.
if *verbose {
log.Println("copy from pipe to stdout finished: pipe closed")
}
os.Exit(0)
}
if err != nil {
log.Fatalln("copy from pipe to stdout failed:", err)
}
if *verbose {
log.Println("copy from pipe to stdout finished")
}
if !*closeOnEOF {
os.Stdout.Close()
// Keep reading until we get ERROR_BROKEN_PIPE or the copy from stdin
// finishes.
go func() {
for {
_, err := conn.Read(nil)
if underlyingError(err) == windows.ERROR_BROKEN_PIPE {
if *verbose {
log.Println("pipe closed")
}
os.Exit(0)
} else if err != nil {
log.Fatalln("pipe error:", err)
}
}
}()
wg.Wait()
}
}