source: launchers/macosx/SBridge.mm @ 22a0f39

Last change on this file since 22a0f39 was 22a0f39, checked in by meeh <meeh@…>, 23 months ago

Mac OS X Launcher:

  • Enabled Apple's "Hardened Runtime", however unsecure memory had to be allowed to spawn java etc.
  • Updated docs about Event Manager code
  • Make the launcher handle cases where extract is incomplete or invalid
  • Bugfixes as always
  • Property mode set to 100644
File size: 4.6 KB
Line 
1//
2//  SBridge.m
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 18/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9#import "SBridge.h"
10
11#ifdef __cplusplus
12#include <functional>
13#include <memory>
14#include <glob.h>
15#include <string>
16#include <list>
17#include <stdlib.h>
18#include <future>
19#include <vector>
20
21#import <AppKit/AppKit.h>
22#import "I2PLauncher-Swift.h"
23
24#include "AppDelegate.h"
25#include "include/fn.h"
26
27
28
29std::future<int> startupRouter(NSString* javaBin, NSArray<NSString*>* arguments, NSString* i2pBaseDir, RouterProcessStatus* routerStatus) {
30  @try {
31    RTaskOptions* options = [RTaskOptions alloc];
32    options.binPath = javaBin;
33    options.arguments = arguments;
34    options.i2pBaseDir = i2pBaseDir;
35    auto instance = [[I2PRouterTask alloc] initWithOptions: options];
36   
37    [[SBridge sharedInstance] setCurrentRouterInstance:instance];
38    [instance execute];
39    sendUserNotification(APP_IDSTR, @"The I2P router is starting up.");
40    auto pid = [instance getPID];
41    NSLog(@"Got pid: %d", pid);
42    if (routerStatus != nil) {
43      [routerStatus setRouterStatus: true];
44      [routerStatus setRouterRanByUs: true];
45      [routerStatus triggerEventWithEn:@"router_start" details:@"normal start"];
46      [routerStatus triggerEventWithEn:@"router_pid" details:[NSString stringWithFormat:@"%d", pid]];
47    }
48   
49    return std::async(std::launch::async, [&pid]{
50      return pid;
51    });
52  }
53  @catch (NSException *e)
54  {
55    auto errStr = [NSString stringWithFormat:@"Expection occurred %@",[e reason]];
56    NSLog(@"%@", errStr);
57    sendUserNotification(APP_IDSTR, errStr);
58    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
59   
60    if (routerStatus != nil) {
61      [routerStatus triggerEventWithEn:@"router_exception" details:errStr];
62    }
63   
64    return std::async(std::launch::async, [&]{
65      return 0;
66    });
67  }
68}
69
70
71
72@implementation SBridge
73
74// this makes it a singleton
75+ (instancetype)sharedInstance {
76  static SBridge *sharedInstance = nil;
77  static dispatch_once_t onceToken;
78 
79  dispatch_once(&onceToken, ^{
80    sharedInstance = [[SBridge alloc] init];
81  });
82  return sharedInstance;
83}
84
85- (void) openUrl:(NSString*)url
86{
87  osx::openUrl(url);
88}
89
90- (NSString*) buildClassPath:(NSString*)i2pPath
91{
92  const char * basePath = [i2pPath UTF8String];
93  auto jarList = buildClassPathForObjC(basePath);
94  const char * classpath = jarList.c_str();
95  NSLog(@"Classpath from ObjC = %s", classpath);
96  return [[NSString alloc] initWithUTF8String:classpath];
97}
98
99
100
101- (void)startupI2PRouter:(NSString*)i2pRootPath javaBinPath:(NSString*)javaBinPath
102{
103  std::string basePath([i2pRootPath UTF8String]);
104 
105  auto classPathStr = buildClassPathForObjC(basePath);
106 
107  RouterProcessStatus* routerStatus = [[RouterProcessStatus alloc] init];
108  try {
109    std::vector<NSString*> argList = {
110      @"-Xmx512M",
111      @"-Xms128m",
112      @"-Djava.awt.headless=true",
113      @"-Dwrapper.logfile=/tmp/router.log",
114      @"-Dwrapper.logfile.loglevel=DEBUG",
115      @"-Dwrapper.java.pidfile=/tmp/routerjvm.pid",
116      @"-Dwrapper.console.loglevel=DEBUG"
117    };
118   
119    std::string baseDirArg("-Di2p.dir.base=");
120    baseDirArg += basePath;
121    std::string javaLibArg("-Djava.library.path=");
122    javaLibArg += basePath;
123    // TODO: pass this to JVM
124    //auto java_opts = getenv("JAVA_OPTS");
125   
126    std::string cpString = std::string("-cp");
127   
128    argList.push_back([NSString stringWithUTF8String:baseDirArg.c_str()]);
129    argList.push_back([NSString stringWithUTF8String:javaLibArg.c_str()]);
130    argList.push_back([NSString stringWithUTF8String:cpString.c_str()]);
131    argList.push_back([NSString stringWithUTF8String:classPathStr.c_str()]);
132    argList.push_back(@"net.i2p.router.Router");
133    auto javaBin = std::string([javaBinPath UTF8String]);
134   
135   
136    sendUserNotification(APP_IDSTR, @"I2P Router is starting up!");
137    auto nsJavaBin = javaBinPath;
138    auto nsBasePath = i2pRootPath;
139    NSArray* arrArguments = [NSArray arrayWithObjects:&argList[0] count:argList.size()];
140   
141    NSLog(@"Trying to run command: %@", javaBinPath);
142    NSLog(@"With I2P Base dir: %@", i2pRootPath);
143    NSLog(@"And Arguments: %@", arrArguments);
144    startupRouter(nsJavaBin, arrArguments, nsBasePath, routerStatus);
145  } catch (std::exception &err) {
146    auto errMsg = [NSString stringWithUTF8String:err.what()];
147    NSLog(@"Exception: %@", errMsg);
148    sendUserNotification(APP_IDSTR, [NSString stringWithFormat:@"Error: %@", errMsg]);
149    [routerStatus setRouterStatus: false];
150    [routerStatus setRouterRanByUs: false];
151    [routerStatus triggerEventWithEn:@"router_exception" details:[NSString stringWithFormat:@"Error: %@", errMsg]];
152  }
153}
154@end
155
156
157#endif
Note: See TracBrowser for help on using the repository browser.