mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-04 01:07:49 +02:00
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:
committed by
Cuong Manh Le
parent
3ca754b438
commit
8491f9c455
+28
-8
@@ -264,7 +264,7 @@ func initCLI() {
|
|||||||
|
|
||||||
// If pin code was set, do not allow running start command.
|
// If pin code was set, do not allow running start command.
|
||||||
if status == service.StatusRunning {
|
if status == service.StatusRunning {
|
||||||
if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) {
|
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
|
||||||
os.Exit(deactivationPinInvalidExitCode)
|
os.Exit(deactivationPinInvalidExitCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -413,7 +413,7 @@ func initCLI() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
initLogging()
|
initLogging()
|
||||||
if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) {
|
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
|
||||||
os.Exit(deactivationPinInvalidExitCode)
|
os.Exit(deactivationPinInvalidExitCode)
|
||||||
}
|
}
|
||||||
if doTasks([]task{{s.Stop, true}}) {
|
if doTasks([]task{{s.Stop, true}}) {
|
||||||
@@ -572,7 +572,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
if iface == "" {
|
if iface == "" {
|
||||||
iface = "auto"
|
iface = "auto"
|
||||||
}
|
}
|
||||||
if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) {
|
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
|
||||||
os.Exit(deactivationPinInvalidExitCode)
|
os.Exit(deactivationPinInvalidExitCode)
|
||||||
}
|
}
|
||||||
uninstall(p, s)
|
uninstall(p, s)
|
||||||
@@ -852,9 +852,9 @@ func RunMobile(appConfig *AppConfig, appCallback *AppCallback, stopCh chan struc
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckDeactivationPin checks if deactivation pin is valid
|
// CheckDeactivationPin checks if deactivation pin is valid
|
||||||
func CheckDeactivationPin(pin int64) int {
|
func CheckDeactivationPin(pin int64, stopCh chan struct{}) int {
|
||||||
deactivationPin = pin
|
deactivationPin = pin
|
||||||
if err := checkDeactivationPin(nil); isCheckDeactivationPinErr(err) {
|
if err := checkDeactivationPin(nil, stopCh); isCheckDeactivationPinErr(err) {
|
||||||
return deactivationPinInvalidExitCode
|
return deactivationPinInvalidExitCode
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
@@ -935,7 +935,7 @@ func run(appCallback *AppCallback, stopCh chan struct{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.router = router.New(&cfg, cdUID != "")
|
p.router = router.New(&cfg, cdUID != "")
|
||||||
cs, err := newControlServer(filepath.Join(sockDir, ctrldControlUnixSock))
|
cs, err := newControlServer(filepath.Join(sockDir, ControlSocketName()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Warn().Err(err).Msg("could not create control server")
|
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
|
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.
|
// checkStrFlagEmpty validates if a string flag was set to an empty string.
|
||||||
// If yes, emitting a fatal error message.
|
// If yes, emitting a fatal error message.
|
||||||
func checkStrFlagEmpty(cmd *cobra.Command, flagName string) {
|
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")
|
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.
|
// 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()
|
dir, err := socketDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Err(err).Msg("could not check deactivation pin")
|
mainLog.Load().Err(err).Msg("could not check deactivation pin")
|
||||||
@@ -2185,7 +2205,7 @@ func checkDeactivationPin(s service.Service) error {
|
|||||||
}
|
}
|
||||||
var cc *controlClient
|
var cc *controlClient
|
||||||
if s == nil {
|
if s == nil {
|
||||||
cc = newControlClient(filepath.Join(dir, ctrldControlUnixSock))
|
cc = newSocketControlClientMobile(dir, stopCh)
|
||||||
} else {
|
} else {
|
||||||
cc = newSocketControlClient(s, dir)
|
cc = newSocketControlClient(s, dir)
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-3
@@ -33,11 +33,22 @@ const (
|
|||||||
defaultSemaphoreCap = 256
|
defaultSemaphoreCap = 256
|
||||||
ctrldLogUnixSock = "ctrld_start.sock"
|
ctrldLogUnixSock = "ctrld_start.sock"
|
||||||
ctrldControlUnixSock = "ctrld_control.sock"
|
ctrldControlUnixSock = "ctrld_control.sock"
|
||||||
upstreamPrefix = "upstream."
|
// iOS unix socket name max length is 11.
|
||||||
upstreamOS = upstreamPrefix + "os"
|
ctrldControlUnixSockMobile = "cd.sock"
|
||||||
upstreamPrivate = upstreamPrefix + "private"
|
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) {
|
var logf = func(format string, args ...any) {
|
||||||
mainLog.Load().Debug().Msgf(format, args...)
|
mainLog.Load().Debug().Msgf(format, args...)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func mapCallback(callback AppCallback) cli.AppCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Stop(Pin int64) int {
|
func (c *Controller) Stop(Pin int64) int {
|
||||||
errorCode := cli.CheckDeactivationPin(Pin)
|
errorCode := cli.CheckDeactivationPin(Pin, c.stopCh)
|
||||||
if errorCode == 0 && c.stopCh != nil {
|
if errorCode == 0 && c.stopCh != nil {
|
||||||
close(c.stopCh)
|
close(c.stopCh)
|
||||||
c.stopCh = nil
|
c.stopCh = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user