gitee.com/aurawing/surguard-go@v0.3.1-0.20240409071558-96509a61ecf3/tun/netstack/examples/http_client.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/http"
    14  	"net/netip"
    15  
    16  	"gitee.com/aurawing/surguard-go/conn"
    17  	"gitee.com/aurawing/surguard-go/device"
    18  	"gitee.com/aurawing/surguard-go/tun/netstack"
    19  )
    20  
    21  func main() {
    22  	tun, tnet, err := netstack.CreateNetTUN(
    23  		[]netip.Addr{netip.MustParseAddr("192.168.4.28")},
    24  		[]netip.Addr{netip.MustParseAddr("8.8.8.8")},
    25  		1420)
    26  	if err != nil {
    27  		log.Panic(err)
    28  	}
    29  	dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, ""))
    30  	err = dev.IpcSet(`private_key=087ec6e14bbed210e7215cdc73468dfa23f080a1bfb8665b2fd809bd99d28379
    31  public_key=c4c8e984c5322c8184c72265b92b250fdb63688705f504ba003c88f03393cf28
    32  allowed_ip=0.0.0.0/0
    33  endpoint=127.0.0.1:58120
    34  `)
    35  	err = dev.Up()
    36  	if err != nil {
    37  		log.Panic(err)
    38  	}
    39  
    40  	client := http.Client{
    41  		Transport: &http.Transport{
    42  			DialContext: tnet.DialContext,
    43  		},
    44  	}
    45  	resp, err := client.Get("http://192.168.4.29/")
    46  	if err != nil {
    47  		log.Panic(err)
    48  	}
    49  	body, err := io.ReadAll(resp.Body)
    50  	if err != nil {
    51  		log.Panic(err)
    52  	}
    53  	log.Println(string(body))
    54  }