// serveHTTPGateway collects options, creates listener, prints status message and starts serving requests func serveHTTPGateway(req cmds.Request) (error, <-chan error) { cfg, err := req.InvocContext().GetConfig() if err != nil { return fmt.Errorf("serveHTTPGateway: GetConfig() failed: %s", err), nil } gatewayMaddr, err := ma.NewMultiaddr(cfg.Addresses.Gateway) if err != nil { return fmt.Errorf("serveHTTPGateway: invalid gateway address: %q (err: %s)", cfg.Addresses.Gateway, err), nil } writable, writableOptionFound, err := req.Option(writableKwd).Bool() if err != nil { return fmt.Errorf("serveHTTPGateway: req.Option(%s) failed: %s", writableKwd, err), nil } if !writableOptionFound { writable = cfg.Gateway.Writable } gwLis, err := manet.Listen(gatewayMaddr) if err != nil { return fmt.Errorf("serveHTTPGateway: manet.Listen(%s) failed: %s", gatewayMaddr, err), nil } // we might have listened to /tcp/0 - lets see what we are listing on gatewayMaddr = gwLis.Multiaddr() if writable { fmt.Printf("Gateway (writable) server listening on %s\n", gatewayMaddr) } else { fmt.Printf("Gateway (readonly) server listening on %s\n", gatewayMaddr) } var opts = []corehttp.ServeOption{ corehttp.PrometheusCollectorOption("gateway"), corehttp.CommandsROOption(*req.InvocContext()), corehttp.VersionOption(), corehttp.IPNSHostnameOption(), corehttp.GatewayOption(writable), } if len(cfg.Gateway.RootRedirect) > 0 { opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect)) } node, err := req.InvocContext().ConstructNode() if err != nil { return fmt.Errorf("serveHTTPGateway: ConstructNode() failed: %s", err), nil } errc := make(chan error) go func() { errc <- corehttp.Serve(node, gwLis.NetListener(), opts...) close(errc) }() return nil, errc }
// serveHTTPGateway collects options, creates listener, prints status message and starts serving requests func serveHTTPGateway(node *core.OpenBazaarNode) (error, <-chan bool, <-chan error) { cfg, err := node.Context.GetConfig() if err != nil { return nil, nil, nil } gatewayMaddr, err := ma.NewMultiaddr(cfg.Addresses.Gateway) if err != nil { return fmt.Errorf("serveHTTPGateway: invalid gateway address: %q (err: %s)", cfg.Addresses.Gateway, err), nil, nil } writable := cfg.Gateway.Writable gwLis, err := manet.Listen(gatewayMaddr) if err != nil { return fmt.Errorf("serveHTTPGateway: manet.Listen(%s) failed: %s", gatewayMaddr, err), nil, nil } // we might have listened to /tcp/0 - lets see what we are listing on gatewayMaddr = gwLis.Multiaddr() log.Infof("Gateway/API server listening on %s\n", gatewayMaddr) var opts = []corehttp.ServeOption{ corehttp.MetricsCollectionOption("gateway"), corehttp.CommandsROOption(node.Context), corehttp.VersionOption(), corehttp.IPNSHostnameOption(), corehttp.GatewayOption(writable, cfg.Gateway.PathPrefixes), } if len(cfg.Gateway.RootRedirect) > 0 { opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect)) } if err != nil { return fmt.Errorf("serveHTTPGateway: ConstructNode() failed: %s", err), nil, nil } errc := make(chan error) cb := make(chan bool) go func() { errc <- api.Serve(cb, node, node.Context, gwLis.NetListener(), opts...) close(errc) }() return nil, cb, errc }
// serveHTTPApi collects options, creates listener, prints status message and starts serving requests func serveHTTPApi(req cmds.Request) (error, <-chan error) { cfg, err := req.InvocContext().GetConfig() if err != nil { return fmt.Errorf("serveHTTPApi: GetConfig() failed: %s", err), nil } apiAddr, _, err := req.Option(commands.ApiOption).String() if err != nil { return fmt.Errorf("serveHTTPApi: %s", err), nil } if apiAddr == "" { apiAddr = cfg.Addresses.API } apiMaddr, err := ma.NewMultiaddr(apiAddr) if err != nil { return fmt.Errorf("serveHTTPApi: invalid API address: %q (err: %s)", apiAddr, err), nil } apiLis, err := manet.Listen(apiMaddr) if err != nil { return fmt.Errorf("serveHTTPApi: manet.Listen(%s) failed: %s", apiMaddr, err), nil } // we might have listened to /tcp/0 - lets see what we are listing on apiMaddr = apiLis.Multiaddr() fmt.Printf("API server listening on %s\n", apiMaddr) unrestricted, _, err := req.Option(unrestrictedApiAccessKwd).Bool() if err != nil { return fmt.Errorf("serveHTTPApi: Option(%s) failed: %s", unrestrictedApiAccessKwd, err), nil } apiGw := corehttp.NewGateway(corehttp.GatewayConfig{ Writable: true, BlockList: &corehttp.BlockList{ Decider: func(s string) bool { if unrestricted { return true } // for now, only allow paths in the WebUI path for _, webuipath := range corehttp.WebUIPaths { if strings.HasPrefix(s, webuipath) { return true } } return false }, }, }) var opts = []corehttp.ServeOption{ corehttp.CommandsOption(*req.InvocContext()), corehttp.WebUIOption, apiGw.ServeOption(), corehttp.VersionOption(), defaultMux("/debug/vars"), defaultMux("/debug/pprof/"), corehttp.LogOption(), corehttp.PrometheusOption("/debug/metrics/prometheus"), } if len(cfg.Gateway.RootRedirect) > 0 { opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect)) } node, err := req.InvocContext().ConstructNode() if err != nil { return fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err), nil } if err := node.Repo.SetAPIAddr(apiAddr); err != nil { return fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err), nil } errc := make(chan error) go func() { errc <- corehttp.Serve(node, apiLis.NetListener(), opts...) close(errc) }() return nil, errc }
// serveHTTPApi collects options, creates listener, prints status message and starts serving requests func serveHTTPApi(req cmds.Request) (error, <-chan error) { cfg, err := req.InvocContext().GetConfig() if err != nil { return fmt.Errorf("serveHTTPApi: GetConfig() failed: %s", err), nil } apiAddr, _, err := req.Option(commands.ApiOption).String() if err != nil { return fmt.Errorf("serveHTTPApi: %s", err), nil } if apiAddr == "" { apiAddr = cfg.Addresses.API } apiMaddr, err := ma.NewMultiaddr(apiAddr) if err != nil { return fmt.Errorf("serveHTTPApi: invalid API address: %q (err: %s)", apiAddr, err), nil } apiLis, err := manet.Listen(apiMaddr) if err != nil { return fmt.Errorf("serveHTTPApi: manet.Listen(%s) failed: %s", apiMaddr, err), nil } // we might have listened to /tcp/0 - lets see what we are listing on apiMaddr = apiLis.Multiaddr() fmt.Printf("API server listening on %s\n", apiMaddr) // by default, we don't let you load arbitrary ipfs objects through the api, // because this would open up the api to scripting vulnerabilities. // only the webui objects are allowed. // if you know what you're doing, go ahead and pass --unrestricted-api. unrestricted, _, err := req.Option(unrestrictedApiAccessKwd).Bool() if err != nil { return fmt.Errorf("serveHTTPApi: Option(%s) failed: %s", unrestrictedApiAccessKwd, err), nil } gatewayOpt := corehttp.GatewayOption(false, corehttp.WebUIPaths...) if unrestricted { gatewayOpt = corehttp.GatewayOption(true, "/ipfs", "/ipns") } var opts = []corehttp.ServeOption{ corehttp.MetricsCollectionOption("api"), corehttp.CommandsOption(*req.InvocContext()), corehttp.WebUIOption, gatewayOpt, corehttp.VersionOption(), defaultMux("/debug/vars"), defaultMux("/debug/pprof/"), corehttp.MetricsScrapingOption("/debug/metrics/prometheus"), corehttp.LogOption(), } if len(cfg.Gateway.RootRedirect) > 0 { opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect)) } node, err := req.InvocContext().ConstructNode() if err != nil { return fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err), nil } if err := node.Repo.SetAPIAddr(apiMaddr); err != nil { return fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err), nil } errc := make(chan error) go func() { errc <- corehttp.Serve(node, apiLis.NetListener(), opts...) close(errc) }() return nil, errc }