Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Add hotkey switch proxy mode(reference: ShadowsocksX-NG). #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions V2RayX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
631B81132134EFC100ED6268 /* MASShortcut.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 631B81122134EFC100ED6268 /* MASShortcut.framework */; };
631B81142134F0FC00ED6268 /* MASShortcut.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 631B81122134EFC100ED6268 /* MASShortcut.framework */; };
631B81152134F0FC00ED6268 /* MASShortcut.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 631B81122134EFC100ED6268 /* MASShortcut.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
631B811A2134F40700ED6268 /* ShortcutsController.m in Sources */ = {isa = PBXBuildFile; fileRef = 631B81192134F40700ED6268 /* ShortcutsController.m */; };
631B811E21351CED00ED6268 /* ToastWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 631B811C21351CED00ED6268 /* ToastWindowController.m */; };
631B811F21351CED00ED6268 /* ToastWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 631B811D21351CED00ED6268 /* ToastWindowController.xib */; };
9504C07D1C662C3000352520 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 9504C07C1C662C3000352520 /* AppDelegate.m */; };
9504C0801C662C3000352520 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 9504C07F1C662C3000352520 /* main.m */; };
9504C0821C662C3000352520 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9504C0811C662C3000352520 /* Assets.xcassets */; };
Expand Down Expand Up @@ -56,6 +62,17 @@
/* End PBXContainerItemProxy section */

