Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register the desktop app path with launch services #1731

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
directionless marked this conversation as resolved.
Show resolved Hide resolved
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
Loading