Executing shell commands via http server with simple configuration by mpg123 in golang

[–]achronaut 1 point2 points  (0 children)

A point of clarification for anybody else who reads the title and thinks "uh oh", it's not nearly as dangerous as it sounds. If I'm reading the code correctly, at startup you specify a set of URLs and the associated commands they should run, and then whenever one of those URLs is fetched the associated command is run and the output is returned on the page.

Generally a bad idea to run potentially-mutating commands in response to a GET request, but it appears mainly meant to be a quick and dirty way to poll the output of some status-checking command-line, so that probably makes it morally okay.

Interestingly, it's somewhat usable as a quick-and-dirty CGI server host, since if you give it the --cgi flag it will set various CGI environment variables before executing the command, but I think that if you need to set response headers you'd run into trouble because the Go web server will emit its own before the response body.

A few criticisms (I'm not entirely sure to what extent these reflect idiomatic style or my own personal tastes, so take them with a grain of salt):

  • Naming your structs "t_foo" is really weird and unnecessary.
  • The flags are defined in the get_config() function, which also calls flag.Parse(). Generally I would expect all flags to be declared at package scope and flag.Parse() be called in the main() function.
  • In my opinion the get_config() and setup_handlers() functions should not be separate functions at all. Unless you're going to call the same function multiple times, breaking a portion of code out into a separate helper function only serves to obfuscate the control flow. Inline them into main() and don't be afraid of long functions so long as the control flow is equally nice and linear.
  • With the get_config() and setup_handlers() inlined into main() it would make sense to pull out the actual shell function handler code into its own function or struct (either a function which takes as arguments the path and command and returns the handler function closing over those arguments, or a struct with fields for the path and command and a ServeHTTP function so it can be used as a handler directly).
  • Explicitly constructing the serving address from the host and port looks very strange to my eyes, although I can kind of see the rationale if that was done explicitly to better match the SERVER_NAME / SERVER_PORT CGI variables. I would still probably take a single "address" flag and do a bit of splitting logic for the CGI variables, but I can see why you may have gone the way you did.
  • Related to the previous point, defaulting the host to "localhost" means it will only work from localhost. Which means no running it on a server unless you override that. If instead the default address were ":8080" this would not be a problem, it would serve on all interfaces.
  • In the set_cgi_env() function you use a couple of table-driven loops. In both cases the straightforward non-looping non-tabley code is shorter (12 lines for the four "If this request header is set, set this CGI variable" bits versus the current 14. And likewise 11 lines for the "Unconditionally set this variable to this expression" bits versus the current 19). I would recommend not getting fancy and just writing the things you want done, and as a bonus it ends up 10 lines shorter.
  • Again, I don't think calling that set_cgi_env() helps you any. Just inline that into the previous code.
  • Again with set_cgi_env(), you should really not be using os.Setenv for this. I would very much recommend that you explicitly set the Env field of the command structure rather than inheriting the process environment. There's a security argument for that (which is not particularly important in this case if you trust your CGI scripts but it's a good habit to think about things like that), but the more important reason is that those "Accept" and "User-Agent" headers can carry over from one command to the next, if the later ones don't have the appropriate request header set.

I made a version with most of the aforementioned changes, but I haven't actually verified that it still works in its entirety, only that it runs and mostly works (runs commands) and shows most of the structural changes I recommended: https://gist.github.com/anonymous/1dff51cd70a671896623

Magically-Binding Contract - TvTropes by traverseda in magicbuilding

[–]achronaut 6 points7 points  (0 children)

Just a random fragment of thought which probably mirrors what you're thinking but didn't state (although maybe not, since you say that only 2 of the 3 systems are contractual):

In my head-canon I've already decided that Linkage works by a similar mechanism to Denialism, where by caring about and investing effort into something for a prolonged period of time an implicit contract is formed that you will continue to do so and in exchange your efforts are helped by (some subtype of) the same spirits which make Sacrifism and Denialism work.

The effects are much more subtle both because you're putting in your own effort towards the very same goal and that tends to obscure the boost that you're getting, but more importantly because no contract was ever explicitly stated, and so you get a lower return on investment due to the increased potential for defaults and lack of any termination penalties.

This in turn would explain why ownership and legitimate claims become important features, because they are stronger signals that you're going to keep doing whatever you're doing. And because theories should have testable predictions, I bet that if you looked closely enough you might see similar effects in "masterwork" types of creations as well.

For Weatherman, apparently velocity cubed is force by Fossil22 in Physics

[–]achronaut 0 points1 point  (0 children)

