summaryrefslogtreecommitdiffhomepage
path: root/tun/netstack/examples/http_server.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-01-21 00:02:32 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2021-01-21 00:16:59 +0100
commit1d4eb2727a3ad6086229f45354933e84d85fc4e3 (patch)
treede1b00c7f44bd3ea4fd021a9bd398cdd7fe5eb76 /tun/netstack/examples/http_server.go
parent294d3bedf959c9c496aaa877919a762acf07c684 (diff)
netstack: introduce new module for gvisor tcp tun adapter
The Go linker isn't smart enough to prevent gvisor from being pulled into modules that use other parts of tun/, due to the types exposed. So, we put this into its own standalone module. We use this as an opportunity to introduce some example code as well. I'm still not happy that this not only clutters this repo's go.sum, but all the other projects that consume it, but it seems like making a new module inside of this repo will lead to even greater confusion. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tun/netstack/examples/http_server.go')
-rw-r--r--tun/netstack/examples/http_server.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/tun/netstack/examples/http_server.go b/tun/netstack/examples/http_server.go
new file mode 100644
index 0000000..e627e55
--- /dev/null
+++ b/tun/netstack/examples/http_server.go
@@ -0,0 +1,48 @@
+// +build ignore
+
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved.
+ */
+
+package main
+
+import (
+ "golang.zx2c4.com/wireguard/device"
+ "golang.zx2c4.com/wireguard/tun/netstack"
+ "io"
+ "log"
+ "net"
+ "net/http"
+)
+
+func main() {
+ tun, tnet, err := netstack.CreateNetTUN(
+ []net.IP{net.ParseIP("192.168.4.29")},
+ []net.IP{net.ParseIP("8.8.8.8"), net.ParseIP("8.8.4.4")},
+ 1420,
+ )
+ if err != nil {
+ log.Panic(err)
+ }
+ dev := device.NewDevice(tun, &device.Logger{log.Default(), log.Default(), log.Default()})
+ dev.IpcSet(`private_key=a8dac1d8a70a751f0f699fb14ba1cff7b79cf4fbd8f09f44c6e6a90d0369604f
+public_key=25123c5dcd3328ff645e4f2a3fce0d754400d3887a0cb7c56f0267e20fbf3c5b
+endpoint=163.172.161.0:12912
+allowed_ip=0.0.0.0/0
+persistent_keepalive_interval=25
+ `)
+ dev.Up()
+ listener, err := tnet.ListenTCP(&net.TCPAddr{Port: 80})
+ if err != nil {
+ log.Panicln(err)
+ }
+ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
+ log.Printf("> %s - %s - %s", request.RemoteAddr, request.URL.String(), request.UserAgent())
+ io.WriteString(writer, "Hello from userspace TCP!")
+ })
+ err = http.Serve(listener, nil)
+ if err != nil {
+ log.Panicln(err)
+ }
+}