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 | #include "LoggerWorker.hpp" |
---|
24 | #include "Logger.h" |
---|
25 | #include "logger_c.h" |
---|
26 | |
---|
27 | #include "AppDelegate.h" |
---|
28 | #include "include/fn.h" |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | std::future<int> startupRouter(NSString* javaBin, NSArray<NSString*>* arguments, NSString* i2pBaseDir, RouterProcessStatus* routerStatus) { |
---|
33 | @try { |
---|
34 | IIProcessInfo* processInfoObj = [[IIProcessInfo alloc] init]; |
---|
35 | [processInfoObj obtainFreshProcessList]; |
---|
36 | auto anyRouterLookingProcs = [processInfoObj findProcessWithStringInNameOrArguments:@"i2p.jar"]; |
---|
37 | if (anyRouterLookingProcs) { |
---|
38 | auto errMessage = @"Seems i2p is already running - I've detected another process with i2p.jar in it's arguments."; |
---|
39 | NSLog(@"%@", errMessage); |
---|
40 | sendUserNotification(APP_IDSTR, errMessage); |
---|
41 | [routerStatus triggerEventWithEn:@"router_already_running" details:@"won't start - another router is running"]; |
---|
42 | return std::async(std::launch::async, []{ |
---|
43 | return -1; |
---|
44 | }); |
---|
45 | } else { |
---|
46 | RTaskOptions* options = [RTaskOptions alloc]; |
---|
47 | options.binPath = javaBin; |
---|
48 | options.arguments = arguments; |
---|
49 | options.i2pBaseDir = i2pBaseDir; |
---|
50 | auto instance = [[I2PRouterTask alloc] initWithOptions: options]; |
---|
51 | |
---|
52 | [[SBridge sharedInstance] setCurrentRouterInstance:instance]; |
---|
53 | [instance execute]; |
---|
54 | sendUserNotification(APP_IDSTR, @"The I2P router is starting up."); |
---|
55 | auto pid = [instance getPID]; |
---|
56 | NSLog(@"Got pid: %d", pid); |
---|
57 | if (routerStatus != nil) { |
---|
58 | // TODO: Merge events router_start and router_pid ? |
---|
59 | [routerStatus triggerEventWithEn:@"router_start" details:@"normal start"]; |
---|
60 | [routerStatus triggerEventWithEn:@"router_pid" details:[NSString stringWithFormat:@"%d", pid]]; |
---|
61 | } |
---|
62 | |
---|
63 | return std::async(std::launch::async, [&pid]{ |
---|
64 | return pid; |
---|
65 | }); |
---|
66 | } |
---|
67 | } |
---|
68 | @catch (NSException *e) |
---|
69 | { |
---|
70 | auto errStr = [NSString stringWithFormat:@"Expection occurred %@",[e reason]]; |
---|
71 | NSLog(@"%@", errStr); |
---|
72 | sendUserNotification(APP_IDSTR, errStr); |
---|
73 | [[SBridge sharedInstance] setCurrentRouterInstance:nil]; |
---|
74 | |
---|
75 | if (routerStatus != nil) { |
---|
76 | [routerStatus triggerEventWithEn:@"router_exception" details:errStr]; |
---|
77 | } |
---|
78 | |
---|
79 | return std::async(std::launch::async, [&]{ |
---|
80 | return 0; |
---|
81 | }); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | @implementation SBridge |
---|
88 | |
---|
89 | // this makes it a singleton |
---|
90 | + (instancetype)sharedInstance { |
---|
91 | static SBridge *sharedInstance = nil; |
---|
92 | static dispatch_once_t onceToken; |
---|
93 | |
---|
94 | dispatch_once(&onceToken, ^{ |
---|
95 | sharedInstance = [[SBridge alloc] init]; |
---|
96 | }); |
---|
97 | return sharedInstance; |
---|
98 | } |
---|
99 | |
---|
100 | - (void) openUrl:(NSString*)url |
---|
101 | { |
---|
102 | osx::openUrl(url); |
---|
103 | } |
---|
104 | |
---|
105 | - (NSString*) buildClassPath:(NSString*)i2pPath |
---|
106 | { |
---|
107 | const char * basePath = [i2pPath UTF8String]; |
---|
108 | auto jarList = buildClassPathForObjC(basePath); |
---|
109 | const char * classpath = jarList.c_str(); |
---|
110 | MLog(0, @"Classpath from ObjC = %s", classpath); |
---|
111 | return [[NSString alloc] initWithUTF8String:classpath]; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | - (void)startupI2PRouter:(NSString*)i2pRootPath javaBinPath:(NSString*)javaBinPath |
---|
117 | { |
---|
118 | std::string basePath([i2pRootPath UTF8String]); |
---|
119 | |
---|
120 | auto classPathStr = buildClassPathForObjC(basePath); |
---|
121 | |
---|
122 | RouterProcessStatus* routerStatus = [[RouterProcessStatus alloc] init]; |
---|
123 | |
---|
124 | NSString *confDir = [NSString stringWithFormat:@"%@/Application\\ Support/i2p", NSHomeDirectory()]; |
---|
125 | |
---|
126 | try { |
---|
127 | std::vector<NSString*> argList = { |
---|
128 | @"-Xmx512M", |
---|
129 | @"-Xms128m", |
---|
130 | @"-Djava.awt.headless=true", |
---|
131 | [NSString stringWithFormat:@"-Dwrapper.logfile=%@/router.log", [NSString stringWithUTF8String:getDefaultLogDir().c_str()]], |
---|
132 | @"-Dwrapper.logfile.loglevel=DEBUG", |
---|
133 | [NSString stringWithFormat:@"-Dwrapper.java.pidfile=%@/router.pid", confDir], |
---|
134 | @"-Dwrapper.console.loglevel=DEBUG" |
---|
135 | }; |
---|
136 | |
---|
137 | std::string baseDirArg("-Di2p.dir.base="); |
---|
138 | baseDirArg += basePath; |
---|
139 | std::string javaLibArg("-Djava.library.path="); |
---|
140 | javaLibArg += basePath; |
---|
141 | // TODO: pass this to JVM |
---|
142 | //auto java_opts = getenv("JAVA_OPTS"); |
---|
143 | |
---|
144 | std::string cpString = std::string("-cp"); |
---|
145 | |
---|
146 | argList.push_back([NSString stringWithUTF8String:baseDirArg.c_str()]); |
---|
147 | argList.push_back([NSString stringWithUTF8String:javaLibArg.c_str()]); |
---|
148 | argList.push_back([NSString stringWithUTF8String:cpString.c_str()]); |
---|
149 | argList.push_back([NSString stringWithUTF8String:classPathStr.c_str()]); |
---|
150 | argList.push_back(@"net.i2p.router.Router"); |
---|
151 | auto javaBin = std::string([javaBinPath UTF8String]); |
---|
152 | |
---|
153 | |
---|
154 | sendUserNotification(APP_IDSTR, @"I2P Router is starting up!"); |
---|
155 | auto nsJavaBin = javaBinPath; |
---|
156 | auto nsBasePath = i2pRootPath; |
---|
157 | NSArray* arrArguments = [NSArray arrayWithObjects:&argList[0] count:argList.size()]; |
---|
158 | |
---|
159 | NSLog(@"Trying to run command: %@", javaBinPath); |
---|
160 | NSLog(@"With I2P Base dir: %@", i2pRootPath); |
---|
161 | NSLog(@"And Arguments: %@", arrArguments); |
---|
162 | startupRouter(nsJavaBin, arrArguments, nsBasePath, routerStatus); |
---|
163 | } catch (std::exception &err) { |
---|
164 | auto errMsg = [NSString stringWithUTF8String:err.what()]; |
---|
165 | NSLog(@"Exception: %@", errMsg); |
---|
166 | sendUserNotification(APP_IDSTR, [NSString stringWithFormat:@"Error: %@", errMsg]); |
---|
167 | [routerStatus setRouterStatus: false]; |
---|
168 | [routerStatus setRouterRanByUs: false]; |
---|
169 | [routerStatus triggerEventWithEn:@"router_exception" details:[NSString stringWithFormat:@"Error: %@", errMsg]]; |
---|
170 | } |
---|
171 | } |
---|
172 | @end |
---|
173 | |
---|
174 | |
---|
175 | #endif |
---|