3
3
package cli
4
4
5
5
import (
6
- "io"
7
- "os"
8
-
9
- "cdr.dev/slog"
10
6
"golang.org/x/xerrors"
11
7
8
+ "cdr.dev/slog"
12
9
"cdr.dev/slog/sloggers/sloghuman"
13
10
"github.com/coder/coder/v2/vpn"
14
11
"github.com/coder/serpent"
@@ -18,7 +15,6 @@ func (r *RootCmd) vpnDaemonRun() *serpent.Command {
18
15
var (
19
16
rpcReadHandleInt int64
20
17
rpcWriteHandleInt int64
21
- logPath string
22
18
)
23
19
24
20
cmd := & serpent.Command {
@@ -42,16 +38,10 @@ func (r *RootCmd) vpnDaemonRun() *serpent.Command {
42
38
Value : serpent .Int64Of (& rpcWriteHandleInt ),
43
39
Required : true ,
44
40
},
45
- {
46
- Flag : "log-path" ,
47
- Env : "CODER_VPN_DAEMON_LOG_PATH" ,
48
- Description : "The path to the log file to write to." ,
49
- Value : serpent .StringOf (& logPath ),
50
- Required : false , // logs will also be written to stderr
51
- },
52
41
},
53
42
Handler : func (inv * serpent.Invocation ) error {
54
43
ctx := inv .Context ()
44
+ logger := inv .Logger .AppendSinks (sloghuman .Sink (inv .Stderr )).Leveled (slog .LevelDebug )
55
45
56
46
if rpcReadHandleInt < 0 || rpcWriteHandleInt < 0 {
57
47
return xerrors .Errorf ("rpc-read-handle (%v) and rpc-write-handle (%v) must be positive" , rpcReadHandleInt , rpcWriteHandleInt )
@@ -60,18 +50,10 @@ func (r *RootCmd) vpnDaemonRun() *serpent.Command {
60
50
return xerrors .Errorf ("rpc-read-handle (%v) and rpc-write-handle (%v) must be different" , rpcReadHandleInt , rpcWriteHandleInt )
61
51
}
62
52
63
- logger := inv .Logger .AppendSinks (sloghuman .Sink (inv .Stderr )).Leveled (slog .LevelDebug )
64
- if logPath != "" {
65
- f , err := os .Create (logPath )
66
- if err != nil {
67
- return xerrors .Errorf ("create log file: %w" , err )
68
- }
69
- defer f .Close ()
70
- logger = logger .AppendSinks (sloghuman .Sink (f ))
71
- }
72
-
53
+ // We don't need to worry about duplicating the handles on Windows,
54
+ // which is different from Unix.
73
55
logger .Info (ctx , "opening bidirectional RPC pipe" , slog .F ("rpc_read_handle" , rpcReadHandleInt ), slog .F ("rpc_write_handle" , rpcWriteHandleInt ))
74
- pipe , err := newBidiPipe (uintptr (rpcReadHandleInt ), uintptr (rpcWriteHandleInt ))
56
+ pipe , err := vpn . NewBidirectionalPipe (uintptr (rpcReadHandleInt ), uintptr (rpcWriteHandleInt ))
75
57
if err != nil {
76
58
return xerrors .Errorf ("create bidirectional RPC pipe: %w" , err )
77
59
}
@@ -91,58 +73,3 @@ func (r *RootCmd) vpnDaemonRun() *serpent.Command {
91
73
92
74
return cmd
93
75
}
94
-
95
- type bidiPipe struct {
96
- read * os.File
97
- write * os.File
98
- }
99
-
100
- var _ io.ReadWriteCloser = bidiPipe {}
101
-
102
- func newBidiPipe (readHandle , writeHandle uintptr ) (bidiPipe , error ) {
103
- read := os .NewFile (readHandle , "rpc_read" )
104
- _ , err := read .Stat ()
105
- if err != nil {
106
- return bidiPipe {}, xerrors .Errorf ("stat rpc_read pipe (handle=%v): %w" , readHandle , err )
107
- }
108
- write := os .NewFile (writeHandle , "rpc_write" )
109
- _ , err = write .Stat ()
110
- if err != nil {
111
- return bidiPipe {}, xerrors .Errorf ("stat rpc_write pipe (handle=%v): %w" , writeHandle , err )
112
- }
113
- return bidiPipe {
114
- read : read ,
115
- write : write ,
116
- }, nil
117
- }
118
-
119
- // Read implements io.Reader. Data is read from the read pipe.
120
- func (b bidiPipe ) Read (p []byte ) (int , error ) {
121
- n , err := b .read .Read (p )
122
- if err != nil {
123
- return n , xerrors .Errorf ("read from rpc_read pipe (handle=%v): %w" , b .read .Fd (), err )
124
- }
125
- return n , nil
126
- }
127
-
128
- // Write implements io.Writer. Data is written to the write pipe.
129
- func (b bidiPipe ) Write (p []byte ) (n int , err error ) {
130
- n , err = b .write .Write (p )
131
- if err != nil {
132
- return n , xerrors .Errorf ("write to rpc_write pipe (handle=%v): %w" , b .write .Fd (), err )
133
- }
134
- return n , nil
135
- }
136
-
137
- // Close implements io.Closer. Both the read and write pipes are closed.
138
- func (b bidiPipe ) Close () error {
139
- err := b .read .Close ()
140
- if err != nil {
141
- return xerrors .Errorf ("close rpc_read pipe (handle=%v): %w" , b .read .Fd (), err )
142
- }
143
- err = b .write .Close ()
144
- if err != nil {
145
- return xerrors .Errorf ("close rpc_write pipe (handle=%v): %w" , b .write .Fd (), err )
146
- }
147
- return nil
148
- }
0 commit comments