Skip to content

Commit 4a652fa

Browse files
Merge pull request #285 from TakayukiHoshi1984/modify_https_server
HTTPS対応
2 parents 91d002a + 2351380 commit 4a652fa

23 files changed

Lines changed: 385 additions & 15 deletions

File tree

dConnectSDK/dConnectBrowserForIOS9/BookmarkDBFramework/GHConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define IS_ORIGIN_ENABLE @"is_origin_enable"
2828
#define IS_EXTERNAL_IP @"is_external_ip"
2929
#define IS_AVAILABILITY @"is_avaiability"
30+
#define IS_SSL @"is_ssl"
3031

3132
// アクセストークン
3233
#define ACCESS_TOKEN @"access_token"

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/AppDelegate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4040
[def setBool:mgr.settings.useOriginEnable forKey:IS_ORIGIN_ENABLE];
4141
[def setBool:mgr.settings.useExternalIP forKey:IS_EXTERNAL_IP];
4242
[def setBool:mgr.settings.useManagerName forKey:IS_AVAILABILITY];
43+
[def setBool:mgr.settings.useSSL forKey:IS_SSL];
4344
[def synchronize];
4445

4546
}

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/classes/GHSettingController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ - (UITableViewCell*)configureCell:(UITableView *)tableView atIndexPath:(NSIndexP
156156
switch (type) {
157157
case SecurityCellTypeDeleteAccessToken:
158158
case SecurityCellTypeOriginWhitelist:
159+
case SecurityCellTypeRootCertInstall:
159160
return [self configureDetailCell:tableView atIndexPath: indexPath];
160161
break;
161162
case SecurityCellTypeOriginBlock:
162163
case SecurityCellTypeLocalOAuth:
163164
case SecurityCellTypeOrigin:
164165
case SecurityCellTypeExternIP:
165166
case SecurityCellTypeAvailability:
167+
case SecurityCellTypeSSL:
166168
{
167169
SwitchableCell *cell = (SwitchableCell*)[tableView dequeueReusableCellWithIdentifier:@"SwitchableCell"
168170
forIndexPath:indexPath];

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/classes/GHSettingViewModel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef NS_ENUM (NSInteger, SecurityCellType) {
4141
SecurityCellTypeLocalOAuth,
4242
SecurityCellTypeOrigin,
4343
SecurityCellTypeExternIP,
44+
SecurityCellTypeSSL,
45+
SecurityCellTypeRootCertInstall,
4446
};
4547

4648
@property (nonatomic, strong) NSArray* datasource;

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/classes/GHSettingViewModel.m

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ - (instancetype)init
3030
@(SecurityCellTypeOriginBlock),
3131
@(SecurityCellTypeLocalOAuth),
3232
@(SecurityCellTypeOrigin),
33-
@(SecurityCellTypeExternIP)]
33+
@(SecurityCellTypeExternIP),
34+
@(SecurityCellTypeSSL),
35+
@(SecurityCellTypeRootCertInstall)]
3436
];
3537
}
3638

@@ -117,6 +119,10 @@ - (NSString*)cellTitle:(NSIndexPath *)indexPath
117119
return @"Origin (有効/無効)";
118120
case SecurityCellTypeExternIP:
119121
return @"外部IPを許可 (有効/無効)";
122+
case SecurityCellTypeSSL:
123+
return @"SSL (ON/OFF)";
124+
case SecurityCellTypeRootCertInstall:
125+
return @"証明書インストール";
120126
}
121127
break;
122128
}
@@ -152,6 +158,10 @@ - (void)updateSwitch:(SecurityCellType)type switchState:(BOOL)isOn
152158
case SecurityCellTypeExternIP:
153159
[DConnectManager sharedManager].settings.useExternalIP = isOn;
154160
break;
161+
case SecurityCellTypeSSL:
162+
[DConnectManager sharedManager].settings.useSSL = isOn;
163+
[[DConnectManager sharedManager] stop];
164+
[[DConnectManager sharedManager] start];
155165
default:
156166
break;
157167
}
@@ -168,6 +178,7 @@ - (void)updateSwitchState
168178
[def setBool:settings.useOriginEnable forKey:IS_ORIGIN_ENABLE];
169179
[def setBool:settings.useExternalIP forKey:IS_EXTERNAL_IP];
170180
[def setBool:settings.useManagerName forKey:IS_AVAILABILITY];
181+
[def setBool:settings.useSSL forKey:IS_SSL];
171182
[def synchronize];
172183
}
173184

