Setup Firefox to use your server as a SOCKS VPN with sshd

Type: ssh -D 1337 -N -f -q -C username@example.com

This starts ssh and makes it listen on port 1337.

  • -N tells ssh not to execute a remote command; needed since we're just forwarding a connection
  • -f starts the program in the background (after you enter your password for the server, if needed)
  • -q suppresses warnings
  • -C compresses the connection

Then open Firefox. Go to preferences. Then network settings. Then click manual proxy settings. Enter these details:

  • SOCKS Host: example.com
  • Port: 1337.
  • Click SOCKS v5
  • Click Proxy DNS when using SOCKS v5.

Done

unix ssh

Autoincrement id column in sqlite3 and postgresql

In postgresql it's: create table whatever(id serial primary key, ....).

And in sqlite3 it's: create table whatever(id integer primary key autoincrement, ....).

In sqlite3, autoincrement comes after primary key. In postgresql you use the serial type. (or bigserial). I always forget this.

sql sqlite postgresql

ExpressJS tutorial: GET, POST, JSON, HTML forms, query & url parameters

npm install express installs node's HTTP framework. express = require('express') gets a reference to express. app = express() creates an instance. app.listen(3000, _ => {}) starts it.

app.get(urlstring, (request, response) => {}) creates a GET route, which is passed a request and response parameter.

request.query is an object with all the query parameters. request.params is an object with all the url parameters, as defined with the syntax :variable_name in the url string.

response.send() returns a string to the client. response.json() will return a json object to the client with the relevant response headers.

const express = require('express')
const app = express()
app.listen(3000, _ => console.log("started on 3000"))

app.get("/:username/:place", (req, res) => {
  var username = req.params.username
  var place = req.params.place
  var debug = req.query.debug
  res.send(`hi ${username} at ${place} with debug status ${debug}`)
})

You can hit that with http://localhost:3000/chris/cross?debug=hell%20yes.


app.use(express.json()) will allow you to access POSTed JSON through the request.body object. app.post() will create a POST route. Then we can access that body object.

const express = require('express')
const app = express()
app.use(express.json())
app.listen(3000, _ => console.log("started on 3000"))

app.post("/post", (req, res) => {
  res.send("sent name via post" + req.body.name)
})

You can access that new endpoint through

fetch("http://localhost:3000/post", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ "name": "Christopher Cross" })
})

You can put express.json() in second parameter in an array in app.post() so the JSON middlewear only applies to that route.


We can create a route that gives us an HTML form. Note the nested style of the 'client' parameters in the form.

app.get("/htmlform", (req, res) => {
  res.send(`
    <form action="/htmlform" method="post">
      <input name="client[name][first]">
      <input name="client[name][second]">
      <button type="submit">press me<button>
    <form>
  `)
})

Now we can use express.urlencoded() in a post route to parse data from the HTML form. (From express 4.0 we no longer need to specify extended: true to allow nested HTML form parameters)

app.post("/htmlform", [express.urlencoded()], (req, res) => {
  res.send(req.body)
})

If we don't want to embed the HTML in our javascript file, we can put the HTML into an external file and use res.sendFile("__dirname" + "/yourfile.html"). __dirname is an internal nodejs variable that points to the directory where node was started.

Finally, if we go to http://localhost:3000/htmlform and fill in the form then press the button our post route will output something like:

{"client":
  {"name":
    {"first":"craig","second":"david"}
  }
}

If you want to redirect to another page, sending of res.send() for example, you can issue res.redirect("/another_route") to send return with a 302 request and a GET request to another_route.

nodejs

Javascript: A quick introduction to __proto__ and prototype

Javascript looks up a property on an object, student in our example. Javascript first looks at all the properties on student. If it doesn't find it, it looks at the properties on the __proto__ object, which is itself a property on student.

var school = {
  school_name: "Handsome boy modelling school"
}

var student = {
  name: "Nathaniel Merryweather"
}

student.name // outputs "Nathaniel Merryweather"
// student.school_name     will be undefined

student.__proto__ = school

console.log(student.name, student.school_name)
// outputs "Nathaniel Merryweather", "Handsome boy modelling school"

If we have lots of students, making a new object every time will become boring. We can use a function with new and this inside the function to make it easier.

var school = {
  school_name: "Handsome boy modelling school"
}

function Student(name) {
  this.name = name
}

var nathanial = new Student("Nathaniel Merryweather")
nathanial.__proto__ = school
console.log(nathanial.name, nathanial.school_name)

var chad = new Student("Chad Baker")
chad.__proto__ = school
console.log(chad.name, chad.school_name)

But we still have to manually add __proto__ to each object. We can make that easier by setting Student's prototype object to be our student object: it sets the object's __proto__ property when we make a new object.

var school = {
  school_name: "Handsome boy modelling school"
}

function Student(name) {
  this.name = name
}
Student.prototype = school

var nathanial = new Student("Nathaniel Merryweather")
console.log(nathanial.name, nathanial.school_name)

var chad = new Student("Chad Baker")
console.log(chad.name, chad.school_name)

Javascript is full of objects, Date as one example. All objects have properties. You can, at runtime, add new properties to all instances of any object. You do this by adding a new property to the prototype object.

var now = new Date()

now.custom_property // outputs undefined

Date.prototype.custom_property = "I am a custom property"

now.custom_property // outputs the above
javascript

Get extension and body from base64 encoded image

Base 64 images start with data:image/. After this comes the image type. Then ;base64,. And finally the base64 encoded data.

We can use that information to find the file extension between data:image/ and ;base64,.

function base64encoded_image_extension_and_body(data) {
  var base64_part = data.search(/;base64,/)
  var ext = data.substring("data:image/".length, base64_part)
  var body = data.substr(base64_part + 8) // ;base64, is 8 chars
  return { body, ext }
}
javascript

Page 1 of 99
next
Click me