-
Notifications
You must be signed in to change notification settings - Fork 8
/
unixsockproxy.go
154 lines (124 loc) · 3.18 KB
/
unixsockproxy.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cluster
import (
"context"
"fmt"
"log"
"net"
"os"
"path/filepath"
"golang.org/x/sys/unix"
"namespacelabs.dev/foundation/framework/netcopy"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/go-ids"
)
type unixSockProxy struct {
TempDir string
SocketAddr string
Cleanup func()
}
type grpcProxyOpts struct {
SocketPath string
Kind string
Blocking bool
Connect func(context.Context) (net.Conn, error)
AnnounceSocket func(string)
}
type unixSockProxyOpts struct {
SocketPath string
Kind string
Blocking bool
Connect func(context.Context) (net.Conn, error)
AnnounceSocket func(string)
}
func runUnixSocketProxy(ctx context.Context, clusterId string, opts unixSockProxyOpts) (*unixSockProxy, error) {
socketPath := opts.SocketPath
var cleanup func()
if socketPath == "" {
sockDir, err := dirs.CreateUserTempDir("", clusterId)
if err != nil {
return nil, err
}
socketPath = filepath.Join(sockDir, opts.Kind+".sock")
cleanup = func() {
os.RemoveAll(sockDir)
}
} else {
cleanup = func() {}
}
if err := unix.Unlink(socketPath); err != nil && !os.IsNotExist(err) {
return nil, err
}
var d net.ListenConfig
listener, err := d.Listen(ctx, "unix", socketPath)
if err != nil {
cleanup()
return nil, err
}
if opts.AnnounceSocket != nil {
opts.AnnounceSocket(socketPath)
}
if opts.Blocking {
defer cleanup()
ch := make(chan struct{})
go func() {
select {
case <-ch:
case <-ctx.Done():
}
_ = listener.Close()
}()
defer close(ch)
if err := serveProxy(ctx, listener, func(ctx context.Context) (net.Conn, error) {
return opts.Connect(ctx)
}); err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return nil, ctxErr
}
return nil, err
}
return nil, nil
} else {
go func() {
if err := serveProxy(ctx, listener, func(ctx context.Context) (net.Conn, error) {
return opts.Connect(ctx)
}); err != nil {
log.Fatal(err)
}
}()
return &unixSockProxy{filepath.Dir(socketPath), socketPath, cleanup}, nil
}
}
func serveProxy(ctx context.Context, listener net.Listener, connect func(context.Context) (net.Conn, error)) error {
for {
rawConn, err := listener.Accept()
if err != nil {
return err
}
conn := withAddress{rawConn, fmt.Sprintf("local connection")}
go func() {
var d netcopy.DebugLogFunc
id := ids.NewRandomBase32ID(4)
fmt.Fprintf(console.Debug(ctx), "[%s] new connection\n", id)
d = func(format string, args ...any) {
fmt.Fprintf(console.Debug(ctx), "["+id+"]: "+format+"\n", args...)
}
defer conn.Close()
peerConn, err := connect(ctx)
if err != nil {
fmt.Fprintf(console.Stderr(ctx), "Failed to connect: %v\n", err)
return
}
defer peerConn.Close()
_ = netcopy.CopyConns(d, conn, peerConn)
}()
}
}
type withAddress struct {
net.Conn
addrDesc string
}
func (w withAddress) RemoteAddrDebug() string { return w.addrDesc }