|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
4 | 5 | "errors"
|
5 | 6 | "flag"
|
6 | 7 | "io"
|
7 | 8 | "log"
|
8 | 9 | "os"
|
9 | 10 | "strconv"
|
10 |
| - "strings" |
11 | 11 | "sync"
|
12 | 12 | "syscall"
|
13 | 13 | "time"
|
|
28 | 28 | closeOnEOF = flag.Bool("ep", false, "terminate on EOF reading from the pipe, even if there is more data to write")
|
29 | 29 | closeOnStdinEOF = flag.Bool("ei", false, "terminate on EOF reading from stdin, even if there is more data to write")
|
30 | 30 | verbose = flag.Bool("v", false, "verbose output on stderr")
|
| 31 | + assuan = flag.Bool("a", false, "treat the target as a libassuan file socket (Used by GnuPG)") |
31 | 32 | )
|
32 | 33 |
|
33 | 34 | func dialPipe(p string, poll bool) (*overlappedFile, error) {
|
@@ -113,37 +114,37 @@ func main() {
|
113 | 114 | log.Fatalln(err)
|
114 | 115 | }
|
115 | 116 |
|
116 |
| - // Not a named pipe, so attempt to read contents and connect to a TCP port for LibAssaaun |
117 |
| - if !strings.HasPrefix("//./", args[0]) { |
118 |
| - tmp := make([]byte, 22) // 5 bytes for ascii port number, 1 for newline, 16 for nonce |
119 |
| - |
| 117 | + // LibAssaaun file socket: Attempt to read contents of the target file and connect to a TCP port |
| 118 | + if *assuan { |
120 | 119 | var port int
|
121 | 120 | var nonce [16]byte
|
122 | 121 |
|
123 |
| - _, err := conn.Read(tmp) |
| 122 | + reader := bufio.NewReader(conn) |
| 123 | + |
| 124 | + // Read the target port number from the first line |
| 125 | + tmp, _, err := reader.ReadLine() |
| 126 | + port, err = strconv.Atoi(string(tmp)) |
124 | 127 | if err != nil {
|
125 |
| - log.Fatalln("Could not open file", err) |
| 128 | + log.Fatalln(err) |
126 | 129 | }
|
127 | 130 |
|
128 |
| - for i, c := range tmp { |
129 |
| - // Find the new line |
130 |
| - if c == 0x0A { |
131 |
| - port, err = strconv.Atoi(string(tmp[:i])) |
132 |
| - if err != nil { |
133 |
| - log.Fatalln(err) |
134 |
| - } |
| 131 | + // Read the rest of the nonce from the file |
| 132 | + n, err := reader.Read(nonce[:]) |
| 133 | + if err != nil { |
| 134 | + log.Fatalln(err) |
| 135 | + } |
135 | 136 |
|
136 |
| - copy(nonce[:], tmp[i+1:]) |
| 137 | + if n != 16 { |
| 138 | + log.Fatalf("Read incorrect number of bytes for nonce. Expected 16, got %d (0x%X)", n, nonce) |
| 139 | + } |
137 | 140 |
|
138 |
| - if *verbose { |
139 |
| - log.Printf("Port: %d, Nonce: %X", port, nonce) |
140 |
| - } |
141 |
| - break |
142 |
| - } |
| 141 | + if *verbose { |
| 142 | + log.Printf("Port: %d, Nonce: %X", port, nonce) |
143 | 143 | }
|
144 | 144 |
|
145 | 145 | _ = conn.Close()
|
146 | 146 |
|
| 147 | + // Try to connect to the libassaun TCP socket hosted on localhost |
147 | 148 | conn, err = dialPort(port, *poll)
|
148 | 149 |
|
149 | 150 | if *poll && (err == WSAETIMEDOUT || err == WSAECONNREFUSED || err == WSAENETUNREACH || err == ERROR_CONNECTION_REFUSED) {
|
|
0 commit comments