Debian Codegraph @

cmd/ironwood-example/main.go

raw
1 package main
2
3 import (
4 "crypto/ed25519"
5 "flag"
6 "fmt"
7 "net"
8 "os"
9 "os/signal"
10 "syscall"
11
12 iwc "github.com/Arceliar/ironwood/encrypted"
13 iwn "github.com/Arceliar/ironwood/network"
14 iws "github.com/Arceliar/ironwood/signed"
15 iwt "github.com/Arceliar/ironwood/types"
16
17 "log"
18 "net/http"
19 _ "net/http/pprof"
20 )
21
22 var ifname = flag.String("ifname", "\000", "interface name to bind to")
23 var pprof = flag.String("pprof", "", "listen to pprof on this port")
24 var enc = flag.Bool("enc", false, "encrypt traffic (must be enabled on all nodes)")
25 var sign = flag.Bool("sign", false, "sign traffic (must be enabled on all nodes)")
26
27 func main() {
28 flag.Parse()
29 if pprof != nil && *pprof != "" {
30 go func() {
31 log.Println(http.ListenAndServe(*pprof, nil))
32 }()
33 }
34 _, key, _ := ed25519.GenerateKey(nil)
35 var pc iwt.PacketConn
36 var opts []iwn.Option
37 var doNotify2 func(key ed25519.PublicKey)
38 doNotify1 := func(key ed25519.PublicKey) {
39 doNotify2(key)
40 }
41 opts = append(opts, iwn.WithBloomTransform(transformKey))
42 opts = append(opts, iwn.WithPathNotify(doNotify1))
43 if *enc && *sign {
44 panic("TODO a useful error message (can't use both -unenc and -sign)")
45 } else if *enc {
46 pc, _ = iwc.NewPacketConn(key, opts...)
47 } else if *sign {
48 pc, _ = iws.NewPacketConn(key, opts...)
49 } else {
50 pc, _ = iwn.NewPacketConn(key, opts...)
51 }
52 defer pc.Close()
53 doNotify2 = func(key ed25519.PublicKey) {
54 putKey(key)
55 flushBuffer(pc, key) // Ugly hack, we need the pc for flushBuffer to work
56 }
57 // get address and pc.SetOutOfBandHandler
58 localAddr := pc.LocalAddr()
59 pubKey := ed25519.PublicKey(localAddr.(iwt.Addr))
60 addrBytes := getAddr(pubKey)
61 // open tun/tap and assign address
62 ip := net.IP(addrBytes[:])
63 fmt.Println("Our IP address is", ip.String())
64 if ifname != nil && *ifname != "none" {
65 tun := setupTun(*ifname, ip.String()+"/8")
66 // read/write between tun/tap and packetconn
67 go tunReader(tun, pc)
68 go tunWriter(tun, pc)
69 }
70 // open multicast and start adding peers
71 mc := newMulticastConn()
72 go mcSender(mc, pubKey)
73 go mcListener(mc, pubKey, pc)
74 // listen for TCP, pass connections to packetConn.HandleConn
75 go listenTCP(pc)
76 sigs := make(chan os.Signal, 1)
77 signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
78 <-sigs
79 }