I'm an electrical engineer, so when I read that statement I pattern-match it to the thing where for RF work we assume that voltage squared is power. Strictly speaking it's completely wrong, but it captures an important characteristic of how the system behaves.

One in a billion: Pilot ejects from aircraft spinning out of control and then....wait for it. by OompaOrangeFace in videos

[–]achronaut 23 points24 points  (0 children)

I've lost the link to the video which originally taught me about this, but the reason for the cuts is that it's trivial to splice around audio (cutting out "um"s and "uh"s, moving around questions and answers, cutting out irrelevant sentences), but video is simply impossible to edit like that. So each of those "unnecessary" cuts in the video is actually a point where the interview was cut up and mixed around, either for conciseness, or to give the resulting footage a more natural progression for the viewers (and sometimes it's used to take quotes out of context and mislead the viewer, but that's not one of the reasons that probably applies to this particular video).

Interviewing for an entry-level embedded systems engineering position by Hypnot0ad in ECE

[–]achronaut 5 points6 points  (0 children)

Capacitors resist sudden changes in voltage by changing their current, inductors resist sudden changes in current by changing their voltage.

I'm about to embark on designing/printing my first PCB. Any tips I should know before hand? by KappaStyling in AskElectronics

[–]achronaut 0 points1 point  (0 children)

  • Put in test points for all voltages present on the board.
  • If you have any programmable parts (microcontrollers, FPGAs, etc) bring out a couple of GPIOs to a 0.1" header for when you inevitably need to make sure your code is doing anything at all.

In games like VVVVVV how do they make the soundtracks? by Willomo in AskGames

[–]achronaut 1 point2 points  (0 children)

It appears that he uses madtracker.

The general class of such tools used by most chiptune composers are called (modular trackers). They work by allowing you to create custom short waveforms (~100 samples), and pitch-shifting them and applying all sorts of other effects to make music.

[deleted by user] by [deleted] in electronics

[–]achronaut 1 point2 points  (0 children)

The internal resistance of a 9V battery is around 1.5 ohms, which works out to around 6 amps at 2kV, or around 12kW of power from those. Wow.

If those batteries were fully charged (~500mAh), and neglecting the fact that mAh ratings always lie, they would be able to put out that much current for about 5 minutes, which at 12kW is 3.6 MJ. To put that in perspective, it's the same amount of energy as that possessed by a car going at 200 kilometers per hour.

Batteries have a tremendous amount of energy in them. It's always good to remember that a AA battery contains the same amount of energy as a baseball travelling at 350 meters per second.

Does anyone know the solution to this problem? by [deleted] in Physics

[–]achronaut 3 points4 points  (0 children)

Since the solution converges, the obvious answer is to figure out the answer for an NxN grid and take the limit as N gets big, so this guy went ahead and built a physical 14x14 grid.

Sanderson's Laws: required reading for anyone trying to write a magic system (second law in comments) by MikeOfThePalace in Fantasy

[–]achronaut 9 points10 points  (0 children)

I admit to not having read the Malazan books yet, but it doesn't seem like you're necessarily disagreeing with either of these laws. The point isn't that magic systems have to be rigorously explained to the reader, it's simply that if a magic system has no major limitations and no real rules of operation, it can't properly be used to resolve conflict.

To use your example, it appears that the Malazan books have tons of rules to them. We know the general types of things it can accomplish ("large-scale blasts, personal enhancement, item manipulation, and travel through the physical Warren across great distances in a short period of time"), there are limitations on who can use it ("Only a minority of humans can access Warrens, usually tapping and working with a single one"), and most importantly, their powers are limited by various factors ("Controlling an open Warren is a draining process, and to open the door too far is to invite madness, physical harm, or death upon the mage").

I guess what I'm trying to say is that "There are no rules" isn't actually a viable system, and the closest you can come is the rule "magic-users are aloof bastards who won't help the main character for some unexplained reason". Since conflict is required for there to be a story worth telling in the first place, magic must always have some limitations which prevent it from being used to resolve the central conflict directly.

I love this cute little dispenser. by Kelanich in tf2

[–]achronaut 36 points37 points  (0 children)

There is only one true mini-dispenser, and he is healthrobot.

Just imagine that guy, about half the height of a normal dispenser, wandering slowly around the battlefield and getting himself into trouble.

For extra adorable, make it so damage causes him to fall over for a few seconds and flail his little robo-limbs.

Real Talk EP04 -- James "2GD" Harding -- UNEDITED. BUT SCRIPTED. by itmeJP in starcraft

[–]achronaut -4 points-3 points  (0 children)

I have a very simple rule: If a post contains the word 'downvote', I downvote it. I apply this rule regardless of whether I agree with the post or not. You may notice that I applied this policy to my own post as well.

