Skip to content

Commit 2399bff

Browse files
committed
mvp baby
1 parent c9aa830 commit 2399bff

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

cmd/apiserver/main.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
_ "github.com/lib/pq"
10+
)
11+
12+
type Client struct {
13+
PSK string `json:"psk"`
14+
Peer
15+
}
16+
17+
type Peer struct {
18+
PublicKey string `json:"public_key"`
19+
IP string `json:"ip"`
20+
}
21+
22+
type GatewayResponse struct {
23+
Clients []Client `json:"clients"`
24+
}
25+
26+
func main() {
27+
http.HandleFunc("/gateways/gw0", func(w http.ResponseWriter, _ *http.Request) {
28+
postgresConnection := fmt.Sprintf("user=%s password=%s dbname=%s host=%s sslmode=disable",
29+
"postgres",
30+
"asdf",
31+
"postgres",
32+
"localhost")
33+
34+
db, err := sql.Open("postgres", postgresConnection)
35+
if err != nil {
36+
panic(fmt.Sprintf("failed to connect to database, error was: %s", err))
37+
}
38+
39+
rows, err := db.Query(`
40+
SELECT public_key, ip, psk from peer
41+
JOIN client c on peer.id = c.peer_id
42+
JOIN ip i on peer.id = i.peer_id
43+
`)
44+
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
var resp GatewayResponse
50+
51+
for rows.Next() {
52+
var client Client
53+
54+
err := rows.Scan(&client.PublicKey, &client.IP, &client.PSK)
55+
56+
if err != nil {
57+
panic(err)
58+
}
59+
60+
resp.Clients = append(resp.Clients, client)
61+
}
62+
63+
w.WriteHeader(http.StatusOK)
64+
json.NewEncoder(w).Encode(resp)
65+
})
66+
67+
bindAddr := fmt.Sprintf("%s:%d", "127.0.0.1", 6969)
68+
fmt.Println("running @", bindAddr)
69+
fmt.Println((&http.Server{Addr: bindAddr}).ListenAndServe())
70+
}

db-bootstrap.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
INSERT INTO public.client (peer_id, serial, psk, healthy, last_check) VALUES (1, 'serial', 'pskkk', true, '2020-03-26 12:26:32.000000');
2+
INSERT INTO public.gateway (peer_id, id, access_group_id, endpoint) VALUES (2, 1, '1234-asdf-aad', '1.2.3.4:1234');
3+
INSERT INTO public.ip (peer_id, ip) VALUES (1, '10.1.1.1');
4+
INSERT INTO public.ip (peer_id, ip) VALUES (2, '10.1.1.2');
5+
INSERT INTO public.peer (id, public_key, kind) VALUES (1, 'publickey', 'client');
6+
INSERT INTO public.peer (id, public_key, kind) VALUES (2, 'pk-gw', 'gateway');
7+
INSERT INTO public.routes (gateway_id, cidr) VALUES (1, '1.2.3.4/23');

model.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CREATE TABLE peer (
22
id serial PRIMARY KEY,
33
public_key varchar(44) NOT NULL UNIQUE,
4-
kind integer NOT NULL
4+
kind varchar(7) NOT NULL
55
);
66

77
CREATE TABLE client (

setup-gcp.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
gcloud beta compute --project=nais-device instances create gateway-1 --zone=europe-north1-a --machine-type=f1-micro --tags=wireguard-gateway --image=ubuntu-1804-bionic-v20200317 --image-project=ubuntu-os-cloud
2+
3+
gcloud compute --project=nais-device firewall-rules create allow-wireguard --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=udp:51820 --source-ranges=0.0.0.0/0 --target-tags=wireguard-gateway # wireguard firewall
4+

0 commit comments

Comments
 (0)