gitee.com/aurawing/surguard-go@v0.3.1-0.20240409071558-96509a61ecf3/tun/netstack/examples/http_server.go (about) 1 //go:build ignore 2 3 /* SPDX-License-Identifier: MIT 4 * 5 * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. 6 */ 7 8 package main 9 10 import ( 11 "io" 12 "log" 13 "net" 14 "net/http" 15 "net/netip" 16 17 "gitee.com/aurawing/surguard-go/conn" 18 "gitee.com/aurawing/surguard-go/device" 19 "gitee.com/aurawing/surguard-go/tun/netstack" 20 ) 21 22 func main() { 23 tun, tnet, err := netstack.CreateNetTUN( 24 []netip.Addr{netip.MustParseAddr("192.168.4.29")}, 25 []netip.Addr{netip.MustParseAddr("8.8.8.8"), netip.MustParseAddr("8.8.4.4")}, 26 1420, 27 ) 28 if err != nil { 29 log.Panic(err) 30 } 31 dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, "")) 32 dev.IpcSet(`private_key=003ed5d73b55806c30de3f8a7bdab38af13539220533055e635690b8b87ad641 33 listen_port=58120 34 public_key=f928d4f6c1b86c12f2562c10b07c555c5c57fd00f59e90c8d8d88767271cbf7c 35 allowed_ip=192.168.4.28/32 36 persistent_keepalive_interval=25 37 `) 38 dev.Up() 39 listener, err := tnet.ListenTCP(&net.TCPAddr{Port: 80}) 40 if err != nil { 41 log.Panicln(err) 42 } 43 http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { 44 log.Printf("> %s - %s - %s", request.RemoteAddr, request.URL.String(), request.UserAgent()) 45 io.WriteString(writer, "Hello from userspace TCP!") 46 }) 47 err = http.Serve(listener, nil) 48 if err != nil { 49 log.Panicln(err) 50 } 51 }