Skip to content

Commit

Permalink
pluh šŸ—£ļø
Browse files Browse the repository at this point in the history
  • Loading branch information
foxt committed Oct 2, 2023
1 parent 1351e67 commit 7731e78
Show file tree
Hide file tree
Showing 46 changed files with 208 additions and 77 deletions.
Binary file added assets/logo/AppIcon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/AppIcon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/AppIcon-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/AppIcon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/AppIcon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/AppIcon-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/logo16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions sk.Core/BaseSecretStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ļ»æusing System;
namespace sk.Core
{
public abstract class BaseSecretStore
{
public virtual string? GetSecret(string service) {
Console.WriteLine("BaseSecretStore#GetSecret called?");
return null;
}
public virtual bool SetSecret(string service, string secret, string username) {
Console.WriteLine("BaseSecretStore#SetSecret called?");
return false;
}
}
}

26 changes: 23 additions & 3 deletions sk.Core/LastFM/LastFMAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using sk.Core;

namespace sk {
public class LastFMAPI {
Expand All @@ -30,19 +31,33 @@ public string? SessionKey {


private HttpClient client = new HttpClient();
private BaseSecretStore secretStore;

public LastFMAPI(BaseSecretStore secretstore) {
this.secretStore = secretstore;

public LastFMAPI() {
var assembliesLoaded = "";
foreach (var assmb in AppDomain.CurrentDomain.GetAssemblies()) {
var name = assmb.GetName();
if (name.Name.StartsWith("System"))
continue;
assembliesLoaded += name.Name + "/" + name.Version + " ";
}
assembliesLoaded += "(https://www.last.fm/user/foxtay)";
assembliesLoaded += "(https://www.last.fm/user/unicodefox)";

Console.WriteLine($"SK Core LFM API: identifying as \"{assembliesLoaded}\" ");
client.DefaultRequestHeaders.Add("User-Agent", assembliesLoaded);
Console.WriteLine($"SK Core Authent: Retrieving secret for {this.BaseURL}");
try {
var secret = secretStore.GetSecret(this.BaseURL);
if (secret == null)
Console.WriteLine($"SK Core Authent: No secret");
else
Console.WriteLine($"SK Core Authent: Got secret {secret.Substring(0, Math.Min(secret.Length,5))}...");
this.SessionKey = secret;
} catch(Exception e) {
Console.WriteLine($"SK Core Authent: " + e.ToString());
}
}

public async Task<bool> CheckAuthenticated(bool deep = false) {
Expand All @@ -59,7 +74,12 @@ public async Task<bool> CheckAuthenticated(bool deep = false) {
return false;
} else {
Console.WriteLine("hello " + userInfo.user.name);
return true;
try {
secretStore.SetSecret(this.BaseURL, this.SessionKey, userInfo.user.name.ToString());
}catch(Exception e) {
Console.WriteLine($"SK Core Authent: " + e.ToString());
}
return true;
}
}

Expand Down
7 changes: 5 additions & 2 deletions sk.Core/LastFM/LastFMScrobbler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using sk.Players.Generic;
using sk.Core;

namespace sk
{
public class SkLastFMScrobbler {
public SkScrobblerCore scrobbler;
public LastFMAPI api = new LastFMAPI();
public SkLastFMScrobbler(SkScrobblerCore scrobbler) {
public LastFMAPI api;
public SkLastFMScrobbler(SkScrobblerCore scrobbler, BaseSecretStore secretStore) {
this.api = new LastFMAPI(secretStore);

this.scrobbler = scrobbler;
scrobbler.OnNowPlaying += OnNowPlaying;
scrobbler.OnScrobble += OnScrobble;
Expand Down
16 changes: 0 additions & 16 deletions sk.Players.Mac.AppleMusic.BindingLibrary/ApiDefinition.cs

This file was deleted.

5 changes: 0 additions & 5 deletions sk.Players.Mac.AppleMusic.BindingLibrary/StructsAndEnums.cs

This file was deleted.

This file was deleted.

43 changes: 38 additions & 5 deletions sk.Players.Mac.AppleMusic/AppleMusicPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@
using sk.Players.Generic;
using Timer = System.Timers.Timer;
using static System.Net.Mime.MediaTypeNames;
using System.Runtime.InteropServices;
using ObjCRuntime;

namespace sk.Players.Mac.AppleMusic {
public class SkMacAppleMusicPlayer : SkPlayer {
[DllImport(Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
static extern IntPtr objc_msgSend_uint(
IntPtr target,
IntPtr selector,
uint code
);
[DllImport(Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
static extern IntPtr objc_msgSend(
IntPtr target,
IntPtr selector
);

private NSDistributedNotificationCenter dnc = NSDistributedNotificationCenter.DefaultCenter;
public SkMacAppleMusicPlayer() {
this.Name = "Apple Music";
Expand Down Expand Up @@ -40,15 +54,34 @@ private void startListener() {

}

iTunesApplication app;
SBApplication app;
SBObject prop;

private void PullElapsedTime(object? sender, ElapsedEventArgs e)
{
if (!this.isPlaying) return;
if (this.app == null) this.app = SBApplication.GetApplication<iTunesApplication>(this.isAppleMusic ? "com.apple.Music" : "com.apple.iTunes");
var playerPos = app.playerPosition;
Console.WriteLine(playerPos);
this.Position = (int)playerPos;
try {

if (this.app == null)
this.app = SBApplication.GetApplication<SBApplication>(this.isAppleMusic ? "com.apple.Music" : "com.apple.iTunes");
if (this.app == null) {
Console.WriteLine("tried to pull app but is still null, assuming the app has been quat(??)");
this.isPlaying = false;
return;
}

if (this.prop == null) {
this.prop = (SBObject)Runtime.GetNSObject(
objc_msgSend_uint(app.Handle, new Selector("propertyWithCode:").Handle, 1884319603)
)!;
}
this.Position = (int)(NSNumber)this.prop.Get;
Console.WriteLine(this.Position);
} catch(Exception err) {
Console.WriteLine(err.Message);
this.app = null;
this.prop = null;
}
}

private string lastId = "";
Expand Down
3 changes: 2 additions & 1 deletion sk.Players.Mac.AppleMusic/sk.Players.Mac.AppleMusic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<CreatePackage>false</CreatePackage>
<UseSGen>false</UseSGen>
<LinkMode>None</LinkMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<CreatePackage>false</CreatePackage>
<UseSGen>false</UseSGen>
<LinkMode>None</LinkMode>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\sk.Players.Generic\sk.Players.Generic.csproj" />
<ProjectReference Include="..\sk.Players.Mac.AppleMusic.BindingLibrary\sk.Players.Mac.AppleMusic.BindingLibrary.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion sk.UI.Mac/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override void DidFinishLaunching (NSNotification notification)
{
var player = new sk.Players.Mac.AppleMusic.SkMacAppleMusicPlayer();
var sk = new SkScrobblerCore(player);
var lfm = new SkLastFMScrobbler(sk);
var lfm = new SkLastFMScrobbler(sk, new MacKeychainSecretStore());
var ui = new UI(lfm);

lfm.api.OnAuthRequired += Api_OnAuthRequired;
Expand Down
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/AppIcon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/AppIcon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/AppIcon-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/AppIcon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sk.UI.Mac/Assets.xcassets/AppIcon.appiconset/AppIcon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions sk.UI.Mac/AuthWebview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ namespace sk.UI.Mac
{
class AuthWebViewDelegate : WKNavigationDelegate {
public event EventHandler<string> OnAuthResult;


[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler) {
var url = navigationAction.Request.Url;
Console.WriteLine("DecidePolicy: " + url.Host + " - " + url.Path);
if (url.Host == "foxt.dev" && url.Path == "/sk/auth") {
// TODO: parse this properly
var token = url.Query.Replace("token=", "");
Console.WriteLine("URL auth ");

// TODO: parse this properly
OnAuthResult.Invoke(this, token);
//decisionHandler(WKNavigationActionPolicy.Cancel);

// Terminating app due to uncaught exception 'NSInternalInconsistencyException',
// 'Completion handler passed to -[sk_UI_Mac_AuthWebViewDelegate webView:decidePolicyForNavigationAction:decisionHandler:] was not called'
decisionHandler(WKNavigationActionPolicy.Cancel);
} else if (
(url.Host == "www.last.fm" && url.Path == "/api/auth") ||
(url.Host == "www.last.fm" && url.Path == "/login")) {
Expand Down
14 changes: 13 additions & 1 deletion sk.UI.Mac/Entitlements.plist
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.automation.apple-events</key>
<true/>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<!--<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)dev.foxt.sk.mac</string>
</array>-->
</dict>
</plist>
4 changes: 3 additions & 1 deletion sk.UI.Mac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<key>CFBundleName</key>
<string>sk</string>
<key>CFBundleIdentifier</key>
<string>dev.foxt.sk.ui.mac</string>
<string>dev.foxt.sk.mac</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
Expand All @@ -28,5 +28,7 @@
<string>Assets.xcassets/AppIcon.appiconset</string>
<key>LSUIElement</key>
<true/>
<key>CFBundleIconFile</key>
<string>AppIcon.icns</string>
</dict>
</plist>
61 changes: 61 additions & 0 deletions sk.UI.Mac/MacKeychainSecretStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
ļ»æusing System;
using sk.Core;
using Security;
namespace sk.UI.Mac
{
public class MacKeychainSecretStore : BaseSecretStore
{
// Clean all keychain entries -- we do this to prevent duplicate entries causing undefined behaviour.
private void KeychainClean(string service) {
var query = new SecRecord(SecKind.GenericPassword) {
Service = service,
Label = "sk stored secret"
};
var status = SecKeyChain.Remove(query);

Console.WriteLine("Keychain Clean: " + status);
}
private void KeychainAdd(string service, string secret, string username) {
KeychainClean(service);
var record = new SecRecord(SecKind.GenericPassword) {
Service = service,
Label = "sk stored secret",
Account = username,
ValueData = secret,
//Accessible = SecAccessible.AfterFirstUnlock,
//UseDataProtectionKeychain = true,
//Synchronizable = true
};
var status = SecKeyChain.Add(record) ;
Console.WriteLine("Keychain Write: " + status);
if (status != SecStatusCode.Success)
throw new SecurityException(status);

}
private string? KeychainGet(string service) {
var query = new SecRecord(SecKind.GenericPassword) {
Service = service,
Label = "sk stored secret"
};
SecStatusCode status;
var data = SecKeyChain.QueryAsData(query, false, out status);

Console.WriteLine("Keychain Get: " + status);
if (status == SecStatusCode.Success)
return data.ToString();
else if (status == SecStatusCode.ItemNotFound)
return null;
else
throw new SecurityException(status);
}

override public string? GetSecret(string service) {
return KeychainGet(service);
}
override public bool SetSecret(string service, string secret, string username) {
KeychainAdd(service,secret,username);
return true;
}
}
}

4 changes: 3 additions & 1 deletion sk.UI.Mac/Main.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
ļ»æusing AppKit;
ļ»æusing System.Diagnostics;
using AppKit;

namespace sk.UI.Mac
{
static class MainClass
{
static void Main (string [] args)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal;
NSApplication.Init ();
NSApplication.Main (args);
}
Expand Down
13 changes: 13 additions & 0 deletions sk.UI.Mac/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>AnyCPU</LastUsedPlatform>
<publishUrl>bin/Release/net7.0-macos10.14/publish</publishUrl>
<DeleteExistingFiles>false</DeleteExistingFiles>
<TargetFramework>net7.0-macos10.14</TargetFramework>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
</Project>
Binary file added sk.UI.Mac/Resources/logo16.png
Binary file added sk.UI.Mac/Resources/[email protected]
Loading

0 comments on commit 7731e78

Please sign in to comment.