Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ RUN addgroup --gid 11000 -S asteroid && adduser --uid 11000 -S asteroid -G aster
&& mkdir /home/asteroid/dist \
&& chown -R asteroid:asteroid /home/asteroid

# Tell docker that all future commands should run as the asteroid user
USER asteroid

# Move to build directory
WORKDIR /home/asteroid/build

Expand All @@ -30,7 +27,11 @@ COPY . .
COPY pkg/config/asteroid_example.yaml /home/asteroid/.asteroid.yaml

# Download dependencies using go mod
RUN go mod download
RUN go mod vendor \
&& go mod download

# Tell docker that all future commands should run as the asteroid user
USER asteroid

# Build the application with specific ENV
RUN GOOS=${GOOS} GOARCH=${GOARCH} go build -o asteroid ./cmd/asteroid
Expand Down
127 changes: 75 additions & 52 deletions cmd/asteroid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"

"github.com/dailymotion/asteroid/pkg/db"
"github.com/dailymotion/asteroid/pkg/network"
"github.com/dailymotion/asteroid/pkg/peer"
"github.com/dailymotion/asteroid/pkg/tools"
Expand All @@ -14,22 +15,28 @@ import (
func main() {
var err error

// Init wireguard to keep all the values in one place
// Init Wireguard to keep all the values in one place
wireguard, err := tools.InitWG(os.Args)
if err != nil {
log.Printf("\nError parsing flags: %v\n", err)
log.Printf("\nError initializing Wireguard: %v\n", err)
os.Exit(1)
}

// Init DB connection
DBConn, DBConf, err := tools.InitDB()
if err != nil {
log.Printf("\nError initializing DB: %v\n", err)
os.Exit(1)
}

// Check which command has been given
switch os.Args[1] {
case "add":

// Checking if enough arguments have been given for the command
if len(os.Args) <= 5 {
log.Printf("Missing Arguments\n")
//addFlag.Usage()
os.Exit(2)
// Checking if Arguments are correct
err := tools.CheckArguments(os.Args, "add")
if err != nil {
log.Fatalln(err)
}

// Check if arguments are empty or haven't all necessary requirements
Expand All @@ -39,33 +46,35 @@ func main() {
}

// Connect to the server and get the connection
conn, err := network.ConnectAndRetrieve(wireguard, "add")
conn, err := network.ConnectAndRetrieve(&wireguard, "add")
if err != nil {
log.Fatalf("\nerror: %v\n", err)
}

// Add new Peer to the server
if err := peer.AddNewPeer(conn, wireguard); err != nil {
log.Fatalf("error: %v\n", err)
} else {
// Message be like:
//################
//# Peer added ! #
//################
fmt.Printf("\n################\n# Peer added ! #\n################\n\n")
// We retrieve all the peer vpn ip to show the new added peer
tmpListPeers, serverPubKey, err := network.RetrieveIPs(conn)
if err != nil {
log.Fatal(err)
}

// We retrieve all the peer vpn ip to show the new added peer
listPeers, serverPubKey, err := network.RetrieveIPs(conn)
// Retrieving peers from server and checking if the one given already exist on it
err = tools.RetrieveAndCheckForDouble(DBConn, DBConf, &wireguard, tmpListPeers, serverPubKey)
if err != nil {
fmt.Println("error: ", err)
os.Exit(1)
log.Fatal(err)
}

// Adding server public key to the wireguard object
wireguard.ServerPubKey = serverPubKey
// Add new Peer to the Wireguard server
if err := peer.AddNewPeer(conn, wireguard); err != nil {
log.Fatalf("error: %v\n", err)
} else {
tools.PrintResult("add", wireguard.PeerKey)
}

// Retrieving and match peer with key and cidr into a list of map
listPeers, err := network.RetrieveAndMatchPeer(conn, DBConn)

//fmt.Printf("\n\nPeers informations\n-------------------\n")
// Showing all the Peers in a nice ASCII table
fmt.Printf("\n\nPeers informations\n-------------------\n")
network.ShowListIPs(listPeers)

// We check that one of the flag is true
Expand All @@ -77,59 +86,73 @@ func main() {
}
case "view":
flag.Parse()
// We alert if too much arguments are given to the command
if len(os.Args) > 2 {
fmt.Printf("View doesn't take options\n\n")
flag.Usage()
os.Exit(2)
}

// Connect to the server and get the connection
conn, err := network.ConnectAndRetrieve(wireguard, "view")
// Checking if Arguments are correct
err := tools.CheckArguments(os.Args, "view")
if err != nil {
log.Fatalf("\nerror: %v\n", err)
log.Fatalln(err)
}

listPeers, _, err := network.RetrieveIPs(conn)
//Connect to the server and get the connection
conn, err := network.ConnectAndRetrieve(&wireguard, "view")
if err != nil {
log.Fatalf("\nerror: %v\n", err)
}

// Retrieving and match peer with key and cidr into a list of map
listPeers, err := network.RetrieveAndMatchPeer(conn, DBConn)

fmt.Printf("\n\nPeers informations\n-------------------\n")
network.ShowListIPs(listPeers)

case "delete":
if len(os.Args) < 3 {
//deleteFlag.Usage()
os.Exit(2)
// Checking if Arguments are correct
err := tools.CheckArguments(os.Args, "delete")
if err != nil {
log.Fatalln(err)
}

// Check if arguments are empty or haven't all necessary requirements
err = tools.CheckFlagValid(wireguard, "delete")
if err != nil {
fmt.Printf("Error with arguments: %v\n", err)
//deleteFlag.Usage()
os.Exit(2)
log.Fatalf("\nerror with arguments: %v\n", err)
}
conn, err := network.ConnectAndRetrieve(wireguard, "delete")

// Connect to the server and get the connection
conn, err := network.ConnectAndRetrieve(&wireguard, "delete")
if err != nil {
log.Fatalf("\nerror: %v\n", err)
}
if err = peer.DeletePeer(conn, wireguard.PeerDeleteKey); err != nil {
log.Fatalf("error: %v\n", err)

// Retrieving and match peer with key and cidr into a list of map
listPeers, err := network.RetrieveAndMatchPeer(conn, DBConn)

// Checks if peer is present on the server
ok := tools.CheckIfPresent(listPeers, wireguard.PeerDeleteKey)
if ok {
// We use the connection to delete the peer on the Wireguard server
if err = peer.DeletePeer(conn, wireguard.PeerDeleteKey); err != nil {
log.Fatalf("error: %v\n", err)
} else {
tools.PrintResult("delete", wireguard.PeerDeleteKey)
}
} else {
// Message be like:
//##################
//# Peer deleted ! #
//##################
fmt.Printf("\n##################\n# Peer deleted ! #\n##################\n")
fmt.Printf("Peer %v has been deleted !\n\n", wireguard.PeerDeleteKey)
log.Fatal("key not found on the server")
}

listPeers, _, err := network.RetrieveIPs(conn)
if err != nil {
log.Fatalf("error: %v\n", err)
// If DB is enabled we delete the peer on the database too
if DBConf.DBEnabled {
err = db.DeleteUserInDB(DBConn, wireguard.PeerDeleteKey)
if err != nil {
log.Fatalf("\nerror: %v\n", err)
}
}

// Retrieve peers after deletion to make sure it has been deleted
listPeers, err = network.RetrieveAndMatchPeer(conn, DBConn)

// Showing all the Peers in a nice ASCII table
fmt.Printf("\n\nPeers informations\n-------------------\n")
network.ShowListIPs(listPeers)

case "-h", "--help":
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/briandowns/spinner v1.11.1
github.com/lib/pq v1.9.0
github.com/olekukonko/tablewriter v0.0.4
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/briandowns/spinner v1.11.1 h1:OixPqDEcX3juo5AjQZAnFPbeUA0jvkp2qzB5gOZ
github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/lib/pq v1.9.0 h1:L8nSXQQzAYByakOFMTwpjRoHsMJklur4Gi59b6VivR8=
github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/asteroid_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ client_config_file:
name: wg0.conf # Name of the client config file to be created
dns: 9.9.9.9 # DNS with which the WG tunnel will used
allowed_ips: 0.0.0.0/0 # List of addresses that will get routed through the tunnel
# ----------------------------------

# Database configuration ----------
db:
enabled: true # Enable the use of the database
host: postgres # Database Host, ex: localhost/192.168.1.5
port: 5432
username: postgres # Username to access db
password: password # Username password
dbname: asteroid # Database name
# ----------------------------------
32 changes: 30 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/user"
"reflect"
"strings"
"time"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
Expand All @@ -17,7 +18,8 @@ const FILENAME = ".asteroid.yaml"
// Config regroup the Wireguard and Client config
type Config struct {
WG Wireguard `yaml:"wireguard"`
ClientConfig ClientConfig `yaml:"client_config_file"`
ClientConfig ClientConfig `yaml:"client_config_file"`
DB ConfigDB `yaml:"db"`
}

// Wireguard regroup all the field needed for WG to works properly
Expand All @@ -30,13 +32,30 @@ type Wireguard struct {
WGPort string `yaml:"wg_port"`
}

// ClientConfig regroup the few fields necessarily to generate WG client config
// ClientConfig regroup the few necessarily fields to generate WG client config
type ClientConfig struct {
Name string `yaml:"name"`
DNS string `yaml:"dns"`
AllowedIPs string `yaml:"allowed_ips"`
}

// Database regroup the fielsds to connect to Postgresql DB
type ConfigDB struct {
DBEnabled bool `yaml:"enabled"`
DbHost string `yaml:"host"`
DbPort int `yaml:"port"`
DbUsername string `yaml:"username"`
DbPassword string `yaml:"password"`
DbName string `yaml:"dbname"`
}

type User struct {
Username string `json:"username"`
Key string `json:"key"`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can use golint to avoid this kind of thing ;)

CIDR string `json:"cidr"`
Date time.Time `json:"time"`
}

func isStructNil(config Config) ([]string, bool) {
e := reflect.ValueOf(&config).Elem()
num := e.NumField()
Expand Down Expand Up @@ -104,3 +123,12 @@ func ReadConfigFile() (Config, error) {

return configWG, nil
}

// RetrieveDBConfig read the config file and add the DB values in the struct
func RetrieveDBConfig() (ConfigDB, error) {
conf, err := ReadConfigFile()
if err != nil {
return conf.DB, err
}
return conf.DB, err
}
Loading