2.html 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="generator" content="Hugo 0.92.0" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="/assets/css/theme.css">
  8. <link rel="alternate" href="/rss.xml" type="application/rss+xml" title="Pleasant Programmer">
  9. <script type="text/javascript" src="//use.typekit.net/iwm5axp.js"></script>
  10. <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
  11. <title>Pleasant Programmer</title>
  12. </head>
  13. <body>
  14. <header id="header" role="banner">
  15. <div id="thomas">
  16. <img src="/assets/img/thomas.gif" alt="DJ THOMAS IN DA HAUS">
  17. <img src="/assets/img/thomas.png" alt="Pleasant Programmer">
  18. </div>
  19. <h1 class="site-title"><a href="/">Pleasant Programmer</a></h1>
  20. <nav id="menu" role="navigation">
  21. <ul>
  22. <li><a href="/pages/projects.html">projects</a></li>
  23. <li><a href="/posts.html">archives</a></li>
  24. <li><a href="/tags.html">tags</a></li>
  25. <li><a href="/rss.xml">rss</a></li>
  26. </ul>
  27. </nav>
  28. </header>
  29. <div id="container">
  30. <main id="content" role="main">
  31. <div class="postindex">
  32. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  33. <header>
  34. <h1 class="p-name entry-title" itemprop="headline">
  35. <a href="/posts/openpreppad.html" class="u-url">OpenPrepPad</a>
  36. </h1>
  37. </header>
  38. <div class="e-content entry-content">
  39. <p>Smart electronics and IoT (Internet of Things) are all the rage these days. You
  40. have a lot of companies sprout up trying to make the next big thing, which also
  41. leads to a lot of failures big and small. Pebble, the maker of my smartwatch,
  42. got bought out by Fitbit recently. This left watch owners without any official
  43. support, but thankfully, community members <a href="http://rebble.io/">stepped up</a> to continue
  44. maintaining it.</p>
  45. <p>Another casualty of the IoT boom was the <a href="http://theorangechef.com/">Orange Chef</a> <a href="https://www.amazon.com/Orange-Chef-Smart-Scale-Silver/dp/B00KFW8L90">Prep Pad</a>. It&rsquo;s a
  46. bluetooth connected weighing scale to make it easy to track your calories and
  47. carb/fat/protein intake. My dad bought it last year only to find out that the
  48. app was incredibly buggy. The search function doesn&rsquo;t work which makes the whole
  49. thing practically useless. I also found out later that you can&rsquo;t even download
  50. the app to use the scale anymore.</p>
  51. <p><strong>Note</strong> I just found out as I was writing this post that it <em>may</em> get supported
  52. by <a href="http://www.prnewswire.com/news-releases/perfect-company-acquires-orange-chefs-prep-pad-related-ip-continues-momentum-in-the-connected-kitchen-300383178.html">another company</a>.</p>
  53. <p>So the app is useless, but at least you can use it as a scale, right?</p>
  54. <p><img src="/galleries/openpreppad/preppad.jpg" alt="Prep Pad"></p>
  55. <p>Nope. The device has no display whatsoever. The only controls on it are the
  56. on/off button and a green LED that isn&rsquo;t even that useful at telling you whether
  57. it&rsquo;s on or not. At this point, it&rsquo;s just a giant paperweight.</p>
  58. <h2 id="reverse-engineering">Reverse Engineering</h2>
  59. <p>Since I essentially had nothing to lose, I tried poking at the thing to figure
  60. out how it works. I didn&rsquo;t really have experience with bluetooth besides trying
  61. to get my bluetooth mouse connected on Linux. The main thing I used then was
  62. <code>bluetoothctl</code> which is essentially a CLI for managing bluetooth devices so I
  63. started there.</p>
  64. <p>I started up <code>bluetoothctl</code> and turned on the Prep Pad. And it showed up!</p>
  65. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">[bluetooth]# power on
  66. [CHG] Controller ... Class: 0x00010c
  67. Changing power on succeeded
  68. [CHG] Controller ... Powered: yes
  69. [bluetooth]# scan on
  70. Discovery started
  71. [CHG] Device 1C:BA:8C:21:7C:BB RSSI: -51
  72. [CHG] Device 1C:BA:8C:21:7C:BB Name: CHSLEEV_00
  73. [CHG] Device 1C:BA:8C:21:7C:BB Alias: CHSLEEV_00
  74. </code></pre></div><p>I then connected to it, which was surprisingly easy.</p>
  75. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">[bluetooth]# connect 1C:BA:8C:21:7C:BB
  76. Attempting to connect to 1C:BA:8C:21:7C:BB
  77. [CHG] Device 1C:BA:8C:21:7C:BB Connected: yes
  78. [CHG] Device 1C:BA:8C:21:7C:BB Name: CH BTScale_00
  79. [CHG] Device 1C:BA:8C:21:7C:BB Alias: CH BTScale_00
  80. </code></pre></div><p>Now normally, when you turn the device on, the green light flashes occasionally.
  81. Once I connected to it, the green light stayed on permanently. Clearly, I was
  82. making progress. A lot of services were also discovered but I had no idea what
  83. those things were at that point.</p>
  84. <p>After a lot of poking around, I could check the general device information. You
  85. could get the hardware, software and firmware version. There&rsquo;s also the device
  86. serial number which was nowhere on the actual physical device.</p>
  87. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">[CHSLEEV_00]# select-attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017
  88. [CH BTScale_00:/service0010/char0017]# attribute-info
  89. Characteristic - Firmware Revision String
  90. UUID: 00002a26-0000-1000-8000-00805f9b34fb
  91. Service: /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010
  92. Value: 0x31
  93. Value: 0x2e
  94. Value: 0x31
  95. Value: 0x33
  96. Value: 0x41
  97. Value: 0x00
  98. Flags: read
  99. [CH BTScale_00:/service0010/char0017]# read
  100. Attempting to read /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017
  101. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017 Value: 0x31
  102. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017 Value: 0x2e
  103. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017 Value: 0x31
  104. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017 Value: 0x33
  105. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017 Value: 0x41
  106. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0010/char0017 Value: 0x00
  107. 31 2e 31 33 41 00 1.13A.
  108. [CH BTScale_00:/service0010/char0017]#
  109. </code></pre></div><p>There was also a service which contained Accel Enable, Accel Range, Accel
  110. X-Coordinate, Accel Y-Coordinate, and Accel Z-Coordinate. I guess it stands for
  111. accelerometer, which is probably what it uses to weigh things.</p>
  112. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">[CHSLEEV_00]# select-attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026
  113. [CH BTScale_00:/service0023/char0024/desc0026]# read
  114. Attempting to read /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026
  115. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x41
  116. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x63
  117. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x63
  118. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x65
  119. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x6c
  120. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x20
  121. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x45
  122. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x6e
  123. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x61
  124. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x62
  125. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x6c
  126. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024/desc0026 Value: 0x65
  127. 41 63 63 65 6c 20 45 6e 61 62 6c 65 Accel Enable
  128. </code></pre></div><p>I couldn&rsquo;t read from any of the Accel Coordinates. It kept saying permission
  129. denied. I could however, notify on them. But that didn&rsquo;t yield anything as well.
  130. What I <em>could</em> read was Accel Enable, which was set to 00. I guess that means it
  131. was off. After writing 01 to Accel Enable, I found I could get values out of
  132. Accel X-Coordinate! Also, the green LED which was permanently on turned off.</p>
  133. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">[CHSLEEV_00]# select-attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024
  134. [CH BTScale_00:/service0023/char0024]# write 01
  135. Attempting to write /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char0024
  136. [CH BTScale_00:/service0023/char0024]# select-attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a
  137. [CH BTScale_00:/service0023/char002a]# notify on
  138. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Notifying: yes
  139. Notify started
  140. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x5b
  141. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0xa3
  142. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x02
  143. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x00
  144. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x55
  145. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0xa3
  146. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x02
  147. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x00
  148. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x59
  149. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0xa3
  150. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x02
  151. [CHG] Attribute /org/bluez/hci0/dev_1C_BA_8C_21_7C_BB/service0023/char002a Value: 0x00
  152. </code></pre></div><p>I tried pressing the scale down a few times, and the values changed accordingly.
  153. Now, I just had to figure out how to convert the values into grams. It looked
  154. like the values were 32-bit integers sent as 4 bytes. In the above example it
  155. would be <code>0x0002a35b</code>, <code>0x0002a355</code>, <code>0x0002a359</code> or 172891, 172855, 172899. The
  156. values also decrease as you exert more effort on the scale. So assuming you take
  157. the initial value as <em>tare</em>, you simply subtract any succeeding value from that
  158. <em>tare</em> and you get the &ldquo;weight&rdquo;.</p>
  159. <p>The values I got didn&rsquo;t seem to be in grams though. After weighing some things
  160. on an actual scale and comparing the values I got, I found I can just divide the
  161. values by 14 and get something in grams. That 14 is entirely a magic number
  162. though and I have no idea whether other Prep Pad&rsquo;s would have the same constant.</p>
  163. <h2 id="openpreppad">OpenPrepPad</h2>
  164. <p>With all that figured out, I went ahead and made a <a href="https://github.com/thatsmydoing/openpreppad">simple CLI application</a>
  165. to interface with the Prep Pad. Ironically, node was the simplest thing I found
  166. that had <a href="https://github.com/sandeepmistry/noble">nice bluetooth library support</a> so that&rsquo;s what I wrote it in. I
  167. also added most of the technical details in the README for that as well.</p>
  168. <p>While this is all well and cool, I doubt the intersection of Linux users and
  169. people who <!-- raw HTML omitted -->got ripped off<!-- raw HTML omitted --> bought the Prep Pad is anyone besides me. In
  170. light of that, I&rsquo;m in the process of making a React Native version of the app,
  171. but that&rsquo;s still a work in progress. Who knows, if the new owners of Prep Pad
  172. are good, I might not even need to finish it.</p>
  173. </div>
  174. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2017-01-15">2017-01-15</time></small>
  175. | <small class="commentline"><a href="/posts/openpreppad.html#isso-thread">Comments</a></small>
  176. </article>
  177. </article>
  178. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  179. <header>
  180. <h1 class="p-name entry-title" itemprop="headline">
  181. <a href="/posts/haproxy-charset.html" class="u-url">Haproxy Charset</a>
  182. </h1>
  183. </header>
  184. <div class="e-content entry-content">
  185. <p>A common problem we encounter is for things like <em>ñ</em> not showing up correctly. This actually caused <a href="http://www.rappler.com/nation/politics/elections/2016/132894-human-error-hash-election-results-code-mismatch">some issues</a> in the recent Philippine elections, but this isn&rsquo;t about hash codes or anything like that.</p>
  186. <p>By default, we use UTF-8 for text storage and rendering. A problem is that browsers don&rsquo;t assume UTF-8 as the default and you need to have either a <code>&lt;meta charset=&quot;utf-8&quot; /&gt;</code> in the HTML or <code>Content-Type: text/html; charset=utf-8</code> in the headers. A few of our services don&rsquo;t set the <code>Content-Type</code> with the <code>charset=utf-8</code> part so you&rsquo;d get piñata instead of piñata.</p>
  187. <p>Being lazy, we usually just correct this at the reverse proxy side. It&rsquo;s trivial to do in nginx. You just need to add <code>charset utf-8;</code> to your configuration and you&rsquo;re good. For haproxy though, I couldn&rsquo;t readily find a solution for it and had to go through the docs to see what I could do.</p>
  188. <p>After a bit of experimenting, I had success with this:</p>
  189. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text"># set content-type to utf-8 if not already
  190. acl has_charset hdr_sub(content-type) -i charset=
  191. rspirep (Content-Type.*) \1;\ charset=utf-8 unless has_charset
  192. </code></pre></div><p>This is probably not the best way to do it. Arguably, we should just fix our services to have the correct <code>Content-Type</code> in the first place, but I can do that some other time.</p>
  193. </div>
  194. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2016-06-24">2016-06-24</time></small>
  195. | <small class="commentline"><a href="/posts/haproxy-charset.html#isso-thread">Comments</a></small>
  196. </article>
  197. </article>
  198. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  199. <header>
  200. <h1 class="p-name entry-title" itemprop="headline">
  201. <a href="/posts/cloudflare-shenanigans.html" class="u-url">Cloudflare Shenanigans</a>
  202. </h1>
  203. </header>
  204. <div class="e-content entry-content">
  205. <p>An old client of ours managed to convince a telco to zero-rate the data for their app. In order to whitelist it though, we needed to use plain HTTP for domain whitelisting. For HTTPS, they can only whitelist by IP address. Like any good developer, we were using HTTPS. Also, like any good developer, we put our server behind Cloudflare.</p>
  206. <p>Now the problem is that Cloudflare can put you behind <a href="https://www.cloudflare.com/ips/">any IP they own</a>, which is a huge range. There&rsquo;s no guarantee that the IP we have now is going to be the same later on. So we did the reasonable thing and asked them to whitelist all of the Cloudflare IPs. And the telco agreed! We were in total disbelief when that happened. But hey, if life gives you free internet, you take it.</p>
  207. <p>We never actually empirically tested whether other sites hosted on Cloudflare were also actually zero-rated. But I like to think that we saved a lot of people on their data costs from browsing Reddit and 4chan. But alas, good things must come to an end.</p>
  208. <p>A few months after we started beta testing the app, Cloudflare added more IPs to their range. Unfortunately, our server got moved to those new IPs which were not whitelisted yet. Apparently, the telco whitelisting process was incredibly convoluted and time consuming. Our client didn&rsquo;t want to bother asking them to whitelist more IPs. We also tried asking Cloudflare to move us back to the original IP range, but they could only do that if we were in their enterprise tier. We couldn&rsquo;t really afford that, so we looked for other options.</p>
  209. <p>Since Cloudflare was essentially just a giant reverse proxy, theoretically there should be no distinction between one IP address from another. The specific IP we get is probably just for load balancing. So we tried accessing the IPs in the range directly and just setting the Host header and it worked! But we get SSL errors because the IP itself doesn&rsquo;t have its own certificate.</p>
  210. <p>After more testing, we figured out that you could actually use any Cloudflare backed domain so long as we properly set the Host header. We just needed to find one still in the old range. Coincidentally, 4chan.org was. Which led to this wonderful commit</p>
  211. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-diff" data-lang="diff">commit 123456789abcdef
  212. Author: ~~~~~~
  213. Date: ~~~~~~
  214. 4chan hack
  215. <span style="font-weight:bold">diff --git a/src/com/client/common/Util.java b/src/com/client/common/Util.java
  216. </span><span style="font-weight:bold"></span>--- a/src/com/client/common/Util.java
  217. +++ b/src/com/client/common/Util.java
  218. <span style="font-weight:bold">@@ -210,7 +210,8 @@ public class Util {
  219. </span><span style="font-weight:bold"></span> }
  220. public static String getServerAddress(Context context) {
  221. - String address = &#34;https://backend.client.com&#34;;
  222. + // String address = &#34;https://backend.client.com&#34;;
  223. + String address = &#34;https://4chan.org&#34;;
  224. if(!isDebug(context)) return address;
  225. try {
  226. <span style="font-weight:bold">diff --git a/src/com/client/common/logging/APIClient.java b/src/com/client/common/logging/APIClient.java
  227. </span><span style="font-weight:bold"></span>--- a/src/com/client/common/logging/APIClient.java
  228. +++ b/src/com/client/common/logging/APIClient.java
  229. <span style="font-weight:bold">@@ -101,6 +101,7 @@ public class APIClient {
  230. </span><span style="font-weight:bold"></span> private HttpResponse postInternal(String url, List&lt;NameValuePair&gt; data, boolean forRegistration) throws ClientProtocolException, IOException {
  231. HttpPost request = new HttpPost(Util.getServerAddress(mContext)+&#34;/api/&#34;+url);
  232. request.setHeader(&#34;X-API-VERSION&#34;, apiVersion);
  233. + request.setHeader(&#34;Host&#34;, &#34;backend.client.com&#34;);
  234. if(data == null) {
  235. data = new ArrayList&lt;NameValuePair&gt;();
  236. </code></pre></div><p>Eventually, we did decide to just abandon Cloudflare for the server. We probably weren&rsquo;t going to be the target of a DDOS or anything. This also allowed us to do more secure things like pinning the server certificate in the application itself. Clearly, this is what we should have just done in the first place, but at the time we just wanted a stopgap solution.</p>
  237. <p>I just still find it funny we were making people&rsquo;s phones go to 4chan.org everyday for more than a year.</p>
  238. </div>
  239. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2015-12-25">2015-12-25</time></small>
  240. | <small class="commentline"><a href="/posts/cloudflare-shenanigans.html#isso-thread">Comments</a></small>
  241. </article>
  242. </article>
  243. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  244. <header>
  245. <h1 class="p-name entry-title" itemprop="headline">
  246. <a href="/posts/tiddlywiki-in-the-sky-or-tiddlyweb-for-tw5.html" class="u-url">TiddlyWiki in the Sky (or TiddlyWeb for TW5)</a>
  247. </h1>
  248. </header>
  249. <div class="e-content entry-content">
  250. <p>I&rsquo;ve always liked <a href="http://tiddlywiki.com">TiddlyWiki</a>. Back when it first came out, it was really amazing. A wiki all in one file, that worked in the browser. It didn&rsquo;t need a backend, it would just save itself as an all new HTML file with all your posts inside. I&rsquo;ve used it a lot over the years, as a personal wiki/journal and a class notebook. I even had a blog with it at one point using one of the server-side forks.</p>
  251. <p>Now, there&rsquo;s TiddlyWiki5 which is a rewrite of the original TiddlyWiki that looks a whole lot snazzier, and I assume has better architecture overall. It also has experimental support for all the server-side platforms (particularly TiddlyWeb) that have cropped up.</p>
  252. <p>If you&rsquo;re just looking for a simple server setup for TiddlyWiki5, it has native support for that on its own. There&rsquo;s plenty of documentation on the site. But if you&rsquo;re looking for more advanced features (like storing your posts in git or a database), then you&rsquo;ll need to use it with TiddlyWeb. The problem is that most of the documentation for TiddlyWeb still refers to the old TiddlyWiki.</p>
  253. <p>To support TiddlyWiki5, we&rsquo;ll need a version of the wiki which has the TiddlyWeb plugin already installed and configured. After that, some tweaking is necessary to get TiddlyWeb to provide what the wiki requires.</p>
  254. <h2 id="setting-up-tiddlywiki">Setting Up TiddlyWiki</h2>
  255. <p>TiddlyWiki5 provides a command line tool via <code>npm</code> that allows building custom versions of the wiki. In fact, it comes with templates, called &ldquo;editions&rdquo;, that we can use for our setup. Assuming you already have it installed, create the wiki using</p>
  256. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">tiddlywiki mywiki --init tw5tank <span style="color:#007f7f"># create wiki from template</span>
  257. </code></pre></div><p>This creates a wiki intended for use with <a href="https://tank.peermore.com/">Tank</a>, which is built on top of TiddlyWeb. From here, you should look in <code>mywiki/tiddlers/system</code> which contain the entries for <code>SiteTitle</code>, <code>SiteSubtitle</code>, <code>DefaultTiddlers</code>, and <code>tiddlyweb-host</code>. The first 3 should be configured however you want. These are necessary because they&rsquo;re needed before the wiki can load them from the server. <code>tiddlyweb-host</code> contains the location of the TiddlyWeb server, this should be <code>http://localhost:8080/</code> if you&rsquo;re just testing locally. With everything configured, you can build the new wiki by running</p>
  258. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">tiddlywiki mywiki --build
  259. </code></pre></div><p>This will output the wiki to <code>mywiki/output/tw5tank.html</code>. You can now serve it using your favorite local webserver, like <code>python -m http.server</code>.</p>
  260. <h2 id="setting-up-tiddlyweb">Setting Up TiddlyWeb</h2>
  261. <p>The TiddlyWeb tutorial recommends using <code>tiddlywebwiki</code> which has all the plugins setup for a nice wiki instance for the old TiddlyWiki. It has a lot of features that aren&rsquo;t really needed, so we won&rsquo;t go with that. So first, we&rsquo;ll need to install TiddlyWeb and any plugins we might want to use.</p>
  262. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">pip install tiddlyweb tiddlywebplugins.status tiddlywebplugins.cherrypy tiddlywebplugins.cors
  263. </code></pre></div><p>Next, we&rsquo;ll need the tiddlyweb configuration in <code>tiddlywebconfig.py</code></p>
  264. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#007f7f"># A basic configuration.</span>
  265. <span style="color:#007f7f"># `pydoc tiddlyweb.config` for details on configuration items.</span>
  266. <span style="color:#fff;font-weight:bold">import</span> tiddlywebplugins.status
  267. config = {
  268. <span style="color:#0ff;font-weight:bold">&#39;system_plugins&#39;</span>: [<span style="color:#0ff;font-weight:bold">&#39;tiddlywebplugins.status&#39;</span>, <span style="color:#0ff;font-weight:bold">&#39;tiddlywebplugins.cors&#39;</span>],
  269. <span style="color:#0ff;font-weight:bold">&#39;secret&#39;</span>: <span style="color:#0ff;font-weight:bold">&#39;36c98d6d14618c79f0ed2d49cd1b9e272d8d4bd0&#39;</span>,
  270. <span style="color:#0ff;font-weight:bold">&#39;wsgi_server&#39;</span>: <span style="color:#0ff;font-weight:bold">&#39;tiddlywebplugins.cherrypy&#39;</span>,
  271. <span style="color:#0ff;font-weight:bold">&#39;cors.enable_non_simple&#39;</span>: <span style="color:#fff;font-weight:bold">True</span>
  272. }
  273. original_gather_data = tiddlywebplugins.status._gather_data
  274. <span style="color:#fff;font-weight:bold">def</span> _status_gather_data(environ):
  275. data = original_gather_data(environ)
  276. data[<span style="color:#0ff;font-weight:bold">&#39;space&#39;</span>] = {<span style="color:#0ff;font-weight:bold">&#39;recipe&#39;</span>: <span style="color:#0ff;font-weight:bold">&#39;default&#39;</span>}
  277. <span style="color:#fff;font-weight:bold">return</span> data
  278. tiddlywebplugins.status._gather_data = _status_gather_data
  279. </code></pre></div><p>The tweaks involved are:</p>
  280. <ul>
  281. <li>using the status plugin which the wiki requires</li>
  282. <li>monkeypatching the status plugin for the wiki to use the correct &ldquo;recipe&rdquo;</li>
  283. <li>using cherrypy server instead of the buggy default one</li>
  284. <li>using cors since we&rsquo;re not hosting the wiki itself on the same server</li>
  285. </ul>
  286. <p>With that, we just need to create the store that will hold our data</p>
  287. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">twanager recipe default <span style="color:#0ff;font-weight:bold">&lt;&lt;EOF
  288. </span><span style="color:#0ff;font-weight:bold">desc: standard TiddlyWebWiki environment
  289. </span><span style="color:#0ff;font-weight:bold">policy: {&#34;read&#34;: [], &#34;create&#34;: [], &#34;manage&#34;: [&#34;R:ADMIN&#34;], &#34;accept&#34;: [], &#34;write&#34;: [&#34;R:ADMIN&#34;], &#34;owner&#34;: &#34;administrator&#34;, &#34;delete&#34;: [&#34;R:ADMIN&#34;]}
  290. </span><span style="color:#0ff;font-weight:bold">
  291. </span><span style="color:#0ff;font-weight:bold">/bags/default/tiddlers
  292. </span><span style="color:#0ff;font-weight:bold">EOF</span>
  293. twanager bag default <span style="color:#0ff;font-weight:bold">&lt;&lt;EOF
  294. </span><span style="color:#0ff;font-weight:bold">{&#34;policy&#34;: {&#34;read&#34;: [], &#34;create&#34;: [], &#34;manage&#34;: [&#34;R:ADMIN&#34;], &#34;accept&#34;: [], &#34;write&#34;: [], &#34;owner&#34;: &#34;administrator&#34;, &#34;delete&#34;: []}}
  295. </span><span style="color:#0ff;font-weight:bold">EOF</span>
  296. </code></pre></div><p>Finally, we can start the TiddlyWeb server</p>
  297. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">twanager server
  298. </code></pre></div><h2 id="putting-it-all-together">Putting it all together</h2>
  299. <p>Once you have the TiddlyWeb server running, you can just go to wherever you&rsquo;re hosting the wiki html and it should work. You can try creating some posts, and the check mark on the sidebar should be red for a while and then turn black. Once that&rsquo;s done it&rsquo;s saved. You can refresh your browser and your posts should still be there.</p>
  300. <p>At this point, you can start customizing your TiddlyWeb instance, by changing your store to something like a database, or adding authorization. You can also tweak the server setup so you won&rsquo;t need CORS anymore.</p>
  301. <p>TiddlyWiki5 is still relatively new. I hope that eventually, support for server-side and the plugin ecosystem grows to be as great as the old TiddlyWiki.</p>
  302. </div>
  303. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2015-12-24">2015-12-24</time></small>
  304. | <small class="commentline"><a href="/posts/tiddlywiki-in-the-sky-or-tiddlyweb-for-tw5.html#isso-thread">Comments</a></small>
  305. </article>
  306. </article>
  307. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  308. <header>
  309. <h1 class="p-name entry-title" itemprop="headline">
  310. <a href="/posts/is-my-terminal-window-active.html" class="u-url">Is My Terminal Window Active?</a>
  311. </h1>
  312. </header>
  313. <div class="e-content entry-content">
  314. <p>I&rsquo;ve been working in OSX for almost 3 years now, but I recently switched back to Linux because of all the problems people encountered with Yosemite. There are some things I missed from OSX though. One of which is <a href="https://github.com/marzocchi/zsh-notify">zsh-notify</a>. It&rsquo;s a zsh plugin that alerts you if your long-running task is complete, and whether it failed or not.</p>
  315. <p>It&rsquo;s pretty convenient when you&rsquo;re compiling something and then go on to browse reddit while waiting. Usually, I spend too much time just reading and forget about the compilation entirely. With the plugin, I get the notification and maybe go back to work.</p>
  316. <p>One nice feature it has is that if you&rsquo;re currently looking at the terminal window of the job that just finished, it won&rsquo;t notify you. It only notifies on windows that aren&rsquo;t currently in focus. To do this, it has to actually talk to Terminal.app or iTerm2 to see if the window and tab are active.</p>
  317. <p>This is alright in OSX since those 2 are the generally most used terminal emulators. On Linux though, everyone has their own favorite terminal. Given that, I figured I could probably rely on talking to X to see if the window is active instead of each single terminal emulator. X can&rsquo;t tell if the tab is active though, but I don&rsquo;t use tabs in my current setup so it should still be good.</p>
  318. <h2 id="xdotool">xdotool</h2>
  319. <p><a href="http://superuser.com/questions/382616/detecting-currently-active-window">Preliminary research</a> reveals that we can easily get what the active window is with xdotool. <code>xdotool getactivewindow</code> gives us the X window id of the active one. Now all we need is a way to get the window id of the terminal we&rsquo;re in.</p>
  320. <h2 id="first-attempt-windowid">First Attempt: $WINDOWID</h2>
  321. <p>Apparently, xterm and similar terminal emulators define an environment variable called <code>$WINDOWID</code> with the window id of the terminal. Obviously, this is too good to be true. In xterm and konsole the <code>$WINDOWID</code> was correct, but in VTE-based terminal emulators, <code>$WINDOWID</code> had the wrong value. In terminology, it didn&rsquo;t define <code>$WINDOWID</code> altogether. So <code>$WINDOWID</code> wasn&rsquo;t going to work.</p>
  322. <h2 id="second-attempt-xdotool-search-magic">Second Attempt: xdotool search $MAGIC</h2>
  323. <p>My second idea was that you can use zsh to change the window title to a magic number and then just check if the active window is the same one as the window with the magic number. This sort of worked for most terminals, except konsole which does whatever it wants with the window title. There&rsquo;s also the problem of some zsh configs automatically settings the window title to the current command.</p>
  324. <p>In hindsight, I could probably have just done <code>xdotool search --name xdotool</code> since in most cases, when you run the search, zsh or konsole will set the window name to the current command. Maybe that&rsquo;s another option I can explore some day.</p>
  325. <h2 id="third-attempt-ppid">Third Attempt: $PPID</h2>
  326. <p>My third idea was another environment variable called <code>$PPID</code>, which is the process id of the parent of the shell. As it happens, the parent is the window containing the zsh instance. This is actually pretty consistent across most terminals. The only problem was if you launched zsh from another shell since your new zsh&rsquo;s parent will now be another zsh instance instead of an X window.</p>
  327. <p>At first glance, launching zsh within zsh doesn&rsquo;t seem like something most people would do, but this is what happens when you run screen or tmux. To work around this, we can actually just save the original <code>$PPID</code> in a different variable and use that instead.</p>
  328. <p>Now that we have the PID of the window from zsh, we can once again use xdotool to get the PID of the current active window with <code>xdotool getactivewindow getwindowpid</code>. We just simply compare that with our <code>$PPID</code> and we can tell if we&rsquo;re in an active window or not. Overall, this approach worked surprisingly well so that&rsquo;s the final solution I went with.</p>
  329. </div>
  330. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2015-06-07">2015-06-07</time></small>
  331. | <small class="commentline"><a href="/posts/is-my-terminal-window-active.html#isso-thread">Comments</a></small>
  332. </article>
  333. </article>
  334. </div>
  335. <nav class="postindexpager">
  336. <ul class="pager clearfix">
  337. <li class="previous">
  338. <a href="/">&larr; Newer posts</a>
  339. </li>
  340. <li class="next">
  341. <a href="/page/3.html">Older posts &rarr;</a>
  342. </li>
  343. </ul>
  344. </nav>
  345. </main>
  346. <footer id="footer" role="contentinfo">
  347. <p>
  348. <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/deed.en_US">
  349. <img alt="CC-BY-SA" style="border-width:0" src="https://licensebuttons.net/l/by-sa/3.0/80x15.png">
  350. </a> &copy; 2022 Thomas Dy - Powered by <a href="http://gohugo.io">Hugo</a></p>
  351. </footer>
  352. </div>
  353. <script src="/assets/js/konami.js"></script>
  354. <script>
  355. var easter_egg = new Konami();
  356. easter_egg.code = function() {
  357. var el = document.getElementById('thomas');
  358. if(el.className == "whoa") {
  359. el.className = "";
  360. }
  361. else {
  362. el.className = "whoa";
  363. }
  364. document.body.scrollTop = document.documentElement.scrollTop = 0;
  365. }
  366. easter_egg.load();
  367. </script>
  368. <script
  369. data-isso="https://isso.pleasantprogrammer.com/"
  370. src="https://isso.pleasantprogrammer.com/js/count.min.js">
  371. </script>
  372. </body>
  373. </html>