-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathpipes_windows.go
102 lines (86 loc) · 2 KB
/
pipes_windows.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
// +build windows
package main
import (
"bufio"
"bytes"
"io/ioutil"
"log"
"net"
"github.com/Binject/binjection/bj"
npipe "gopkg.in/natefinch/npipe.v2"
)
// MakePipe - Create a named pipe
func MakePipe(pipename string) string {
return `\\.\pipe\` + pipename
}
// ListenPipeDry - Handle events on the dry pipe (dry=not yet injected)
func ListenPipeDry(pipename string, config *bj.BinjectConfig) {
ln, err := npipe.Listen(pipename)
if err != nil {
log.Fatalf("Listen(%s) failed: %v", pipename, err)
}
for {
conn, err := ln.Accept()
if err == npipe.ErrClosed {
return
}
if err != nil {
log.Fatalf("Error accepting connection: %v", err)
}
go handleDryConnection(conn, config)
}
}
// ListenPipeWet - Handle events on the wet pipe (wet=injected)
func ListenPipeWet(pipename string) {
ln, err := npipe.Listen(pipename)
if err != nil {
log.Fatalf("Listen(%s) failed: %v", pipename, err)
}
for {
conn, err := ln.Accept()
if err == npipe.ErrClosed {
return
}
if err != nil {
log.Fatalf("Error accepting connection: %v", err)
}
go handleWetConnection(conn)
}
}
var lastBytes []byte
func handleDryConnection(conn net.Conn, config *bj.BinjectConfig) {
r := bufio.NewReader(conn)
b, err := ioutil.ReadAll(r)
if err != nil {
log.Printf("Error reading from connection: %v", err)
return
}
bb := bytes.NewBuffer(b)
i, err := Inject(bb, config)
if err != nil {
log.Printf("Error injecting: %v", err)
return
}
if i != nil {
lastBytes = i.Bytes()
log.Println("Set lastBytes: ", len(lastBytes))
}
if err := conn.Close(); err != nil {
log.Printf("Error closing server side of connection: %v", err)
return
}
}
func handleWetConnection(conn net.Conn) {
w := bufio.NewWriter(conn)
_, err := w.Write(lastBytes)
log.Println("Wrote wet bytes: ", len(lastBytes))
if err != nil {
log.Printf("Error on writing to pipe: %v", err)
return
}
if err := conn.Close(); err != nil {
log.Printf("Error closing server side of connection: %v", err)
return
}
lastBytes = nil
}