diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 75249cd..7176356 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -753,6 +753,11 @@ func isMobile() bool { return runtime.GOOS == "android" || runtime.GOOS == "ios" } +// isAndroid reports whether the current OS is Android. +func isAndroid() bool { + return runtime.GOOS == "android" +} + // RunCobraCommand runs ctrld cli. func RunCobraCommand(cmd *cobra.Command) { noConfigStart = isNoConfigStart(cmd) @@ -771,7 +776,7 @@ func RunMobile(appConfig *AppConfig, appCallback *AppCallback, stopCh chan struc homedir = appConfig.HomeDir verbose = appConfig.Verbose cdUID = appConfig.CdUID - cdUpstreamProto = ctrld.ResolverTypeDOH + cdUpstreamProto = appConfig.UpstreamProto logPath = appConfig.LogPath run(appCallback, stopCh) } @@ -1618,10 +1623,18 @@ type listenerConfigCheck struct { // mobileListenerPort returns hardcoded port for mobile platforms. func mobileListenerPort() int { - if runtime.GOOS == "ios" { - return 53 + if isAndroid() { + return 5354 } - return 5354 + return 53 +} + +// mobileListenerIp returns hardcoded listener ip for mobile platforms +func mobileListenerIp() string { + if isAndroid() { + return "0.0.0.0" + } + return "127.0.0.1" } // updateListenerConfig updates the config for listeners if not defined, @@ -1670,9 +1683,8 @@ func tryUpdateListenerConfig(cfg *ctrld.Config, infoLogger *zerolog.Logger, fata delete(cfg.Listener, k) } } - // In cd mode, always use 127.0.0.1:5354. if cdMode { - firstLn.IP = "127.0.0.1" // Mobile platforms allows running listener only on loop back address. + firstLn.IP = mobileListenerIp() firstLn.Port = mobileListenerPort() // TODO: use clear(lcc) once upgrading to go 1.21 for k := range lcc { diff --git a/cmd/cli/library.go b/cmd/cli/library.go index 80612c9..d302644 100644 --- a/cmd/cli/library.go +++ b/cmd/cli/library.go @@ -11,8 +11,9 @@ type AppCallback struct { // AppConfig allows overwriting ctrld cli flags from mobile platforms. type AppConfig struct { - CdUID string - HomeDir string - Verbose int - LogPath string + CdUID string + HomeDir string + UpstreamProto string + Verbose int + LogPath string } diff --git a/cmd/ctrld_library/main.go b/cmd/ctrld_library/main.go index 526dd3b..9bcc151 100644 --- a/cmd/ctrld_library/main.go +++ b/cmd/ctrld_library/main.go @@ -28,14 +28,15 @@ type AppCallback interface { // Start configures utility with config.toml from provided directory. // This function will block until Stop is called // Check port availability prior to calling it. -func (c *Controller) Start(CdUID string, HomeDir string, logLevel int, logPath string) { +func (c *Controller) Start(CdUID string, HomeDir string, UpstreamProto string, logLevel int, logPath string) { if c.stopCh == nil { c.stopCh = make(chan struct{}) c.Config = cli.AppConfig{ - CdUID: CdUID, - HomeDir: HomeDir, - Verbose: logLevel, - LogPath: logPath, + CdUID: CdUID, + HomeDir: HomeDir, + UpstreamProto: UpstreamProto, + Verbose: logLevel, + LogPath: logPath, } appCallback := mapCallback(c.AppCallback) cli.RunMobile(&c.Config, &appCallback, c.stopCh)