@@ -192,6 +203,9 @@ - (void)didSelectedRow:(NSIndexPath *)indexPath
192203
case SecurityCellTypeOriginWhitelist:
193204
[DConnectUtil showOriginWhitelist];
194205
break;
206+
case SecurityCellTypeRootCertInstall:
207+
[self showRootCertInstallDialog];
208+
break;
195209
}
196210
break;
197211
}
@@ -221,6 +235,10 @@ - (BOOL)switchState:(SecurityCellType)type
221235
[DConnectManager sharedManager].settings.useManagerName
222236
= [[NSUserDefaults standardUserDefaults] boolForKey:IS_AVAILABILITY];
223237
return [DConnectManager sharedManager].settings.useManagerName;
238+
case SecurityCellTypeSSL:
239+
[DConnectManager sharedManager].settings.useSSL
240+
= [[NSUserDefaults standardUserDefaults] boolForKey:IS_SSL];
241+
return [DConnectManager sharedManager].settings.useSSL;
224242

225243
default:
226244
break;
@@ -351,6 +369,50 @@ - (void)changeManagerName
351369
[[self rootViewController] presentViewController:alertController animated:YES completion:nil];
352370
}
353371

372+
/*!
373+
@brief 証明書の取得先を入力するためのダイアログを表示する。
374+
*/
375+
- (void)showRootCertInstallDialog
376+
{
377+
NSString *prompt = @"Safari経由で証明書をインストールします。証明書のURLを入力してください。";
378+
if (@available(iOS 10.3, *)) {
379+
prompt = [NSString stringWithFormat:@"%@\n\nインストール後、以下の設定で証明書を有効にすると、SSL通信が可能になります。\n\n一般 > 情報 > 証明書信頼設定", prompt];
380+
}
381+
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"証明書インストール"
382+
message:prompt
383+
preferredStyle:UIAlertControllerStyleAlert];
384+
[alertController addTextFieldWithConfigurationHandler:^(UITextField * textField) {
385+
textField.placeholder = @"URL";
386+
textField.text = @"https://raw.githubusercontent.com/TakayukiHoshi1984/DeviceConnect-iOS/modify_https_server/dConnectSDK/dConnectSDKForIOS/DConnectSDK/Resources/dconnect-ios.crt";
387+
}];
388+
UIAlertAction * cancelAction =
389+
[UIAlertAction actionWithTitle:@"キャンセル"
390+
style:UIAlertActionStyleCancel
391+
handler:nil];
392+
[alertController addAction:cancelAction];
393+
394+
UIAlertAction * createAction =
395+
[UIAlertAction actionWithTitle:@"Safariを起動"
396+
style:UIAlertActionStyleDefault
397+
handler:^(UIAlertAction * action) {
398+
UITextField * textField = alertController.textFields[0];
399+
if (textField.text.length > 0) {
400+
dispatch_async(dispatch_get_main_queue(), ^{
401+
NSURL *url = [NSURL URLWithString:textField.text];
402+
if (@available(iOS 10, *)) {
403+
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
404+
} else {
405+
[[UIApplication sharedApplication] openURL:url];
406+
}
407+
});
408+
}
409+
}];
410+
411+
[alertController addAction:createAction];
412+
413+
[[self rootViewController] presentViewController:alertController animated:YES completion:nil];
414+
}
415+
354416

