github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/example/example.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"log"
     8  	"net"
     9  	"net/http"
    10  
    11  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    12  	_ "github.com/Asutorufa/yuhaiin/pkg/net/proxy/simple"
    13  	_ "github.com/Asutorufa/yuhaiin/pkg/net/proxy/socks5"
    14  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/point"
    15  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/protocol"
    16  )
    17  
    18  func main() {
    19  	node := &point.Point{
    20  		Protocols: []*protocol.Protocol{
    21  			{
    22  				Protocol: &protocol.Protocol_Simple{
    23  					Simple: &protocol.Simple{
    24  						Host: "127.0.0.1",
    25  						Port: 1080,
    26  					},
    27  				},
    28  			},
    29  			{
    30  				Protocol: &protocol.Protocol_Socks5{
    31  					Socks5: &protocol.Socks5{},
    32  				},
    33  			},
    34  		},
    35  	}
    36  
    37  	pro, err := point.Dialer(node)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	c := http.Client{
    43  		Transport: &http.Transport{
    44  			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    45  				add, err := netapi.ParseAddress(netapi.PaseNetwork(network), addr)
    46  				if err != nil {
    47  					return nil, fmt.Errorf("parse address failed: %w", err)
    48  				}
    49  				return pro.Conn(ctx, add)
    50  			}},
    51  	}
    52  	resp, err := c.Get("https://www.google.com")
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	buf := new(bytes.Buffer)
    58  	_, _ = buf.ReadFrom(resp.Body)
    59  	defer resp.Body.Close()
    60  	log.Println(buf.String())
    61  }