If you ever had to do portforwarding on network that was not yours, you know it can be pain in the ass sometimes and you most probbably do not want to deal with it. o
This method is very easy to do and will allow you to expose your port to public and even stop local computers from sniffing data to and from that port.
How is this done in theory
Well, if you ever used SSH, you probbaly know it is just encrypted tunnel…
What you maybe did not know is that it can carry a lot more than text. (you cna use it to copy files, to open graphical application, as a SOCKS proxy, etc.)
One of things a lot of people do not know about is that you can also use SSH to forward ports from one side of tunnel to another…
Lets say your computer can access some port and someone in network you are connecting to can not…
What you can do is specify port forwarding in SSH configuration.
Let’s say you want to forward local port to WAN…
What happens is remote machine you are connected to will listen on specified port and when someone connect to that port, your computer will connect to local port and data will be tunneled inside SSH.
How to do this in practice
This is actually very simple
1
ssh -R 221:localhost:22 server
This will make server listen on port 221 and when someone connects to it, you will connect to localhost:22 and connection will be established.
One small trick is that in OpenSSH (implementation of SSH cost often used) server will listen only on loopback interface (localhost) and not on other interfaces.
To solve this, we need to add:
1
GatewayPortsyes
to the end of /etc/ssh/sshd_config and restart SSH server. (systemctl restart sshd for SystemD or service ssh restart)
I created a few applications that stream torrents, so, I get asked quite a bit about it.
Today I am going to write a little bit about how it works and how to utilize it.
Where do I get torrents from?
First of all, to get torrents, you can use search engine/file someone sent you/create your own torrent…
So, what torrent file/megnet link is?
Torrent file/magnet link
When you get torrent in any form, what you actually get is hash of that torrent.
It can be either looked up on Torrent trackers (web services) or DHT (distributed hash table).
The part that is actually looked up is hash of that torrent.
That hash is used to identify and verify that data integrity.
What do we do with data from tracker/DHT?
What we get from DHT is list of peers that have torrent we are looking for.
What happens next is we download data about torrent from those peers. (File list, sizes, etc.)
Ok, how do we download files now?
What is happening now is we are reaching out to peers and they are offering parts they have.
For streaming, we are simply accepting parts that are near area we need and deny those that we do not need at the momment.
How can we implement it?
If you are writing your application in Node.JS, what I would recommend to you is torrent-stream package. It has very simple API and it is very easy to pass it through HTTP layer.
If you are writing your application in any other language, I would recommend using peerflix which is CLI version of torrent-stream.
AudioNode is basicaly Object which you pipe audio into and/or get audio out of it.
AudioNodes connect to each other just like any audio equipment would in real life.
For example you maybe have microphone, speaker, spectrum analyzer with passthrough and amplifier.
You can connect for example microphone -> amplifier -> spectrum analyzer -> speaker
Getting audio inside system
What most of the people in Web Audio API would use if they wanted to play music inside it is MediaElementAudioSourceNode (implementation of AudioNode that gets sound from HTML5 audio tag).
It simply plays audio and that is it.
Problem with this one is that it is impossible to change speed. (might be possible to slow it down, but I did not find any ways to make it play music faster)
decodeAudioData
What I ended up doing is getting mp3 using XHR as ArrayBuffer and running decodeAudioData from audio context on it.
That gave me back buffer containing whole audio:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module.exports = function(url){
returnnewPromise((resolve, reject) => {
const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // Getting audio context. Anything you do with Web Audio API requires use of audio context
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer'; // This will make sure I get response in format I need for passing it to Web Audio API
xhr.onload = function(e) {
var audioData = xhr.response;
audioCtx.decodeAudioData(audioData, function(buffer) { // This will decode that audio file and return buffer using callback.
resolve(buffer);
}, function(e){
reject(e);
});
};
xhr.send();
});
}
What I can do now is take data from buffer at any rate.
Scaling audio using input audio buffer
Now that I have list of amplitudes I can play that audio back.
Web Audio API does not have any way to generate sound, just process it (at least I did not find anything like that).
What I ended up doing is using ScriptProcessor (AudioNode for transforming sound).
I did not hook up any audio sources to it, just hooked it up to sink.
transformationFunction is function that is getting called each time you need to process audio. Inside it I am taking audio from music buffer and putting it into output buffer.
Calculating sample in output buffer
What I have is huge buffer that contains song and I have very small buffer I am writing to.
I have counter i which tells me where I am inside input buffer.
What I do is iterate inside output buffer and calculate what I want to take from input buffer for that sample.
Let’s say playback speed is 1.5.
What I need to put into output buffer are samples 1.5, 3, 4.5, etc.
But, there are samples 1 and 2, and there is no sample 1.5, what do I do now?
Dokku is extremly simple PaaS (platform as a service) . Philosophy of this software is quite simple. It is as simple as possible clone of Heroku.
Advantage of system like this is that you do not have to (but you can) think about container application is going to run inside but only about application itself. All you need to do is git push and Dokku will make fully functional container out of it and deploy it.
What happends when I do git push?
First thing you need to know about Dokku, Heroku and similar software is that they use something called buildpacks. Buildpack is collection of additional software required to build and run your application. For example there is PHP buildpack that contains PHP, ngginx, apache, composer, etc.
What is buildpack and how do we use it?
Buildpack is collection of 3 scripts with specific role.
bin/detect - Role of this script is to detect if that buildpack was developed to be used with application like yours. This one is skiped if user already defined buildpack and is used for autodetect of language only.
bin/compile - Role of this script is to prepare your application for running (installing dependencies, building, minification or stuff like that)
bin/release - Role of this script is to tell which commands should you run to start this container.
As you have probbably already figured out, first we need to figure out which buildpack should be used for your application.
It can be either specified by user or detected by Dokku/Heroku/etc. by running all buildpacks against script until detect script returns true.
After that, compile script is run to prepare your application for deployment,
After that, for Dokku to be able to know how to run container, release return list of commands.
Example:
1
web: nodeindex.js
If your application does not have Procfile, the one from release script will be used.
There is just one step left after this… Image is commited and ran.
The only remaining whing is communication between your application and outside world. For anyone to be able to connect to your application from outside, your application needs to listen on specific port. For default http port, dokku is setting global variable called PORT to port number. (And nginx is used to proxy to your application)
You can also configure Dokku to forward other ports to your application.
What are advantages of system like this?
There are a lot advantages but thrse are the ones that got me to switch:
Consistent enviroment - You are sure your application will act the same way on any server that uses Dokku.
Easy deployment and fast first time configuration
There is big community and a lot of plugins for it.
My friend was quite confused about OAuth 2 today (facebook specifically) so I decided to write a couple of tricks for facebook and instagram authentication.
Types of authentication on Instagram/Facebook
There are two types of authentification.
With backend callback
With frontend callback (facebook specific implementation)
Backend callback
On your redirect url you are getting code GET argument and that code is what you use to get token.
Frontend callback (facebook implementation)
When you trigger authentication from javascript, facebook sdk opens popup for authentication and when popup authentification is finished, window will get closed and you will get token back.
It is all OAuth 2, why is facebook different?
Most modern applications today are modern and it is just not natural experience to have redirect and to reaload page.
A lot of people would like to use same style of authentification for Instagram as for facebook.
What is the actual difference between facebook auth and normal OAuth2?
What facebook did is build one more layer on top of OAuth.
They developed backend that just returns token to your frontend application.
How do I do same thing for instagram?
Before we implement the same thing for instagram, we need to get familiar with couple of concepts.