Skip to content

Instantly share code, notes, and snippets.

View brad-jones's full-sized avatar

Brad Jones brad-jones

View GitHub Profile
@jerblack
jerblack / Elevate when needed in Go.md
Last active April 9, 2024 15:14
Relaunch Windows Golang program with UAC elevation when admin rights needed.

I'm buiding a command line tool in Go that has an option to install itself as a service on Windows, which it needs admin rights for. I wanted to be able to have it reliably detect if it was running as admin already and if not, relaunch itself as admin. When the user runs the tool with the specific switch to trigger this functionality (-install or -uninstall in my case) they are prompted by UAC (User Account Control) to run the program as admin, which allows the tool to relaunch itself with the necessary rights.

To detect if I was admin, I tried the method described here first:
https://coolaj86.com/articles/golang-and-windows-and-admins-oh-my/
This wasn't accurately detecting that I was elevated, and was reporting that I was not elevated even when running the tool in CMD prompt started with "Run as Administrator" so I needed a more reliable method.

I didn't want to try writing to an Admin protected area of the filesystem or registry because Windows has the ability to transparently virtualize those writes

@sjindel-google
sjindel-google / BUILDSTEPS
Last active May 19, 2023 04:46
Passing strings between Go and Dart (via FFI)
go build -o godart.so -buildmode=c-shared
dart godart.dart
@mohanpedala
mohanpedala / bash_strict_mode.md
Last active April 23, 2024 11:47
set -e, -u, -o, -x pipefail explanation
@kizzx2
kizzx2 / with-env.ps1
Last active December 3, 2023 23:06
Run command with environment variables in PowerShell
$ori = @{}
Try {
$i = 0
# Loading .env files
if(Test-Path $args[0]) {
foreach($line in (Get-Content $args[0])) {
if($line -Match '^\s*$' -Or $line -Match '^#') {
continue
}
@hinzundcode
hinzundcode / libc.js
Created January 16, 2018 01:24
call execve from nodejs!
const { syscall, getAddress } = require("libsys");
const os = require("os");
if (os.endianess() != "LE")
throw "only little endian supported";
let dontGC = [];
function ref(buffer) {
dontGC.push(buffer);
return getAddress(buffer);
@dsherret
dsherret / Using.ts
Last active October 26, 2023 13:30
Typescript Disposable (using statement)
// NOTE: This is now rolled up in a package and supports more scenarios: https://github.com/dsherret/using-statement
interface IDisposable {
dispose();
}
function using<T extends IDisposable>(resource: T, func: (resource: T) => void) {
try {
func(resource);
} finally {
@jageall
jageall / SemverSort.ps1
Last active March 15, 2022 19:09
Sorts semver in powershell
#powershell script port of https://github.com/maxhauser/semver/
function toSemVer($version){
$version -match "^(?<major>\d+)(\.(?<minor>\d+))?(\.(?<patch>\d+))?(\-(?<pre>[0-9A-Za-z\-\.]+))?(\+(?<build>[0-9A-Za-z\-\.]+))?$" | Out-Null
$major = [int]$matches['major']
$minor = [int]$matches['minor']
$patch = [int]$matches['patch']
if($matches['pre'] -eq $null){$pre = @()}
else{$pre = $matches['pre'].Split(".")}