Skip to content

Commit

Permalink
Register the desktop app path with launch services (#1731)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Jun 3, 2024
1 parent 8e5c472 commit ed17220
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
19 changes: 19 additions & 0 deletions ee/desktop/user/universallink/handler.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build darwin
// +build darwin

#import <Foundation/Foundation.h>

BOOL registerAppBundle(char *cAppBundlePath) {
NSString *appBundlePath = [NSString stringWithUTF8String:cAppBundlePath];
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appBundlePath];

// Set inUpdate to true to ensure app gets updated
OSStatus status = LSRegisterURL((CFURLRef)url, YES);

if (status != noErr) {
NSLog(@"could not register app bundle: LSRegisterURL returned error: %jd", (intmax_t)status);
return NO;
}

return YES;
}
41 changes: 40 additions & 1 deletion ee/desktop/user/universallink/handler_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@

package universallink

/*
#cgo darwin CFLAGS: -DDARWIN -x objective-c
#cgo darwin LDFLAGS: -framework Foundation
#include <stdbool.h>
#include <stdlib.h>
bool registerAppBundle(char *cAppBundlePath);
*/
import "C"
import (
"context"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"unsafe"

"github.com/kolide/launcher/ee/gowrapper"
"github.com/kolide/launcher/ee/localserver"
Expand Down Expand Up @@ -41,6 +53,13 @@ func NewUniversalLinkHandler(slogger *slog.Logger) (*universalLinkHandler, chan
}

func (u *universalLinkHandler) Execute() error {
// Register self
if err := register(); err != nil {
u.slogger.Log(context.TODO(), slog.LevelWarn,
"could not register desktop app with Launch Services on startup",
"err", err,
)
}
for {
select {
case i := <-u.urlInput:
Expand Down Expand Up @@ -74,6 +93,26 @@ func (u *universalLinkHandler) Interrupt(_ error) {
close(u.urlInput)
}

func register() error {
currentExecutable, err := os.Executable()
if err != nil {
return fmt.Errorf("getting current executable: %w", err)
}

// Point to `Kolide.app`
currentExecutable = strings.TrimSuffix(currentExecutable, "/Contents/MacOS/launcher")

currentExecutableCStr := C.CString(currentExecutable)
defer C.free(unsafe.Pointer(currentExecutableCStr))

success := C.registerAppBundle(currentExecutableCStr)
if !success {
return fmt.Errorf("could not register %s", currentExecutable)
}

return nil
}

// handleUniversalLinkRequest receives requests, validates them, and forwards them
// to launcher root's localserver.
func (u *universalLinkHandler) handleUniversalLinkRequest(requestUrl string) error {
Expand Down Expand Up @@ -103,7 +142,7 @@ func (u *universalLinkHandler) handleUniversalLinkRequest(requestUrl string) err
return
}
u.slogger.Log(ctx, slog.LevelWarn,
"could not make universal link request",
"could not forward universal link request",
"port", p,
"err", err,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !darwin
// +build !darwin

package universallink

import (
Expand Down

0 comments on commit ed17220

Please sign in to comment.