-
Notifications
You must be signed in to change notification settings - Fork 77
/
unix_proxy.go
126 lines (108 loc) · 4.1 KB
/
unix_proxy.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
//go:build !no_proxies && !no_services
// +build !no_proxies,!no_services
package services
import (
"crypto/tls"
"fmt"
"net"
"os"
"runtime"
"github.com/ansible/receptor/pkg/netceptor"
"github.com/ansible/receptor/pkg/utils"
"github.com/ghjm/cmdline"
)
// UnixProxyServiceInbound listens on a Unix socket and forwards connections over the Receptor network.
func UnixProxyServiceInbound(s *netceptor.Netceptor, filename string, permissions os.FileMode,
node string, rservice string, tlscfg *tls.Config,
) error {
uli, lock, err := utils.UnixSocketListen(filename, permissions)
if err != nil {
return fmt.Errorf("error opening Unix socket: %s", err)
}
go func() {
defer lock.Unlock()
for {
uc, err := uli.Accept()
if err != nil {
s.Logger.Error("Error accepting Unix socket connection: %s", err)
return
}
go func() {
qc, err := s.Dial(node, rservice, tlscfg)
if err != nil {
s.Logger.Error("Error connecting on Receptor network: %s", err)
return
}
utils.BridgeConns(uc, "unix socket service", qc, "receptor connection", s.Logger)
}()
}
}()
return nil
}
// UnixProxyServiceOutbound listens on the Receptor network and forwards the connection via a Unix socket.
func UnixProxyServiceOutbound(s *netceptor.Netceptor, service string, tlscfg *tls.Config, filename string) error {
qli, err := s.ListenAndAdvertise(service, tlscfg, map[string]string{
"type": "Unix Proxy",
"filename": filename,
})
if err != nil {
return fmt.Errorf("error listening on Receptor network: %s", err)
}
go func() {
for {
qc, err := qli.Accept()
if err != nil {
s.Logger.Error("Error accepting connection on Receptor network: %s\n", err)
return
}
uc, err := net.Dial("unix", filename)
if err != nil {
s.Logger.Error("Error connecting via Unix socket: %s\n", err)
continue
}
go utils.BridgeConns(qc, "receptor service", uc, "unix socket connection", s.Logger)
}
}()
return nil
}
// unixProxyInboundCfg is the cmdline configuration object for a Unix socket inbound proxy.
type unixProxyInboundCfg struct {
Filename string `required:"true" description:"Socket filename, which will be overwritten"`
Permissions int `description:"Socket file permissions" default:"0600"`
RemoteNode string `required:"true" description:"Receptor node to connect to"`
RemoteService string `required:"true" description:"Receptor service name to connect to"`
TLS string `description:"Name of TLS client config for the Receptor connection"`
}
// Run runs the action.
func (cfg unixProxyInboundCfg) Run() error {
netceptor.MainInstance.Logger.Debug("Running Unix socket inbound proxy service %v\n", cfg)
tlscfg, err := netceptor.MainInstance.GetClientTLSConfig(cfg.TLS, cfg.RemoteNode, netceptor.ExpectedHostnameTypeReceptor)
if err != nil {
return err
}
return UnixProxyServiceInbound(netceptor.MainInstance, cfg.Filename, os.FileMode(cfg.Permissions),
cfg.RemoteNode, cfg.RemoteService, tlscfg)
}
// unixProxyOutboundCfg is the cmdline configuration object for a Unix socket outbound proxy.
type unixProxyOutboundCfg struct {
Service string `required:"true" description:"Receptor service name to bind to"`
Filename string `required:"true" description:"Socket filename, which must already exist"`
TLS string `description:"Name of TLS server config for the Receptor connection"`
}
// Run runs the action.
func (cfg unixProxyOutboundCfg) Run() error {
netceptor.MainInstance.Logger.Debug("Running Unix socket inbound proxy service %s\n", cfg)
tlscfg, err := netceptor.MainInstance.GetServerTLSConfig(cfg.TLS)
if err != nil {
return err
}
return UnixProxyServiceOutbound(netceptor.MainInstance, cfg.Service, tlscfg, cfg.Filename)
}
func init() {
if runtime.GOOS != "windows" {
cmdline.RegisterConfigTypeForApp("receptor-proxies",
"unix-socket-server", "Listen on a Unix socket and forward via Receptor", unixProxyInboundCfg{}, cmdline.Section(servicesSection))
cmdline.RegisterConfigTypeForApp("receptor-proxies",
"unix-socket-client", "Listen via Receptor and forward to a Unix socket", unixProxyOutboundCfg{}, cmdline.Section(servicesSection))
}
}