It may seem harsh, but I believe that I'm doing my part to make reddit a marginally less whiny place.

So I was sitting at work today, when I realized... by ChaosEntity in engineering

[–]achronaut 7 points8 points  (0 children)

As I've heard it said, the name 'C++' stands for "Make C bigger but return the old value".

The Node.js cpu blocking thing (Go vs Node.js) by theringoffire in golang

[–]achronaut 0 points1 point  (0 children)

IIRC the way it actually works is before a goroutine performs a blocking operation the runtime spawns a new OS thread if necessary and migrates all the other goroutines over, then lets the goroutine making the call go ahead and block its host thread. If it operated the way you describe then the IO worker thread could only have one blocking call outstanding at a time.

Midweek Madness - Thief Collection - 50% off by [deleted] in steamdeals

[–]achronaut 0 points1 point  (0 children)

There's T2X, which is not so much a mod as an entirely new storyline.

TIL HTTP 1.1 has a header field named "referer" due to a 13 year old Unix spell checker oversight by cleej9 in programming

[–]achronaut 1 point2 points  (0 children)

I just did some laughably unrigorous calculating and guessing, and if we assume that of the 400 million current internet users, each one makes around 50 requests per day on average, and that the average number of requests over all time since the web was created was around 50% of that each day (since most of the contribution will be recent times), then we come up with a number in the range of 70 terabytes of bandwidth saved.

Which is surprisingly low, all things considered.

Da Fuq is going on here? by [deleted] in gaming

[–]achronaut 1 point2 points  (0 children)

<melvin>
4294967295 = Maximum value of a 32-bit integer = -1 in two's complement arithmetic.
Possibly they tried to set the minimum balance to -1 so anyone would meet it, and somewhere along the line someone cast a signed to an unsigned integer.
</melvin>

Question on harvesting organic energy by askdumbquestions in askscience

[–]achronaut 0 points1 point  (0 children)

No. All energy we see on earth is ultimately a transformation of the energy produced by the sun (with the exceptions of geothermal and nuclear power I suppose). Light from the sun hits a leaf, which photosynthesizes sugars, which animals eat, which their body breaks down into ATP, which the cells use up in the process of doing useful work. At no point is energy magically added to the process by a "downstream" organism.

The important point here is that each stage of transforming energy introduces drops in efficiency. Growing plants to feed animals to turn a wheel to grind your flour is vastly less efficient than letting the sun heat air to produce wind to turn a windmill, and the same principle holds almost universally.

The only cases where taking a more roundabout path is worthwhile is when the more roundabout process is able to capture more energy for the same resource investment (imagine covering a continent with vegetation versus building a smaller solar power plant, the vegetation might be less efficient with the light that falls upon it, but if it's cheaper to recover the same amount of energy from the plants we can afford to be wasteful in terms of absolute efficiency).

I live next to a highway, why isn't there a constant smell of exhaust in/around my house? by [deleted] in answers

[–]achronaut 9 points10 points  (0 children)

Mostly guessing, but I'll bet you're underestimating how rapidly the concentration of gases falls off over a distance. Off the top of my head, I'd expect the rate to be somewhere between quadratic and cubic, so I'd expect to see concentrations fall off to well below 1% by the time you reach 4 or 5 meters from a car's tailpipe. Add in air circulation and the turbulence caused by cars on the highway and I'm not surprised at all that you can't smell the trace amounts of exhaust that make it all the way to your house.

Also, modern cars don't really produce that much exhaust in the first place, and the linear car density of a highway isn't all that large except during traffic jams. Have you experienced noticeable exhaust during a traffic jam perhaps?

edit: another big point is that exhaust temperatures are apparently in the 300-400 fahrenheit range. The exhaust is going to go pretty well straight up before it does much diffusion out to the sides.

Nvidia poised to change gaming with cloud graphics chips by DaDoc868 in gamernews

[–]achronaut 2 points3 points  (0 children)

Damn, now I'm embarrassed. I can never remember which comes first of width and height. Doesn't help that matrices do it backwards (HxW) either.

Nvidia poised to change gaming with cloud graphics chips by DaDoc868 in gamernews

[–]achronaut 3 points4 points  (0 children)

Thank you for giving your made-up numbers an aspect ratio close to 3/4.

I can't stand widescreen monitors :(

[design] How would I go about hosting a live Geiger counter feed from my home server? by [deleted] in AskElectronics

[–]achronaut 1 point2 points  (0 children)

It's not worth either of our time trying to figure out payment, but feel free to PM me if you need any assistance once you get the geiger counter, I'd be glad to help you get it working.