1 | package net.i2p |
---|
2 | |
---|
3 | import java.io.{File, InputStream} |
---|
4 | |
---|
5 | import net.i2p.router.Router |
---|
6 | import org.json4s._ |
---|
7 | import org.json4s.native.JsonMethods._ |
---|
8 | |
---|
9 | /** |
---|
10 | * |
---|
11 | * For java developers: |
---|
12 | * A scala object is like an instance of a class. |
---|
13 | * If you define a method inside an object, it's equals to |
---|
14 | * java's static methods. |
---|
15 | * |
---|
16 | * Also, in scala, the body of a class/object is executed as it's |
---|
17 | * constructor. |
---|
18 | * |
---|
19 | * Also noteworthy; |
---|
20 | * val is immutable |
---|
21 | * var is mutable |
---|
22 | * |
---|
23 | * |
---|
24 | * @author Meeh |
---|
25 | * @version 0.0.1 |
---|
26 | * @since 0.9.35 |
---|
27 | */ |
---|
28 | object RouterLauncherApp extends App { |
---|
29 | |
---|
30 | def toBytes(xs: Int*) = xs.map(_.toByte).toArray |
---|
31 | |
---|
32 | def getInt(bytes: Array[Byte]): Int = (bytes(3) << 24) & 0xff000000 | (bytes(2) << 16) & 0x00ff0000 | (bytes(1) << 8) & 0x0000ff00 | (bytes(0) << 0) & 0x000000ff |
---|
33 | |
---|
34 | /** |
---|
35 | * Encodes bytes in "binary form" as mentioned in Native Messaging in the WebExtension API. |
---|
36 | * @param length |
---|
37 | * @return |
---|
38 | */ |
---|
39 | def getBytes(length: Int): Array[Byte] = { |
---|
40 | val bytes = new Array[Byte](4) |
---|
41 | bytes(0) = (length & 0xFF).toByte |
---|
42 | bytes(1) = ((length >> 8) & 0xFF).toByte |
---|
43 | bytes(2) = ((length >> 16) & 0xFF).toByte |
---|
44 | bytes(3) = ((length >> 24) & 0xFF).toByte |
---|
45 | bytes |
---|
46 | } |
---|
47 | |
---|
48 | def readMessage(in: InputStream): String = { |
---|
49 | val arr = new Array[Byte](4) |
---|
50 | in.read(arr) |
---|
51 | val bytes = new Array[Byte](getInt(arr)) |
---|
52 | in.read(bytes) |
---|
53 | new String(bytes, "UTF-8") |
---|
54 | } |
---|
55 | |
---|
56 | def sendMessage(message: String): Unit = { |
---|
57 | System.out.write(getBytes(message.length)) |
---|
58 | System.out.write(message.getBytes("UTF-8")) |
---|
59 | } |
---|
60 | |
---|
61 | // Executed at launch |
---|
62 | val basePath = Option(System.getProperty("i2p.dir.base")).getOrElse(System.getenv("I2PBASE")) |
---|
63 | val configPath = Option(System.getProperty("i2p.dir.config")).getOrElse(System.getenv("I2PCONFIG")) |
---|
64 | |
---|
65 | println(s"basePath => ${basePath}\nconfigPath => ${configPath}") |
---|
66 | |
---|
67 | object ErrorUtils { |
---|
68 | def errorMessageInJson(message: String, solution: String) : JObject = JObject( |
---|
69 | List( |
---|
70 | ("error", |
---|
71 | JObject( |
---|
72 | ("message", JString(message)), |
---|
73 | ("solution", JString(solution)) |
---|
74 | ) |
---|
75 | ) |
---|
76 | ) |
---|
77 | ) |
---|
78 | |
---|
79 | def printError(message: String, solution: String): Unit = { |
---|
80 | println(compact(render( errorMessageInJson(message,solution) ))) |
---|
81 | } |
---|
82 | |
---|
83 | def printErrorAndExit(message: String, solution: String, exitCode: Int = 1): Unit = { |
---|
84 | printError(message, solution) |
---|
85 | System.exit(exitCode) |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | // Path related error checking |
---|
90 | if (basePath == null || basePath.isEmpty) ErrorUtils.printErrorAndExit("I2P Base path is missing", "set property i2p.dir.base or environment variable I2PBASE") |
---|
91 | if (configPath == null || configPath.isEmpty) ErrorUtils.printErrorAndExit("I2P Config path is missing", "set property i2p.dir.config or environment variable I2PCONFIG") |
---|
92 | if (!new File(basePath).exists()) ErrorUtils.printErrorAndExit("I2P Base path don't exist", "Reinstall the Browser Bundle") |
---|
93 | if (!new File(configPath).exists()) ErrorUtils.printErrorAndExit("I2P Config path don't exist", "Delete your config directory for the Browser Bundle") |
---|
94 | |
---|
95 | |
---|
96 | val deployer = new DeployProfile(configPath,basePath) |
---|
97 | deployer.verifyExistenceOfConfig() |
---|
98 | |
---|
99 | // Required/mocked properties |
---|
100 | System.setProperty("wrapper.version", "portable-1") |
---|
101 | System.setProperty("i2p.dir.portableMode", "true") |
---|
102 | System.setProperty("loggerFilenameOverride", "router-@.log") |
---|
103 | |
---|
104 | //ErrorUtils.printError(s"Starting up with arguments ${(args mkString ", ")}",":)") |
---|
105 | |
---|
106 | Router.main(args) |
---|
107 | } |
---|
108 | |
---|
109 | |
---|