Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Friday, May 20, 2022

Fiddler + EXE + localhost server

(a friend was asking about this one, so here's a repost / rewrite of a previous blog from 2014-12-03, back when Visual Studio 2012 was state of the art)

Capturing fiddler traffic between a local EXE and a web server running on localhost turned out to be a little more difficult than I originally imagined. I tried to set my server to localhost or 127.0.0.1 while my local EXE had a proxy through fiddler (localhost:8888), but some/most/many/mine web requests appear to skip proxies when going to a local address.

To get this done:

    1. Run fiddler (Download Fiddler Web Debugging Tool for Free by Telerik), make sure "File / Capture Traffic" is checked.
    2. Make sure fiddler's filters show traffic to localhost.fiddler

      I also like to Hide if URL contains /arterySignalR, which is a special endpoint that IIS express/Visual Studio will use on localhost for debugging. It can generate a lot of noise in fiddler.

      See below for a screen cap of my fiddler filter settings.
    3. Start your web server on localhost
    4. Reconfigure your EXE's target server to point to http://localhost.fiddler:>PORT< 
      This will be EXE specific.
      (note, >PORT< depends on the server you're running and what port it binds to)
    5. Start your EXE
    6. Make sure your EXE's proxy is pointing to localhost:8888, so all traffic goes through fiddler.

    Here's my current fiddler filter settings





    Saturday, April 24, 2021

    Chrome and Tab Groups: Where the heck am I?

    Task Context Switching 

    If you're like me (and why wouldn't you be? come on!) you've got a lot of tabs open in Google Chrome. Like, an unsettling amount - enough to make your memory paging system go "Hol' up". 


    I like to organize my browser so I have separate Chrome windows that represent a task context. 

    For example, currently I have separate Chrome windows dedicated to: 
    1. A code change associated with a work ticket around some performance improvements. I have a spreadsheet on there for tracking timing changes, a few tabs for some associated research, and a few tabs for watching our metrics system.
    2. Articles I want to read unassociated with any active work tickets. I park "this looks interesting and I want to get to it soon™" articles there.
    3. A presentation that I'm working on, with a tab for the actual presentation and another tab for some survey results from anonymous questions I'm asking.
    4. Et cetera, et cetera, et cetera
    This system works well for me, but it can provide some tough context switches, when I go from Task A to Task B, I have to hunt through my current 11 (yes 11!) Chrome windows to find the right context, so I can work on my task.

    what follows is an ACTUAL screen shot. Feel my pain.

    Context switching means looking at each window in turn, and trying to figure out what task context that window is dedicated to by tab titles. Easy to do once, but omg, try doing it dozens of times a day.

    I want to make this process easier, so context switches require less effort.

    All Hail Chrome Tab Groups

    Chrome Tab Groups are a relatively new feature where you can take one or more tabs and associate them with a label. It's pretty easy to use, with a right mouse click and a little bit of typing. The linked blog is the best introduction and guide.

    When I create a new Chrome window with a set of tabs, I will associate the leftmost tab with a Tab Group and give it a short name so I understand the context quickly.

    For instance:

    You probably don't know what wi2524860 (dms load means, but I do. When I want to work on that task, I can quickly loop through my Chrome windows, and, by keeping my eye on the upper left corner, find the right window to bring into focus.

    Bonus Points

    Windows 7 introduced a great application shortcut key feature, where the Windows Logo key + a number will switch to that number program pinned to the taskbar

    If you're like me and have Chrome pinned as task 1, you can hold down the Windows Logo key and repeatedly press 1 to cycle through your windows, looking for the correct task using the tab group label.

    Anybody that's been in a meeting with me (may the Lord have mercy on your soul), has probably noticed me doing this at the beginning of a meeting, where I will say "no, no, no, no, yes" as I find the right window. I'm sorry you had to listen to that, but here we are.

    Conclusion

    Anyway, it's a minor efficiency thing, but whatever we can do to save those precious, precious seconds. Thanks.



    Thursday, March 25, 2021

    power(shell), corruption & lies: Building command line arguments

    I hate typing long command lines to command line utilities. Invariably I will get one obscure path wrong and spend an hour with a copy / pasted command line in notepad to split out the components and figure out what I screwed up this time.

    A lot of those long typing jobs have to do with feeding a bunch of file or directory names to an .exe

        foo_the_bar.exe --blort first\dir second\dir third\dir hey\heres\a\filename.txt

    If I only had a tool that I could use to generate that list of directories and filenames.

    powershell and Invoke-Expression (iex to it's friends) to the frickin' rescue.

    1. Write a powershell expression using Get-ChildItem (gci), Where-Object (?) and Select-Object (select) to build your list of things. Use -join ' ' to space separate the list of things

      PS c:\users\kpk> (gci c:\my\dir -recurse | ?{ $_.FullName -match 'some.*criteria$' } | select -expand FullName) -join ' '

    2. Use string interpolation to embed that expression with the name of the .exe you want to run. In my case, I was using cloc to (duh) Count Lines Of Code.

      PS c:\users\kpk> "cloc $((gci c:\my\dir -recurse | ?{ $_.FullName -match 'some.*criteria$' } | select -expand FullName) -join ' ')"

    3. Run that expression a few times to make sure you're happy with it. Add some command line arguments as needed.
    4. Use iex (Invoke-Expression) to run that beautiful mess.

    Let's unpack the expression a bit

        PS c:\users\kpk> (gci c:\my\dir -recurse | ?{ $_.FullName -match 'some.*criteria$' } | select -expand FullName) -join ' '

    1. gci c:\my\dir -recurse |

      gci (Get-ChildItem) recurses through a directory structure. It's more complicated than that, but things often are.

    2. ?{ $_.FullName -match 'some.*criteria$' } |

      ? (Where-Object) will test each produced file & directory for a criteria. In this case, a regular expression. Only the worthy shall pass.

    3. select -expand FullName

      select (Select-Object) extracts the FullName property from the produced collection of objects and expands it to a list

    4. -join ' '

      I need that list turned into a single flattened string with space separation, because cloc wants space separation. Other command line utilities want other kind of formatting (prefixed by a magic argument, comma separated, etc.). -join's your friend here.
    Once all of this is looking good, and you've prefixed it with the name of the command you want to run, iex takes care of the rest.

    Never let a human do a job that a robot can do better. Ok, maybe not 'Never', but mostly. Sometimes.... Whatever.

    Saturday, June 20, 2020

    power(shell), corruption & lies: Directory Info Aggregation

    To get a recursive summary of files, folders and sizes from a root directory:

    $len=0; Get-ChildItem -Recurse | 
        %{ $_.GetType().Name; $len+=$_.Length } | 
        Group-Object -NoElement | ft -auto; "Total size= $($len / 1Gb) GB"

    Output looks like this
    Count Name
    ----- ----
    18386 DirectoryInfo
    57784 FileInfo
    Total size= 25.7907013082877 GB
     
    How it works:

    1. $len=0 sets the variable len to 0. No duh here.
    2. Get-ChildItem -Recurse recurses through a directory tree from the current directory. You can use the -Path option for a different root directory.
    3. %{} is the alias of ForEach-Object and is a shorthand way of looping over results that are piped from the previous command
    4. $_.GetType().Name is emitting the type of the directory entry (DirectoryInfo or FileInfo)
    5. $len+=$_.Length is summing up directory entry sizes
    6. Group-Object -NoElement is counting directory entry types from step 4 and printing them out.
    7. ft -auto automatically formats the output so that large numbers are not truncated to ellipsis (...).
    8. "Total size= $($len / 1Gb) GB" prints out the total accumulated size from the $len variable in Gigabytes.

    Wednesday, April 3, 2019

    FILE_FLAG_DELETE_ON_CLOSE, temporary files and tidying up your room

    I'm sure most of you have a favorite CreateFile flag (I kid, I kid). Mine is FILE_FLAG_DELETE_ON_CLOSE and I've been using it in my software development career for many years, to great effect, IMHO.

    I'd like to write a few articles to describe what it does, why it's awesome, and to present some frustrations with its usage as a challenge to library writers.

    OVERVIEW

    At the lowest API level, when opening up a file for reading or writing in a Windows process, your call will wind up in the CreateFile function. Actually, CreateFile does a ton more than just reading and writing - check out that MSDN link above - but for this article, let's just focus on regular disk files.

    NOTE: The above link takes you to CreateFileW, which is different from CreateFileA. The W and A are Microsoft's way of distinguishing Wide (2 byte) characters from nArrow (1 byte) characters. Macros hide this from you in your C++ code, so your calls are all done using CreateFile without the W or A suffix.

    Resolving to CreateFile is done whether the call originates from a higher level API like C++'s std::fstream, .Net's File.Open, whatever.

    CreateFile has a number of flags that tweak behavior that can be bitwise OR'ed together in the 6th paramter called dwFlagsAndAttributes.

    When you use the flag FILE_FLAG_DELETE_ON_CLOSE, this tells the Windows operating system to delete the file when all handles to it are closed.

    In case you don't see it yet, this is super effective for temporary files that are created during process execution, where data has to live outside of the process and on disk for whatever reason (too big for process memory, needs to be accessed by an external process, library requires filesystem access to do it's thang, ...).

    When you're all done with the temporary file, you need to tidy up your room, by getting rid of the junk piles on the floor.  To delete those files after usage (Mom says "clean your room... NOW!") instead of executing DeleteFile, you can just CloseHandle and let the operating system take care of it.

    Now, here's the awesomeness: Think about what happens if your process is torn down by an exception, taskkill, whatever. You've never reached the line of code that calls DeleteFile, so you've left a mess.

    <IMPORTANT>
    If you use FILE_FLAG_DELETE_ON_CLOSE, the operating system will delete the file for you as it tears down the process.
    </IMPORTANT>

    As the operating system tears down a process for whatever reason, it iterates through all open handles and, effectively, calls CloseHandle one by one. Side effects of the CloseHandle, like honoring FILE_FLAG_DELETE_ON_CLOSE, are done.

    Combining this with RAII semantics in code can make the resource of the temporary file automatically go away as soon as it is no longer needed, where the destructor of your temporary file management class closes the handle before the object dies, with the added safeguard that if something bad happens during your process execution, your file will still be removed.

    NOTE: FILE_FLAG_DELETE_ON_CLOSE can't protect you against all events: a power outage or a well placed hammer strike on the CPU will not go through the operating system's process teardown routine. YMMV.

    Next up, FILE_FLAG_DELETE_ON_CLOSE all the things! Where using this flag doesn't work well. Soon.