mihomo/adapter/outbound/http.go

187 lines
4.5 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outbound
import (
"bufio"
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"net"
"net/http"
"strconv"
2023-11-03 21:01:45 +08:00
N "github.com/metacubex/mihomo/common/net"
"github.com/metacubex/mihomo/component/ca"
"github.com/metacubex/mihomo/component/dialer"
"github.com/metacubex/mihomo/component/proxydialer"
C "github.com/metacubex/mihomo/constant"
)
type Http struct {
2018-12-22 23:56:42 +08:00
*Base
user string
pass string
tlsConfig *tls.Config
2022-04-14 13:07:39 +08:00
option *HttpOption
}
type HttpOption struct {
BasicOption
2022-04-14 13:07:39 +08:00
Name string `proxy:"name"`
Server string `proxy:"server"`
Port int `proxy:"port"`
UserName string `proxy:"username,omitempty"`
Password string `proxy:"password,omitempty"`
TLS bool `proxy:"tls,omitempty"`
SNI string `proxy:"sni,omitempty"`
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
2022-07-11 13:42:28 +08:00
Fingerprint string `proxy:"fingerprint,omitempty"`
2022-04-14 13:07:39 +08:00
Headers map[string]string `proxy:"headers,omitempty"`
}
// StreamConnContext implements C.ProxyAdapter
func (h *Http) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
if h.tlsConfig != nil {
cc := tls.Client(c, h.tlsConfig)
err := cc.HandshakeContext(ctx)
c = cc
if err != nil {
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
}
}
if err := h.shakeHand(metadata, c); err != nil {
return nil, err
}
return c, nil
}
2021-04-29 11:23:14 +08:00
// DialContext implements C.ProxyAdapter
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
2022-12-20 00:11:02 +08:00
return h.DialContextWithDialer(ctx, dialer.NewDialer(h.Base.DialOptions(opts...)...), metadata)
2022-12-19 21:34:07 +08:00
}
// DialContextWithDialer implements C.ProxyAdapter
func (h *Http) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
if len(h.option.DialerProxy) > 0 {
dialer, err = proxydialer.NewByName(h.option.DialerProxy, dialer)
if err != nil {
return nil, err
}
}
2022-12-19 21:34:07 +08:00
c, err := dialer.DialContext(ctx, "tcp", h.addr)
if err != nil {
2019-12-05 17:51:21 +08:00
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
}
N.TCPKeepAlive(c)
2022-12-16 22:15:44 +08:00
defer func(c net.Conn) {
2022-12-13 13:20:40 +08:00
safeConnClose(c, err)
2022-12-16 22:15:44 +08:00
}(c)
c, err = h.StreamConnContext(ctx, c, metadata)
if err != nil {
return nil, err
}
return NewConn(c, h), nil
}
2022-12-19 21:34:07 +08:00
// SupportWithDialer implements C.ProxyAdapter
func (h *Http) SupportWithDialer() C.NetWork {
return C.TCP
2022-12-19 21:34:07 +08:00
}
func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
2019-10-11 20:11:18 +08:00
addr := metadata.RemoteAddress()
2023-05-02 20:55:25 +08:00
HeaderString := "CONNECT " + addr + " HTTP/1.1\r\n"
tempHeaders := map[string]string{
"Host": addr,
"User-Agent": "Go-http-client/1.1",
"Proxy-Connection": "Keep-Alive",
}
2023-05-02 20:55:25 +08:00
for key, value := range h.option.Headers {
tempHeaders[key] = value
2022-04-14 13:07:39 +08:00
}
if h.user != "" && h.pass != "" {
auth := h.user + ":" + h.pass
2023-05-02 20:55:25 +08:00
tempHeaders["Proxy-Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}
for key, value := range tempHeaders {
HeaderString += key + ": " + value + "\r\n"
}
2023-05-02 20:55:25 +08:00
HeaderString += "\r\n"
_, err := rw.Write([]byte(HeaderString))
if err != nil {
return err
}
2023-05-02 20:55:25 +08:00
resp, err := http.ReadResponse(bufio.NewReader(rw), nil)
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
return nil
}
if resp.StatusCode == http.StatusProxyAuthRequired {
return errors.New("HTTP need auth")
}
if resp.StatusCode == http.StatusMethodNotAllowed {
return errors.New("CONNECT method not allowed by proxy")
}
if resp.StatusCode >= http.StatusInternalServerError {
return errors.New(resp.Status)
}
return fmt.Errorf("can not connect remote err code: %d", resp.StatusCode)
}
2022-07-11 13:42:28 +08:00
func NewHttp(option HttpOption) (*Http, error) {
var tlsConfig *tls.Config
if option.TLS {
2020-10-02 11:34:40 +08:00
sni := option.Server
if option.SNI != "" {
sni = option.SNI
}
2023-09-22 14:45:34 +08:00
var err error
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(&tls.Config{
InsecureSkipVerify: option.SkipCertVerify,
ServerName: sni,
}, option.Fingerprint)
if err != nil {
return nil, err
}
}
return &Http{
2018-12-22 23:56:42 +08:00
Base: &Base{
2022-08-28 13:41:19 +08:00
name: option.Name,
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
tp: C.Http,
2023-02-24 13:53:44 +08:00
tfo: option.TFO,
2023-08-09 16:57:39 +08:00
mpTcp: option.MPTCP,
2022-08-28 13:41:19 +08:00
iface: option.Interface,
rmark: option.RoutingMark,
prefer: C.NewDNSPrefer(option.IPVersion),
2018-12-22 23:56:42 +08:00
},
user: option.UserName,
pass: option.Password,
2022-07-11 13:42:28 +08:00
tlsConfig: tlsConfig,
2022-04-14 13:07:39 +08:00
option: &option,
2022-07-11 13:42:28 +08:00
}, nil
}