Last active
March 4, 2021 10:47
-
-
Save pereztr5/a1e39647714191620c028a873d77ddc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fork of https://github.com/WireGuard/wireguard-go/blob/master/tun/netstack/examples/http_server.go | |
// Usage: | |
// go run main.go privateKey peerPublicKey | |
// go run main.go YL7A9IqywiX7P4yMnsm1ocbOdgzIpaiyXa7JBPhcAFo= FiHXTbyGNzbXJDGhw15Laa3t+FHgUQMqcePTnA7wJzM= | |
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net" | |
"net/http" | |
"os" | |
"golang.zx2c4.com/wireguard/conn" | |
"golang.zx2c4.com/wireguard/device" | |
"golang.zx2c4.com/wireguard/tun/netstack" | |
"tailscale.com/types/wgkey" | |
) | |
func main() { | |
// Wireguard | |
tun, tnet, err := netstack.CreateNetTUN( | |
[]net.IP{net.ParseIP("192.168.4.29")}, | |
[]net.IP{net.ParseIP("8.8.8.8")}, | |
1420) | |
if err != nil { | |
log.Panic(err) | |
} | |
logger := device.NewLogger(1, "") | |
dev := device.NewDevice(tun, conn.NewDefaultBind(), logger) | |
// Grab base64 Key | |
privateKey, err := wgkey.Parse(os.Args[1]) | |
if err != nil { | |
log.Fatalln("Could not decode private key: ", err) | |
} | |
// Grab base64 Key | |
publicKey, err := wgkey.Parse(os.Args[2]) | |
if err != nil { | |
log.Fatalln("Could not decode public key: ", err) | |
} | |
cfg := fmt.Sprintf(`private_key=%s | |
public_key=%s | |
endpoint=147.75.199.245:51820 | |
allowed_ip=0.0.0.0/0 | |
persistent_keepalive_interval=25 | |
`, privateKey.HexString(), publicKey.HexString()) | |
dev.IpcSet(cfg) | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment