Skip to content

Commit a7b3771

Browse files
committed
add dial
1 parent dda5e47 commit a7b3771

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

dial.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package commandproxy
2+
3+
import (
4+
"context"
5+
"net"
6+
)
7+
8+
type DialProxyCommand []string
9+
10+
func (p *DialProxyCommand) DialContext(ctx context.Context, network string, address string) (net.Conn, error) {
11+
host, port, err := net.SplitHostPort(address)
12+
if err != nil {
13+
return nil, err
14+
}
15+
m := map[byte]string{
16+
'n': network,
17+
'h': host,
18+
'p': port,
19+
}
20+
proxy := make([]string, len(*p))
21+
copy(proxy, *p)
22+
for i := range proxy {
23+
proxy[i] = ReplaceEscape(proxy[i], m)
24+
}
25+
cp := ProxyCommand(ctx, proxy[0], proxy[1:]...)
26+
return cp.Stdio()
27+
}

dial_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package commandproxy
2+
3+
import (
4+
"context"
5+
"net"
6+
"testing"
7+
)
8+
9+
func TestDialProxyCommand(t *testing.T) {
10+
listener, err := net.Listen("tcp", ":0")
11+
if err != nil {
12+
t.Fatal(err)
13+
}
14+
15+
command, err := SplitCommand("nc %n %p")
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
dial := DialProxyCommand(command)
20+
conn, err := dial.DialContext(context.Background(), listener.Addr().Network(), listener.Addr().String())
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
err = conn.Close()
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
}

0 commit comments

Comments
 (0)