/
main.go
113 lines (94 loc) · 2.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"time"
"golang.org/x/crypto/ssh"
)
func main() {
packet := Packet{}
flag.StringVar(&packet.FQDN, "fqdn", "_service._tcp.example.com", "the hostname of the record you want to update")
flag.UintVar(&packet.Priority, "priority", 10, "the priority of the record you want to update")
flag.UintVar(&packet.Weight, "weight", 1, "the weight of the record you want to update")
interval := flag.Duration("interval", time.Minute, "sending interval")
keyPath := flag.String("key", "~/.ssh/id_ed25519", "private key")
dstPort := flag.Int("dst-port", 55, "the destination port")
dstHost := flag.String("dst-host", "dynv6.com", "the destination host")
srcPort := flag.Int("src-port", 10000, "the local port")
ip := flag.String("src-ip", defaultIP(), "the local ip address")
flag.Parse()
// No arguments given?
if len(os.Args) < 2 {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
// Expand home directory
if str := *keyPath; strings.HasPrefix(str, "~/") {
home, _ := os.UserHomeDir()
home += str[1:]
keyPath = &home
}
privKey := loadPrivateKey(*keyPath)
// Marshal JSON data
packet.Timestamp = time.Now().Unix()
packet.Key = base64.RawStdEncoding.EncodeToString(privKey.PublicKey().Marshal())
payload := packet.MarshalAndSign(privKey)
// Parse local IP address
srcIP := net.ParseIP(*ip)
// Resolve destination host
dstIP, err := net.ResolveIPAddr("ip", *dstHost)
if err != nil {
log.Panicln("unable to resolve destination host:", err)
}
// open raw socket
conn, err := openSocket()
if err != nil {
panic(err)
}
defer conn.Close()
srcAddr := net.UDPAddr{
IP: srcIP,
Port: *srcPort,
}
dstAddr := net.UDPAddr{
IP: dstIP.IP,
Port: *dstPort,
}
// send packet method
sendPacket := func() {
log.Printf("sending from %s to %s", srcAddr.String(), dstAddr.String())
b, err := buildUDPPacket(dstAddr, srcAddr, payload)
if err != nil {
panic(err)
}
_, err = conn.WriteTo(b, &net.IPAddr{IP: dstIP.IP})
if err != nil {
log.Println(err)
}
}
// start sending packets
sendPacket()
for range time.NewTicker(*interval).C {
sendPacket()
}
}
func loadPrivateKey(path string) ssh.Signer {
// Load private key
pemBytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
// Parse private key
privKey, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
panic(err)
}
return privKey
}