RustConf 2021 - How I Used Rust to Become Extremely Offline by Luke Westby

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so i'm luke and i'm a relative newcomer to the russ community i've been using rest for about a year um i'm coming to you from the elm community where you know there's a lot of conceptual overlap between the type systems and what have you but there's also a lot of stuff that's really different and so i'm really enjoying learning and using rust i'm happy to be here and today i want to tell you about how i use rust to become extremely offline if you aren't familiar with the term it's a it's a reference to being extremely online which is a jokey expression for when you spend so much of your time and energy on the internet that it becomes a self-consciously important part of your life and so what i've been trying to do is sort of the the opposite of that where i'm only using the internet when i need to in more of like a utilitarian fashion so of course for work and for things like watching tv or finding guitar tabs or you know messaging family and friends that kind of thing and so the uh the logical next question here would be why would you want to do that you know especially with the pandemic more and more of life is moving onto the internet and why would you want to back away from that in that way and i will tell you so around 2017 i had reached a point with my internet usage habits where i was no longer just distracted by browsing i was spending too much time and investing too much emotionally in what i encountered online and it transitioned from just merely a distraction to a serious and potent source of anxiety and my internet habits were not just affecting me at this point they were also impacting the people that i love and my responsibility to them and so with very patient help from friends and family i confronted the situation and decided the best thing for me to do would be to to log off and leave it all behind to the extent that i possibly could so step one of this journey was the classic deleting social media accounts and uh i don't actually have a lot to say about that i'm glad that i did it and that i had the option to do it i don't really miss it but it it turned out to just be kind of a surface level fix it wasn't really the posting and the follower accounts that was getting to me it was a much more pernicious underlying habit um that you can pick up while you use social media and that is doom scrolling right it means scrolling through doom it's uh the thing where you kind of endlessly swipe through your timeline and kind of inundate your brain with you know the day's evidence of humanity's real capacity for cruelty and it feels awful to do but for some reason it's also really addictive and breaking this habit has taken a lot of time and a lot of energy and a lot of creativity and i'll tell you how i how i went about it so i had deleted my social media accounts but all of the timelines that i scrolled through are still public so i had to uh in order to to cut that off i had to take kind of a drastic step and i got a site blocker right the one i chose is called cold turkey and it's a very good piece of software i highly recommend it if you're interested in that kind of thing and uh the creator is really nice about answering my emails and how it works is it blocks urls by pattern using a browser extension and then it's got anti-circumvention features where if you try to disable it or uninstall it a separate process the text detects that and will close the browser to annoy you into not doing that anymore and so i used cold turkey to block social media timelines and then over the course of a couple of years one by one i went and i blocked all of the news outlets that were producing these doom-filled headlines in the first place and uh so now as of this year i built up a fairly comprehensive ban list in cold turkey that blocks all of the new sites that i've come across that i would really care to browse and it's good progress and i'm really proud of the the point that i was able to reach with that approach but it left one problem still on the table and that's the the search engine okay so i'm a programmer and i have to use a search engine for work i have to use a search engine to do anything useful and uh so i i'll come over here and uh you know i'll search for some info on the problem that i'm working on and look for some docs and i'll get some results but i'm always tempted to come back to the search bar and maybe search for some doom laden topic and uh search engines now they recognize when you've typed in something news relevant and they'll show you this like interstitial content within the results latest headlines or popular social media posts or related videos or what have you and uh this little list of headlines here even though the sites underneath them are blocked in in all likelihood um or are of no interest i still find that just seeing the headlines is actually enough to get into this loop of negative feedback where i'm typing in the news and i'm getting anxious at the headlines and then i go back up and i type in climate change and i get even more anxious and it goes on like that until the whole day is over and i'm sitting here just like shaking with rage and anxiety and i'm in a terrible mood and i got nothing done but cold turkey can't help here as of the current version because it only blocks urls and not content within a page that you still want to access all right so what do we do what do i do here uh well i'm a programmer and recently i'm a web programmer so i know how to build a browser extension that can take care of this and actually doesn't take much right so we've got a few lines of javascript here and we'll take the document and search for the distracting content on the page using a css selector and then for each element just hide it simple as that and we take that and we put it inside of a mutation observer which is a web api that invokes a callback whenever content on the page changes so that covers the initial page load and it also takes care of uh subsequent updates in a single page app kind of scenario and it totally works right we'll come over here and enable the extension and uh you know come back to the search page and uh you know we'll search for the news again and we'll see that the content is is no longer visible and that's good that's great but if it's that easy to go up there and search for a distracting topic it's just as easy to quickly go over to the corner and remove the extension if the temptation is strong enough right so it doesn't really work if you can just turn it off some argue that the same is true of type systems i don't want to get involved in that in that discussion too much um okay so we need uh what we need to do is implement the the same anti-circumvention features that cold turkey has and we're gonna do it with rust all right so here's what we need to build okay so we've got the extension javascript running in the browser and browser extension apis have this feature called a native messaging host that lets javascript communicate with a pre-installed native binary over standard i o and the browser starts that native binary in a sub-process and manages its life cycle along with the extension all right so that allows uh that allows us to communicate directly with the extension through another program right so we're gonna we're gonna build a daemon that will periodically ask the extension if it's enabled and uh if the browser fails to respond through the native messaging hosts or it responds with something unexpected or incorrect then the daemon is going to shut down the browser and again you know it'll annoy me into not doing this anymore all right so let's uh let's check out some of this code here let's get into it starting with the daemon this program is trying to decide to do one of two things you know close the browser or leave it open and the first step we need to take is to make sure that we have a pin for the browser process id if we can't find one that means the browser isn't running so we can just exit without worrying about it we don't care um if we do find one we're going to hold on to it because we'll need it if we have to close the browser later all right then we're going to be coordinating a couple of things concurrently and we're going to use threads and we're going to use a channel to do that channels are good for this because you can clone the center you can move it into a closure you can coordinate you can communicate between between threads this way or maybe not between threads but you can coordinate what threads are trying to accomplish in this case so uh first thing we got to do is set up communication with the native messaging host kick off thread and then we'll connect to the hosts over ipc inter-process communication then we send a message that just says ping we're going to wait for a response and if we get that response back and the response is pong we're all good we're going to tell the channel we don't want to close the browser and if anything else comes back you know malformed input or unrecognized input we're just going to tell the tell the uh the program to go ahead and close the browser and uh so at the same time as we're doing that we're also going to start another thread and we're going to immediately suspend it for 20 seconds and in that 20 seconds if that 20 seconds elapses without a response from the native messaging hosts we're going to push an outcome onto the channel to close the browser because it's timed out and we don't think it's ever going to respond um and so now the last thing that we have to do is wait for something to appear on the channel and once something does if we're if we're told to close the browser then we'll use the browser pin that we got earlier to shut it down and either way the damon's work at this point is is finished so we'll exit all right so that gives us the daemon let's now take a look at the native messaging host side of this there's not a whole lot going on here it's just a proxy between the daemon and the browser itself so what we're going to do is we're going to accept incoming ipc connections the native message host runs continuously whereas the daemon will run once and exit right so we're accepting one connection after the next uh over time and for each one that comes in we'll await a request and if the request is ping then we're going to move forward and proxy that up to the browser and then when we receive a response from the browser sometime later we're going to check it out we're going to see if it says pong and if this function here returns true then we will send pong back to the daemon so that it can finish up pretty yeah pretty pretty simple it's just a proxy and then finally we're going to close the circuit here right so the javascript side also needs to communicate with the uh with the native messaging host and uh to do that we've just got to throw in a couple more lines of javascript so we're going to use the browsers api to connect to the to the host we're going to wait for requests when a request comes in we first need to ask if the extension is enabled in private browsing at this point we know everything else is enabled just because the code is running but we do have to ask this of the browser at runtime so if we get a an acceptable response there if everything looks good we're going to send pong back down to the native messaging host the native messaging host is going to send it back to the damon which will exit uneventfully and if anything should go wrong in there if it should take too long or if the response is incorrect or you know any other kind of failure failure happens uh the the daemon is going to assume that it happened because i tried to circumvent the system and uh it's going to close the browser and annoy me into not doing that and i'm really pleased to say that this this does work both uh technically and uh you know psychologically or however you want to put that um it uh it has the intended effect so uh if i come over here and the temptation is just so overwhelming i go and i just i remove the extension i can't take it um damon's gonna execute and the browser will close and uh you know in reality the damon is is running once every 10 seconds i don't have to do it manually but um uh yes it works and uh i would say at this point that i i feel like i've achieved the goal of at least becoming like pretty offline if not extremely so so uh yeah i'm thrilled with this it was a great opportunity to learn rust and i just want to wrap up here first by shouting out some of the crates that i used so there's ps util for getting the pid inner process for for ipc libc for closing the browser and then sturdy and byte order for communicating with the extension from the messaging host and then i want to uh plug that i work at struction site we're building project tracking tools for the construction industry and we use a lot of rust and we're only going to be using more right we've got a server we've got processing jobs and now we've even got a front end code that we've built with rust and compiled to web assembly and uh we've also used elm for our user interfaces so if any of that sounds interesting you can head to stretchingsite.com careers and check out the positions we've got open and feel free to reach out to me as well and then lastly here are some places that you can reach me you know feel free to send me an email or you can find me on github and if you like to play chess you can find me at chess.com so thank you so much i'm really honored to be able to speak at rest conf this year and i hope you enjoyed this you
Info
Channel: Confreaks
Views: 320
Rating: 5 out of 5
Keywords: Rust, Lang
Id: 2K-HhAagG6o
Channel Id: undefined
Length: 15min 27sec (927 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.