Skip to content

Commit

Permalink
Merge 5369c28 into 358ba70
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafał Słota committed Sep 2, 2019
2 parents 358ba70 + 5369c28 commit c5f9ee7
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 129 deletions.
17 changes: 10 additions & 7 deletions .travis.yml
Expand Up @@ -5,6 +5,10 @@ sudo: required
services:
- docker

stages:
- test
- deploy

install:
- mix local.rebar --force
- mix local.hex --force
Expand All @@ -25,10 +29,8 @@ script:
after_script:
mix inch.report

after_success:
- .travis/docker.sh

elixir:
- 1.9.0
- 1.8.0
otp_release:
- 22.0
Expand All @@ -38,17 +40,18 @@ env:

matrix:
include:

- otp_release: 20.3
elixir: 1.6.0
env: PRESET=test
- otp_release: 22.0
elixir: 1.9.0
env: PRESET=test
- otp_release: 22.0
elixir: 1.8.0
env: PRESET=dialyzer

jobs:
include:
- stage: deploy
script: .travis/docker.sh

cache:
directories:
- .dialyzer
3 changes: 1 addition & 2 deletions .travis/docker.sh
Expand Up @@ -6,8 +6,7 @@ APP_NAME='mongooseice'
# Skip this step for jobs that don't run exunit
test "${PRESET}" == "test" || exit 0

MIX_ENV=prod mix docker.build
MIX_ENV=prod mix docker.release
docker build . -f docker/Dockerfile -t ${APP_NAME}:release

DOCKERHUB_TAG="${TRAVIS_BRANCH//\//-}"

Expand Down
5 changes: 0 additions & 5 deletions config/config.exs
@@ -1,8 +1,3 @@
use Mix.Config

config :mix_docker, image: "mongooseice"
config :mix_docker,
dockerfile_build: "docker/Dockerfile.build",
dockerfile_release: "docker/Dockerfile.release"

import_config "#{Mix.env}.exs"
88 changes: 88 additions & 0 deletions docker/Dockerfile
@@ -0,0 +1,88 @@
FROM debian:stretch as builder

ARG ERLANG_VERSION=22.0
ARG ELIXIR_VERSION=1.9
ARG MIX_ENV=prod

ENV ERLANG_VERSION ${ERLANG_VERSION}
ENV ELIXIR_VERSION ${ELIXIR_VERSION}
ENV MIX_ENV ${MIX_ENV}

SHELL ["/bin/bash", "-c"]

# Handle locales
RUN apt-get update && \
apt-get install --yes locales && \
sed -i '/^#.* en_US.UTF-8 /s/^#//' /etc/locale.gen && \
locale-gen

ENV LANG="en_US.UTF-8" LANGUAGE="en_US:en"

# Install build utils
RUN apt-get install --yes \
wget \
unzip \
build-essential \
apt-transport-https \
git

# Install Erlang & Elixir
RUN echo "deb https://packages.erlang-solutions.com/debian stretch contrib" >> /etc/apt/sources.list && \
cat /etc/apt/sources.list && \
wget --quiet -O - https://packages.erlang-solutions.com/debian/erlang_solutions.asc | apt-key add - && \
apt-get update && \
export EXACT_ERLANG_VERSION=`apt-cache madison esl-erlang | awk '{print \$3;}' | grep ${ERLANG_VERSION} | head -1` && \
export EXACT_ELIXIR_VERSION=`apt-cache madison elixir | awk '{print \$3;}' | grep ${ELIXIR_VERSION} | head -1` && \
apt-get install --yes \
esl-erlang=$EXACT_ERLANG_VERSION \
elixir=$EXACT_ELIXIR_VERSION

WORKDIR /build

COPY mix.exs mix.lock ./

RUN mix local.hex --force && \
mix local.rebar --force && \
mix deps.get && \
mix deps.compile

COPY config config/
COPY lib lib/
COPY rel rel/
COPY priv priv/