355417
/*!
356418
@brief 最前面のViewControllerを取得する。

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/classes/TopViewModel.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ - (void)initialSetup
159159
mgr.settings.useExternalIP = isExternalIp;
160160
BOOL isManagerName = [[NSUserDefaults standardUserDefaults] boolForKey:IS_AVAILABILITY];
161161
mgr.settings.useManagerName = isManagerName;
162+
BOOL isSSL = [[NSUserDefaults standardUserDefaults] boolForKey:IS_SSL];
163+
mgr.settings.useSSL = isSSL;
162164
}
163165

164166
- (void)saveSettings
@@ -170,6 +172,7 @@ - (void)saveSettings
170172
[def setBool:mgr.settings.useOriginEnable forKey:IS_ORIGIN_ENABLE];
171173
[def setBool:mgr.settings.useExternalIP forKey:IS_EXTERNAL_IP];
172174
[def setBool:mgr.settings.useManagerName forKey:IS_AVAILABILITY];
175+
[def setBool:mgr.settings.useSSL forKey:IS_SSL];
173176
[def synchronize];
174177
}
175178

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/classes/WebViewController.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99

1010
#import "WebViewController.h"
11+
#import <DConnectSDK/DConnectSDK.h>
1112
#import <objc/runtime.h>
1213

1314
static const char kAssocKey_Window;
@@ -115,6 +116,42 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat
115116
self.title = webView.title;
116117
}
117118

119+
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
120+
{
121+
NSURLRequest *request = navigationAction.request;
122+
NSURL *url = request.URL;
123+
NSURLComponents *components = [NSURLComponents componentsWithURL:request.URL resolvingAgainstBaseURL:NO];
124+
if ([url.scheme isEqualToString:@"file"]) {
125+
// SSL通信フラグの存在確認
126+
NSArray<NSURLQueryItem *> *items = components.queryItems;
127+
for (NSURLQueryItem *item in items) {
128+
// すでに設定されている場合はここで終了. (コールバックの無限ループ回避)
129+
if ([item.name isEqualToString:@"ssl"]) {
130+
decisionHandler(WKNavigationActionPolicyAllow);
131+
return;
132+
}
133+
}
134+
135+
// SSL通信フラグをHTMLアプリ側へ共有.
136+
BOOL useSSL = [DConnectManager sharedManager].settings.useSSL;
137+
NSURL *newURL = [[self components:components appendSSL:useSSL] URL];
138+
[webView loadRequest:[NSURLRequest requestWithURL:newURL]];
139+
decisionHandler(WKNavigationActionPolicyCancel);
140+
} else {
141+
decisionHandler(WKNavigationActionPolicyAllow);
142+
}
143+
}
144+
145+
- (NSURLComponents *)components:(NSURLComponents *)components appendSSL:(BOOL)useSSL
146+
{
147+
// URLにsslパラメータを追加
148+
NSMutableArray<NSURLQueryItem *> *items = [NSMutableArray arrayWithArray:components.queryItems];
149+
NSURLQueryItem *ssl = [NSURLQueryItem queryItemWithName:@"ssl" value:(useSSL ? @"on" : @"off")];
150+
[items addObject:ssl];
151+
components.queryItems = items;
152+
return components;
153+
}
154+
118155
//--------------------------------------------------------------//
119156
#pragma mark - view cycle
120157
//--------------------------------------------------------------//

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/demo/js/checker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ var main = (function(parent, global) {
121121
hideEventText(nav);
122122

123123
if (xType == 'event') {
124-
var uri = "http://localhost:4035" + path.toLowerCase() + "?" + createBody(nav).join('&');
124+
var uri = util.getUri(path.toLowerCase()) + "?" + createBody(nav).join('&');
125125

126126
setRequestText(nav, createRequest(method + " " + path));
127127

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/demo/js/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ var main = (function(parent, global) {
6565

6666
function createProfile(profile) {
6767
var url = 'checker.html?serviceId=' + util.getServiceId() + '&profile=' + profile;
68+
url += '&ssl=' + (util.isSSL() ? 'on' : 'off');
69+
url += '&port=' + util.getPort();
6870
var icon = mIconList[profile.toLowerCase()];
6971
if (!icon) {
7072
icon = 'images/icon21_other.png';

dConnectSDK/dConnectBrowserForIOS9/dConnectBrowserForIOS9/demo/js/util.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,24 @@ var util = (function(parent, global) {
4949
var mSessionKey = "test-session-key";
5050
var mHost = "localhost";
5151
var mScopes = initScope();
52+
var mSSLEnabled = getQuery("ssl") === "on";
53+
var mPort = getQuery("port") == null ? 4035 : parseInt(getQuery("port"), 10);
5254

5355
function init(callback) {
5456
loadScope();
5557

5658
dConnect.setHost(mHost);
59+
dConnect.setPort(mPort);
60+
dConnect.setSSLEnabled(mSSLEnabled);
5761
dConnect.setExtendedOrigin("file://");
5862
checkDeviceConnect(callback);
5963
}
6064
parent.init = init;
6165

66+
function isSSL() {
67+
return mSSLEnabled;
68+
}
69+
parent.isSSL = isSSL;
6270

6371
function loadScope() {
6472
var scopeStr = getCookie("scope");
@@ -380,12 +388,20 @@ var util = (function(parent, global) {
380388
}
381389
parent.removeEventListener = removeEventListener;
382390

391+
function getProtocol() {
392+
return isSSL() ? 'https' : 'http';
393+
}
394+
parent.getProtocol = getProtocol;
383395

384396
function getUri(path) {
385-
return 'http://' + mHost + ':4035' + path;
397+
return getProtocol() + '://' + mHost + ':' + mPort + path;
386398
}
387399
parent.getUri = getUri;
388400

401+
function getPort() {
402+
return mPort;
403+
}
404+
parent.getPort = getPort;
389405

390406
function getProfile() {
391407
return getQuery('profile');

0 commit comments

Comments
 (0)