Skip to content

Commit

Permalink
Merge pull request #86 from MysteriumNetwork/feature/MYST-27-broker-o…
Browse files Browse the repository at this point in the history
…n-public-domain

Feature/myst 27 broker on public domain
  • Loading branch information
zolia committed Jan 8, 2018
2 parents 1c0d0c4 + 1345cec commit 25c671b
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 42 deletions.
12 changes: 9 additions & 3 deletions bin/client_docker/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ RUN go get github.com/debber/debber-v0.3/cmd/debber
# Compile application
WORKDIR /go/src/github.com/mysterium/node
ADD . .
RUN GOOS=linux GOARCH=amd64 bin/server_build \
&& bin/server_package_debian ${PACKAGE_VERSION} amd64
RUN GOOS=linux GOARCH=amd64 bin/client_build \
&& bin/client_package_debian ${PACKAGE_VERSION} amd64



Expand All @@ -24,9 +24,15 @@ MAINTAINER Valdas Petrulis <petrulis.valdas@gmail.com>

# Install packages
COPY --from=builder /go/src/github.com/mysterium/node/build/package/mysterium-client_linux_amd64.deb /tmp/mysterium-client.deb

RUN apt-get update \
&& apt-get install -y curl \
&& curl -s https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add \
&& echo "deb http://build.openvpn.net/debian/openvpn/stable xenial main" > /etc/apt/sources.list.d/openvpn-aptrepo.list \
&& apt-get update \
&& apt-get install -y openvpn ca-certificates iptables \
&& dpkg --install --force-depends /tmp/mysterium-client.deb \
&& apt-get install -y --fix-broken \
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/* /tmp/mysterium-client.deb

ENTRYPOINT ["/usr/bin/mysterium_client", "--node=${NODE}"]
ENTRYPOINT ["/usr/bin/mysterium_client"]
2 changes: 1 addition & 1 deletion bin/server_docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ if [ ! -d "$OS_DIR_RUN" ]; then
mkdir -p $OS_DIR_RUN
fi

exec /usr/bin/mysterium_server --config-dir=$OS_DIR_CONFIG --runtime-dir=$OS_DIR_RUN --node=$NODE
exec /usr/bin/mysterium_server --config-dir=$OS_DIR_CONFIG --runtime-dir=$OS_DIR_RUN
6 changes: 6 additions & 0 deletions bin/server_docker/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ MAINTAINER Valdas Petrulis <petrulis.valdas@gmail.com>

# Install packages
COPY --from=builder /go/src/github.com/mysterium/node/build/package/mysterium-node_linux_amd64.deb /tmp/mysterium-node.deb

RUN apt-get update \
&& apt-get install -y curl \
&& curl -s https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add \
&& echo "deb http://build.openvpn.net/debian/openvpn/stable xenial main" > /etc/apt/sources.list.d/openvpn-aptrepo.list \
&& apt-get update \
&& apt-get install -y openvpn ca-certificates iptables \
&& dpkg --install --force-depends /tmp/mysterium-node.deb \
&& apt-get install -y --fix-broken \
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/* /tmp/mysterium-node.deb
Expand Down
2 changes: 1 addition & 1 deletion cmd/mysterium_server/command_run/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewCommandWith(
mysteriumClient: mysteriumClient,
natService: natService,
dialogWaiterFactory: func(identity identity.Identity) (communication.DialogWaiter, dto_discovery.Contact) {
address := nats_discovery.NewAddressForIdentity(identity)
address := nats_discovery.NewAddressGenerate(identity)
return nats_dialog.NewDialogWaiter(address), address.GetContact()
},
sessionManagerFactory: func(vpnServerIp string) session.ManagerInterface {
Expand Down
2 changes: 1 addition & 1 deletion communication/nats_dialog/dialog_establisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package nats_dialog

import (
"fmt"
"github.com/mgutz/logxi/v1"
log "github.com/cihub/seelog"
"github.com/mysterium/node/communication"
"github.com/mysterium/node/communication/nats"
"github.com/mysterium/node/communication/nats_discovery"
Expand Down
2 changes: 1 addition & 1 deletion communication/nats_dialog/dialog_waiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestDialogWaiter_Interface(t *testing.T) {
}

func TestDialogWaiter_Factory(t *testing.T) {
address := nats_discovery.NewAddress("127.0.0.1", 4222, "custom")
address := nats_discovery.NewAddress("custom", "nats://127.0.0.1:4222",)
waiter := NewDialogWaiter(address)

assert.NotNil(t, waiter)
Expand Down
17 changes: 11 additions & 6 deletions communication/nats_discovery/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import (

var natsServerIp string

func NewAddress(server string, port int, topic string) *NatsAddress {
func NewAddress(topic string, address string) *NatsAddress {
return &NatsAddress{
servers: []string{
fmt.Sprintf("nats://%s:%d", server, port),
address,
},
topic: topic,
}
}

func NewAddressForIdentity(identity identity.Identity) *NatsAddress {
return NewAddress(natsServerIp, 4222, identity.Address)
func NewAddressGenerate(identity identity.Identity) *NatsAddress {
address := "nats://" + natsServerIp + ":4222"
return NewAddress(identity.Address, address)
}

func NewAddressForContact(contact dto_discovery.Contact) (*NatsAddress, error) {
Expand All @@ -33,7 +34,10 @@ func NewAddressForContact(contact dto_discovery.Contact) (*NatsAddress, error) {
return nil, fmt.Errorf("Invalid contact definition: %#v", contact.Definition)
}

return NewAddress(natsServerIp, 4222, contactNats.Topic), nil
return &NatsAddress{
servers: contactNats.BrokerAddresses,
topic: contactNats.Topic,
}, nil
}

func NewAddressWithConnection(connection nats.Connection, topic string) *NatsAddress {
Expand Down Expand Up @@ -74,7 +78,8 @@ func (address *NatsAddress) GetContact() dto_discovery.Contact {
return dto_discovery.Contact{
Type: CONTACT_NATS_V1,
Definition: ContactNATSV1{
Topic: address.topic,
Topic: address.topic,
BrokerAddresses: address.servers,
},
}
}
19 changes: 12 additions & 7 deletions communication/nats_discovery/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
)

func TestNewAddress(t *testing.T) {
address := NewAddress("far-server", 1234, "topic")
address := NewAddress("topic1234", "nats://far-server:1234")

assert.Equal(
t,
&NatsAddress{
servers: []string{"nats://far-server:1234"},
topic: "topic",
topic: "topic1234",
},
address,
)
}

func TestNewAddressForIdentity(t *testing.T) {
identity := identity.FromAddress("provider1")
address := NewAddressForIdentity(identity)
address := NewAddressGenerate(identity)

assert.Equal(
t,
Expand All @@ -39,15 +39,16 @@ func TestNewAddressForContact(t *testing.T) {
address, err := NewAddressForContact(dto_discovery.Contact{
Type: "nats/v1",
Definition: ContactNATSV1{
Topic: "123456",
Topic: "123456",
BrokerAddresses: []string{"nats://far-server:4222"},
},
})

assert.NoError(t, err)
assert.Equal(
t,
&NatsAddress{
servers: []string{"nats://" + natsServerIp + ":4222"},
servers: []string{"nats://far-server:4222"},
topic: "123456",
},
address,
Expand Down Expand Up @@ -89,14 +90,18 @@ func TestAddress_GetTopic(t *testing.T) {
}

func TestAddress_GetContact(t *testing.T) {
address := &NatsAddress{topic: "123456"}
address := &NatsAddress{
servers: []string{"nats://far-server:4222"},
topic: "123456",
}

assert.Equal(
t,
dto_discovery.Contact{
Type: "nats/v1",
Definition: ContactNATSV1{
Topic: "123456",
Topic: "123456",
BrokerAddresses: []string{"nats://far-server:4222"},
},
},
address.GetContact(),
Expand Down
4 changes: 3 additions & 1 deletion communication/nats_discovery/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ const CONTACT_NATS_V1 = "nats/v1"

type ContactNATSV1 struct {
// Topic on which client is getting message
Topic string
Topic string `json:"topic"`
// NATS servers used by node and should be contacted via
BrokerAddresses []string `json:"broker_addresses"`
}
85 changes: 85 additions & 0 deletions communication/nats_discovery/contact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nats_discovery

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)

func TestContactSerialize(t *testing.T) {
var tests = []struct {
model ContactNATSV1
expectedJson string
}{
{
ContactNATSV1{
Topic: "topic1234",
BrokerAddresses: []string{"nats://far-server:4222"},
},
`{
"topic": "topic1234",
"broker_addresses":["nats://far-server:4222"]
}`,
},
{
ContactNATSV1{
Topic: "topic1234",
BrokerAddresses: []string{"nats://far-server1:4222", "nats://far-server2:4222"},
},
`{
"topic": "topic1234",
"broker_addresses":["nats://far-server1:4222", "nats://far-server2:4222"]
}`,
},
}

for _, test := range tests {
jsonBytes, err := json.Marshal(test.model)

assert.Nil(t, err)
assert.JSONEq(t, test.expectedJson, string(jsonBytes))
}
}

func TestLocationUnserialize(t *testing.T) {
var tests = []struct {
json string
expectedModel ContactNATSV1
expectedError error
}{
{
`{
"topic": "topic1234",
"broker_addresses":["nats://far-server1:4222"]
}`,
ContactNATSV1{
Topic: "topic1234",
BrokerAddresses: []string{"nats://far-server1:4222"},
},
nil,
},
{
`{
"topic": "topic1234",
"broker_addresses":["nats://far-server1:4222", "nats://far-server2:4222"]
}`,
ContactNATSV1{
Topic: "topic1234",
BrokerAddresses: []string{"nats://far-server1:4222", "nats://far-server2:4222"},
},
nil,
},
}

for _, test := range tests {
var model ContactNATSV1
err := json.Unmarshal([]byte(test.json), &model)

assert.Equal(t, test.expectedModel, model)
if test.expectedError != nil {
assert.EqualError(t, err, test.expectedError.Error())
} else {
assert.NoError(t, err)
}
}
}
21 changes: 5 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '2'
services:

broker:
tty: true
image: nats
container_name: mysterium-broker
networks:
Expand All @@ -11,6 +12,8 @@ services:
- 8222:8222

server-ubuntu:
privileged: true
tty: true
build:
context: .
dockerfile: bin/server_docker/ubuntu/Dockerfile
Expand All @@ -29,22 +32,9 @@ services:
environment:
NODE: server-ubuntu

server-alpine:
build:
context: .
dockerfile: bin/server_docker/alpine/Dockerfile
container_name: mysterium-server-alpine
depends_on:
- broker
cap_add:
- MKNOD
- NET_ADMIN
networks:
- default
environment:
NODE: server-alpine

client:
privileged: true
tty: true
build:
context: .
dockerfile: bin/client_docker/ubuntu/Dockerfile
Expand All @@ -54,7 +44,6 @@ services:
depends_on:
- broker
- server-ubuntu
- server-alpine
networks:
- default
environment:
Expand Down
8 changes: 3 additions & 5 deletions nat/service_iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ func (service *serviceIpTables) Add(rule RuleForwarding) {
}

func (service *serviceIpTables) Start() error {
if err := service.clearStaleRules(); err != nil {
return err
}
service.clearStaleRules()

if err := service.enableIPForwarding(); err != nil {
return err
Expand Down Expand Up @@ -102,6 +100,6 @@ func (service *serviceIpTables) disableRules() error {
return nil
}

func (service *serviceIpTables) clearStaleRules() error {
return service.disableRules()
func (service *serviceIpTables) clearStaleRules() {
service.disableRules()
}

0 comments on commit 25c671b

Please sign in to comment.