/* Begin PBXCopyFilesBuildPhase section */
631B81162134F0FC00ED6268 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
631B81152134F0FC00ED6268 /* MASShortcut.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
9504C0C41C662DB400352520 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
Expand All @@ -77,6 +94,12 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
631B81122134EFC100ED6268 /* MASShortcut.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = MASShortcut.framework; sourceTree = "<group>"; };
631B81182134F40700ED6268 /* ShortcutsController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShortcutsController.h; sourceTree = "<group>"; };
631B81192134F40700ED6268 /* ShortcutsController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ShortcutsController.m; sourceTree = "<group>"; };
631B811B21351CED00ED6268 /* ToastWindowController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ToastWindowController.h; sourceTree = "<group>"; };
631B811C21351CED00ED6268 /* ToastWindowController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ToastWindowController.m; sourceTree = "<group>"; };
631B811D21351CED00ED6268 /* ToastWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ToastWindowController.xib; sourceTree = "<group>"; };
9504C0781C662C3000352520 /* V2RayX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = V2RayX.app; sourceTree = BUILT_PRODUCTS_DIR; };
9504C07B1C662C3000352520 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
9504C07C1C662C3000352520 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -144,7 +167,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
631B81132134EFC100ED6268 /* MASShortcut.framework in Frameworks */,
95A143CC1C87FCDC008756B9 /* Cocoa.framework in Frameworks */,
631B81142134F0FC00ED6268 /* MASShortcut.framework in Frameworks */,
9504C0C11C662D9400352520 /* libz.tbd in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -204,6 +229,11 @@
9504C0D21C66511B00352520 /* ServerProfile.h */,
9504C0D31C66511B00352520 /* ServerProfile.m */,
9504C0861C662C3000352520 /* Info.plist */,
631B81182134F40700ED6268 /* ShortcutsController.h */,
631B81192134F40700ED6268 /* ShortcutsController.m */,
631B811B21351CED00ED6268 /* ToastWindowController.h */,
631B811C21351CED00ED6268 /* ToastWindowController.m */,
631B811D21351CED00ED6268 /* ToastWindowController.xib */,
9504C07E1C662C3000352520 /* Supporting Files */,
);
path = V2RayX;
Expand Down Expand Up @@ -296,6 +326,7 @@
95FCB2451C688BB20081B6FD /* Frameworks */ = {
isa = PBXGroup;
children = (
631B81122134EFC100ED6268 /* MASShortcut.framework */,
95A143CA1C87FC6E008756B9 /* Cocoa.framework */,
9504C0C01C662D9400352520 /* libz.tbd */,
);
Expand Down Expand Up @@ -334,6 +365,7 @@
9DB94D77209D273A0001E9DD /* Run Script */,
953B60541DB3E31D00D40654 /* Run Script */,
9504C0761C662C3000352520 /* Resources */,
631B81162134F0FC00ED6268 /* Embed Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -426,6 +458,7 @@
buildActionMask = 2147483647;
files = (
9D5EAC151FDCD70300C14B46 /* geoip.dat in Resources */,
631B811F21351CED00ED6268 /* ToastWindowController.xib in Resources */,
9D5EAC161FDCD70300C14B46 /* geosite.dat in Resources */,
9D3CEE6F209E070F0077CCFE /* customizedConfigWindow.xib in Resources */,
9D5EAC171FDCD70300C14B46 /* v2ctl in Resources */,
Expand Down Expand Up @@ -486,6 +519,7 @@
9577E1BE1C6B065E00093BD3 /* GCDWebServerStreamedResponse.m in Sources */,
9504C0801C662C3000352520 /* main.m in Sources */,
9577E1BB1C6B065E00093BD3 /* GCDWebServerDataResponse.m in Sources */,
631B811A2134F40700ED6268 /* ShortcutsController.m in Sources */,
9577E1BA1C6B065E00093BD3 /* GCDWebServerURLEncodedFormRequest.m in Sources */,
9577E1B71C6B065E00093BD3 /* GCDWebServerDataRequest.m in Sources */,
9577E1BC1C6B065E00093BD3 /* GCDWebServerErrorResponse.m in Sources */,
Expand All @@ -497,6 +531,7 @@
9577E1B61C6B065E00093BD3 /* GCDWebServerResponse.m in Sources */,
9577E1B21C6B065E00093BD3 /* GCDWebServer.m in Sources */,
9504C0D41C66511B00352520 /* ServerProfile.m in Sources */,
631B811E21351CED00ED6268 /* ToastWindowController.m in Sources */,
9577E1B41C6B065E00093BD3 /* GCDWebServerFunctions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -643,6 +678,11 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/V2RayX",
"$(PROJECT_DIR)/V2RayX/Dependencies",
);
INFOPLIST_FILE = V2RayX/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cenmrev.V2RayX;
Expand All @@ -655,6 +695,11 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/V2RayX",
"$(PROJECT_DIR)/V2RayX/Dependencies",
);
INFOPLIST_FILE = V2RayX/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cenmrev.V2RayX;
Expand Down
1 change: 1 addition & 0 deletions V2RayX/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int runCommandLine(NSString* launchPath, NSArray* arguments);
@interface AppDelegate : NSObject <NSApplicationDelegate> {
BOOL proxyState;
ProxyMode proxyMode;
ProxyMode lastProxyMode;
NSInteger localPort;
NSInteger httpPort;
BOOL udpSupport;
Expand Down
46 changes: 46 additions & 0 deletions V2RayX/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import "ConfigWindowController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "ServerProfile.h"
#import "ShortcutsController.h"
#import "ToastWindowController.h"

#define kUseAllServer -10

Expand All @@ -23,6 +25,7 @@ @interface AppDelegate () {
FSEventStreamRef fsEventStream;

NSData* v2rayJSONconfig;
ToastWindowController *tosat;
}

@end
Expand Down Expand Up @@ -117,6 +120,10 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

// resume the service when mac wakes up
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(configurationDidChange) name:NSWorkspaceDidWakeNotification object:NULL];

// Register global hotkey
[ShortcutsController bindShortcuts];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(hotkeyChangeProxyMode) name:@"NOTIFY_SWITCH_PROXY_MODE_SHORTCUT" object:nil];
}

- (void) writeDefaultSettings {
Expand Down Expand Up @@ -216,6 +223,7 @@ - (IBAction)chooseV2rayRules:(id)sender {
[self backupSystemProxy];
}
proxyMode = rules;
lastProxyMode = proxyMode;
[self configurationDidChange];
}

Expand All @@ -224,13 +232,15 @@ - (IBAction)choosePacMode:(id)sender {
[self backupSystemProxy];
}
proxyMode = pac;
lastProxyMode = proxyMode;
[self configurationDidChange];
}

- (IBAction)chooseGlobalMode:(id)sender {
if(proxyState == true && proxyMode == manual) {
[self backupSystemProxy];
}
lastProxyMode = proxyMode;
proxyMode = global;
[self configurationDidChange];
}
Expand All @@ -240,9 +250,45 @@ - (IBAction)chooseManualMode:(id)sender {
[self restoreSystemProxy];
}
proxyMode = manual;
lastProxyMode = proxyMode;
[self configurationDidChange];
}

- (void)hotkeyChangeProxyMode {
if (proxyMode == global) {
switch (lastProxyMode) {
case rules:
[self chooseV2rayRules:nil];
[self makeToast:@"V2rayRules Mode"];
break;
case pac:
[self choosePacMode:nil];
[self makeToast:@"Pac Mode"];
break;
case manual:
[self chooseManualMode:nil];
[self makeToast:@"Manual Mode"];
break;
default:
break;
}
} else {
[self chooseGlobalMode:nil];
[self makeToast:@"Global Mode"];
}
}

- (void)makeToast:(NSString *)message {
if (tosat) {
[tosat close];
}
tosat = [[ToastWindowController alloc] initWithWindowNibName:@"ToastWindowController"];
tosat.message = message;
[tosat showWindow:self];
[tosat fadeInHud];

}

- (IBAction)showConfigWindow:(id)sender {
if (configWindowController) {
[configWindowController close];
Expand Down
27 changes: 26 additions & 1 deletion V2RayX/ConfigWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="551" height="479"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="778"/>
<view key="contentView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="551" height="479"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down Expand Up @@ -557,6 +557,31 @@ Gw
<action selector="addRemoveServer:" target="-2" id="o8X-ua-ltZ"/>
</connections>
</segmentedControl>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="URA-jB-Suh">
<rect key="frame" x="239" y="413" width="4" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" id="sAZ-Fe-R7L">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="XZH-KT-e39">
<rect key="frame" x="239" y="411" width="124" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Switch proxy mode:" id="LRo-6i-M4h">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="wix-ZI-6eD" customClass="MASShortcutView">
<rect key="frame" x="373" y="409" width="130" height="19"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="associatedUserDefaultsKey" value="SwitchProxyMode"/>
</userDefinedRuntimeAttributes>
</customView>
</subviews>
</view>
<point key="canvasLocation" x="174.5" y="181.5"/>
Expand Down
2 changes: 1 addition & 1 deletion V2RayX/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>296</string>
<string>330</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
1 change: 1 addition & 0 deletions V2RayX/MASShortcut.framework/Headers
1 change: 1 addition & 0 deletions V2RayX/MASShortcut.framework/MASShortcut
1 change: 1 addition & 0 deletions V2RayX/MASShortcut.framework/Modules
1 change: 1 addition & 0 deletions V2RayX/MASShortcut.framework/Resources
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extern NSString *const MASDictionaryTransformerName;

/**
Converts shortcuts for storage in user defaults.

User defaults can’t stored custom types directly, they have to
be serialized to `NSData` or some other supported type like an
`NSDictionary`. In Cocoa Bindings, the conversion can be done
using value transformers like this one.

There’s a built-in transformer (`NSKeyedUnarchiveFromDataTransformerName`)
that converts any `NSCoding` types to `NSData`, but with shortcuts
it makes sense to use a dictionary instead – the defaults look better
when inspected with the `defaults` command-line utility and the
format is compatible with an older sortcut library called Shortcut
Recorder.
*/
@interface MASDictionaryTransformer : NSValueTransformer
@end
44 changes: 44 additions & 0 deletions V2RayX/MASShortcut.framework/Versions/A/Headers/MASKeyCodes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#import <Carbon/Carbon.h>
#import <AppKit/AppKit.h>
#import "MASKeyMasks.h"

// These glyphs are missed in Carbon.h
typedef NS_ENUM(unsigned short, kMASShortcutGlyph) {
kMASShortcutGlyphEject = 0x23CF,
kMASShortcutGlyphClear = 0x2715,
kMASShortcutGlyphDeleteLeft = 0x232B,
kMASShortcutGlyphDeleteRight = 0x2326,
kMASShortcutGlyphLeftArrow = 0x2190,
kMASShortcutGlyphRightArrow = 0x2192,
kMASShortcutGlyphUpArrow = 0x2191,
kMASShortcutGlyphDownArrow = 0x2193,
kMASShortcutGlyphEscape = 0x238B,
kMASShortcutGlyphHelp = 0x003F,
kMASShortcutGlyphPageDown = 0x21DF,
kMASShortcutGlyphPageUp = 0x21DE,
kMASShortcutGlyphTabRight = 0x21E5,
kMASShortcutGlyphReturn = 0x2305,
kMASShortcutGlyphReturnR2L = 0x21A9,
kMASShortcutGlyphPadClear = 0x2327,
kMASShortcutGlyphNorthwestArrow = 0x2196,
kMASShortcutGlyphSoutheastArrow = 0x2198,
};

NS_INLINE NSString* NSStringFromMASKeyCode(unsigned short ch)
{
return [NSString stringWithFormat:@"%C", ch];
}

NS_INLINE NSUInteger MASPickCocoaModifiers(NSUInteger flags)
{
return (flags & (NSEventModifierFlagControl | NSEventModifierFlagShift | NSEventModifierFlagOption | NSEventModifierFlagCommand));
}

NS_INLINE UInt32 MASCarbonModifiersFromCocoaModifiers(NSUInteger cocoaFlags)
{
return
(cocoaFlags & NSEventModifierFlagCommand ? cmdKey : 0)
| (cocoaFlags & NSEventModifierFlagOption ? optionKey : 0)
| (cocoaFlags & NSEventModifierFlagControl ? controlKey : 0)
| (cocoaFlags & NSEventModifierFlagShift ? shiftKey : 0);
}
18 changes: 18 additions & 0 deletions V2RayX/MASShortcut.framework/Versions/A/Headers/MASKeyMasks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import <Availability.h>

// https://github.com/shpakovski/MASShortcut/issues/99
//
// Long story short: NSControlKeyMask and friends were replaced with NSEventModifierFlagControl
// and similar in macOS Sierra. The project builds fine & clean, but including MASShortcut in
// a project with deployment target set to 10.12 results in several deprecation warnings because
// of the control masks. Simply replacing the old symbols with the new ones isn’t an option,
// since it breaks the build on older SDKs – in Travis, for example.
//
// It should be safe to remove this whole thing once the 10.12 SDK is ubiquitous.

#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101200
#define NSEventModifierFlagCommand NSCommandKeyMask
#define NSEventModifierFlagControl NSControlKeyMask
#define NSEventModifierFlagOption NSAlternateKeyMask
#define NSEventModifierFlagShift NSShiftKeyMask
#endif
Loading