Wednesday, August 14, 2013

Asterisk, Google Voice, and Amazon EC2

I've been running Asterisk on my DD-WRT router for some time now. However, Juniper was awesome enough to give me a WLA and a virtual controller when I was invited to their tech summit this year, so I've decided to change things up with my home setup (for like the 80th time haha). I've been thinking of different ways to setup a home phone for free, utilizing Asterisk and Google Voice. One way would be by repurposing an Android Mini PC as shown to me by a buddy I do business with, but since I'm a cheap bastard and this is really more just for fun than anything else, I decided to look into setting up a micro instance in Amazon Web Services. One advantage to this is the fact that if you have a new account you can have a micro instance for a year for free. Anyway, I found a forum post here that does a great job of taking you through the process. I am going to copy the process here so that in the event that something happens to that site there will be a copy. I also made a few edits as there were a few steps missing from the original tutorial.

This guide assumes that you've already setup your AWS account and figured out how to set the security group. You will need to open some ports (TCP: 22, 1723, 5060. UDP: 5060, 10000-20000). On a side note, I understand that opening up these ports is a security risk, so you may want to look at using an AWS VPC or some other form of security to lock things down a little better. In my case, I don't really care about security since I'm using a throw away email and my endpoint is on a completely separate network.

Step 1. Goto: and pick the t1.micro instance (ebs 64 bit) for the region that you setup in AWS. Launch this instance (there is a button) and get it working with the security group that you configured. After it's launched you need to setup an Elastic IP and associate it with the instance. After that go ahead and log into your new micro instance server. Once you get to this point, then you can continue with the guide. There are TONS of resources (including youtube videos) on how to get to this point. It's not rocket science.

Step 2. Setup firewall settings for asterisk. Lucid also has firewall settings that need to be adjusted.

#Uncomplicated Firewall
sudo ufw enable
sudo ufw allow 22/tcp
sudo ufw allow 1723/tcp
sudo ufw allow 5060/tcp
sudo ufw allow 5060/udp
sudo ufw allow 10000:20000/udp

#check status
sudo ufw status

#edit /etc/default/ufw and enable forward policy

#edit /etc/ufw/sysctl.conf and uncomment

#edit /etc/ufw/before.rules and add this after the header comments

# nat Table rules

# Forward traffic through eth0.

# don't delete the 'COMMIT' line or these nat table rules won't be processed

#disable and enable to apply changes
sudo ufw disable && sudo ufw enable

Step 3. Recompile Kernel. The default kernel is set at 100HZ timing, this will give you HORRIBLE VOIP quality. The kernel needs to be recompiled to 1000HZ timing.

# Make yourself root
sudo su

# Update source list:
aptitude update

# Upgrade everything:
aptitude upgrade 

# Install dependencies:
apt-get build-dep linux-image-$(uname -r)
apt-get build-dep linux
apt-get install fakeroot build-essential
apt-get install crash kexec-tools makedumpfile kernel-wedge
apt-get install libncurses5 libncurses5-dev
apt-get install libelf-dev asciidoc binutils-dev kernel-package
apt-get install git-core

cd /usr/src
git clone git:// 
cd ubuntu*
git checkout --track -b ec2 origin/ec2
fakeroot debian/rules clean
fakeroot debian/rules editconfigs

# Configuration window should now appear, do the following:

Select YES

# Navigate to:
Processor type and features -> Timer frequency
# Select the 1000HZ frequency
Yes (Save)

#After saving and returning to prompt it may ask you to do it again for i386, select yes and repeat!

Step 4. This next command will take about 7 hours to recompile the kernel. But, there is a shortcut. Amazon charges by the minute for each instance type that you use. I recommend shutting down your instance at this point and changing it to a m1 extra large instance type (this will cost you about 70 cents). This will increase your micro instance from:

613 MiB memory
Up to 2 EC2 Compute Units (for short periodic bursts)
EBS storage only
32-bit or 64-bit platform
I/O Performance: Low
EBS-Optimized Available: No
API name: t1.micro


15 GiB memory
8 EC2 Compute Units (4 virtual cores with 2 EC2 Compute Units each)
1,690 GB instance storage
64-bit platform
I/O Performance: High
EBS-Optimized Available: 1000 Mbps
API name: m1.xlarge

The compiling time will be reduced to about 25 minutes. Once you got the instance backup with the m1.xlarge instance, continue like so:

sudo su
cd /usr/src/ubuntu*
fakeroot debian/rules binary

#Check if your deb files were created
cd ..
ls *.deb

#install new kernel
sudo dpkg -i linux-*.deb

Then shutdown your system again and change it back to a micro instance. Then boot it back up.

#Check your new Kernel version
uname -r

#Check if Kernel HZ value change persisted:
cat /boot/config-`uname -r` | grep HZ

#If value 1000HZ=yes then you did it right!

Step 4a. Add missing dependencies. I ran into an issue with RTP not working following the guide provided, so I had to run the following command, which adds res_rtp_asterisk to the list of supported modules that you need to ensure is checked when you get to the Asterisk menu below.

apt-get install uuid-dev

Step 5. Install Asterisk 11

#get source. note: dahdi needs to be installed to compile and install libpri -- we don't really need it for any other reason

cd /usr/src/

#extract source

tar zxvf dahdi-*
tar zxvf libpri-*
tar zxvf asterisk-11*

#resolve error for compiling dahdi

ln -nsf /usr/src/linux-headers-`uname -r`/include/asm-x86 /usr/src/linux-headers-`uname -r`/include/asm

#install dahdi

cd /usr/src/dahdi*
make && make install && make config

#install libpri

cd /usr/src/libpri-1.4*
make && make install

#install asterisk. note: once the menu pops up check and make sure you have chan_motif and xmpp (should have a * next to them)

cd /usr/src/asterisk*
./configure && make menuselect && make && make install && make config && make samples

Step 6. Configure Google Voice

#Backup original conf files (you should still be root)

cd /etc/asterisk
cp extensions.conf extensions.conf.orig
cp motif.conf motif.conf.orig
cp sip.conf sip.conf.orig
cp xmpp.conf xmpp.conf.orig

Now you will want to replace the following files with these (change USERNAME to whatever you want and make sure you google account info is correct):

#extensions.conf - Don't forget the USERNAME on the last line


; If an unauthenticated request some how gets through, send them to free 411.
exten => 411,1,Answer()
same => n,Dial(Motif/google/

exten => _1XXXXXXXXXX,1,Dial(Motif/google/${EXTEN},,r)
exten => _XXXXXXXXXX,1,Dial(Motif/google/${EXTEN},,r)
exten => _+1XXXXXXXXXX,1,Dial(Motif/google/${EXTEN},,r)

exten => s,1,NoOp()
 same => n,Set(crazygooglecid=${CALLERID(name)})
 same => n,Set(stripcrazysuffix=${CUT(crazygooglecid,@,1)})
 same => n,Set(CALLERID(all)=${stripcrazysuffix})
 same => n,Dial(SIP/USERNAME,20,D(:1))



#sip.conf - Pay attention to externhost, secret, and USERNAME






# Stop/Start asterisk

sudo /etc/init.d/asterisk stop
sudo /etc/init.d/asterisk start

If everything went at planned your Asterisk Server with Google voice should be working, you can now login with your SIP client utilizing the extension username and password that you chose in sip.conf. In my case I used a SNOM 300 from our office. So far, I have been on business calls with it and have yet to have any call quality issues. Enjoy!

No comments:

Post a Comment