4
"context"
5
"errors"
6
"fmt"
7
+ "regexp"
8
"strings"
9
"time"
10
116
enableRelayService := cfg.Swarm.RelayService.Enabled.WithDefault(enableRelayTransport)
117
enableRelayClient := cfg.Swarm.RelayClient.Enabled.WithDefault(enableRelayTransport)
118
enableAutoTLS := cfg.AutoTLS.Enabled.WithDefault(config.DefaultAutoTLSEnabled)
119
+ enableAutoWSS := cfg.AutoTLS.AutoWSS.WithDefault(config.DefaultAutoWSS)
120
+ atlsLog := log.Logger("autotls")
121
122
// Log error when relay subsystem could not be initialized due to missing dependency
123
if !enableRelayTransport {
128
logger.Fatal("Failed to enable `Swarm.RelayClient`, it requires `Swarm.Transports.Network.Relay` to be true.")
129
}
130
}
131
+
132
if enableAutoTLS {
133
+ if !cfg.Swarm.Transports.Network.TCP.WithDefault(true) {
134
+ logger.Fatal("Invalid configuration: AutoTLS.Enabled=true requires Swarm.Transports.Network.TCP to be true as well.")
135
+ }
136
if !cfg.Swarm.Transports.Network.Websocket.WithDefault(true) {
137
logger.Fatal("Invalid configuration: AutoTLS.Enabled=true requires Swarm.Transports.Network.Websocket to be true as well.")
138
}
139
140
+ // AutoTLS for Secure WebSockets: ensure WSS listeners are in place (manual or automatic)
141
wssWildcard := fmt.Sprintf("/tls/sni/*.%s/ws", cfg.AutoTLS.DomainSuffix.WithDefault(config.DefaultDomainSuffix))
142
wssWildcardPresent := false
143
+ customWsPresent := false
144
+ customWsRegex := regexp.MustCompile(`/wss?$`)
145
+ tcpRegex := regexp.MustCompile(`/tcp/\d+$`)
146
+
147
+ // inspect listeners defined in config at Addresses.Swarm
148
+ var tcpListeners []string
149
for _, listener := range cfg.Addresses.Swarm {
150
+ // detect if user manually added /tls/sni/.../ws listener matching AutoTLS.DomainSuffix
151
if strings.Contains(listener, wssWildcard) {
152
+ atlsLog.Infof("found compatible wildcard listener in Addresses.Swarm. AutoTLS will be used on %s", listener)
153
wssWildcardPresent = true
154
break
155
}
156
+ // detect if user manually added own /ws or /wss listener that is
157
+ // not related to AutoTLS feature
158
+ if customWsRegex.MatchString(listener) {
159
+ atlsLog.Infof("found custom /ws listener set by user in Addresses.Swarm. AutoTLS will not be used on %s.", listener)
160
+ customWsPresent = true
161
+ break
162
+ }
163
+ // else, remember /tcp listeners that can be reused for /tls/sni/../ws
164
+ if tcpRegex.MatchString(listener) {
165
+ tcpListeners = append(tcpListeners, listener)
166
+ }
167
}
141
- if !wssWildcardPresent {
142
- logger.Fatal(fmt.Sprintf("Invalid configuration: AutoTLS.Enabled=true requires a catch-all Addresses.Swarm listener ending with %q to be present, see https://github.com/ipfs/kubo/blob/master/docs/config.md#autotls", wssWildcard))
168
+
169
+ // Append AutoTLS's wildcard listener
170
+ // if no manual /ws listener was set by the user
171
+ if enableAutoWSS && !wssWildcardPresent && !customWsPresent {
172
+ if len(tcpListeners) == 0 {
173
+ logger.Fatal("Invalid configuration: AutoTLS.AutoWSS=true requires at least one /tcp listener present in Addresses.Swarm, see https://github.com/ipfs/kubo/blob/master/docs/config.md#autotls")
174
+ }
175
+ for _, tcpListener := range tcpListeners {
176
+ wssListener := tcpListener + wssWildcard
177
+ cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, wssListener)
178
+ atlsLog.Infof("appended AutoWSS listener: %s", wssListener)
179
+ }
180
+ }
181
+
182
+ if !wssWildcardPresent && !enableAutoWSS {
183
+ logger.Fatal(fmt.Sprintf("Invalid configuration: AutoTLS.Enabled=true requires a /tcp listener ending with %q to be present in Addresses.Swarm or AutoTLS.AutoWSS=true, see https://github.com/ipfs/kubo/blob/master/docs/config.md#autotls", wssWildcard))
184
}
185
}
186
193
194
// Services (resource management)
195
fx.Provide(libp2p.ResourceManager(bcfg.Repo.Path(), cfg.Swarm, userResourceOverrides)),
155
- maybeProvide(libp2p.P2PForgeCertMgr(bcfg.Repo.Path(), cfg.AutoTLS), enableAutoTLS),
196
+ maybeProvide(libp2p.P2PForgeCertMgr(bcfg.Repo.Path(), cfg.AutoTLS, atlsLog), enableAutoTLS),
197
maybeInvoke(libp2p.StartP2PAutoTLS, enableAutoTLS),
198
fx.Provide(libp2p.AddrFilters(cfg.Swarm.AddrFilters)),
199
fx.Provide(libp2p.AddrsFactory(cfg.Addresses.Announce, cfg.Addresses.AppendAnnounce, cfg.Addresses.NoAnnounce)),