Added upstream proto option to mobile library

Changed android listener IP to 0.0.0.0
This commit is contained in:
Ginder Singh
2024-01-06 00:42:41 -05:00
committed by Cuong Manh Le
parent 51b235b61a
commit cfaf32f71a
3 changed files with 29 additions and 15 deletions
+18 -6
View File
@@ -753,6 +753,11 @@ func isMobile() bool {
return runtime.GOOS == "android" || runtime.GOOS == "ios" 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. // RunCobraCommand runs ctrld cli.
func RunCobraCommand(cmd *cobra.Command) { func RunCobraCommand(cmd *cobra.Command) {
noConfigStart = isNoConfigStart(cmd) noConfigStart = isNoConfigStart(cmd)
@@ -771,7 +776,7 @@ func RunMobile(appConfig *AppConfig, appCallback *AppCallback, stopCh chan struc
homedir = appConfig.HomeDir homedir = appConfig.HomeDir
verbose = appConfig.Verbose verbose = appConfig.Verbose
cdUID = appConfig.CdUID cdUID = appConfig.CdUID
cdUpstreamProto = ctrld.ResolverTypeDOH cdUpstreamProto = appConfig.UpstreamProto
logPath = appConfig.LogPath logPath = appConfig.LogPath
run(appCallback, stopCh) run(appCallback, stopCh)
} }
@@ -1618,10 +1623,18 @@ type listenerConfigCheck struct {
// mobileListenerPort returns hardcoded port for mobile platforms. // mobileListenerPort returns hardcoded port for mobile platforms.
func mobileListenerPort() int { func mobileListenerPort() int {
if runtime.GOOS == "ios" { if isAndroid() {
return 53 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, // 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) delete(cfg.Listener, k)
} }
} }
// In cd mode, always use 127.0.0.1:5354.
if cdMode { if cdMode {
firstLn.IP = "127.0.0.1" // Mobile platforms allows running listener only on loop back address. firstLn.IP = mobileListenerIp()
firstLn.Port = mobileListenerPort() firstLn.Port = mobileListenerPort()
// TODO: use clear(lcc) once upgrading to go 1.21 // TODO: use clear(lcc) once upgrading to go 1.21
for k := range lcc { for k := range lcc {
+5 -4
View File
@@ -11,8 +11,9 @@ type AppCallback struct {
// AppConfig allows overwriting ctrld cli flags from mobile platforms. // AppConfig allows overwriting ctrld cli flags from mobile platforms.
type AppConfig struct { type AppConfig struct {
CdUID string CdUID string
HomeDir string HomeDir string
Verbose int UpstreamProto string
LogPath string Verbose int
LogPath string
} }
+6 -5
View File
@@ -28,14 +28,15 @@ type AppCallback interface {
// Start configures utility with config.toml from provided directory. // Start configures utility with config.toml from provided directory.
// This function will block until Stop is called // This function will block until Stop is called
// Check port availability prior to calling it. // 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 { if c.stopCh == nil {
c.stopCh = make(chan struct{}) c.stopCh = make(chan struct{})
c.Config = cli.AppConfig{ c.Config = cli.AppConfig{
CdUID: CdUID, CdUID: CdUID,
HomeDir: HomeDir, HomeDir: HomeDir,
Verbose: logLevel, UpstreamProto: UpstreamProto,
LogPath: logPath, Verbose: logLevel,
LogPath: logPath,
} }
appCallback := mapCallback(c.AppCallback) appCallback := mapCallback(c.AppCallback)
cli.RunMobile(&c.Config, &appCallback, c.stopCh) cli.RunMobile(&c.Config, &appCallback, c.stopCh)