Skip to content

Commit

Permalink
Feature: add option to support allow connection from lan
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Aug 26, 2018
1 parent dcc961f commit 8fe2512
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
4 changes: 0 additions & 4 deletions ClashX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
4952C3D02117027C004A4FA8 /* ConfigFileFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4952C3CF2117027C004A4FA8 /* ConfigFileFactory.swift */; };
495340B020DE5F7200B0D3FF /* StatusItemView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 495340AF20DE5F7200B0D3FF /* StatusItemView.xib */; };
495340B320DE68C300B0D3FF /* StatusItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495340B220DE68C300B0D3FF /* StatusItemView.swift */; };
495A44BF20D2660A00888A0A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495A44BE20D2660A00888A0A /* AppDelegate.swift */; };
495A44CD20D266BA00888A0A /* ClashXLaunchHelper.app in CopyFiles */ = {isa = PBXBuildFile; fileRef = 495A44BC20D2660A00888A0A /* ClashXLaunchHelper.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
495A44D320D267D000888A0A /* LaunchAtLogin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495A44D220D267D000888A0A /* LaunchAtLogin.swift */; };
4966E9E32118153A00A391FB /* NSUserNotificationCenter+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4966E9E22118153A00A391FB /* NSUserNotificationCenter+Extension.swift */; };
Expand Down Expand Up @@ -95,7 +94,6 @@
495340AF20DE5F7200B0D3FF /* StatusItemView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = StatusItemView.xib; sourceTree = "<group>"; };
495340B220DE68C300B0D3FF /* StatusItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusItemView.swift; sourceTree = "<group>"; };
495A44BC20D2660A00888A0A /* ClashXLaunchHelper.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ClashXLaunchHelper.app; sourceTree = BUILT_PRODUCTS_DIR; };
495A44BE20D2660A00888A0A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
495A44C720D2660B00888A0A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
495A44C820D2660B00888A0A /* ClashXLaunchHelper.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ClashXLaunchHelper.entitlements; sourceTree = "<group>"; };
495A44D220D267D000888A0A /* LaunchAtLogin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LaunchAtLogin.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -199,7 +197,6 @@
isa = PBXGroup;
children = (
49E07A8920D501A000A088A3 /* Main.storyboard */,
495A44BE20D2660A00888A0A /* AppDelegate.swift */,
495A44C720D2660B00888A0A /* Info.plist */,
495A44C820D2660B00888A0A /* ClashXLaunchHelper.entitlements */,
);
Expand Down Expand Up @@ -557,7 +554,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
495A44BF20D2660A00888A0A /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
28 changes: 23 additions & 5 deletions ClashX/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var proxyModeGlobalMenuItem: NSMenuItem!
@IBOutlet weak var proxyModeDirectMenuItem: NSMenuItem!
@IBOutlet weak var proxyModeRuleMenuItem: NSMenuItem!
@IBOutlet weak var allowFromLanMenuItem: NSMenuItem!

@IBOutlet weak var proxyModeMenuItem: NSMenuItem!
@IBOutlet weak var showNetSpeedIndicatorMenuItem: NSMenuItem!
Expand Down Expand Up @@ -107,8 +108,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
case .global:self.proxyModeGlobalMenuItem.state = .on
case .rule:self.proxyModeRuleMenuItem.state = .on
}


self.allowFromLanMenuItem.state = config!.allowLan ? .on : .off
self.proxyModeMenuItem.title = "Proxy Mode (\(config!.mode.rawValue))"

self.updateProxyList()
Expand Down Expand Up @@ -170,6 +170,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

func selectAllowLanWithMenory() {
ApiRequest.updateAllowLan(allow: ConfigManager.allowConnectFromLan){
self.syncConfig()
}
}

