From 054a818321d59c85451658e64b97c0885d78fd77 Mon Sep 17 00:00:00 2001 From: beck-8 <1504068285@qq.com> Date: Thu, 20 Nov 2025 21:29:25 +0800 Subject: [PATCH] chore(pdp): add ExternalUrl for IPNI to be announced correctly --- cuhttp/server.go | 13 +++++++++---- deps/config/doc_gen.go | 6 ++++++ deps/config/types.go | 3 +++ .../configuration/default-curio-configuration.md | 5 +++++ market/ipni/ipni-provider/ipni-provider.go | 9 +++++++-- tasks/indexing/task_ipni.go | 9 +++++++-- tasks/indexing/task_pdp_ipni.go | 9 +++++++-- tasks/pdp/watch_piece_delete.go | 9 +++++++-- web/api/webrpc/pdp.go | 16 ++++++++++++---- 9 files changed, 63 insertions(+), 16 deletions(-) diff --git a/cuhttp/server.go b/cuhttp/server.go index 57e43d562..780d3f935 100644 --- a/cuhttp/server.go +++ b/cuhttp/server.go @@ -156,7 +156,12 @@ func StartHTTPServer(ctx context.Context, d *deps.Deps, sd *ServiceDeps, dm *sto chiRouter.Use(corsHeaders) if cfg.EnableCORS { - chiRouter.Use(handlers.CORS(handlers.AllowedOrigins([]string{"https://" + cfg.DomainName}))) + chiRouter.Use(handlers.CORS(handlers.AllowedOrigins([]string{func() string { + if d.Cfg.HTTP.ExternalUrl != "" { + return d.Cfg.HTTP.ExternalUrl + } + return "https://" + d.Cfg.HTTP.DomainName + }()}))) } // Set up the compression middleware with custom compression levels @@ -219,7 +224,7 @@ func StartHTTPServer(ctx context.Context, d *deps.Deps, sd *ServiceDeps, dm *sto // Start the server with TLS go func() { - log.Infof("Starting HTTPS server for https://%s on %s", cfg.DomainName, cfg.ListenAddress) + log.Infof("Starting HTTP(S) server for http(s)://%s on %s", cfg.DomainName, cfg.ListenAddress) var serr error if !cfg.DelegateTLS { serr = server.ListenAndServeTLS("", "") @@ -227,7 +232,7 @@ func StartHTTPServer(ctx context.Context, d *deps.Deps, sd *ServiceDeps, dm *sto serr = server.ListenAndServe() } if serr != nil { - log.Errorf("Failed to start HTTPS server: %s", serr) + log.Errorf("Failed to start HTTP(S) server: %s", serr) panic(serr) } }() @@ -238,7 +243,7 @@ func StartHTTPServer(ctx context.Context, d *deps.Deps, sd *ServiceDeps, dm *sto if err := server.Shutdown(context.Background()); err != nil { log.Errorf("shutting down web server failed: %s", err) } - log.Warn("HTTP Server graceful shutdown successful") + log.Warn("HTTP(S) Server graceful shutdown successful") }() return nil diff --git a/deps/config/doc_gen.go b/deps/config/doc_gen.go index 8c53e351b..4d213a4fa 100644 --- a/deps/config/doc_gen.go +++ b/deps/config/doc_gen.go @@ -880,6 +880,12 @@ an IP address`, Comment: `DelegateTLS allows the server to delegate TLS to a reverse proxy. When enabled the listen address will serve HTTP and the reverse proxy will handle TLS termination.`, }, + { + Name: "ExternalUrl", + Type: "string", + + Comment: `ExternalUrl is the final URL that external users can access directly.`, + }, { Name: "ReadTimeout", Type: "time.Duration", diff --git a/deps/config/types.go b/deps/config/types.go index 49bb4b2d0..894b9fdb9 100644 --- a/deps/config/types.go +++ b/deps/config/types.go @@ -841,6 +841,9 @@ type HTTPConfig struct { // HTTP and the reverse proxy will handle TLS termination. DelegateTLS bool + // ExternalUrl is the final URL that external users can access directly. + ExternalUrl string + // ReadTimeout is the maximum duration for reading the entire or next request, including body, from the client. // Time duration string (e.g., "1h2m3s") in TOML format. (Default: "30m0s") ReadTimeout time.Duration diff --git a/documentation/en/configuration/default-curio-configuration.md b/documentation/en/configuration/default-curio-configuration.md index 818fd6ad2..5b7409e79 100644 --- a/documentation/en/configuration/default-curio-configuration.md +++ b/documentation/en/configuration/default-curio-configuration.md @@ -540,6 +540,11 @@ description: The default curio configuration # type: bool #DelegateTLS = false + # ExternalUrl is the final URL that external users can access directly. + # + # type: string + #ExternalUrl = "" + # ReadTimeout is the maximum duration for reading the entire or next request, including body, from the client. # Time duration string (e.g., "1h2m3s") in TOML format. (Default: "30m0s") # diff --git a/market/ipni/ipni-provider/ipni-provider.go b/market/ipni/ipni-provider/ipni-provider.go index bd9eb6fc4..dc8b232ef 100644 --- a/market/ipni/ipni-provider/ipni-provider.go +++ b/market/ipni/ipni-provider/ipni-provider.go @@ -164,12 +164,17 @@ func NewProvider(d *deps.Deps) (*Provider, error) { httpServerAddresses := map[string]multiaddr.Multiaddr{} { - u, err := url.Parse(fmt.Sprintf("https://%s", d.Cfg.HTTP.DomainName)) + u, err := url.Parse(func() string { + if d.Cfg.HTTP.ExternalUrl != "" { + return d.Cfg.HTTP.ExternalUrl + } + return fmt.Sprintf("https://%s", d.Cfg.HTTP.DomainName) + }()) if err != nil { return nil, xerrors.Errorf("parsing announce address domain: %w", err) } - if build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { + if d.Cfg.HTTP.ExternalUrl == "" && build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { ls := strings.Split(d.Cfg.HTTP.ListenAddress, ":") u, err = url.Parse(fmt.Sprintf("http://%s:%s", d.Cfg.HTTP.DomainName, ls[1])) if err != nil { diff --git a/tasks/indexing/task_ipni.go b/tasks/indexing/task_ipni.go index 30878edea..eeee505da 100644 --- a/tasks/indexing/task_ipni.go +++ b/tasks/indexing/task_ipni.go @@ -190,11 +190,16 @@ func (I *IPNITask) Do(taskID harmonytask.TaskID, stillOwned func() bool) (done b } { - u, err := url.Parse(fmt.Sprintf("https://%s", I.cfg.HTTP.DomainName)) + u, err := url.Parse(func() string { + if I.cfg.HTTP.ExternalUrl != "" { + return I.cfg.HTTP.ExternalUrl + } + return fmt.Sprintf("https://%s", I.cfg.HTTP.DomainName) + }()) if err != nil { return false, xerrors.Errorf("parsing announce address domain: %w", err) } - if build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { + if I.cfg.HTTP.ExternalUrl == "" && build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { ls := strings.Split(I.cfg.HTTP.ListenAddress, ":") u, err = url.Parse(fmt.Sprintf("http://%s:%s", I.cfg.HTTP.DomainName, ls[1])) if err != nil { diff --git a/tasks/indexing/task_pdp_ipni.go b/tasks/indexing/task_pdp_ipni.go index 209caf198..645c9e976 100644 --- a/tasks/indexing/task_pdp_ipni.go +++ b/tasks/indexing/task_pdp_ipni.go @@ -197,11 +197,16 @@ func (P *PDPIPNITask) Do(taskID harmonytask.TaskID, stillOwned func() bool) (don } { - u, err := url.Parse(fmt.Sprintf("https://%s:443", P.cfg.HTTP.DomainName)) + u, err := url.Parse(func() string { + if P.cfg.HTTP.ExternalUrl != "" { + return P.cfg.HTTP.ExternalUrl + } + return fmt.Sprintf("https://%s", P.cfg.HTTP.DomainName) + }()) if err != nil { return false, xerrors.Errorf("parsing announce address domain: %w", err) } - if build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { + if P.cfg.HTTP.ExternalUrl == "" && build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { ls := strings.Split(P.cfg.HTTP.ListenAddress, ":") u, err = url.Parse(fmt.Sprintf("http://%s:%s", P.cfg.HTTP.DomainName, ls[1])) if err != nil { diff --git a/tasks/pdp/watch_piece_delete.go b/tasks/pdp/watch_piece_delete.go index 2ac3df62d..b492bf05e 100644 --- a/tasks/pdp/watch_piece_delete.go +++ b/tasks/pdp/watch_piece_delete.go @@ -299,11 +299,16 @@ func processIndexingAndIPNICleanup(ctx context.Context, db *harmonydb.DB, cfg *c } { - u, err := url.Parse(fmt.Sprintf("https://%s:443", cfg.DomainName)) + u, err := url.Parse(func() string { + if cfg.ExternalUrl != "" { + return cfg.ExternalUrl + } + return fmt.Sprintf("https://%s", cfg.DomainName) + }()) if err != nil { return false, xerrors.Errorf("parsing announce address domain: %w", err) } - if build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { + if cfg.ExternalUrl == "" && build.BuildType != build.BuildMainnet && build.BuildType != build.BuildCalibnet { ls := strings.Split(cfg.ListenAddress, ":") u, err = url.Parse(fmt.Sprintf("http://%s:%s", cfg.DomainName, ls[1])) if err != nil { diff --git a/web/api/webrpc/pdp.go b/web/api/webrpc/pdp.go index 448da3e1c..908224fe8 100644 --- a/web/api/webrpc/pdp.go +++ b/web/api/webrpc/pdp.go @@ -436,10 +436,18 @@ func (a *WebRPC) FSRegister(ctx context.Context, name, description, location str return xerrors.Errorf("provider is already registered") } - serviceURL := url.URL{ - Scheme: "https", - Host: a.deps.Cfg.HTTP.DomainName, - } + serviceURL := func() *url.URL { + if a.deps.Cfg.HTTP.ExternalUrl != "" { + u, err := url.Parse(a.deps.Cfg.HTTP.ExternalUrl) + if err == nil { + return u + } + } + return &url.URL{ + Scheme: "https", + Host: a.deps.Cfg.HTTP.DomainName, + } + }() tokenAddress, err := contract.USDFCAddress() if err != nil {