Skip to content

Commit

Permalink
New discovery protocol over HTTPS
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Sep 20, 2015
1 parent e11b3dd commit 8687657
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 169 deletions.
48 changes: 30 additions & 18 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/bin/bash
set -euo pipefail
set nullglob

echo Get dependencies
go get -d -tags purego

rm -rf discosrv-*-*

build() {
export GOOS="$1"
Expand All @@ -25,15 +19,33 @@ build() {
fi
}

for goos in linux darwin windows freebsd openbsd netbsd solaris ; do
build "$goos" amd64
done
for goos in linux windows freebsd openbsd netbsd ; do
build "$goos" 386
done
build linux arm

# Hack used because we run as root under Docker
if [[ ${CHOWN_USER:-} != "" ]] ; then
chown -R $CHOWN_USER .
fi
buildpkg() {
echo Get dependencies
go get -d -tags purego

rm -rf discosrv-*-*

for goos in linux darwin windows freebsd openbsd netbsd solaris ; do
build "$goos" amd64
done
for goos in linux windows freebsd openbsd netbsd ; do
build "$goos" 386
done
build linux arm

# Hack used because we run as root under Docker
if [[ ${CHOWN_USER:-} != "" ]] ; then
chown -R $CHOWN_USER .
fi
}

case "${1:-default}" in
pkg)
buildpkg
;;

default)
go install -v
;;
esac

19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
package main

import (
"crypto/tls"
"database/sql"
"flag"
"log"
"net"
"os"
"time"

Expand All @@ -21,6 +21,9 @@ var (
statsFile string
backend = "ql"
dsn = getEnvDefault("DISCOSRV_DB_DSN", "memory://discosrv")
certFile = "cert.pem"
keyFile = "key.pem"
debug = false
)

func main() {
Expand All @@ -34,18 +37,23 @@ func main() {
log.SetOutput(os.Stdout)
log.SetFlags(0)

flag.StringVar(&listen, "listen", ":22027", "Listen address")
flag.StringVar(&listen, "listen", ":8443", "Listen address")
flag.IntVar(&lruSize, "limit-cache", lruSize, "Limiter cache entries")
flag.IntVar(&limitAvg, "limit-avg", limitAvg, "Allowed average package rate, per 10 s")
flag.IntVar(&limitBurst, "limit-burst", limitBurst, "Allowed burst size, packets")
flag.StringVar(&statsFile, "stats-file", statsFile, "File to write periodic operation stats to")
flag.StringVar(&backend, "db-backend", backend, "Database backend to use")
flag.StringVar(&dsn, "db-dsn", dsn, "Database DSN")
flag.StringVar(&certFile, "cert", certFile, "Certificate file")
flag.StringVar(&keyFile, "key", keyFile, "Key file")
flag.BoolVar(&debug, "debug", debug, "Debug")
flag.Parse()

addr, _ := net.ResolveUDPAddr("udp", listen)
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatalln("Failed to load X509 key pair:", err)
}

var err error
db, err := sql.Open(backend, dsn)
if err != nil {
log.Fatalln("sql.Open:", err)
Expand All @@ -58,7 +66,8 @@ func main() {
main := suture.NewSimple("main")

main.Add(&querysrv{
addr: addr,
addr: listen,
cert: cert,
db: db,
prep: prep,
})
Expand Down
6 changes: 3 additions & 3 deletions psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func postgresCompile(db *sql.DB) (map[string]*sql.Stmt, error) {
"insertAddress": "INSERT INTO Addresses (DeviceID, Seen, Address) VALUES ($1, now(), $2)",
"insertRelay": "INSERT INTO Relays (DeviceID, Seen, Address, Latency) VALUES ($1, now(), $2, $3)",
"insertDevice": "INSERT INTO Devices (DeviceID, Seen) VALUES ($1, now())",
"selectAddress": "SELECT Address from Addresses WHERE DeviceID=$1 AND Seen > now() - '1 hour'::INTERVAL ORDER BY random() LIMIT 16",
"selectRelay": "SELECT Address, Latency from Relays WHERE DeviceID=$1 AND Seen > now() - '1 hour'::INTERVAL ORDER BY random() LIMIT 16",
"updateRelay": "UPDATE Relays SET Seen=now(), Latency=$3 WHERE DeviceID=$1 AND Address=$2",
"selectAddress": "SELECT Address FROM Addresses WHERE DeviceID=$1 AND Seen > now() - '1 hour'::INTERVAL ORDER BY random() LIMIT 16",
"selectRelay": "SELECT Address, Latency FROM Relays WHERE DeviceID=$1 AND Seen > now() - '1 hour'::INTERVAL ORDER BY random() LIMIT 16",
"updateAddress": "UPDATE Addresses SET Seen=now() WHERE DeviceID=$1 AND Address=$2",
"updateDevice": "UPDATE Devices SET Seen=now() WHERE DeviceID=$1",
"deleteRelay": "DELETE FROM Relays WHERE DeviceID=$1",
}
Expand Down
2 changes: 1 addition & 1 deletion ql.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func qlCompile(db *sql.DB) (map[string]*sql.Stmt, error) {
"insertDevice": "INSERT INTO Devices (DeviceID, Seen) VALUES ($1, now())",
"selectAddress": `SELECT Address from Addresses WHERE DeviceID==$1 AND Seen > now() - duration("1h") LIMIT 16`,
"selectRelay": `SELECT Address, Latency from Relays WHERE DeviceID==$1 AND Seen > now() - duration("1h") LIMIT 16`,
"updateAddress": "UPDATE Addresses Seen=now()WHERE DeviceID==$1 AND Address==$2",
"updateAddress": "UPDATE Addresses Seen=now() WHERE DeviceID==$1 AND Address==$2",
"updateDevice": "UPDATE Devices Seen=now() WHERE DeviceID==$1",
"deleteRelay": "DELETE FROM Relays WHERE DeviceID==$1",
}
Expand Down
Loading

3 comments on commit 8687657

@maklaut
Copy link

Choose a reason for hiding this comment

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

@calmh
What about docs?
Please update the docs page.
Thanks!

@calmh
Copy link
Member Author

@calmh calmh commented on 8687657 Oct 30, 2015

Choose a reason for hiding this comment

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

@maklaut It is documented. http://docs.syncthing.net/specs/globaldisco-v3.html
Discosrv docs will be updated at some point near the release.

@maklaut
Copy link

Choose a reason for hiding this comment

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

@calmh, yes I mean user's manual, not developer.
Thanks!

Please # to comment.