func updateProxyList() {
ProxyMenuItemFactory.menuItems { [unowned self] (menus) in
let startIndex = self.statusMenu.items.index(of: self.separatorLineTop)!+1
Expand Down Expand Up @@ -207,17 +213,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if (error != "success") {
NSUserNotificationCenter.default.postConfigErrorNotice(msg:error)
} else {
self.resetStreamApi()
self.selectOutBoundModeWithMenory()
self.syncConfig() {
self.resetStreamApi()
self.selectOutBoundModeWithMenory()
self.selectAllowLanWithMenory()
}
}
}

}

func syncConfig(){
func syncConfig(completeHandler:(()->())? = nil){
ApiRequest.requestConfig{ (config) in
guard config.port > 0 else {return}
ConfigManager.shared.currentConfig = config
completeHandler?()
}
}

Expand Down Expand Up @@ -257,6 +267,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let port = ConfigManager.shared.currentConfig?.port ?? 0
pasteboard.setString("export https_proxy=http://127.0.0.1:\(port);export http_proxy=http://127.0.0.1:\(port)", forType: .string)
}
@IBAction func actionAllowFromLan(_ sender: NSMenuItem) {
ApiRequest.updateAllowLan(allow: !ConfigManager.allowConnectFromLan) {
[unowned self] in
self.syncConfig()
ConfigManager.allowConnectFromLan = !ConfigManager.allowConnectFromLan
}

}

@IBAction func actionStartAtLogin(_ sender: NSMenuItem) {
LaunchAtLogin.shared.isEnabled = !LaunchAtLogin.shared.isEnabled
Expand Down
7 changes: 7 additions & 0 deletions ClashX/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="ClashX" customModuleProvider="target">
<connections>
<outlet property="allowFromLanMenuItem" destination="Vz8-7n-vx6" id="Fzz-EG-huC"/>
<outlet property="autoStartMenuItem" destination="B1J-XB-BiZ" id="xaS-h5-qd0"/>
<outlet property="logLevelMenuItem" destination="3Da-fL-Mzr" id="ZCW-Il-fOZ"/>
<outlet property="proxyModeDirectMenuItem" destination="Np6-Pm-Lo3" id="DER-K7-DNW"/>
Expand Down Expand Up @@ -84,6 +85,12 @@
<action selector="actionShowNetSpeedIndicator:" target="Voe-Tx-rLC" id="iHm-b9-VSa"/>
</connections>
</menuItem>
<menuItem title="Allow connct from Lan" id="Vz8-7n-vx6">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="actionAllowFromLan:" target="Voe-Tx-rLC" id="BGp-Tp-MiH"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="UpD-DI-Smv"/>
<menuItem title="Config" id="JMV-Dy-CI0">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
12 changes: 12 additions & 0 deletions ClashX/General/ApiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ class ApiRequest{
}
}

static func updateAllowLan(allow:Bool,completeHandler:@escaping (()->())) {
request(ConfigManager.apiUrl + "/configs",
method: .put,
parameters: ["allow-lan":allow,
"socks-port":ConfigManager.shared.currentConfig!.socketPort,
"port":ConfigManager.shared.currentConfig!.port],
encoding: JSONEncoding.default).response{
_ in
completeHandler()
}
}

static func updateProxyGroup(group:String,selectProxy:String,callback:@escaping ((Bool)->())) {
request(ConfigManager.apiUrl + "/proxies/\(group)", method: .put, parameters: ["name":selectProxy], encoding: JSONEncoding.default).responseJSON { (response) in
callback(response.response?.statusCode == 204)
Expand Down
9 changes: 9 additions & 0 deletions ClashX/General/ConfigManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ class ConfigManager {
}
}

static var allowConnectFromLan:Bool {
get{
return UserDefaults.standard.bool(forKey: "allowConnectFromLan")
}
set {
UserDefaults.standard.set(newValue, forKey: "allowConnectFromLan")
}
}

static var selectLoggingApiLevel:ClashLogLevel {
get{
return ClashLogLevel(rawValue: UserDefaults.standard.string(forKey: "selectLoggingApiLevel") ?? "") ?? .info
Expand Down

0 comments on commit 8fe2512

Please sign in to comment.