mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 20:07:38 +03:00
41 lines
868 B
Go
41 lines
868 B
Go
package network
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
func HTTPClientWithNetwork(network string) *http.Client {
|
|
return &http.Client{
|
|
Transport: &http.Transport{
|
|
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
|
|
var dialer net.Dialer
|
|
return dialer.DialContext(ctx, network, addr)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func Get(url string) ([]byte, error) {
|
|
response, err := http.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
return ioutil.ReadAll(response.Body)
|
|
}
|
|
|
|
func GetContext(ctx context.Context, client *http.Client, url string) ([]byte, error) {
|
|
request, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
return ioutil.ReadAll(response.Body)
|
|
}
|