Skip to content

Commit dbbc905

Browse files
djkaziclosh11
authored andcommitted
Allow lnd to use tor over unix socket
1 parent 0a7cc6b commit dbbc905

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

config.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,14 +1049,16 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
10491049
cfg.Tor.DNS = dns.String()
10501050
}
10511051

1052-
control, err := lncfg.ParseAddressString(
1053-
cfg.Tor.Control, strconv.Itoa(defaultTorControlPort),
1054-
cfg.net.ResolveTCPAddr,
1055-
)
1056-
if err != nil {
1057-
return nil, mkErr("error parsing tor control address: %v", err)
1052+
if !strings.HasPrefix(cfg.Tor.Control, "unix://") {
1053+
control, err := lncfg.ParseAddressString(
1054+
cfg.Tor.Control, strconv.Itoa(defaultTorControlPort),
1055+
cfg.net.ResolveTCPAddr,
1056+
)
1057+
if err != nil {
1058+
return nil, mkErr("error parsing tor control address: %v", err)
1059+
}
1060+
cfg.Tor.Control = control.String()
10581061
}
1059-
cfg.Tor.Control = control.String()
10601062

10611063
// Ensure that tor socks host:port is not equal to tor control
10621064
// host:port. This would lead to lnd not starting up properly.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ replace github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
210210
// We want to format raw bytes as hex instead of base64. The forked version
211211
// allows us to specify that as an option.
212212
replace google.golang.org/protobuf => github.com/lightninglabs/protobuf-go-hex-display v1.30.0-hex-display
213+
replace github.com/lightningnetwork/lnd/tor => ./tor
213214

214215
// If you change this please also update .github/pull_request_template.md and
215216
// docs/INSTALL.md.

tor/controller.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ type Controller struct {
120120
// controller connections on.
121121
controlAddr string
122122

123+
// controlAddrType is the type of address the Tor server is listening
124+
// locally for controller connections on.
125+
controlAddrType string
126+
123127
// password, if non-empty, signals that the controller should attempt to
124128
// authenticate itself with the backing Tor daemon through the
125129
// HASHEDPASSWORD authentication method with this value.
@@ -141,9 +145,16 @@ type Controller struct {
141145
// a Tor server.
142146
func NewController(controlAddr string, targetIPAddress string,
143147
password string) *Controller {
144-
148+
cleanedControlAddr := controlAddr
149+
controlAddrType := "tcp"
150+
if strings.HasPrefix(controlAddr, "unix://") {
151+
controlAddrType = "unix"
152+
cleanedControlAddr = strings.Replace(controlAddr, "unix://", "", -1)
153+
}
154+
log.Infof("Tor controller controlAddr: %s", controlAddr)
145155
return &Controller{
146-
controlAddr: controlAddr,
156+
controlAddr: cleanedControlAddr,
157+
controlAddrType: controlAddrType,
147158
targetIPAddress: targetIPAddress,
148159
password: password,
149160
}
@@ -159,7 +170,7 @@ func (c *Controller) Start() error {
159170

160171
log.Info("Starting tor controller")
161172

162-
conn, err := textproto.Dial("tcp", c.controlAddr)
173+
conn, err := textproto.Dial(c.controlAddrType, c.controlAddr)
163174
if err != nil {
164175
return fmt.Errorf("unable to connect to Tor server: %v", err)
165176
}
@@ -222,7 +233,7 @@ func (c *Controller) Reconnect() error {
222233
}
223234

224235
// Make a new connection and authenticate.
225-
conn, err := textproto.Dial("tcp", c.controlAddr)
236+
conn, err := textproto.Dial(c.controlAddrType, c.controlAddr)
226237
if err != nil {
227238
return fmt.Errorf("unable to connect to Tor server: %w", err)
228239
}

0 commit comments

Comments
 (0)