RUN mix compile && \
mix distillery.release --no-tar && \
mkdir /release && \
cp -r _build/prod/rel/mongooseice/* /release/


FROM debian:stretch-slim

ENV MONGOOSEICE_UDP_BIND_IP=0.0.0.0 MONGOOSEICE_UDP_PORT=3478 MIX_ENV=prod \
MONGOOSEICE_TCP_BIND_IP=0.0.0.0 MONGOOSEICE_TCP_PORT=3479 MIX_ENV=prod \
REPLACE_OS_VARS=true SHELL=/bin/bash
ENV HOME /app

# Handle locales
RUN apt-get update && \
apt-get install --yes locales && \
sed -i '/^#.* en_US.UTF-8 /s/^#//' /etc/locale.gen && \
locale-gen
ENV LANG="en_US.UTF-8" LANGUAGE="en_US:en"

# libcrypto.so is required by ERTS. curl for healthcheck.
RUN apt-get install --yes \
libssl1.1

WORKDIR /app

COPY --from=builder /release/ .
COPY docker/start.sh /app/start.sh

RUN groupadd -r app && useradd -r -g app app
RUN chown -R app /app
USER app

ENTRYPOINT [ "/app/start.sh" ]
CMD [ "foreground" ]
37 changes: 0 additions & 37 deletions docker/Dockerfile.build

This file was deleted.

33 changes: 0 additions & 33 deletions docker/Dockerfile.release

This file was deleted.

2 changes: 1 addition & 1 deletion docker/start.sh
Expand Up @@ -9,4 +9,4 @@ DEFAULT_RELAY_IP=`hostname -I | awk '{print $1}'`
export MONGOOSEICE_UDP_RELAY_IP=${MONGOOSEICE_UDP_RELAY_IP:=$DEFAULT_RELAY_IP}
export MONGOOSEICE_TCP_RELAY_IP=${MONGOOSEICE_TCP_RELAY_IP:=$DEFAULT_RELAY_IP}

/opt/app/bin/mongooseice "$@"
trap 'exit' INT; /app/bin/mongooseice "$@"
19 changes: 13 additions & 6 deletions lib/mongooseice/evaluator.ex
Expand Up @@ -18,12 +18,16 @@ defmodule MongooseICE.Evaluator do
@spec service(Params.t, MongooseICE.client_info, MongooseICE.UDP.server_opts, TURN.t)
:: {Params.t, TURN.t} | {:void, TURN.t}
def service(params, client, server, turn_state) do
case service_(params, client, server, turn_state) do
{new_params, new_turn_state} ->
{response(new_params), new_turn_state}
new_params ->
{response(new_params), turn_state}
end
Logger.debug("Received #{params.class}:#{params.method}[#{params.identifier}] from #{inspect(client.ip)}:#{inspect(client.port)}")
{resp_params, next_turn_state} =
case service_(params, client, server, turn_state) do
{new_params, new_turn_state} ->
{response(new_params), new_turn_state}
new_params ->
{response(new_params), turn_state}
end
Logger.debug("Responding to #{inspect(client.ip)}:#{inspect(client.port)} with #{response_class(resp_params)}:#{params.method}[#{params.identifier}]")
{resp_params, next_turn_state}
end

defp service_(params, client, server, turn_state) do
Expand Down Expand Up @@ -90,4 +94,7 @@ defmodule MongooseICE.Evaluator do
defp handler(:request), do: MongooseICE.Evaluator.Request
defp handler(:indication), do: MongooseICE.Evaluator.Indication

defp response_class(:void), do: :void
defp response_class(params), do: Params.get_class(params)

end
16 changes: 8 additions & 8 deletions lib/mongooseice/evaluator/allocate/request.ex
Expand Up @@ -49,18 +49,18 @@ defmodule MongooseICE.Evaluator.Allocate.Request do
:not_requested -> []
%Attribute.ReservationToken{} -> [reservation_token]
end ++ [
%Attribute.XORMappedAddress{
family: family(a),
address: a,
port: p
},
%Attribute.XORRelayedAddress{
family: family(addr),
address: addr,
port: port
},
%Attribute.Lifetime{
duration: lifetime
},
%Attribute.XORMappedAddress{
family: family(a),
address: a,
port: p
}
]
{%{params | attributes: attrs}, turn_state}
Expand Down Expand Up @@ -141,7 +141,7 @@ defmodule MongooseICE.Evaluator.Allocate.Request do
open_this_relay(params, new_state, server)
end
{_, {:error, reason}} ->
Logger.warn(":gen_udp.open/2 error: #{reason}, port: 0, opts: #{opts}")
Logger.warn(":gen_udp.open/2 error: #{inspect(reason)}, port: 0, opts: #{inspect(opts)}")
{:error, ErrorCode.new(:insufficient_capacity)}
end
end
Expand Down Expand Up @@ -175,8 +175,8 @@ defmodule MongooseICE.Evaluator.Allocate.Request do
r.token
end

defp udp_opts(server) do
[:binary, active: UDP.Worker.burst_length(), ip: server[:relay_ip]]
defp udp_opts(_server) do
[:binary, active: UDP.Worker.burst_length(), ip: {0, 0, 0, 0}]
end

defp verify_existing_allocation(params, state, client, server, turn_state) do
Expand Down
17 changes: 16 additions & 1 deletion lib/mongooseice/stun.ex
Expand Up @@ -8,6 +8,7 @@ defmodule MongooseICE.STUN do
alias MongooseICE.Auth
alias Jerboa.Params
alias Jerboa.ChannelData
alias Jerboa.Format.Body.Attribute.{Username, Realm}

@doc """
This function implements phase 1 of message processing which is encoding/decoding.
Expand All @@ -20,7 +21,7 @@ defmodule MongooseICE.STUN do
with secret = Auth.get_secret(),
{:ok, %Params{} = params} <- Jerboa.Format.decode(data, [secret: secret]),
{%Params{} = resp, new_turn_state} <- Evaluator.service(params, client, server, turn_state) do
{:ok, {Jerboa.Format.encode(resp), new_turn_state}}
{:ok, {Jerboa.Format.encode(resp, msg_integrity_opts(params)), new_turn_state}}
else
{:ok, %ChannelData{} = channel_data} ->
process_channel_data(channel_data, turn_state)
Expand All @@ -37,4 +38,18 @@ defmodule MongooseICE.STUN do
{:ok, {:void, new_turn_state}}
end

defp msg_integrity_opts(params) do
maybe_username =
case Params.get_attr(params, Username) do
%Username{value: username} -> username
_ -> nil
end
maybe_realm =
case Params.get_attr(params, Realm) do
%Realm{value: realm} -> realm
_ -> nil
end
[secret: Auth.get_secret(), realm: maybe_realm, username: maybe_username]
end

end
7 changes: 3 additions & 4 deletions mix.exs
Expand Up @@ -3,7 +3,7 @@ defmodule MongooseICE.Mixfile do

def project do
[app: :mongooseice,
version: "0.4.1",
version: "0.4.2",
name: "MongooseICE",
description: "STUN/TURN server",
source_url: "https://github.com/esl/mongooseice",
Expand All @@ -30,14 +30,13 @@ defmodule MongooseICE.Mixfile do

defp deps do
[{:confex, "~> 3.4.0"},
{:mix_docker, "~> 0.5.0", runtime: false},
{:ex_doc, "~> 0.14", runtime: false, only: :dev},
{:credo, "~> 1.0", runtime: false, only: :dev},
{:dialyxir, "~> 1.0-pre", runtime: false, only: :dev},
{:excoveralls, "~> 0.5", runtime: false, only: :test},
{:excoveralls, "~> 0.11", runtime: false, only: :test},
{:inch_ex, "~> 0.5", runtime: false, only: :dev},
{:mock, "~> 0.3.3", only: :test},
{:distillery, "~> 2.0", override: true},
{:distillery, "~> 2.1"},
{:jerboa, "~> 0.3"}]
end

Expand Down

0 comments on commit c5f9ee7

Please sign in to comment.