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