Deactivation pin fixes

- short control socket name.(in IOS max length is 11)
- wait for control server to reply before checking for deactivation pin.
- Added separate name for control socket for mobile.
- Added stop channel reference to Control client constructor.
This commit is contained in:
Ginder Singh
2024-02-11 16:37:25 -05:00
committed by Cuong Manh Le
parent 3ca754b438
commit 8491f9c455
3 changed files with 43 additions and 12 deletions

View File

@@ -264,7 +264,7 @@ func initCLI() {
// If pin code was set, do not allow running start command.
if status == service.StatusRunning {
if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) {
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
os.Exit(deactivationPinInvalidExitCode)
}
}
@@ -413,7 +413,7 @@ func initCLI() {
return
}
initLogging()
if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) {
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
os.Exit(deactivationPinInvalidExitCode)
}
if doTasks([]task{{s.Stop, true}}) {
@@ -572,7 +572,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
if iface == "" {
iface = "auto"
}
if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) {
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
os.Exit(deactivationPinInvalidExitCode)
}
uninstall(p, s)
@@ -852,9 +852,9 @@ func RunMobile(appConfig *AppConfig, appCallback *AppCallback, stopCh chan struc
}
// CheckDeactivationPin checks if deactivation pin is valid
func CheckDeactivationPin(pin int64) int {
func CheckDeactivationPin(pin int64, stopCh chan struct{}) int {
deactivationPin = pin
if err := checkDeactivationPin(nil); isCheckDeactivationPinErr(err) {
if err := checkDeactivationPin(nil, stopCh); isCheckDeactivationPinErr(err) {
return deactivationPinInvalidExitCode
}
return 0
@@ -935,7 +935,7 @@ func run(appCallback *AppCallback, stopCh chan struct{}) {
}
p.router = router.New(&cfg, cdUID != "")
cs, err := newControlServer(filepath.Join(sockDir, ctrldControlUnixSock))
cs, err := newControlServer(filepath.Join(sockDir, ControlSocketName()))
if err != nil {
mainLog.Load().Warn().Err(err).Msg("could not create control server")
}
@@ -2097,6 +2097,26 @@ func newSocketControlClient(s service.Service, dir string) *controlClient {
return cc
}
func newSocketControlClientMobile(dir string, stopCh chan struct{}) *controlClient {
bo := backoff.NewBackoff("self-check", logf, 3*time.Second)
bo.LogLongerThan = 3 * time.Second
ctx := context.Background()
cc := newControlClient(filepath.Join(dir, ControlSocketName()))
for {
select {
case <-stopCh:
return nil
default:
_, err := cc.post("/", nil)
if err == nil {
return cc
} else {
bo.BackOff(ctx, err)
}
}
}
}
// checkStrFlagEmpty validates if a string flag was set to an empty string.
// If yes, emitting a fatal error message.
func checkStrFlagEmpty(cmd *cobra.Command, flagName string) {
@@ -2177,7 +2197,7 @@ var errInvalidDeactivationPin = errors.New("deactivation pin is invalid")
var errRequiredDeactivationPin = errors.New("deactivation pin is required to stop or uninstall the service")
// checkDeactivationPin validates if the deactivation pin matches one in ControlD config.
func checkDeactivationPin(s service.Service) error {
func checkDeactivationPin(s service.Service, stopCh chan struct{}) error {
dir, err := socketDir()
if err != nil {
mainLog.Load().Err(err).Msg("could not check deactivation pin")
@@ -2185,7 +2205,7 @@ func checkDeactivationPin(s service.Service) error {
}
var cc *controlClient
if s == nil {
cc = newControlClient(filepath.Join(dir, ctrldControlUnixSock))
cc = newSocketControlClientMobile(dir, stopCh)
} else {
cc = newSocketControlClient(s, dir)
}

View File

@@ -33,11 +33,22 @@ const (
defaultSemaphoreCap = 256
ctrldLogUnixSock = "ctrld_start.sock"
ctrldControlUnixSock = "ctrld_control.sock"
upstreamPrefix = "upstream."
upstreamOS = upstreamPrefix + "os"
upstreamPrivate = upstreamPrefix + "private"
// iOS unix socket name max length is 11.
ctrldControlUnixSockMobile = "cd.sock"
upstreamPrefix = "upstream."
upstreamOS = upstreamPrefix + "os"
upstreamPrivate = upstreamPrefix + "private"
)
// ControlSocketName returns name for control unix socket.
func ControlSocketName() string {
if isMobile() {
return ctrldControlUnixSockMobile
} else {
return ctrldControlUnixSock
}
}
var logf = func(format string, args ...any) {
mainLog.Load().Debug().Msgf(format, args...)
}

View File

@@ -62,7 +62,7 @@ func mapCallback(callback AppCallback) cli.AppCallback {
}
func (c *Controller) Stop(Pin int64) int {
errorCode := cli.CheckDeactivationPin(Pin)
errorCode := cli.CheckDeactivationPin(Pin, c.stopCh)
if errorCode == 0 && c.stopCh != nil {
close(c.stopCh)
c.stopCh = nil