diff options
Diffstat (limited to 'pkg/tcpip/sample/wg_tunnel')
-rw-r--r-- | pkg/tcpip/sample/wg_tunnel/config.yaml | 116 | ||||
-rw-r--r-- | pkg/tcpip/sample/wg_tunnel/gobgp/main.go | 119 | ||||
-rw-r--r-- | pkg/tcpip/sample/wg_tunnel/test/main.go | 66 |
3 files changed, 301 insertions, 0 deletions
diff --git a/pkg/tcpip/sample/wg_tunnel/config.yaml b/pkg/tcpip/sample/wg_tunnel/config.yaml new file mode 100644 index 000000000..7a0c250dc --- /dev/null +++ b/pkg/tcpip/sample/wg_tunnel/config.yaml @@ -0,0 +1,116 @@ +network: + version: 2 + renderer: gvisor + ethernets: + lo: + addresses: + - 127.0.0.1/8 + - ::1/128 + - 10.1.0.1/24 + - 2001:470:de6f:5310::1/64 + routes: + - to: 10.1.15.0/24 + - to: 2001:470:de6f:531f::/64 + tuntaps: + tun: + name: tun1 + mode: tun + mtu: 1280 +# fd: 0 +# name: tap1 +# mode: tap + addresses: + - 10.1.1.2/24 + - 2001:470:de6f:5311::2/64 + nameservers: + addresses: + - 8.8.8.8 + - 8.8.4.4 +# routes: +# - to: 0.0.0.0/0 +# via: 10.1.1.1 +# metric: 200 +# - to: ::/0 +# via: 2001:470:de6f:5311::1 +# metric: 200 + macaddress: aa:00:01:01:02:01 + + tunnels: + tun1: + mode: udp + local: 0.0.0.0:10002 + remote: 127.0.0.1:10001 + #FIXME detect MTU + mtu: 1280 #1500 + addresses: + - 10.1.2.1/24 + - 2001:470:de6f:5312::1/64 + routes: +# - to: 0.0.0.0/0 +# via: 10.1.2.2 +# metric: 100 +# - to: ::/0 +# via: 2001:470:de6f:5312::2 +# metric: 100 + macaddress: aa:00:01:01:02:02 + + wireguards: +# wg1: +# name: wg1 +# addresses: +# - 10.1.3.2/24 +# - 2001:470:de6f:5313::2/64 +# listen_port: 51820 +# private_key: cCBLRrAKF0oqLua2IGYr6ngQRLdgCSTa8hzDLQvezUI= +# peers: +# - public_key: igb6I+JFOEXPN4JjZvSslxNDPQK1/Ofi6310RzH2HAk= +# endpoint: 10.49.50.1:51820 +# allowed_ips: +# - 10.1.2.3/32 +# - 0.0.0.0/0 +# - ::/0 +# # persistent_keepalive: 3600 +# nameservers: +# addresses: +# - 8.8.8.8 +# - 8.8.4.4 +# routes: +# - to: 0.0.0.0/0 +# via: 10.1.3.1 +# metric: 100 +# mark: 1 +# mask: 255 +# - to: ::/0 +# via: 2001:470:de6f:5313::1 +# metric: 100 +# mark: 1 +# mask: 255 +# macaddress: aa:00:01:01:02:03 + + wg2: + name: wg2 + addresses: + - 10.49.124.111/32 + - 2001:470:dfae:6300::111/128 + - 2001:470:dfae:6300::1:111/128 + - fe80::111/64 + listen_port: 51820 + private_key: cCBLRrAKF0oqLua2IGYr6ngQRLdgCSTa8hzDLQvezUI= + peers: + - public_key: 5Q5KIFIeskMh/QanwH9/5lHQ9NhWBsY16kwaS0ELQyg= + endpoint: 10.49.50.215:51820 + allowed_ips: + - 2001:470:dfae:6300::1:3/128 + - ::/0 + - 0.0.0.0/0 + persistent_keepalive: 3600 + nameservers: + addresses: + - 8.8.8.8 + - 8.8.4.4 + routes: + - to: 10.49.124.0/24 + - to: 0.0.0.0/0 + - to: ::/0 + - to: 2001:470:dfae:6300::/64 + macaddress: aa:00:01:01:02:04 diff --git a/pkg/tcpip/sample/wg_tunnel/gobgp/main.go b/pkg/tcpip/sample/wg_tunnel/gobgp/main.go new file mode 100644 index 000000000..3f51adc69 --- /dev/null +++ b/pkg/tcpip/sample/wg_tunnel/gobgp/main.go @@ -0,0 +1,119 @@ +package main + +import ( + "context" + "time" + + "github.com/golang/protobuf/ptypes" + "github.com/golang/protobuf/ptypes/any" + api "github.com/osrg/gobgp/api" + gobgp "github.com/osrg/gobgp/pkg/server" + log "github.com/sirupsen/logrus" +) + +func main() { + log.SetLevel(log.DebugLevel) + s := gobgp.NewBgpServer() + go s.Serve() + + // global configuration + if err := s.StartBgp(context.Background(), &api.StartBgpRequest{ + Global: &api.Global{ + As: 65003, + RouterId: "10.0.255.254", + ListenPort: -1, // gobgp won't listen on tcp:179 + }, + }); err != nil { + log.Fatal(err) + } + + // monitor the change of the peer state + if err := s.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(p *api.Peer) { log.Info(p) }); err != nil { + log.Fatal(err) + } + + // neighbor configuration + n := &api.Peer{ + Conf: &api.PeerConf{ + NeighborAddress: "172.17.0.2", + PeerAs: 65002, + }, + } + + if err := s.AddPeer(context.Background(), &api.AddPeerRequest{ + Peer: n, + }); err != nil { + log.Fatal(err) + } + + // add routes + nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{ + Prefix: "10.0.0.0", + PrefixLen: 24, + }) + + a1, _ := ptypes.MarshalAny(&api.OriginAttribute{ + Origin: 0, + }) + a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{ + NextHop: "10.0.0.1", + }) + a3, _ := ptypes.MarshalAny(&api.AsPathAttribute{ + Segments: []*api.AsSegment{ + { + Type: 2, + Numbers: []uint32{6762, 39919, 65000, 35753, 65000}, + }, + }, + }) + attrs := []*any.Any{a1, a2, a3} + + _, err := s.AddPath(context.Background(), &api.AddPathRequest{ + Path: &api.Path{ + Family: &api.Family{Afi: api.Family_AFI_IP, Safi: api.Family_SAFI_UNICAST}, + Nlri: nlri, + Pattrs: attrs, + }, + }) + if err != nil { + log.Fatal(err) + } + + v6Family := &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_UNICAST, + } + + // add v6 route + nlri, _ = ptypes.MarshalAny(&api.IPAddressPrefix{ + PrefixLen: 64, + Prefix: "2001:db8:1::", + }) + v6Attrs, _ := ptypes.MarshalAny(&api.MpReachNLRIAttribute{ + Family: v6Family, + NextHops: []string{"2001:db8::1"}, + Nlris: []*any.Any{nlri}, + }) + + c, _ := ptypes.MarshalAny(&api.CommunitiesAttribute{ + Communities: []uint32{100, 200}, + }) + + _, err = s.AddPath(context.Background(), &api.AddPathRequest{ + Path: &api.Path{ + Family: v6Family, + Nlri: nlri, + Pattrs: []*any.Any{a1, v6Attrs, c}, + }, + }) + if err != nil { + log.Fatal(err) + } + + s.ListPath(context.Background(), &api.ListPathRequest{Family: v6Family}, func(p *api.Destination) { + log.Info(p) + }) + + // do something useful here instead of exiting + time.Sleep(time.Minute * 3) +} diff --git a/pkg/tcpip/sample/wg_tunnel/test/main.go b/pkg/tcpip/sample/wg_tunnel/test/main.go new file mode 100644 index 000000000..5373ca840 --- /dev/null +++ b/pkg/tcpip/sample/wg_tunnel/test/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "log" + "net" + "runtime" + "syscall" + "time" +) + +func CheckError(err error) { + if err != nil { + log.Fatal("Error: " , err) + } +} + +func TestFinalize(conn *net.UDPConn) { + event := make(chan string) + + runtime.SetFinalizer(conn, func (obj *net.UDPConn) { + fmt.Println("Finalize: UDPConn", obj.LocalAddr(), obj.RemoteAddr()) + event <- "Finalize UDPConn" + }) + + runtime.GC() + + select { + case res := <-event: + fmt.Println(res) + case <-time.After(2 * time.Second): + fmt.Println("No finalize") + } +} + +func test() { + ServerAddr,err := net.ResolveUDPAddr("udp", "127.0.0.1:10001") + CheckError(err) + + LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10002") + CheckError(err) + + conn, err := net.DialUDP("udp", LocalAddr, ServerAddr) + CheckError(err) + + runtime.KeepAlive(conn) + + sd, err := conn.File() + CheckError(err) + + TestFinalize(conn) + + var fd int + fd = int(sd.Fd()) + + syscall.SetNonblock(fd, true) + + // conn.Write([]byte("HelloWorld!")) + //_, err = sd.Write([]byte("HelloWorld!")) + _, err = syscall.Write(fd, []byte("HelloWorld!")) + CheckError(err) +} + +func main() { + test() +} |