2014-06-01 13:50:14 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2013-12-22 19:35:05 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-02-20 09:40:15 -07:00
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"flag"
|
2014-04-19 14:14:56 -07:00
|
|
|
"fmt"
|
2014-05-21 23:46:19 -07:00
|
|
|
"io"
|
2013-12-22 19:35:05 -07:00
|
|
|
"log"
|
|
|
|
"net"
|
2014-02-20 09:40:15 -07:00
|
|
|
"os"
|
2013-12-22 19:35:05 -07:00
|
|
|
"sync"
|
2014-02-17 01:23:37 -07:00
|
|
|
"time"
|
2013-12-22 19:35:05 -07:00
|
|
|
|
|
|
|
"github.com/calmh/syncthing/discover"
|
2014-04-03 14:38:32 -07:00
|
|
|
"github.com/golang/groupcache/lru"
|
|
|
|
"github.com/juju/ratelimit"
|
2013-12-22 19:35:05 -07:00
|
|
|
)
|
|
|
|
|
2014-05-21 23:46:19 -07:00
|
|
|
type node struct {
|
|
|
|
addresses []address
|
|
|
|
updated time.Time
|
2014-02-20 09:40:15 -07:00
|
|
|
}
|
|
|
|
|
2014-05-21 23:46:19 -07:00
|
|
|
type address struct {
|
|
|
|
ip []byte
|
|
|
|
port uint16
|
2013-12-22 19:35:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2014-06-27 13:39:03 -07:00
|
|
|
nodes = make(map[string]node)
|
|
|
|
lock sync.Mutex
|
|
|
|
queries = 0
|
|
|
|
announces = 0
|
|
|
|
answered = 0
|
|
|
|
limited = 0
|
|
|
|
unknowns = 0
|
|
|
|
debug = false
|
|
|
|
lruSize = 1024
|
|
|
|
limitAvg = 1
|
|
|
|
limitBurst = 10
|
|
|
|
limiter *lru.Cache
|
2013-12-22 19:35:05 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-02-20 09:40:15 -07:00
|
|
|
var listen string
|
|
|
|
var timestamp bool
|
2014-04-19 14:14:56 -07:00
|
|
|
var statsIntv int
|
|
|
|
var statsFile string
|
2014-02-20 09:40:15 -07:00
|
|
|
|
|
|
|
flag.StringVar(&listen, "listen", ":22025", "Listen address")
|
|
|
|
flag.BoolVar(&debug, "debug", false, "Enable debug output")
|
|
|
|
flag.BoolVar(×tamp, "timestamp", true, "Timestamp the log output")
|
2014-04-19 14:14:56 -07:00
|
|
|
flag.IntVar(&statsIntv, "stats-intv", 0, "Statistics output interval (s)")
|
|
|
|
flag.StringVar(&statsFile, "stats-file", "/var/log/discosrv.stats", "Statistics file name")
|
2014-06-27 13:39:03 -07:00
|
|
|
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")
|
2014-02-20 09:40:15 -07:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-06-27 13:39:03 -07:00
|
|
|
limiter = lru.New(lruSize)
|
|
|
|
|
2014-02-20 09:40:15 -07:00
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
if !timestamp {
|
|
|
|
log.SetFlags(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, _ := net.ResolveUDPAddr("udp", listen)
|
2013-12-22 19:35:05 -07:00
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
2014-04-03 13:44:40 -07:00
|
|
|
log.Fatal(err)
|
2013-12-22 19:35:05 -07:00
|
|
|
}
|
|
|
|
|
2014-04-19 14:14:56 -07:00
|
|
|
if statsIntv > 0 {
|
|
|
|
go logStats(statsFile, statsIntv)
|
|
|
|
}
|
2014-02-17 01:23:37 -07:00
|
|
|
|
2013-12-22 19:35:05 -07:00
|
|
|
var buf = make([]byte, 1024)
|
|
|
|
for {
|
2014-02-20 09:40:15 -07:00
|
|
|
buf = buf[:cap(buf)]
|
2013-12-22 19:35:05 -07:00
|
|
|
n, addr, err := conn.ReadFromUDP(buf)
|
2014-04-03 14:38:32 -07:00
|
|
|
|
|
|
|
if limit(addr) {
|
|
|
|
// Rate limit in effect for source
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-22 19:35:05 -07:00
|
|
|
if err != nil {
|
2014-04-03 13:44:40 -07:00
|
|
|
log.Fatal(err)
|
2013-12-22 19:35:05 -07:00
|
|
|
}
|
2014-04-03 14:38:32 -07:00
|
|
|
|
2014-02-20 09:40:15 -07:00
|
|
|
if n < 4 {
|
|
|
|
log.Printf("Received short packet (%d bytes)", n)
|
2013-12-22 19:35:05 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-02-20 09:40:15 -07:00
|
|
|
buf = buf[:n]
|
|
|
|
magic := binary.BigEndian.Uint32(buf)
|
|
|
|
|
|
|
|
switch magic {
|
2014-04-03 13:44:40 -07:00
|
|
|
case discover.AnnouncementMagicV2:
|
|
|
|
handleAnnounceV2(addr, buf)
|
2014-02-20 09:40:15 -07:00
|
|
|
|
2014-04-03 13:44:40 -07:00
|
|
|
case discover.QueryMagicV2:
|
|
|
|
handleQueryV2(conn, addr, buf)
|
2014-04-19 14:14:56 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
lock.Lock()
|
|
|
|
unknowns++
|
|
|
|
lock.Unlock()
|
2014-04-03 13:44:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-20 09:40:15 -07:00
|
|
|
|
2014-04-03 14:38:32 -07:00
|
|
|
func limit(addr *net.UDPAddr) bool {
|
|
|
|
key := addr.IP.String()
|
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
bkt, ok := limiter.Get(key)
|
|
|
|
if ok {
|
|
|
|
bkt := bkt.(*ratelimit.Bucket)
|
|
|
|
if bkt.TakeAvailable(1) != 1 {
|
|
|
|
// Rate limit exceeded; ignore packet
|
|
|
|
if debug {
|
2014-04-16 06:06:54 -07:00
|
|
|
log.Println("Rate limit exceeded for", key)
|
2014-04-03 14:38:32 -07:00
|
|
|
}
|
|
|
|
limited++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if debug {
|
2014-04-16 06:06:54 -07:00
|
|
|
log.Println("New limiter for", key)
|
2014-04-03 14:38:32 -07:00
|
|
|
}
|
|
|
|
// One packet per ten seconds average rate, burst ten packets
|
2014-06-27 13:39:03 -07:00
|
|
|
limiter.Add(key, ratelimit.NewBucket(10*time.Second/time.Duration(limitAvg), int64(limitBurst)))
|
2014-04-03 14:38:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-03 13:44:40 -07:00
|
|
|
func handleAnnounceV2(addr *net.UDPAddr, buf []byte) {
|
|
|
|
var pkt discover.AnnounceV2
|
|
|
|
err := pkt.UnmarshalXDR(buf)
|
2014-05-21 23:46:19 -07:00
|
|
|
if err != nil && err != io.EOF {
|
2014-04-03 13:44:40 -07:00
|
|
|
log.Println("AnnounceV2 Unmarshal:", err)
|
|
|
|
log.Println(hex.Dump(buf))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
log.Printf("<- %v %#v", addr, pkt)
|
|
|
|
}
|
|
|
|
|
2014-04-19 14:14:56 -07:00
|
|
|
lock.Lock()
|
|
|
|
announces++
|
|
|
|
lock.Unlock()
|
|
|
|
|
2014-04-03 13:44:40 -07:00
|
|
|
ip := addr.IP.To4()
|
|
|
|
if ip == nil {
|
|
|
|
ip = addr.IP.To16()
|
|
|
|
}
|
|
|
|
|
2014-05-21 23:46:19 -07:00
|
|
|
var addrs []address
|
|
|
|
for _, addr := range pkt.This.Addresses {
|
2014-04-03 13:44:40 -07:00
|
|
|
tip := addr.IP
|
|
|
|
if len(tip) == 0 {
|
|
|
|
tip = ip
|
|
|
|
}
|
2014-05-21 23:46:19 -07:00
|
|
|
addrs = append(addrs, address{
|
|
|
|
ip: tip,
|
|
|
|
port: addr.Port,
|
2014-04-03 13:44:40 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-05-21 23:46:19 -07:00
|
|
|
node := node{
|
|
|
|
addresses: addrs,
|
|
|
|
updated: time.Now(),
|
2014-04-03 13:44:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
lock.Lock()
|
2014-05-21 23:46:19 -07:00
|
|
|
nodes[pkt.This.ID] = node
|
2014-04-03 13:44:40 -07:00
|
|
|
lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleQueryV2(conn *net.UDPConn, addr *net.UDPAddr, buf []byte) {
|
|
|
|
var pkt discover.QueryV2
|
|
|
|
err := pkt.UnmarshalXDR(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("QueryV2 Unmarshal:", err)
|
|
|
|
log.Println(hex.Dump(buf))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
log.Printf("<- %v %#v", addr, pkt)
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
node, ok := nodes[pkt.NodeID]
|
|
|
|
queries++
|
|
|
|
lock.Unlock()
|
|
|
|
|
2014-05-21 23:46:19 -07:00
|
|
|
if ok && len(node.addresses) > 0 {
|
|
|
|
ann := discover.AnnounceV2{
|
|
|
|
Magic: discover.AnnouncementMagicV2,
|
|
|
|
This: discover.Node{
|
|
|
|
ID: pkt.NodeID,
|
|
|
|
},
|
2014-04-03 13:44:40 -07:00
|
|
|
}
|
2014-05-21 23:46:19 -07:00
|
|
|
for _, addr := range node.addresses {
|
|
|
|
ann.This.Addresses = append(ann.This.Addresses, discover.Address{IP: addr.ip, Port: addr.port})
|
2014-04-03 13:44:40 -07:00
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
log.Printf("-> %v %#v", addr, pkt)
|
|
|
|
}
|
|
|
|
|
2014-05-21 23:46:19 -07:00
|
|
|
tb := ann.MarshalXDR()
|
2014-04-03 13:44:40 -07:00
|
|
|
_, _, err = conn.WriteMsgUDP(tb, nil, addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("QueryV2 response write:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
answered++
|
|
|
|
lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-19 14:14:56 -07:00
|
|
|
func next(intv int) time.Time {
|
|
|
|
d := time.Duration(intv) * time.Second
|
|
|
|
t0 := time.Now()
|
|
|
|
t1 := t0.Add(d).Truncate(d)
|
|
|
|
time.Sleep(t1.Sub(t0))
|
|
|
|
return t1
|
|
|
|
}
|
|
|
|
|
|
|
|
func logStats(file string, intv int) {
|
|
|
|
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-03 13:44:40 -07:00
|
|
|
for {
|
2014-04-19 14:14:56 -07:00
|
|
|
t := next(intv)
|
2014-04-03 13:44:40 -07:00
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
|
|
|
|
var deleted = 0
|
|
|
|
for id, node := range nodes {
|
2014-05-21 23:46:19 -07:00
|
|
|
if time.Since(node.updated) > 60*time.Minute {
|
2014-04-03 13:44:40 -07:00
|
|
|
delete(nodes, id)
|
|
|
|
deleted++
|
2013-12-22 19:35:05 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-04 03:00:52 -07:00
|
|
|
|
2014-04-19 14:14:56 -07:00
|
|
|
fmt.Fprintf(f, "%d Nr:%d Ne:%d Qt:%d Qa:%d A:%d U:%d Lq:%d Lc:%d\n",
|
|
|
|
t.Unix(), len(nodes), deleted, queries, answered, announces, unknowns, limited, limiter.Len())
|
|
|
|
f.Sync()
|
|
|
|
|
2014-04-03 13:44:40 -07:00
|
|
|
queries = 0
|
2014-04-19 14:14:56 -07:00
|
|
|
announces = 0
|
2014-04-03 13:44:40 -07:00
|
|
|
answered = 0
|
2014-04-03 14:38:32 -07:00
|
|
|
limited = 0
|
2014-04-19 14:14:56 -07:00
|
|
|
unknowns = 0
|
2014-04-03 13:44:40 -07:00
|
|
|
|
|
|
|
lock.Unlock()
|
2013-12-22 19:35:05 -07:00
|
|
|
}
|
|
|
|
}
|