github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/config/http.go (about)

     1  package config
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"net/url"
     7  	"strconv"
     8  
     9  	"github.com/laof/lite-speed-test/outbound"
    10  )
    11  
    12  func HttpLinkToHttpOption(link string) (*outbound.HttpOption, error) {
    13  	u, err := url.Parse(link)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	if u.Scheme != "http" {
    18  		return nil, errors.New("not a trojan link")
    19  	}
    20  	pass := u.User.Username()
    21  	hostport := u.Host
    22  	host, _, err := net.SplitHostPort(hostport)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	port, err := strconv.Atoi(u.Port())
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	// frag := u.Fragment
    31  	// fmt.Printf("password: %s, host: %s, port: %d, frag: %s\n", pass, host, port, frag)
    32  	httpOption := outbound.HttpOption{
    33  		Name:     "http",
    34  		Remarks:  u.Fragment,
    35  		Password: pass,
    36  		Port:     port,
    37  		Server:   host,
    38  	}
    39  	if rawQuery, err := url.ParseQuery(u.RawQuery); err == nil {
    40  		httpOption.UserName = rawQuery.Get("username")
    41  		httpOption.TLS = rawQuery.Get("tls") == "true"
    42  		httpOption.SNI = rawQuery.Get("sni")
    43  		httpOption.SkipCertVerify = rawQuery.Get("allowInsecure") == "1"
    44  	}
    45  	return &httpOption, nil
    46  }
    47  
    48  func init() {
    49  	outbound.RegisterDialerCreator("http", func(link string) (outbound.Dialer, error) {
    50  		httpOption, err := HttpLinkToHttpOption(link)
    51  		if err != nil {
    52  			return nil, err
    53  		}
    54  		return outbound.NewHttp(*httpOption), nil
    55  	})
    56  }