2022-11-23 22:29:10 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-15 00:28:36 +01:00
|
|
|
"errors"
|
|
|
|
"flag"
|
2022-11-29 21:15:39 +01:00
|
|
|
"fmt"
|
2022-11-23 22:29:10 +01:00
|
|
|
"log"
|
2022-12-15 00:28:36 +01:00
|
|
|
"musique/server/proto"
|
2022-12-16 02:11:43 +01:00
|
|
|
"musique/server/router"
|
2022-11-23 22:29:10 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
2022-12-09 16:05:25 +01:00
|
|
|
"strings"
|
2022-12-09 15:04:41 +01:00
|
|
|
"sync"
|
2022-12-05 21:50:30 +01:00
|
|
|
"time"
|
2022-11-23 22:29:10 +01:00
|
|
|
)
|
|
|
|
|
2022-11-29 21:15:39 +01:00
|
|
|
func scanError(scanResult []string, conn net.Conn) {
|
|
|
|
if scanResult == nil {
|
|
|
|
conn.Write([]byte("Empty scan result, run 'scan' first.\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
type TimeExchange struct {
|
2022-12-09 15:04:41 +01:00
|
|
|
before, after, remote int64
|
|
|
|
}
|
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
func (e *TimeExchange) estimateFor(host string) bool {
|
2022-12-09 15:04:41 +01:00
|
|
|
e.before = time.Now().UnixMilli()
|
2022-12-16 16:34:52 +01:00
|
|
|
var response proto.TimeResponse
|
|
|
|
if err := proto.Command(host, proto.Time(), &response); err != nil {
|
|
|
|
log.Printf("failed to send time command: %v\n", err)
|
2022-12-09 15:04:41 +01:00
|
|
|
return false
|
|
|
|
}
|
2022-12-16 16:34:52 +01:00
|
|
|
e.after = time.Now().UnixMilli()
|
2022-12-09 15:04:41 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
func timesync() {
|
2022-12-09 15:04:41 +01:00
|
|
|
wg := sync.WaitGroup{}
|
2022-12-16 16:34:52 +01:00
|
|
|
wg.Add(len(remotes))
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
TimeExchange
|
|
|
|
key string
|
|
|
|
}
|
2022-12-09 15:04:41 +01:00
|
|
|
|
|
|
|
// Gather time from each host
|
2022-12-16 16:34:52 +01:00
|
|
|
responses := make(chan response, len(remotes))
|
|
|
|
for key, remote := range remotes {
|
|
|
|
key, remote := key, remote
|
2022-12-09 15:04:41 +01:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-12-16 16:34:52 +01:00
|
|
|
exchange := TimeExchange{}
|
|
|
|
if exchange.estimateFor(remote.Address) {
|
|
|
|
responses <- response{exchange, key}
|
2022-12-09 15:04:41 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(responses)
|
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
for response := range responses {
|
|
|
|
remote := remotes[response.key]
|
|
|
|
remote.TimeExchange = response.TimeExchange
|
2022-12-09 15:04:41 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 22:29:10 +01:00
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
var maxReactionTime = int64(1_000)
|
2022-11-23 22:29:10 +01:00
|
|
|
|
2022-12-15 00:28:36 +01:00
|
|
|
func isThisMyAddress(address string) bool {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ip, _, err := net.ParseCIDR(addr.String())
|
|
|
|
if err == nil && ip.To4() != nil && fmt.Sprintf("%s:%d", ip, port) == address {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
func notifyAll() <-chan time.Time {
|
2022-12-09 15:04:41 +01:00
|
|
|
wg := sync.WaitGroup{}
|
2022-12-16 16:34:52 +01:00
|
|
|
wg.Add(len(remotes))
|
|
|
|
startDeadline := time.After(time.Duration(maxReactionTime) * time.Millisecond)
|
2022-12-09 15:04:41 +01:00
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
for _, remote := range remotes {
|
|
|
|
remote := remote
|
2022-12-09 15:04:41 +01:00
|
|
|
go func() {
|
2022-12-16 16:34:52 +01:00
|
|
|
startTime := maxReactionTime - (remote.after-remote.before)/2
|
|
|
|
var response proto.StartResponse
|
|
|
|
err := proto.CommandTimeout(
|
|
|
|
remote.Address,
|
|
|
|
proto.Start(startTime),
|
|
|
|
&response,
|
|
|
|
time.Duration(startTime)*time.Millisecond,
|
|
|
|
)
|
2022-12-09 15:04:41 +01:00
|
|
|
if err != nil {
|
2022-12-16 16:34:52 +01:00
|
|
|
log.Printf("failed to notify %s: %v\n", remote.Address, err)
|
2022-12-09 15:04:41 +01:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:05:25 +01:00
|
|
|
return startDeadline
|
2022-12-09 15:04:41 +01:00
|
|
|
}
|
2022-11-23 22:29:10 +01:00
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
var (
|
|
|
|
pinger chan struct{}
|
|
|
|
)
|
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
func registerRoutes(r *router.Router) {
|
|
|
|
r.Add("handshake", func(incoming net.Conn, request proto.Request) interface{} {
|
2022-12-15 00:28:36 +01:00
|
|
|
var response proto.HandshakeResponse
|
|
|
|
response.Version = proto.Version
|
|
|
|
response.Nick = nick
|
2022-12-16 02:11:43 +01:00
|
|
|
return response
|
|
|
|
})
|
2022-12-15 00:28:36 +01:00
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
r.Add("hosts", func(incoming net.Conn, request proto.Request) interface{} {
|
2022-12-15 00:28:36 +01:00
|
|
|
var response proto.HostsResponse
|
|
|
|
for _, remote := range remotes {
|
|
|
|
response.Hosts = append(response.Hosts, proto.HostsResponseEntry{
|
|
|
|
Nick: remote.Nick,
|
|
|
|
Version: remote.Version,
|
|
|
|
Address: remote.Address,
|
|
|
|
})
|
|
|
|
}
|
2022-12-16 02:11:43 +01:00
|
|
|
return response
|
|
|
|
})
|
2022-12-15 00:28:36 +01:00
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
r.Add("synchronize-hosts", func(incoming net.Conn, request proto.Request) interface{} {
|
|
|
|
return synchronizeHosts(request.HostsResponse)
|
|
|
|
})
|
2022-12-15 00:28:36 +01:00
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
r.Add("synchronize-hosts-with-remotes", func(incoming net.Conn, request proto.Request) interface{} {
|
|
|
|
synchronizeHostsWithRemotes()
|
|
|
|
return nil
|
|
|
|
})
|
2022-12-16 16:34:52 +01:00
|
|
|
|
|
|
|
r.Add("time", func(incoming net.Conn, request proto.Request) interface{} {
|
|
|
|
return proto.TimeResponse{
|
|
|
|
Time: time.Now().UnixMilli(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
pinger = make(chan struct{}, 12)
|
|
|
|
r.Add("start", func(incoming net.Conn, request proto.Request) interface{} {
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Duration(request.StartTime) * time.Millisecond)
|
|
|
|
pinger <- struct{}{}
|
|
|
|
}()
|
|
|
|
return proto.StartResponse{
|
|
|
|
Succeded: true,
|
|
|
|
}
|
|
|
|
})
|
2022-12-15 00:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Remote struct {
|
2022-12-16 16:34:52 +01:00
|
|
|
TimeExchange
|
2022-12-15 00:28:36 +01:00
|
|
|
Address string
|
|
|
|
Nick string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-12-16 02:11:43 +01:00
|
|
|
baseIP string = ""
|
2022-12-15 00:28:36 +01:00
|
|
|
nick string
|
2022-12-16 02:11:43 +01:00
|
|
|
port int = 8888
|
2022-12-16 16:34:52 +01:00
|
|
|
remotes map[string]*Remote
|
2022-12-15 00:28:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func synchronizeHosts(incoming proto.HostsResponse) (response proto.HostsResponse) {
|
|
|
|
visitedHosts := make(map[string]struct{})
|
|
|
|
|
|
|
|
// Add all hosts that are in incoming to our list of remotes
|
|
|
|
// Additionaly build set of all hosts that remote knows
|
|
|
|
for _, incomingHost := range incoming.Hosts {
|
|
|
|
if _, ok := remotes[incomingHost.Address]; !ok && !isThisMyAddress(incomingHost.Address) {
|
2022-12-16 16:34:52 +01:00
|
|
|
remotes[incomingHost.Address] = &Remote{
|
2022-12-15 00:28:36 +01:00
|
|
|
Address: incomingHost.Address,
|
|
|
|
Nick: incomingHost.Nick,
|
|
|
|
Version: incomingHost.Version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visitedHosts[incomingHost.Address] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build list of hosts that incoming doesn't know
|
|
|
|
for _, remote := range remotes {
|
|
|
|
if _, ok := visitedHosts[remote.Address]; !ok {
|
|
|
|
response.Hosts = append(response.Hosts, proto.HostsResponseEntry{
|
|
|
|
Address: remote.Address,
|
|
|
|
Version: remote.Version,
|
|
|
|
Nick: remote.Nick,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func myAddressInTheSameNetwork(remote string) (string, error) {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("myAddressInTheSameNetwork: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteInParts := strings.Split(remote, ":")
|
|
|
|
if len(remoteInParts) == 2 {
|
|
|
|
remote = remoteInParts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteIP := net.ParseIP(remote)
|
|
|
|
if remoteIP == nil {
|
|
|
|
// TODO Hoist error to global variable
|
|
|
|
return "", errors.New("Cannot parse remote IP")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ip, ipNet, err := net.ParseCIDR(addr.String())
|
|
|
|
if err == nil && ipNet.Contains(remoteIP) {
|
2023-01-02 06:34:54 +01:00
|
|
|
return fmt.Sprintf("%s:%d", ip, port), nil
|
2022-12-15 00:28:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Hoist error to global variable
|
|
|
|
return "", errors.New("Cannot find matching IP addr")
|
|
|
|
}
|
|
|
|
|
|
|
|
func synchronizeHostsWithRemotes() {
|
|
|
|
previousResponseLength := -1
|
|
|
|
var response proto.HostsResponse
|
|
|
|
|
|
|
|
for previousResponseLength != len(response.Hosts) {
|
|
|
|
response = proto.HostsResponse{}
|
|
|
|
|
|
|
|
// Add all known remotes
|
|
|
|
for _, remote := range remotes {
|
|
|
|
response.Hosts = append(response.Hosts, proto.HostsResponseEntry{
|
|
|
|
Address: remote.Address,
|
|
|
|
Nick: remote.Nick,
|
|
|
|
Version: remote.Version,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send constructed list to each remote
|
|
|
|
previousResponseLength = len(response.Hosts)
|
|
|
|
for _, remote := range response.Hosts {
|
|
|
|
var localResponse proto.HostsResponse
|
|
|
|
localResponse.Hosts = make([]proto.HostsResponseEntry, len(response.Hosts))
|
|
|
|
copy(localResponse.Hosts, response.Hosts)
|
|
|
|
|
|
|
|
myAddress, err := myAddressInTheSameNetwork(remote.Address)
|
|
|
|
// TODO Report when err != nil
|
|
|
|
if err == nil {
|
|
|
|
localResponse.Hosts = append(localResponse.Hosts, proto.HostsResponseEntry{
|
|
|
|
Address: myAddress,
|
|
|
|
Nick: nick,
|
|
|
|
Version: proto.Version,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var remoteResponse proto.HostsResponse
|
|
|
|
proto.Command(remote.Address, proto.SynchronizeHosts(localResponse), &remoteResponse)
|
|
|
|
synchronizeHosts(remoteResponse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:04:41 +01:00
|
|
|
func main() {
|
2022-12-15 00:28:36 +01:00
|
|
|
var (
|
|
|
|
logsPath string
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.StringVar(&baseIP, "ip", "", "IP where server will listen")
|
|
|
|
flag.StringVar(&nick, "nick", "", "Name that is going to be used to recognize this server")
|
|
|
|
flag.IntVar(&port, "port", 8081, "TCP port where server receives connections")
|
|
|
|
flag.StringVar(&logsPath, "logs", "", "Target file for logs from server. By default stdout")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if len(logsPath) != 0 {
|
|
|
|
// TODO Is defer logFile.Close() needed here? Dunno
|
|
|
|
logFile, err := os.OpenFile(logsPath, os.O_WRONLY|os.O_APPEND, 0o640)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Cannot open log file: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
log.SetOutput(logFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nick) == 0 {
|
|
|
|
log.Fatalln("Please provide nick via --nick flag")
|
|
|
|
}
|
|
|
|
|
2023-01-02 05:47:27 +01:00
|
|
|
server, err := registerDNS()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to register DNS:", err)
|
|
|
|
}
|
|
|
|
defer server.Shutdown()
|
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
r := router.Router{}
|
|
|
|
registerRoutes(&r)
|
2023-01-02 07:23:30 +01:00
|
|
|
exit, err := r.Run(baseIP, &port)
|
2022-12-15 00:28:36 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2023-01-02 05:47:27 +01:00
|
|
|
if err := registerRemotes(5); err != nil {
|
2022-12-16 02:11:43 +01:00
|
|
|
log.Fatalln(err)
|
2022-12-15 00:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for range exit {
|
|
|
|
}
|
|
|
|
}
|