2019-12-08 12:17:24 +08:00
|
|
|
package outbound
|
2018-12-03 23:27:00 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2018-12-03 23:27:00 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-12-05 18:22:07 +08:00
|
|
|
"net/url"
|
2018-12-03 23:27:00 +08:00
|
|
|
"strconv"
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-12-03 23:27:00 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Http struct {
|
2018-12-22 23:56:42 +08:00
|
|
|
*Base
|
2019-12-05 18:22:07 +08:00
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
tlsConfig *tls.Config
|
2023-03-31 20:33:41 +08:00
|
|
|
Headers http.Header
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type HttpOption struct {
|
2021-11-07 16:48:51 +08:00
|
|
|
BasicOption
|
2023-03-31 20:33:41 +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"`
|
|
|
|
Headers map[string]string `proxy:"headers,omitempty"`
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-03-21 23:46:49 +08:00
|
|
|
func (h *Http) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
if h.tlsConfig != nil {
|
2018-12-03 23:27:00 +08:00
|
|
|
cc := tls.Client(c, h.tlsConfig)
|
2022-08-13 12:35:39 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err := cc.HandshakeContext(ctx)
|
2018-12-03 23:27:00 +08:00
|
|
|
c = cc
|
2020-03-21 23:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
|
|
|
|
}
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
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
|
2021-11-07 16:48:51 +08:00
|
|
|
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", h.addr, h.Base.DialOptions(opts...)...)
|
2018-12-03 23:27:00 +08:00
|
|
|
if err != nil {
|
2019-12-05 17:51:21 +08:00
|
|
|
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2020-03-21 23:46:49 +08:00
|
|
|
|
2022-12-22 12:42:38 +08:00
|
|
|
defer func(c net.Conn) {
|
|
|
|
safeConnClose(c, err)
|
|
|
|
}(c)
|
2021-03-22 23:26:20 +08:00
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
c, err = h.StreamConn(c, metadata)
|
|
|
|
if err != nil {
|
2018-12-03 23:27:00 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
return NewConn(c, h), nil
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
|
2019-10-11 20:11:18 +08:00
|
|
|
addr := metadata.RemoteAddress()
|
2019-12-05 18:22:07 +08:00
|
|
|
req := &http.Request{
|
|
|
|
Method: http.MethodConnect,
|
|
|
|
URL: &url.URL{
|
|
|
|
Host: addr,
|
|
|
|
},
|
2023-03-31 20:33:41 +08:00
|
|
|
Host: addr,
|
|
|
|
Header: h.Headers.Clone(),
|
2019-12-05 18:22:07 +08:00
|
|
|
}
|
2018-12-03 23:27:00 +08:00
|
|
|
|
2023-03-31 20:33:41 +08:00
|
|
|
req.Header.Add("Proxy-Connection", "Keep-Alive")
|
|
|
|
|
2018-12-03 23:27:00 +08:00
|
|
|
if h.user != "" && h.pass != "" {
|
|
|
|
auth := h.user + ":" + h.pass
|
2019-12-05 18:22:07 +08:00
|
|
|
req.Header.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:22:07 +08:00
|
|
|
if err := req.Write(rw); err != nil {
|
2018-12-03 23:27:00 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:22:07 +08:00
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(rw), req)
|
2018-12-03 23:27:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-05 09:33:40 +08:00
|
|
|
if resp.StatusCode == http.StatusOK {
|
2018-12-03 23:27:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-05 09:33:40 +08:00
|
|
|
if resp.StatusCode == http.StatusProxyAuthRequired {
|
2018-12-03 23:27:00 +08:00
|
|
|
return errors.New("HTTP need auth")
|
|
|
|
}
|
|
|
|
|
2019-10-05 09:33:40 +08:00
|
|
|
if resp.StatusCode == http.StatusMethodNotAllowed {
|
2018-12-03 23:27:00 +08:00
|
|
|
return errors.New("CONNECT method not allowed by proxy")
|
|
|
|
}
|
2019-10-05 09:33:40 +08:00
|
|
|
|
|
|
|
if resp.StatusCode >= http.StatusInternalServerError {
|
|
|
|
return errors.New(resp.Status)
|
|
|
|
}
|
2019-12-05 18:22:07 +08:00
|
|
|
|
2018-12-03 23:27:00 +08:00
|
|
|
return fmt.Errorf("can not connect remote err code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttp(option HttpOption) *Http {
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if option.TLS {
|
2020-10-02 11:34:40 +08:00
|
|
|
sni := option.Server
|
|
|
|
if option.SNI != "" {
|
|
|
|
sni = option.SNI
|
|
|
|
}
|
2018-12-03 23:27:00 +08:00
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: option.SkipCertVerify,
|
2020-10-02 11:34:40 +08:00
|
|
|
ServerName: sni,
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:33:41 +08:00
|
|
|
headers := http.Header{}
|
|
|
|
for name, value := range option.Headers {
|
|
|
|
headers.Add(name, value)
|
|
|
|
}
|
|
|
|
|
2018-12-03 23:27:00 +08:00
|
|
|
return &Http{
|
2018-12-22 23:56:42 +08:00
|
|
|
Base: &Base{
|
2021-11-07 16:48:51 +08:00
|
|
|
name: option.Name,
|
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
|
|
|
tp: C.Http,
|
|
|
|
iface: option.Interface,
|
2022-03-19 13:29:30 +08:00
|
|
|
rmark: option.RoutingMark,
|
2018-12-22 23:56:42 +08:00
|
|
|
},
|
2019-12-05 18:22:07 +08:00
|
|
|
user: option.UserName,
|
|
|
|
pass: option.Password,
|
|
|
|
tlsConfig: tlsConfig,
|
2023-03-31 20:33:41 +08:00
|
|
|
Headers: headers,
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
}
|