@@ -7,13 +7,11 @@ package libp2p
77import (
88 "context"
99 "crypto/ecdsa"
10- "crypto/tls"
1110 "errors"
1211 "fmt"
1312 "net"
1413 "net/netip"
1514 "os"
16- "path/filepath"
1715 "runtime"
1816 "slices"
1917 "strconv"
@@ -22,7 +20,6 @@ import (
2220 "time"
2321
2422 ocprom "contrib.go.opencensus.io/exporter/prometheus"
25- "github.com/caddyserver/certmagic"
2623 "github.com/coreos/go-semver/semver"
2724 "github.com/ethersphere/bee/v2"
2825 "github.com/ethersphere/bee/v2/pkg/addressbook"
@@ -41,6 +38,7 @@ import (
4138 "github.com/ethersphere/bee/v2/pkg/topology"
4239 "github.com/ethersphere/bee/v2/pkg/topology/lightnode"
4340 "github.com/ethersphere/bee/v2/pkg/tracing"
41+ p2pforge "github.com/ipshipyard/p2p-forge/client"
4442 "github.com/libp2p/go-libp2p"
4543 "github.com/libp2p/go-libp2p/config"
4644 "github.com/libp2p/go-libp2p/core/crypto"
@@ -65,9 +63,6 @@ import (
6563 "github.com/prometheus/client_golang/prometheus"
6664 "go.uber.org/atomic"
6765 "go.uber.org/zap"
68- "go.uber.org/zap/zapcore"
69-
70- p2pforge "github.com/ipshipyard/p2p-forge/client"
7166)
7267
7368// loggerName is the tree path name of the logger for this package.
@@ -134,14 +129,6 @@ type lightnodes interface {
134129 EachPeer (pf topology.EachPeerFunc ) error
135130}
136131
137- // autoTLSCertManager defines the interface for managing TLS certificates.
138- type autoTLSCertManager interface {
139- Start () error
140- Stop ()
141- TLSConfig () * tls.Config
142- AddressFactory () config.AddrsFactory
143- }
144-
145132type Options struct {
146133 PrivateKey * ecdsa.PrivateKey
147134 NATAddr string
@@ -294,78 +281,25 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
294281 if o .autoTLSCertManager != nil {
295282 certManager = o .autoTLSCertManager
296283 } else {
297- // create a zap logger needed for cert manager to be as close to
298- // swarm logger as possible
299- l , err := zap.Config {
300- Level : zap .NewAtomicLevelAt (zap .DebugLevel ),
301- Development : false ,
302- Sampling : & zap.SamplingConfig {
303- Initial : 100 ,
304- Thereafter : 100 ,
305- },
306- Encoding : "json" ,
307- EncoderConfig : zapcore.EncoderConfig {
308- TimeKey : "time" ,
309- LevelKey : "level" ,
310- NameKey : "logger" ,
311- CallerKey : "caller" ,
312- FunctionKey : zapcore .OmitKey ,
313- MessageKey : "msg" ,
314- StacktraceKey : "stacktrace" ,
315- LineEnding : zapcore .DefaultLineEnding ,
316- EncodeLevel : zapcore .LowercaseLevelEncoder ,
317- EncodeTime : zapcore .EpochTimeEncoder ,
318- EncodeDuration : zapcore .SecondsDurationEncoder ,
319- EncodeCaller : zapcore .ShortCallerEncoder ,
320- },
321- OutputPaths : []string {"stderr" },
322- ErrorOutputPaths : []string {"stderr" },
323- }.Build ()
284+ forgeMgr , err := newP2PForgeCertManager (logger , P2PForgeOptions {
285+ Domain : o .AutoTLSDomain ,
286+ RegistrationEndpoint : o .AutoTLSRegistrationEndpoint ,
287+ CAEndpoint : o .AutoTLSCAEndpoint ,
288+ StorageDir : o .AutoTLSStorageDir ,
289+ })
324290 if err != nil {
325291 return nil , err
326292 }
327293
328- // assing zap logger as it needs to be synced when the service stops
329- zapLogger = l
330-
331- defer func () {
332- _ = zapLogger .Sync ()
333- }()
334-
335- // Use AutoTLS storage dir with domain subdir for easier management
336- // of different registers.
337- storagePath := filepath .Join (o .AutoTLSStorageDir , o .AutoTLSDomain )
338-
339- if err := os .MkdirAll (storagePath , 0700 ); err != nil {
340- return nil , fmt .Errorf ("create certificate storage directory %s: %w" , storagePath , err )
341- }
342-
343- certManager , err = p2pforge .NewP2PForgeCertMgr (
344- p2pforge .WithForgeDomain (o .AutoTLSDomain ),
345- p2pforge .WithForgeRegistrationEndpoint (o .AutoTLSRegistrationEndpoint ),
346- p2pforge .WithCAEndpoint (o .AutoTLSCAEndpoint ),
347- p2pforge .WithCertificateStorage (& certmagic.FileStorage {Path : storagePath }),
348- p2pforge .WithLogger (zapLogger .Sugar ()),
349- p2pforge .WithUserAgent (userAgent ()),
350- p2pforge .WithAllowPrivateForgeAddrs (),
351- p2pforge .WithRegistrationDelay (0 ),
352- p2pforge .WithOnCertLoaded (func () {
353- logger .Info ("auto tls certificate is loaded" )
354- }),
355- p2pforge .WithOnCertRenewed (func () {
356- logger .Info ("auto tls certificate is renewed" )
357- }),
358- )
359- if err != nil {
360- return nil , fmt .Errorf ("initialize AutoTLS: %w" , err )
361- }
294+ certManager = forgeMgr .CertMgr ()
295+ zapLogger = forgeMgr .ZapLogger ()
362296 }
363297
364298 defer func () {
365299 if returnErr != nil {
366- // certificate manager has to be stopped if service is not
367- // constructed
300+ // call if service is not constructed
368301 certManager .Stop ()
302+ _ = zapLogger .Sync ()
369303 }
370304 }()
371305
0 commit comments